首页 > 解决方案 > Python,为什么 Elasticsearch 不根据给定的查询返回记录?

问题描述

我是弹性搜索的新手,我正在尝试通过多匹配查询获取数据。

以下数据示例:

{   
    .......
    "_source": {
        "itemid": 40233,
        "value": "176016",
    }
}
{   
    .......
    "_source": {
        "itemid": 40238,
        "value": "176016",
    }
}
{   
    "_index": "uint-2018-12-04",
    .......
    "_source": {
        "itemid": 40203,
        "value": "176016",
    }
}

我的代码:

def __init__(self, CustomerName, DeviceID):
    self.client = Elasticsearch(hosts=['***.***.***.***'])
    self.search = Search(using=self.client, index="ind-*").extra(size=50)

def get_data_test(self):
    self.search.query("match", itemid=40233)
    response = self.search.execute()
    for hitX in response.hits.hits:
        print(hitX)

它有效,但没有返回预期的记录,它返回所有记录

标签: pythonelasticsearchelasticsearch-dsl

解决方案


query方法返回对象的副本Search。你需要做:

s = self.search.query("match", itemid=40233)
response = s.execute()
...

推荐阅读