# Sorting by relevance problem

**URL:** <https://discuss.elastic.co/t/sorting-by-relevance-problem/142329>\
**Category:** Elasticsearch\
**Created:** [July 31, 2018, 9:53am UTC](https://discuss.elastic.co/t/sorting-by-relevance-problem/142329 "2018-07-31T09:53:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vidya\_Lalitha\_S\_K](https://avatars.discourse-cdn.com/v4/letter/v/ecae2f/32.png) [@Vidya\_Lalitha\_S\_K](https://discuss.elastic.co/u/Vidya_Lalitha_S_K)\
**Post date:** [July 31, 2018, 9:53am UTC](https://discuss.elastic.co/t/sorting-by-relevance-problem/142329/1 "2018-07-31T09:53:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tamara](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tamara/32/47095_2.png) [@tamara](https://discuss.elastic.co/u/tamara)\
**Post date:** [August 17, 2018, 12:50pm UTC](https://discuss.elastic.co/t/sorting-by-relevance-problem/142329/2 "2018-08-17T12:50:00Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-near-query.html)

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.

---

<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:** [September 14, 2018, 12:50pm UTC](https://discuss.elastic.co/t/sorting-by-relevance-problem/142329/3 "2018-09-14T12:50:05Z")

</div>

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