Search code examples
elasticsearchproximity

Does Elasticsearch proximity queries work good?


I happen to look into a scenario in Elasticsearch, where proximity search is not working as expected. Let me explain it below.

When I tried the search term "apple samsung"~1 it brought me around 10 results from my local cluster. But when the proximity term is "samsung apple"~1 it brought me only 2 results.

As per the Elasticsearch documentation in below URL, both the terms should bring me same number of results

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_proximity_searches

Could anybody here would help me on this.

Thanks in advance, Manoj


Solution

  • It's going to depend on your data - if your query is:

     curl -XGET 'http://localhost:9200/test/message/_search?pretty' -d '{
       "query": {
          "query_string": { "query":"\"apple samsung\"~1"}
        }
      }'
    

    this is a slop of one, it would match

    "samsung apple"
    "samsung xxxx apple" 
    

    to match where "apple" appears first, you'd need to specify a slop 2:

     curl -XGET 'http://localhost:9200/test/message/_search?pretty' -d '{
       "query": {
          "query_string": { "query":"\"apple samsung\"~2"}
        }
      }'