【问题标题】:Elastic Search multi-value field aggregationElastic Search 多值字段聚合
【发布时间】:2015-06-08 22:28:43
【问题描述】:

我的索引文档有一个架构:

{
  ...
  'authors': [{'first name': 'John', 'last name': 'Smith'},
              {'first name': 'Mark', 'last name': 'Spencer'}]
  ...
}

我想搜索它们并按各个作者进行汇总,因此请获取我的热门文章中出现的顶级作者列表。 Terms aggregation 似乎符合我的需求,但我无法让它适用于具有值列表的字段。有什么帮助吗?

【问题讨论】:

  • 您是否使用nested 类型和nested 聚合?到目前为止,您可以显示您的查询吗?

标签: elasticsearch aggregation multivalue


【解决方案1】:

您可能想要使用nested type,然后您可以在作者姓名上使用nested aggregation

作为一个例子,我设置了一个这样的简单索引:

PUT /test_index
{
   "settings": {
      "number_of_shards": 1
   },
   "mappings": {
      "doc": {
         "properties": {
            "title": {
               "type": "string"
            },
            "authors": {
               "type": "nested",
               "properties": {
                  "first_name": {
                     "type": "string"
                  },
                  "last_name": {
                     "type": "string"
                  }
               }
            }
         }
      }
   }
}

然后添加了几个文档:

PUT /test_index/doc/1
{
    "title": "Book 1",
   "authors": [
      {
         "first_name": "John",
         "last_name": "Smith"
      },
      {
         "first_name": "Mark",
         "last_name": "Spencer"
      }
   ]
}

PUT /test_index/doc/2
{
   "title": "Book 2",
   "authors": [
      {
         "first_name": "Ben",
         "last_name": "Jones"
      },
      {
         "first_name": "Tom",
         "last_name": "Lawrence"
      }
   ]
}

然后我可以得到(分析的)作者姓氏列表:

POST /test_index/_search?search_type=count
{
   "aggs": {
      "nested_authors": {
         "nested": {
            "path": "authors"
         },
         "aggs": {
            "author_last_names": {
               "terms": {
                  "field": "authors.last_name"
               }
            }
         }
      }
   }
} 
...
{
   "took": 71,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "nested_authors": {
         "doc_count": 4,
         "author_last_names": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
               {
                  "key": "jones",
                  "doc_count": 1
               },
               {
                  "key": "lawrence",
                  "doc_count": 1
               },
               {
                  "key": "smith",
                  "doc_count": 1
               },
               {
                  "key": "spencer",
                  "doc_count": 1
               }
            ]
         }
      }
   }
}

这是我使用的代码:

http://sense.qbox.io/gist/ca94cc11a12f8e4fed5c62c52966128b9a6f58de

【讨论】:

  • 这正是我所需要的。谢谢!
  • 如何列出未分析的作者姓氏?
  • 在创建索引时,只需在last_name的映射中添加"index":"not_analyzed",返回的姓氏就不会改变。
猜你喜欢
  • 1970-01-01
  • 2021-03-09
  • 2018-01-14
  • 1970-01-01
  • 2015-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-29
相关资源
最近更新 更多