你说得对,你想要的是术语聚合。您遇到的问题是ES正在将它返回的结果中的“BrandName”字段拆分。这是ES中字段的预期默认行为。
我建议您将BrandName更改为“Multifield”,这将允许您搜索所有各个部分,以及在“未分析”(又名完整的“20世纪福克斯” “)术语。
这里是来自ES的文档。
https://www.elasticsearch.org/guide/en/elasticsearch/reference/0.90/mapping-multi-field-type.html
[更新] 如果您正在使用ES版本1.4或更新版本的语法,多领域,现在有一点不同。
https://www.elasticsearch.org/guide/en/elasticsearch/reference/current/_multi_fields.html#_multi_fields
这里是一个完整的工作示例的示出了在ES 1.4.4的点。请注意,映射会指定该字段的“not_analyzed”版本。
PUT hilden1
PUT hilden1/type1/_mapping
{
"properties": {
"brandName": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
POST hilden1/type1
{
"brandName": "foo"
}
POST hilden1/type1
{
"brandName": "bar"
}
POST hilden1/type1
{
"brandName": "20th Century Fox"
}
POST hilden1/type1
{
"brandName": "20th Century Fox"
}
POST hilden1/type1
{
"brandName": "foo bar"
}
GET hilden1/type1/_search
{
"size": 0,
"aggs": {
"analyzed_field": {
"terms": {
"field": "brandName",
"size": 10
}
},
"non_analyzed_field": {
"terms": {
"field": "brandName.raw",
"size": 10
}
}
}
}
最后一次查询的结果:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 0,
"hits": []
},
"aggregations": {
"non_analyzed_field": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "20th Century Fox",
"doc_count": 2
},
{
"key": "bar",
"doc_count": 1
},
{
"key": "foo",
"doc_count": 1
},
{
"key": "foo bar",
"doc_count": 1
}
]
},
"analyzed_field": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "20th",
"doc_count": 2
},
{
"key": "bar",
"doc_count": 2
},
{
"key": "century",
"doc_count": 2
},
{
"key": "foo",
"doc_count": 2
},
{
"key": "fox",
"doc_count": 2
}
]
}
}
}
注意,不是分析的领域保持“20世纪福克斯”和“富巴”在一起,其中作为分析的领域打破了起来。
我刚刚从一周前开始。所以我正在研究最新的1.4.4版本。 – Bart 2015-02-23 15:52:43
你是什么意思改变品牌名称。更新数据库模式?或者在我的查询中改变它内联? – Bart 2015-02-23 15:59:26
更改ES(数据库)索引器。 – jhilden 2015-02-23 16:40:36