首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Elasticsearch术语聚合和匹配项

Elasticsearch术语聚合和匹配项
EN

Stack Overflow用户
提问于 2019-12-01 04:51:00
回答 1查看 35关注 0票数 0

有没有一种方法可以获得索引中最常用的单词列表以及这些单词出现的记录?

顺便说一句。如果我没有在查询中请求hits,有没有办法从聚合结果中去掉它?

我使用的是Elasticsearch 7.4.0。

示例查询:

代码语言:javascript
复制
POST text-analysis/_search
{
    "aggs" : {
        "most_common_words" : {
            "terms": {
                "field" : "sentence"
            }
        }
    }
}

响应:

代码语言:javascript
复制
{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 63,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "text-analysis",
                "_type": "_doc",
                "_id": "T64Nvm4BAE1yJwCxLvwo",
                "_score": 1.0,
                "_source": {
                    "data_set": "lorem_ipsum",
                    "sentence": "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
                }
            },
            {
                "_index": "text-analysis",
                "_type": "_doc",
                "_id": "UK4Nvm4BAE1yJwCxLvwx",
                "_score": 1.0,
                "_source": {
                    "data_set": "lorem_ipsum",
                    "sentence": "Nam sodales justo eget varius blandit."
                }
            },
            {
                "_index": "text-analysis",
                "_type": "_doc",
                "_id": "Ua4Nvm4BAE1yJwCxLvw1",
                "_score": 1.0,
                "_source": {
                    "data_set": "lorem_ipsum",
                    "sentence": "Fusce velit erat, molestie nec varius et, semper quis arcu."
                }
            },
            {
                "_index": "text-analysis",
                "_type": "_doc",
                "_id": "Uq4Nvm4BAE1yJwCxLvw5",
                "_score": 1.0,
                "_source": {
                    "data_set": "lorem_ipsum",
                    "sentence": "Quisque maximus porta purus at dignissim."
                }
            },
            {
                "_index": "text-analysis",
                "_type": "_doc",
                "_id": "U64Nvm4BAE1yJwCxLvw8",
                "_score": 1.0,
                "_source": {
                    "data_set": "lorem_ipsum",
                    "sentence": "Donec blandit ipsum mauris, ornare aliquam mauris malesuada et."
                }
            },
            {
                "_index": "text-analysis",
                "_type": "_doc",
                "_id": "VK4Nvm4BAE1yJwCxLvw_",
                "_score": 1.0,
                "_source": {
                    "data_set": "lorem_ipsum",
                    "sentence": "Sed et venenatis nibh."
                }
            },
            {
                "_index": "text-analysis",
                "_type": "_doc",
                "_id": "Va4Nvm4BAE1yJwCxLvxD",
                "_score": 1.0,
                "_source": {
                    "data_set": "lorem_ipsum",
                    "sentence": "Aliquam semper, lorem et commodo cursus, quam turpis commodo sem, eget aliquet est mi in nibh."
                }
            },
            {
                "_index": "text-analysis",
                "_type": "_doc",
                "_id": "Vq4Nvm4BAE1yJwCxLvxG",
                "_score": 1.0,
                "_source": {
                    "data_set": "lorem_ipsum",
                    "sentence": "Maecenas metus diam, volutpat pharetra elit consequat, posuere pretium nisi."
                }
            },
            {
                "_index": "text-analysis",
                "_type": "_doc",
                "_id": "V64Nvm4BAE1yJwCxLvxJ",
                "_score": 1.0,
                "_source": {
                    "data_set": "lorem_ipsum",
                    "sentence": "Nam sem ligula, laoreet varius viverra quis, tincidunt id dui."
                }
            },
            {
                "_index": "text-analysis",
                "_type": "_doc",
                "_id": "WK4Nvm4BAE1yJwCxLvxL",
                "_score": 1.0,
                "_source": {
                    "data_set": "lorem_ipsum",
                    "sentence": "Integer tempor pulvinar mi, ut dignissim erat blandit et."
                }
            }
        ]
    },
    "aggregations": {
        "most_common_words": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 425,
            "buckets": [
                {
                    "key": "et",
                    "doc_count": 11
                },
                {
                    "key": "nec",
                    "doc_count": 11
                },
                {
                    "key": "ut",
                    "doc_count": 11
                },
                {
                    "key": "eget",
                    "doc_count": 9
                },
                {
                    "key": "sed",
                    "doc_count": 9
                },
                {
                    "key": "aliquam",
                    "doc_count": 8
                },
                {
                    "key": "est",
                    "doc_count": 8
                },
                {
                    "key": "in",
                    "doc_count": 8
                },
                {
                    "key": "molestie",
                    "doc_count": 8
                },
                {
                    "key": "quis",
                    "doc_count": 8
                }
            ]
        }
    }
}

预期响应:

代码语言:javascript
复制
{
    "aggregations": {
        "most_common_words": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 425,
            "buckets": [
                {
                    "key": "et",
                    "doc_count": 11,
                    "occurrences": [
                        "A sentence with word et in it.",
                        "A sentence with word et in it.",
                        "A sentence with word et in it.",
                        "A sentence with word et in it.",
                        "A sentence with word et in it.",
                        "A sentence with word et in it.",
                        "A sentence with word et in it.",
                        "A sentence with word et in it.",
                        "A sentence with word et in it.",
                        "A sentence with word et in it.",
                        "A sentence with word et in it."
                    ]
                },
                {
                    "key": "nec",
                    "doc_count": 11,
                    "occurrences": [
                        "A sentence with word nec in it.",
                        "A sentence with word nec in it.",
                        "A sentence with word nec in it.",
                        "A sentence with word nec in it.",
                        "A sentence with word nec in it.",
                        "A sentence with word nec in it.",
                        "A sentence with word nec in it.",
                        "A sentence with word nec in it.",
                        "A sentence with word nec in it.",
                        "A sentence with word nec in it.",
                        "A sentence with word nec in it."
                    ]
                }
                ...
            ]
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2019-12-01 05:21:12

使用size:0,你不会在你的结果中得到命中。

代码语言:javascript
复制
POST text-analysis/_search
{
    "size":0,
    "aggs" : {
        "most_common_words" : {
            "terms": {
                "field" : "sentence"
            }
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59120153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档