你可以用下面的命令插入資料,比如我放了3個檔案,但我有大約1000多個檔案
test = [{'id':1,'name': 'A', 'subject': ['數學', '會計']。
'type':'Contract', 'Location':'NY'},
{ 'id':2, 'name': 'AB', 'subject': ['物理', '工程']。
'type':'Permanent','Location':'NY'},
{'id':3, 'name': 'ABC', 'subject': ['數學', '工程']。
'type':'Permanent','Location':'NY'}]
from elasticsearch import Elasticsearch
es = Elasticsearch()
for e in test:
es.index(index="myindex", body=e, id=e['id'] )
- 我需要從如果
name匹配AB的檔案中洗掉。
我試著用下面的命令
names = ['AB'/span>]
for name in names。
es.delete_by_query(index="myindex"/span>, body={'name': name})
我得到了決議例外,[name]中的VALUE_STRING的未知鍵
。
在插入索引時,我可以忽略帶有
AB的名字,但在這里,如果名字與AB相匹配,我需要從索引中洗掉。
uj5u.com熱心網友回復:
你需要向delete_by_query函式的bodyArgument提供一個實際的查詢。 像這樣:
for name in names。
es.delete_by_query(index="myindex"/span>, body={
"query": {
"匹配": {
"name": name
}
}
})
# or if you can't update the doc mapping for the field: name.
for name in names:
es.delete_by_query(index="myindex"/span>, body={
"query": {
"term": {
"name.keyword": name
}
}
})
請記住,這里使用的term-query只給你精確的匹配。
這里有一些進一步的閱讀。https://elasticsearch-py.readthedocs.io/en/7.10.0/api.html#elasticsearch.Elasticsearch.delete_by_query of the Api-Documentation
更新使用的查詢:
term用于查詢精確的值,檔案中還指出該術語不應該用于文本欄位在文本欄位上,你應該使用
match查詢,它可以回傳有一定分數的檔案在這種情況下,為了獲得精確的匹配,你可以在id上做一個
term查詢,或者更新你的doc-mapping,使name-field成為一個關鍵詞你也可以用
.keyword來更新查詢,但這并不被建議作為一般的解決方案,因為它對查詢性能有負面影響(單詞標記化是在查詢時間而不是在索引時間完成的)
更多閱讀:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/315179.html
標籤:
上一篇:如何使用彈性搜索在查詢中搜索術語
