弹性搜索多值字段聚合 [英] Elastic Search multi-value field aggregation

查看:104
本文介绍了弹性搜索多值字段聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的索引文档具有以下架构:

My indexed documents have a schema:

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

我想搜索它们并按单个作者进行汇总,因此获取与我的命中发生的最佳作者有关的列表。 术语汇总似乎可以满足我的需求,但是我无法使它适用于带有值列表的字段。有帮助吗?

I would like to search them and aggregate by the individual authors, so get a list with top authors which occurred in my hits. Terms aggregation seems to be a match for my needs, but I'm not able to get it working for field with a list of values. Any help?

推荐答案

您可能需要使用嵌套类型,那么您可以使用嵌套聚合关于作者姓名。

You will probably want to use a nested type, then you can use a nested aggregation on the author names.

例如,我设置了一个简单的索引,如下所示:

As an example, I set up a simple index like this:

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"
      }
   ]
}

然后我可以使用以下信息获取(分析的)作者姓氏列表:

Then I can get the list of (analyzed) author last names with:

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

这篇关于弹性搜索多值字段聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆