【问题标题】:ElasticSearch term query vs query_string?ElasticSearch 术语查询与 query_string?
【发布时间】:2016-05-15 10:10:22
【问题描述】:

当我使用 query_string 查询我的索引时,我得到了结果

{
"query": {
"bool": {
"must": [ ],
"must_not": [ ],
"should": [
{
"query_string": {
"default_field": "Printer.Name",
"query": "HL-2230"
}
}
]
}
},
"from": 0,
"size": 10,
"sort": [ ],
"aggs": { }
}

但是当我使用术语查询进行查询时,我没有得到任何结果

{
"query": {
"bool": {
"must": [ ],
"must_not": [ ],
"should": [
{
"term": {
"Printer.Name": "HL-2230"
}
}
]
}
},
"from": 0,
"size": 10,
"sort": [ ],
"aggs": { }
}

我知道 term 是 not_analyzed 并且 query_string 被分析了但是 Name 已经是“HL-2230”,为什么它与 term 查询不匹配?我也尝试使用“hl-2230”搜索,但仍然没有得到任何结果。

编辑:映射如下所示。打印机是产品的孩子。不知道这是否有区别

    {
"state": "open",
"settings": {
"index": {
"creation_date": "1453816191454",
"number_of_shards": "5",
"number_of_replicas": "1",
"version": {
"created": "1070199"
},
"uuid": "TfMJ4M0wQDedYSQuBz5BjQ"
}
},
"mappings": {
"Product": {
"properties": {
"index": "not_analyzed",
"store": true,
"type": "string"
},
"ProductName": {
"type": "nested",
"properties": {
"Name": {
"store": true,
"type": "string"
}
}
},
"ProductCode": {
"type": "string"
},
"Number": {
"index": "not_analyzed",
"store": true,
"type": "string"
},
"id": {
"index": "no",
"store": true,
"type": "integer"
},
"ShortDescription": {
"store": true,
"type": "string"
},
"Printer": {
"_routing": {
"required": true
},
"_parent": {
"type": "Product"
},
"properties": {
"properties": {
"RelativeUrl": {
"index": "no",
"store": true,
"type": "string"
}
}
},
"PrinterId": {
"index": "no",
"store": true,
"type": "integer"
},
"Name": {
"store": true,
"type": "string"
}
}
},
"aliases": [ ]
}
}

【问题讨论】:

  • 你的Printer.Name字段的映射是什么?
  • @Val 请看我的编辑。我也添加了映射。打印机基本上是 Product 的子级。这可能是问题或区别吗?

标签: elasticsearch


【解决方案1】:

根据您上面提供的映射

"Name": {
 "store": true,
 "type": "string"
 }

Name 被分析。所以HL-2230 将分成两个标记,HL2230。这就是为什么term query 不工作而query_string 工作的原因。当您使用term query 时,它会搜索不存在的确切术语HL-2230

【讨论】:

  • 是不是表示term查询只能用于not_analyzed字段?
  • 不一定,但仅用于搜索已编入索引的确切标记,在您的情况下为 hl2230
  • 你是对的。我已经通过插件验证了不同的情况,它可以按照你说的那样工作。谢谢。
猜你喜欢
  • 1970-01-01
  • 2018-05-20
  • 2021-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-06
相关资源
最近更新 更多