Elastic search (DSL): term does not match anything

948 Views Asked by At

I'm new to ES, I would like to do a simple query to get all the matching values of a domain, but it does not return any thing. The match_all query returns:

curl -XGET localhost:9200/zone/20161201/_search -d '{"query": { "match_all": { }}}'

{"took":4,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":8,"max_score":1.0,"hits":[{"_index":"zone","_type":"20161201","_id":"AVkZ3-ic6lL49IXWU8TZ","_score":1.0,"_source":{"name":"domain2.com"}},{"_index":"zone","_type":"20161201","_id":"AVkZ3-ic6lL49IXWU8Ta","_score":1.0,"_source":{"name":"domain3.com"}},{"_index":"zone","_type":"20161201","_id":"AVkZ4AAQ6lL49IXWU8Te","_score":1.0,"_source":{"name":"domain3.com"}},{"_index":"zone","_type":"20161201","_id":"AVkZ3-ic6lL49IXWU8Tb","_score":1.0,"_source":{"name":"domain4.com"}},{"_index":"zone","_type":"20161201","_id":"AVkZ4AAQ6lL49IXWU8Tc","_score":1.0,"_source":{"name":"domain1.com"}},{"_index":"zone","_type":"20161201","_id":"AVkZ4AAQ6lL49IXWU8Td","_score":1.0,"_source":{"name":"domain2.com"}},{"_index":"zone","_type":"20161201","_id":"AVkZ4AAQ6lL49IXWU8Tf","_score":1.0,"_source":{"name":"domain4.com"}},{"_index":"zone","_type":"20161201","_id":"AVkZ3-ic6lL49IXWU8TY","_score":1.0,"_source":{"name":"domain1.com"}}]}}

but when I want to search for one specific value, it can not find anything:

curl -XGET localhost:9200/zone/20161201/_search -d '{"query":{"term":{"name":"domain3.com"}}}'

Can you please help me? What's wrong with this?

2

There are 2 best solutions below

0
On BEST ANSWER

As I can't add comments yet , I need to create a full answer to help you.

What are the mappings for your type? Term queries are used for not analyzed fields and only return exact matches.

Please take a look at the documentation for more details. Specifically at the section "Why doesn’t the term query match my document?"

https://www.elastic.co/guide/en/elasticsearch/reference/2.3/query-dsl-term-query.html

0
On

Try next query:

GET zone/20161201/_search
{
   "query": {
      "match": {
         "name": "domain3.com"
      }
   }
}

it's will work. Now try next indexing and query:

POST zone/20161201/
{
  "name":"domain.com"
}

note that we index domain, without number.

then try youre query:

GET zone/20161201/_search
{
    "query": {
      "term": {
         "name": "domain.com"
      }
   }
} 

this query work also.

when you indexing string with string like "domain3", elastic index it as [domain,3] (default analyzer), and then if you use term query, elastic look at exact term, so he look at "domain3" but not found because elastic index it as domain and 3. so the better way to use match query. if you know exactly what you doing you can use term query.