maral
maral

Reputation: 97

elasticsearch, find exact phrase in a field

I want to search exact phrase in a single field.. and this is my approach:

"query": {
  "match_phrase": {                                             
    "word": "developer"
  }
}

but the point is this query will find any document that have this keyword developer :

like "word": "developer" and "word": "php developer"

how can I create a query, that when I search for developer just return "word": "developer" doc,

and when I searched for php developer return "word": "php developer" doc

thanks

Upvotes: 0

Views: 554

Answers (1)

Kamal Kunjapur
Kamal Kunjapur

Reputation: 8860

In a simple way, if your field word would be of type keyword, you can then make use of Term Query as shown below:

POST <your_index_name>/_search
{
  "query": {
    "term" : { "word" : "developer" } 
  }
}

If you have word as only of type text I'd suggest you to add its keyword field as multi-fields, in that way you can make use of word for text matches and word.keyword for exact matches.

PUT <your_index_name>
{
  "mappings": {
    "_doc": {
      "properties": {
        "word": {
          "type": "text",
          "fields": {
            "keyword": { 
              "type":  "keyword"
            }
          }
        }
      }
    }
  }
}

Upvotes: 2

Related Questions