弹性搜索(DSL):术语与任何内容都不匹配

时间:2016-12-20 17:50:41

标签: elasticsearch dsl

我是ES的新手,我想做一个简单的查询来获取域的所有匹配值,但它不会返回任何内容。 match_all查询返回:

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"}}]}}

但是当我想搜索一个特定值时,它找不到任何东西:

curl -XGET localhost:9200/zone/20161201/_search -d '{"query":{"term":{"name":"domain3.com"}}}'
你能帮帮我吗?这有什么问题?

2 个答案:

答案 0 :(得分:1)

由于我无法添加评论,我需要创建一个完整的答案来帮助您。

您的类型有哪些映射?术语查询用于未分析的字段仅返回完全匹配。

请查看文档以获取更多详细信息。特别是在" 为什么术语查询不匹配我的文档?"

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

答案 1 :(得分:1)

尝试下一个查询:

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

它会起作用。 现在尝试下一个索引和查询:

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

注意我们索引域名,没有号码。

然后尝试查询:

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

此查询也可以。

当您使用" domain3"等字符串索引字符串时,弹性索引为[domain,3](默认分析器),然后如果您使用术语查询,弹性看看确切的术语,所以他看起来at" domain3"但没有找到因为弹性索引它作为域和3。 所以使用匹配查询的更好方法。如果您确切知道自己在做什么,可以使用术语查询。