ElasticSearch术语查询vs query_string?

时间:2016-02-05 12:16:57

标签: elasticsearch

当我使用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": { }
}

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

enter image description here

编辑:映射如下所示。打印机是产品的孩子。不确定这是否有所不同

    {
"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": [ ]
}
}

1 个答案:

答案 0 :(得分:2)

根据您在上面提供的地图

"Name": {
 "store": true,
 "type": "string"
 }
分析了

Name。因此HL-2230将分为两个令牌HL2230。这就是为什么term query无效并且query_string正在运作的原因。当您使用term query时,它会搜索不存在的确切字词HL-2230

相关问题