如何计算ES中所有文档中匹配的关键字数量 [英] How to count the number of keyword matched across the docs in ES

查看:101
本文介绍了如何计算ES中所有文档中匹配的关键字数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AWS托管服务中托管了弹性搜索文档,我想获取结果集中存在的关键字数量.即假设我的文档中的键中包含文本数据,而我正在搜索键X,Y,Z.它给我返回了N个文档.我怎么能得到,N个文档中X出现了多少次,Y存在而Z出现了多少次?例如:我有以下文档:

Hi I have elastic search docs hosted in AWS managed services, I want to get the number of keywords present in the resultset. i.e lets say my docs have a textual data in a key and I am searching for keys X,Y,Z. it returns me an N number of docs. How can I get, how many times X is present in N docs, how many times Y is present and Z is present. For e.g: I have the following docs :

{
"id":1,
"date":"2019-04-24 12:10:05",
"text":"Hi I am from India and here for few days only",
"groupName":"India Toruist"
}

{
"id":2
"date":"2019-04-24 12:10:06",
"text":"Hi I am from Sri Lanka went to India in past and will stay here for 18 days only",
"groupName":"Sri Lanks Tourist"
}

现在我正在根据文本键搜索斯里兰卡和印度.它将同时返回文件1和2.我想知道印度发生了多少次,斯里兰卡发生了多少次.

Now I am searching for Sri Lanka and India against text key.It will return me both doc 1 and 2. I want how many time India has occurred and how many times Sri Lanka has occurred.

推荐答案

您可以通过使用聚合来做到这一点.以下是可能对您有帮助的查询.

You can do this by using aggregations. Below is the query which might help you.

    {
    "size": "0",
    "aggs": {
        "docs_groupName_in": {
            "terms": {"field": "groupName.keyword","include" : ".*India.*"}
        },
"docs_groupName_sl": {
            "terms": {"field": "groupName.keyword","include" : ".*Sri Lanka.*"}
        }
    }
}

希望有帮助.

这篇关于如何计算ES中所有文档中匹配的关键字数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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