Python 查询 Elasticsearch 两个索引的差集
你可以使用Elasticsearch的查询功能和Python的集合操作来查询两个索引的差集。下面是一个示例代码:
from elasticsearch import Elasticsearch
# 创建Elasticsearch客户端
es = Elasticsearch()
# 定义两个索引的名称
index1 = "index1"
index2 = "index2"
# 查询index1中的所有文档
query = {
"query": {
"match_all": {}
}
}
res1 = es.search(index=index1, body=query, size=10000)
# 查询index2中的所有文档
res2 = es.search(index=index2, body=query, size=10000)
# 获取index1和index2的文档ID集合
ids1 = set(hit['_id'] for hit in res1['hits']['hits'])
ids2 = set(hit['_id'] for hit in res2['hits']['hits'])
# 计算差集
diff = ids1 - ids2
# 打印差集的文档ID
print("差集:", diff)
这段代码使用Elasticsearch的search方法查询两个索引中的所有文档,并通过集合操作计算差集。请确保你已经安装了Elasticsearch的Python客户端库elasticsearch。
原文地址: https://www.cveoy.top/t/topic/pL8c 著作权归作者所有。请勿转载和采集!