Query Terms = NULL or []

Hi guys, you know I have a problem with a query in "Terms" where the query is:

GET /user/_search
{
  "query": {
    "terms" : { 
      "user" : ["user-1"]
    }
  }
}

something so simple but it does not return anything. I put an example of index and response.

Mapper:

PUT /user/_doc/1
{
  "user" : "user-1"
}

PUT /user/_doc/2
{
  "user" : "user-2"
}

PUT /user/_doc/3
{
  "user" : "user-3"
}

Query Failed:

GET /user/_search
{
  "query": {
    "terms" : { 
      "user" : ["user-1"]
    }
  }
}

Respose Failed:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

When removing "-" from terms, the result is returned but I need it to be exact, since it will be used for location queries where it must be exact, because if only "california" is indicated, it can return results from the US and California. Mexico.

Query Success:

GET /user/_search
{
  "query": {
    "terms" : { 
      "user" : ["1"]
    }
  }
}

Response Success:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "user",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "user" : "user-1"
        }
      }
    ]
  }
}

I use Terms since you can specify several locations in the same query.

I hope you help me.

Greetings.

It's the way analyzer are working.
You can change the mapping. Or in your case you can run:

GET /user/_search
{
  "query": {
    "terms" : { 
      "user.keyword" : ["user-1"]
    }
  }
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.