我正在尝试更新Elasticsearch Index的特定条目的一些值。我可以通过以下方式查看终端中的数据
curl -X GET "localhost:9200/pelias/_doc/custompoi:restaurant:33316604?pretty"哪一个返回
{
"_index" : "pelias",
"_type" : "_doc",
"_id" : "custompoi:restaurant:33316604",
...
"_source":{
"source": "custompoi"
"layer": "restaurant"
...其中_id的33316604的最后一部分对于每条记录都是唯一的。
curl -X POST "localhost:9200/pelias/_update/custompoi:restaurant:33316604?refresh=true&pretty=true" \
-H 'Content-Type: application/json' -d'
{
"script" : "ctx._source.layer = \u0027fast_food\u0027"
}
'问题是,我只有_id的这一部分33316604可以搜索。在关系数据库中,我可以使用*33316604搜索类似这样的内容。我尝试过类似的操作,但这里的星号*33316604不起作用。
有没有办法只使用_id的33316604部分来更新数据?
发布于 2021-08-11 04:13:27
您不能配置elasticsearch official documentation中提到的_id字段映射。
但是有一种方法,您可以基于_id字段更新数据。
使用n元语法分词器创建一个具有id字段的新索引。使用索引映射,如下所示
PUT old-index
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "ngram",
"min_gram": 2,
"max_gram": 10,
"token_chars": [
"letter",
"digit"
]
}
}
},
"max_ngram_diff": 10
},
"mappings": {
"properties": {
"id": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}将旧索引中的数据重新编制索引到新索引中
POST /_reindex
{
"source": {
"index": "old-index-name"
},
"dest": {
"index": "new-index-name"
}
}将_id字段的数据复制到id字段中
POST /new-index/_update_by_query
{
"script": "ctx._source.id = ctx._id"
}然后,您可以根据id字段更新数据。
{
"query": {
"match": {
"id": "33316604"
}
},
"script" : "ctx._source.layer = \u0027fast_food\u0027"
}发布于 2021-08-10 23:12:10
最好的方法是将该数值或整个字符串作为另一个字段(而不是_id字段)添加到文档中,并将其用于搜索。_id字段有点special;并不是所有的查询都被支持。
https://stackoverflow.com/questions/68732622
复制相似问题