user3175226
user3175226

Reputation: 3659

elasticsearch - confused on how to searching items that a field contains string

This query is returning fine only one item "steve_jobs".

{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "name":"steve_jobs"
        }
      }
    }
  }
}

So, now I want to get all people with name prefix steve_. So I try this:

{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "name": "steve_"
        }
      }
    }
  }
}

This is returning nothing. Why?

I'm confused about when to use term query / term filter / terms filter / querystring query.

Upvotes: 0

Views: 264

Answers (1)

vaidik
vaidik

Reputation: 2213

What you need is Prefix Query.

If you are indexing your document like so:

POST /testing_nested_query/class/
{
    "name": "my name is steve_jobs"
}

And you are using the default analyzer, then the problem is that the term steve_jobs will be indexed as one term. So your Term Query will never be able to find any docs matching the term steve as there is no term like in the index. Prefix Query helps you solve your problem by searching for a prefix in all the indexed terms.

You can solve the same problem by making your custom analyzers (read this and this) so that steve_jobs is stored as steve and jobs.

Upvotes: 1

Related Questions