Sokmesa Khiev
Sokmesa Khiev

Reputation: 2920

ElasticSearch query text

My index data is

    {
      "full_name":"Edwin Powell Hubble",
      "job": "IT"
    }
    {
      "full_name":"John Edwin",
      "job": "Accountant"
    }
    {
      "first_name":"Eric Petterson",
      "job": "Accountant"
    }

I am not sure if anyone could help me to build a query to get data that have full_name as Edwin. It tried with term query seem not really work.

Upvotes: 0

Views: 41

Answers (2)

Abhishek Jain
Abhishek Jain

Reputation: 51

Since full_name can be of any length and should be analyzed when indexed, I believe you have mapped the attribute as of type text.

For the same reason I also believe you will have requirements to return results as 'Edwin Powell Hubble' and 'John Edwin' when searched with 'Edwin' and return 'Edwin Powell Hubble' when search with 'Edwin Pow'

match_phrase_prefix should help you with these use cases.

GET /_search
{
  "query": {
    "match_phrase_prefix": {
      "full_name": "Edwin"
    }
  }
}

Upvotes: 1

Bhavya
Bhavya

Reputation: 16192

You can use the match query to get data that have full_name as Edwin

{
  "query": {
    "match": {
      "full_name": "edwin"
    }
  }
}

Term query works on exact text match, so you will not get any document for Edwin since there is no data in your sample index data that have a match for full_name as Edwin

Upvotes: 1

Related Questions