简体   繁体   中英

Elasticsearch terms aggregation with occurrences

Is there a way to get a list of the most common words in the index along with the records in which those words occurred?

And btw. is there a way to get rid of hits from the result of the aggregation if I did not ask for it in the query?

I'm using Elasticsearch 7.4.0.

Sample query:

POST text-analysis/_search
{
    "aggs" : {
        "most_common_words" : {
            "terms": {
                "field" : "sentence"
            }
        }
    }
}

Response:

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

Expected response:

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

With size:0 you won't get hits in your results.

POST text-analysis/_search
{
    "size":0,
    "aggs" : {
        "most_common_words" : {
            "terms": {
                "field" : "sentence"
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM