Kumaresh Roy
Kumaresh Roy

Reputation: 29

implementing exact full text search in elasticsearch

I have indexed few resume in elastic search. So when I am searching for text basically employeeid : "IIIPL-4309". It is giving result for all "IIIPL-xxxx". How can I restrict the search result to just "IIIPL-4309?

I have tried with custom analyzer

Upvotes: 1

Views: 1310

Answers (2)

Richa
Richa

Reputation: 7649

In case you want to search with exact match you should go with Term Query on the keyword field like this

GET /_search
{
    "query": {
        "term": {
            "someField.keyword": {
                "value": "IIIPL-4309"
            }
        }
    }
}

Upvotes: 0

Kamal Kunjapur
Kamal Kunjapur

Reputation: 8860

What you are looking for is exact match. You would need to use keyword type in elasticsearch in order to achieve that.

From the link,

They are typically used for filtering (Find me all blog posts where status is published), for sorting, and for aggregations. Keyword fields are only searchable by their exact value.

Below is how the mapping would be, I'm just creating a sibling field for the employee_id as employee_id.keyword

Mapping:

PUT employee
{  
   "mappings":{  
      "properties":{  
         "employee_id":{  
            "type":"text",
            "fields":{  
               "keyword":{  
                  "type":"keyword"
               }
            }
         }
      }
   }
}

Sample Documents

POST employee/_doc/1
{
  "employee_id": "IIIPL-4309"
}

POST employee/_doc/2
{
  "employee_id": "IIIPL-4229"
}

Query

POST employee/_search
{
  "query": {
    "term": {
      "employee_id.keyword": "IIIPL-4309"
    }
  }
}

Hope this helps!

Upvotes: 1

Related Questions