我在elasticsearch中有一个JSON文件列表。
我有一个字符串列表matching
,我想将其用作搜索条件。
其中,matching = ["223232_ds","dnjsnsd_22","2ee2i33","mkddsj2220","23e3efdjn"
我现在需要在elasticsearch中找到这些记录,其中两个键包含此列表中的值matching
。
如果没有elasticsearch并只是将JSON加载为python对象,我可以这样做:
results= []
for record in JSON_list:
if record['key_1'] in matching and record['key_2'] in matching:
results.append(record)
JSON_list
看起来像这样:
[{'key_1' : "blahaksds",
'key_2' : "njasdnjkns"},
{'key_1' : "bladfgfdf",
'key_2' : "njasdsfsdrr"}]
如何在es中搜索多个条件?以前,我已经使用此设置直接搜索record_id。
es = elasticsearch.Elasticsearch()
name = "so_sample"
# Formulate query
query = str("_id:"+'"'+ record_id +'"')
# Query
result = es.search(name,q=query)
答案 0 :(得分:0)
您可以在must子句中使用bool query和terms queries两个{{3}},如下所示:
{
"query": {
"bool": {
"must": [
{
"terms": {
"key_1": ["223232_ds","dnjsnsd_22","2ee2i33","mkddsj2220","23e3efdjn"]
}
},
{
"terms": {
"key_2": ["223232_ds","dnjsnsd_22","2ee2i33","mkddsj2220","23e3efdjn"]
}
}
]
}
}
}