Score is same for single value match vs multiple match for an array field using terms query

PUT dvt_test_long/_doc/1
{
  "items" : [1] 
}

PUT dvt_test_long/_doc/2
{
  "items" : [1, 2] 
}


PUT dvt_test_long/_doc/3
{
  "items" : [1, 2, 3] 
}

PUT dvt_test_long/_doc/4
{
  "items" : [1, 2, 3, 4] 
}

Query:

GET dvt_test_long/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "items": [
              "3",
              "4"
            ]
          }
        }
      ]
    }
  }
}

Response:

{
  "took" : 917,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "dvt_test_long",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "items" : [
            1,
            2,
            3
          ]
        }
      },
      {
        "_index" : "dvt_test_long",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "items" : [
            1,
            2,
            3,
            4
          ]
        }
      }
    ]
  }
}

As you can see, [1, 2, 3] has same score as [1, 2, 3, 4] where there were two matches [3, 4].

Is it possible to score the documents more based on no. of values matched ?

I want [1, 2, 3, 4] to score higher.

Hi @Divit_Sharma

Try add match inside should clasule.

 {
          "match": {
            "items": "3"
          }
        },
        {
          "match": {
            "items": "4"
          }
        }

Two items to search was just an example.

Array to be searched can be of any no. of values. This example would make me use multiple match blocks based on the no. of values in array. So not what I am looking for.

Another solution was to use:

{ "match": { "items": "3 4" }

But this fails for long values.

Okay this seems to work but I can't just pass the list will have to build the search string as value1 OR value2 OR value3 etc.

GET dvt_test_long/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "query_string": {
            "query": "items: (3 OR 4)"
          }
        }
      ]
    }
  }
} 
[
      {
        "_index" : "dvt_test_long",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 2.0,
        "_source" : {
          "items" : [
            1,
            2,
            3,
            4
          ]
        }
      },
      {
        "_index" : "dvt_test_long",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "items" : [
            1,
            2,
            3
          ]
        }
      }
    ]

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