在 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"}


DIEA
浏览 135回答 1
1回答

qq_遁去的一_1

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"]注意星期一产生的查询,现在还有一个大小参数,否则elasticsearch默认只检索20个命中
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python