Home > Software design >  Searching in elasticsearch with proximity(slop) zero and one
Searching in elasticsearch with proximity(slop) zero and one

Time:10-07

I have created the following index

PUT /proximity_example_1
{
  
    "mappings":{
      "properties":{
        "doc_id": {
          "type": "text"
        },
        "test_name":{
          "type": "text"
        }
      }
  }
}

Then indexed a document

POST proximity_example_2/_doc
{
  "doc_id": "id1",
  "test_name": "test proximity here"
}

Then queried with proximity 0, as follow

GET proximity_example_2/_search
{
  "query": {
    "match_phrase": {
      "test_name": {
        "query": "proximity test",
        "slop": 0.0 
      }
    }
  }
}

But I didn't get any result, Then I searched with proximity 1 , and this time also I didn't get any document. But when I searched with proximity greater than 1, I got results.

GET proximity_example_2/_search
{
  "query": {
    "match_phrase": {
      "test_name": {
        "query": "proximity test",
        "slop": 2.0 
      }
    }
  }
}
GET proximity_example_2/_search
{
  "query": {
    "match_phrase": {
      "test_name": {
        "query": "proximity test",
        "slop": 3.0 
      }
    }
  }
}

So does that mean in elasticsearch when we do a search with proximity 1 or 0 order of the search term matters?

Thank you...

CodePudding user response:

Slop with value 0 is as good as normal phrase search(very restrictive and should have search terms in the exact same order in the Elasticsearch), as you increase the slope this restrictiveness gets reduce and you will have more search results, but beware that increasing to to high number will defeat the purpose of phrase search and you will get irrelevant results.

You can read this and this detailed blog post that explains how it works internally

  • Related