# Custom dotproduct with long type field value

**URL:** <https://discuss.elastic.co/t/custom-dotproduct-with-long-type-field-value/333150>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [May 11, 2023, 5:07am UTC](https://discuss.elastic.co/t/custom-dotproduct-with-long-type-field-value/333150 "2023-05-11T05:07:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akhilendra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akhilendra/32/118515_2.png) [@Akhilendra](https://discuss.elastic.co/u/Akhilendra)\
**Post date:** [May 11, 2023, 5:07am UTC](https://discuss.elastic.co/t/custom-dotproduct-with-long-type-field-value/333150/1 "2023-05-11T05:07:05Z")

</div>

An array long type field in document geting automatically sorted when calculating dotproduct via custom painless script.

Index Mapping

```auto
PUT /custom_dot_product
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "level1": {
        "type": "nested",
        "properties": {
          "level2": {
            "properties": {
              "vector_3": {
                "type": "long"
              }
            }
          }
        }
      }
    }
  }
}

```

Document data .

```auto
POST /custom_dot_product/_doc/1
{
  "level1": {
    "level2": {
      "vector_3": [
       2,
       2,
       2,
       0
      ]
    }
  }
}

```

Search query

```auto
POST custom_dot_product/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "level1",
            "query": {
              "function_score": {
                "query": {
                  "match_all": {}
                },
                "script_score": {
                  "script": {
                    "source": " if(doc['level1.level2.vector_3'].size()>0){ double result = 0.0; for (int i = 0; i < params.user_vector.length; i++) result += params.user_vector[i] * doc['level1.level2.vector_3'][i]; return result;} ",
                    "params": {
                      "user_vector": [
                        1,
                        1,
                        1,
                        0
                      ]
                    }
                  }
                },
                "boost_mode": "replace"
              }
            },
            "inner_hits": {
              "sort": []
            }
          }
        }
      ]
    }
  }
}

```

Output :

```auto
    {
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 4.0,
    "hits" : [
      {
        "_index" : "custom_dot_product",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 4.0,
        "_source" : {
          "level1" : {
            "level2" : {
              "vector_3" : [
                2,
                2,
                2,
                0
              ]
            }
          }
        },
        "inner_hits" : {
          "level1" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 4.0,
              "hits" : [
                {
                  "_index" : "custom_dot_product",
                  "_type" : "_doc",
                  "_id" : "1",
                  "_nested" : {
                    "field" : "level1",
                    "offset" : 0
                  },
                  "_score" : 4.0,
                  "_source" : {
                    "level2" : {
                      "vector_3" : [
                        2,
                        2,
                        2,
                        0
                      ]
                    }
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

```

How to avoid array field sorting while calculating the dotproduct with painless?

We are not using dense\_vector as we have to migrate this index to opensearch, where inbulid dotproduct not avalible hence writing custom dotproduct.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 11, 2023, 5:07am UTC](https://discuss.elastic.co/t/custom-dotproduct-with-long-type-field-value/333150/2 "2023-05-11T05:07:05Z")

</div>

OpenSearch/OpenDistro are AWS run products and differ from the original Elasticsearch and Kibana products that Elastic builds and maintains. You may need to contact them directly for further assistance.

(This is an automated response from your friendly Elastic bot. Please report this post if you have any suggestions or concerns :elasticheart: )

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 8, 2023, 5:07am UTC](https://discuss.elastic.co/t/custom-dotproduct-with-long-type-field-value/333150/3 "2023-06-08T05:07:53Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
