0

我已将数据存储在 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
    }
    ]

}}

4

1 回答 1

1

看来您已经为类型"fielddata": "true"为 的字段设置了。这不好,因为它最终会消耗大量的堆空间,如链接中所述。company_nametext

此外,字段的类型值text被分解为标记,并使用名为Analysis的过程保存在倒排索引中。设置fielddata文本类型的字段将导致聚合按照您在问题中提到的方式工作。

您需要做的是创建此链接keyword中提到的类型的同级等效项,并在该字段上执行聚合。

基本上修改您的映射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
      }
    }
  }
}

希望这可以帮助!

于 2019-04-22T17:00:02.007 回答