你說得對,你想要的是術語聚合。您遇到的問題是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