elasticsearchelasticsearch-nested

Elasticsearch : minimum_should_match for nested query


I have a nested field like

{
  "tags": [
    {
      "tag": "lorem ipsum"
    },
    {
      "tag": "Lorem ipsum dolor sit amet"
    }
  ]
}

And mapping like

{
  "tags": {
    **"type": "nested",**
    "properties": {
      "tag": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

can we use something like minimum_should_match : 80 for a nested tag field? So that I will be able to control the relevance level through it?
Exa: if I search for "Lorem ipsum dolor" with minimum_should_match: 90 , I should not get lorem ipsum as a result.


Solution

  • Nested query is just a syntax to access nested fields so minimum_should_match can be used as in other queries

    Query

    {
      "query": {
        "nested": {
          "path": "tags",
          "query": {
            "match": {
              "tags.tag": 
              {
                "query": "lorem ipsum dolor",
                "minimum_should_match": "90%"
              }
            }
          },
          "inner_hits": {}
        }
      }
    }
    

    Result:

      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 0.671082,
        "hits" : [
          {
            "_index" : "index56",
            "_type" : "_doc",
            "_id" : "01We63ABq1Ib1oOmkJxn",
            "_score" : 0.671082,
            "_source" : {
              "tags" : [
                {
                  "tag" : "lorem ipsum"
                },
                {
                  "tag" : "Lorem ipsum dolor sit amet"
                }
              ]
            },
            "inner_hits" : {
              "tags" : {
                "hits" : {
                  "total" : {
                    "value" : 2,
                    "relation" : "eq"
                  },
                  "max_score" : 0.89999837,
                  "hits" : [
                    {
                      "_index" : "index56",
                      "_type" : "_doc",
                      "_id" : "01We63ABq1Ib1oOmkJxn",
                      "_nested" : {
                        "field" : "tags",
                        "offset" : 1
                      },
                      "_score" : 0.89999837,
                      "_source" : {
                        "tag" : "Lorem ipsum dolor sit amet"
                      }
                    },
                    {
                      "_index" : "index56",
                      "_type" : "_doc",
                      "_id" : "01We63ABq1Ib1oOmkJxn",
                      "_nested" : {
                        "field" : "tags",
                        "offset" : 0
                      },
                      "_score" : 0.44216567,
                      "_source" : {
                        "tag" : "lorem ipsum"
                      }
                    }
                  ]
                }
              }
            }
          }
        ]
      }
    

    With minimum-should-match:90% both the nested docs are returned in inner_hits. Reason: From docs

    The number computed from the percentage is rounded down and used as the minimum.

    Since 90% of 22.7 it will be rounded down to 2. So 2 tokens should match. If minimum-should-match:100% then only one nested doc will be returned