【问题标题】:Elasticsearch: facet or aggregation returning doc counts over multiple fieldsElasticsearch:方面或聚合返回多个字段的文档计数
【发布时间】:2014-12-24 06:01:49
【问题描述】:

我有一个 elasticsearch 文档结构,我希望有一个术语方面(或聚合),我获得独立于它们出现的字段的文档数量。

例如,以下结果显示文档和分面搜索结果:

    {
        "_shards": {
            "failed": 0, "successful": 5, "total": 5
        },
        "hits": {
            "hits": [
                {
                    "_id": "003", "_index": "test", "_score": 1.0, "_type": "test",
                    "_source": {
                        "root": {
                            "content": [
                                "five",
                                "five",
                                "five"
                            ],
                            "title": "four"
                        }
                    }
                },
                {
                    "_id": "002", "_index": "test", "_score": 1.0, "_type": "test",
                    "_source": {
                        "root": {
                            "content": "two three",
                            "title": "three"
                        }
                    }
                },
                {
                    "_id": "001", "_index": "test", "_score": 1.0, "_type": "test",
                    "_source": {
                        "root": {
                            "content": "one two",
                            "title": "one"
                        }
                    }
                }
            ],
            "max_score": 1.0, "total": 3
        },
        "facets": {
            "terms": {
                "_type": "terms", "missing": 0, "other": 0,
                "terms": [
                    {
                        "count": 2,
                        "term": "two"
                    },
                    {
                        "count": 2,
                        "term": "three"
                    },
                    {
                        "count": 2,
                        "term": "one"
                    },
                    {
                        "count": 1,
                        "term": "four"
                    },
                    {
                        "count": 1,
                        "term": "five"
                    }
                ],
                "total": 8
            }
        },
        "timed_out": false,
        "took": 18,
    }

我们可以看到术语“一”和“三”的计数为 2(对于同一文档的每个字段一次),我希望它们计数为 1。唯一计数为 2 的术语应该是“二”。

我研究了聚合以查看它是否有帮助,但它似乎不适用于多个字段(或者我错过了一些东西)。

在“根”而不是单个字段上构建“术语”方面会很好......但这似乎也不可能。

任何想法,如何解决这个问题?

【问题讨论】:

    标签: elasticsearch aggregation facets


    【解决方案1】:

    您可以使用聚合术语中的脚本来实现此目的。 在脚本中,从两个字段中收集标记,执行集合并集操作,然后返回集合。

    {
        "aggs" : {
            "genders" : {
                "terms" : {
                    "script" : "union(doc['content'].values, doc['title'].values) "
                }
            }
        }
    }
    

    您需要了解如何在您用作脚本语言的任何语言中应用联合操作。

    【讨论】:

      【解决方案2】:

      您可以添加新字段,该字段保留内容和标题字段中的唯一术语,并在其上进行构面聚合。

      【讨论】:

      • 是的,如果我找不到替代品,我会这样做,但如果可能的话,我宁愿不复制内容。
      猜你喜欢
      • 2020-11-14
      • 2018-06-13
      • 2016-11-21
      • 1970-01-01
      • 2022-06-30
      • 1970-01-01
      • 2020-02-18
      • 2016-05-14
      • 2016-04-14
      相关资源
      最近更新 更多