我是Elasticsearch的新手,所以我来这里是为了寻求建议。 我有两个不同的csv文件中的两个弹性索引。
index_1具有以下映射:
{'settings': {
'number_of_shards' : 3
},
'mappings': {
'properties': {
'place': {'type': 'keyword' },
'address': {'type': 'keyword' },
}
}
}
该文件长约40万个文档。 文件较小(约50个文档)的index_2具有以下映射:
{'settings': {
"number_of_shards" : 1
},
'mappings': {
'properties': {
'place': {'type': 'text' },
'address': {'type': 'keyword' },
}
}
}
索引_2中的字段“位置”是索引_1中的字段“位置”的所有唯一值。 在两个索引中,“地址”字段都是数据类型关键字的邮政编码,其结构为:0000AZ。
基于索引_1中的“位置”字段关键字,我想从索引_2中分配“地址”字段的术语。
我尝试使用pandas库,但是index_1文件太大。我还不得不尝试基于熊猫和elasticsearch创建模块,但是没有成功。尽管我相信这是一个有希望的方向。一个很好的解决方案是尽可能多地留在Elasticsearch库中,因为这些索引将在以后用于进一步分析。
答案 0 :(得分:0)
如果我理解正确,听起来您想使用updateByQuery。
请求主体应如下所示:
{
'query': {'term': {'place': "placeToMatch"}},
'script': 'ctx._source.address = "updatedZipCode"'
}
这将使用匹配的位置更新所有文档的地址字段。
编辑:
所以我们要做的是在遍历index2中的所有文档时使用updateByQuery。
第一步:从index2获取所有文档,只需使用基本的search功能即可
{
"index": 'index2',
"size": 100 // get all documents, once size is over 10,000 you'll have to padginate.
"body": {"query": {"match_all": {}}}
}
现在,我们遍历所有结果,并对每个结果使用updateByQuery
:
// sudo
doc = response[i]
// update by query request.
{
index: 'index1',
body: {
'query': {'term': {'address': doc._source.address}},
'script': 'ctx._source.place = "`${doc._source.place}`"'
}
}