通过python中的Elastic Search搜索唯一值

绝招

我正在尝试在“说明”列中获取唯一值。根据我的数据,我有很多类似的描述。我只想要独特的。

con.search(index='data', body={
        "aggs": {
            "query": {
                "match": {"description": query_input}
            },
            "size": 30,
            "distinct_description": {
            }
        }


    })

但是,这根本无法解决。有什么建议。

例:

{id: 1, state: "OP", description: "hot and humid"}
{id: 2, state: "LO", description: "dry"}
{id: 3, state: "WE", description: "hot and humid"}
{id: 4, state: "OP", description: "green and vegetative"}
{id: 5, state: "HP", description: "dry"}

结果:

{id: 1, state: "OP", description: "hot and humid"}
{id: 2, state: "LO", description: "dry"}
{id: 4, state: "OP", description: "green and vegetative"}
羽扇豆类

您应该尝试在description.keyword子字段上进行术语汇总

body = {
  "query": {
    "match": {"state": query_input}
  },
   "size":1000,
  "aggs": {
    "distinct_descriptions": {
      "terms": {
        "field": "description.keyword"
      }
    }
  }
}

result = con.search(index='data', body=body)
occurrences_list = list()
occurrences_dict = {"description":None, "score":None}
for res in result["aggregations"]["distinct_descriptions"]["buckets"]:
    occurrences_dict["description"] = {res['key'] : res['doc_count'] }
    occurrences_list.append( occurrences_dict )

for res in result["hits"]["hits"]:
    for elem in occurrences_list:
        if res["_source"]["description"] == elem['description']:
            if not elem["score"]:
                elem["score"] = res["_score"]

请注意星期一产生的查询,现在还有一个size参数,否则elasticsearch默认只检索20个匹配

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

根据 Excel 列中的唯一值进行搜索

来自分类Dev

从搜索查询中获取唯一值以填充搜索过滤器选项

来自分类Dev

唯一值python

来自分类Dev

在电子表格的所有列中搜索唯一值

来自分类Dev

Excel 公式用于在两列中搜索一列中的唯一值并在另一列中搜索固定值

来自分类Dev

列表中的唯一值

来自分类Dev

Sequelize中的唯一值

来自分类Dev

数组中的唯一值

来自分类Dev

聚合Python中的唯一值

来自分类Dev

Python 列表中的唯一值

来自分类Dev

一种非索引的方式来搜索R中两个列表之间的唯一值

来自分类Dev

MySQL:根据一列进行搜索,每列中的每个唯一值均与最长时间

来自分类Dev

如何使用通用唯一键从各种表中搜索并显示每个表中的相应值

来自分类Dev

通过字典中的键具有唯一值

来自分类Dev

通过唯一值设置面板数据

来自分类Dev

通过唯一值融合多列

来自分类Dev

CouchDB中唯一键的唯一值

来自分类Dev

在主键不是唯一的CSV中解析唯一值

来自分类Dev

如何在Python中为唯一值分配值

来自分类Dev

使用Elastic Search + Kibana检索列中搜索的字符串的值计数

来自分类Dev

动态分配唯一值-Python

来自分类Dev

Python汇总值并收集唯一值

来自分类Dev

Python - 唯一值的平均值

来自分类Dev

遍历PANDAS中的唯一值

来自分类Dev

合并R中的唯一值

来自分类Dev

如何检查序列中的值是唯一的

来自分类Dev

确保QTableView中的列的唯一值

来自分类Dev

如何从向量中删除唯一值

来自分类Dev

Matlab中唯一值的累积计数