Sorting by relevance problem

I have a set of documents like
[Egg Duck Whole Fresh, Egg Goose Whole Fresh, Egg Quail Whole Fresh, Egg Whole Fresh]

and if I search egg fresh whole, its returning ,
[Egg Duck Whole Fresh, Egg Whole Fresh, Egg Goose Whole Fresh Egg Quail Whole Fresh]
Actually it should return Egg Whole Fresh first but its giving relevance to Egg Duck Whole Fresh.
My query is
GET /_search
{
"query": {
"match": {
"name": {
"query": "egg whole fresh",
"fuzziness": 1,
"fuzzy_transpositions": false,
"operator": "and"
}
}
}
}
Any idea why happening this?
Thanks

This happens because elasticsearch search for the terms separated in any order, if you want the order to be considered you should change your query to the span query Span Near Query

Here is how I solved your problem:

{
  "query": {
    "span_near": {
      "clauses": [
        {
          "span_multi": {
            "match": {
              "fuzzy": {
                "name": {
                  "value": "egg",
                  "fuzziness": 1,
                  "transpositions": false
                }
              }
            }
          }
        },
        {
          "span_multi": {
            "match": {
              "fuzzy": {
                "name": {
                  "value": "whole",
                  "fuzziness": 1,
                  "transpositions": false
                }
              }
            }
          }
        },
        {
          "span_multi": {
            "match": {
              "fuzzy": {
                "name": {
                  "value": "fresh",
                  "fuzziness": 1,
                  "transpositions": false
                }
              }
            }
          }
        }
      ],
      "slop" : 2
    }
  }
}

note: slop parameter controls the maximum number of intervening unmatched positions permitted.

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