我将数据存储在如下所示的elasticsearch中。它只返回给定字段中的不同单词,而不返回整个不同短语。
{
"_index" : "test01",
"_type" : "whatever01",
"_id" : "1234",
"_score" : 1.0,
"_source" : {
"company_name" : "State Bank of India",
"user" : ""
}
},
{
"_index" : "test01",
"_type" : "whatever01",
"_id" : "5678",
"_score" : 1.0,
"_source" : {
"company_name" : "State Bank of India",
"user" : ""
}
},
{
"_index" : "test01",
"_type" : "whatever01",
"_id" : "8901",
"_score" : 1.0,
"_source" : {
"company_name" : "Kotak Mahindra Bank",
"user" : ""
}
}
我尝试使用术语汇总功能
GET /test01/_search/
{
"aggs" : {
"genres":
{
"terms" :
{ "field": "company_name"}
}
}
}
我得到以下输出
"aggregations" : {
"genres" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 10531,
"buckets" : [
{
"key" : "bank",
"doc_count" : 2818
},
{
"key" : "mahindra",
"doc_count" : 1641
},
{
"key" : "state",
"doc_count" : 1504
}]
}}
如何在“ company_name”字段中获取仅具有下面给出的唯一值的整个字符串?
"aggregations" : {
"genres" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 10531,
"buckets" : [
{
"key" : "Kotak Mahindra Bank",
"doc_count" : 2818
},
{
"key" : "State Bank of India",
"doc_count" : 1641
}
]
}}
答案 0 :(得分:1)
似乎您已经为"fielddata": "true"
类型的字段company_name
设置了text
。这样做不好,因为它可能会占用大量link中提到的堆空间。
此外,类型text
的字段值被分解为令牌,并使用称为Analysis的过程保存在倒排索引中。在文本类型的字段上设置fielddata
会使聚合按照您在问题中提到的那样工作。
您需要做的是创建其keyword
类型的同级兄弟link,然后对该字段执行汇总。
基本上为company_name
修改映射,如下所示:
PUT <your_index_name>/_search
{
"mappings": {
"mydocs": {
"properties": {
"company_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
}
在此company_name.keyword
字段上运行以下汇总查询,您将获得所需的内容。
POST <your_index_name>/_search
{
"aggs": {
"unique_names": {
"terms": {
"field": "company_name.keyword", <----- Run on this field
"size": 10
}
}
}
}
希望这会有所帮助!