How to use slop and minimum_should_match together in some query?

Basically, my objective is to search for some sentences with slope and also with minimum_should_match.
For example:

> GET my_index/_search
> {
>   "query": {
>     "field": "provided that immediately after giving effect to any such incurrence",
>     "minimum_should_match": 7
>   }
>   "slop": 3
> }

This code is wrong but I want something like this. If I search for some sentence then I can tune it with minimum_should_match and also with slop.
If this objective can be sort by some other query then feel free to comment

Hi @princesahu04,

I'm not sure I fully understand but you could combine a match_phrase to declare the slop and a match query to declare the minimum should match.

PUT test/_doc/1
{
  "project": "token1 token2 token3 token4 token5"
}

The following query will return results:

GET test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "project": {
              "query": "token1 token4 token5",
              "slop": 3
            }
          }
        },
        {
          "match": {
            "project": {
              "query": "token1 token4 token5",
              "minimum_should_match": 3
            }
          }
        }
      ]
    }
  }
}

Results:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 2.5617933,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 2.5617933,
        "_source" : {
          "project" : "token1 token2 token3 token4 token5"
        }
      }
    ]
  }
}

But if the query contains only 2 terms it won't return results because it doesn't have the minimum should match terms:

GET test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "project": {
              "query": "token1 token5",
              "slop": 3
            }
          }
        },
        {
          "match": {
            "project": {
              "query": "token1 token5",
              "minimum_should_match": 3
            }
          }
        }
      ]
    }
  }
}

Results:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

I hope it helps!

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