elasticsearch,Search,Indexing,elasticsearch" /> elasticsearch,Search,Indexing,elasticsearch" />

仅更新elasticsearch中的特定字段值

仅更新elasticsearch中的特定字段值,search,indexing,elasticsearch,Search,Indexing,elasticsearch,是否可以在不覆盖其他字段的情况下更新elasticsearch中的某些特定字段值 是,Elasticsearch支持部分更新。这意味着您可以提交: 将与现有文档合并的部分文档 将在现有文档上执行的脚本 看一看这张照片。 在这两种情况下,由于底层lucene库的工作方式,在后台发生的事情是检索要更新的文档,对其应用更改,并用新文档覆盖旧文档。在一天结束时,它实际上是对文档的完全重写,但您不必提交整个文档,除非您禁用了,默认情况下已启用,该字段允许您检索整个文档以便对其应用更改。作为此答案的基于

是否可以在不覆盖其他字段的情况下更新elasticsearch中的某些特定字段值

是,Elasticsearch支持部分更新。这意味着您可以提交:

  • 将与现有文档合并的部分文档
  • 将在现有文档上执行的脚本
看一看这张照片。
在这两种情况下,由于底层lucene库的工作方式,在后台发生的事情是检索要更新的文档,对其应用更改,并用新文档覆盖旧文档。在一天结束时,它实际上是对文档的完全重写,但您不必提交整个文档,除非您禁用了,默认情况下已启用,该字段允许您检索整个文档以便对其应用更改。

作为此答案的基于代码的贡献,可以使用以下查询:

POST /index/type/100100471/_update
{
    "doc" : {
        "yourProperty" : 10000
    }
}
此查询仅更新
yourProperty
属性

因此,出现以下响应:

{
   "_index": "index",
   "_type": "type",
   "_id": "100100471",
   "_version": 1,
   "_shards": {
      "total": 0,
      "successful": 1,
      "failed": 0
   }
}

如果只想更新现有字段值,则必须尝试以下解决方案:

POST IndexName/_update_by_query
{
  "script": {
    "source": """

   if (ctx._source?.Field != null) 
    {  
        ctx._source.remove('Field');
        ctx._source.put('Field', 'Value');
    }   
    """,
    "lang": "painless"
  },
  "query": {
    "terms": {
        "_id": [
          1 (Replace with Document ID)
        ]
      }
  }
}
POST IndexName/_update_by_query
{
  "script": {
    "source": """

   if (ctx._source?.NewField == null) 
    {  
        ctx._source.hf.put('NewField', 'Value');
    }   
    """,
    "lang": "painless"
  },
  "query": {
    "terms": {
        "_id": [
          1 (Replace with Document ID)
        ]
      }
  }
}
如果要添加具有值的新字段,则必须尝试以下解决方案:

POST IndexName/_update_by_query
{
  "script": {
    "source": """

   if (ctx._source?.Field != null) 
    {  
        ctx._source.remove('Field');
        ctx._source.put('Field', 'Value');
    }   
    """,
    "lang": "painless"
  },
  "query": {
    "terms": {
        "_id": [
          1 (Replace with Document ID)
        ]
      }
  }
}
POST IndexName/_update_by_query
{
  "script": {
    "source": """

   if (ctx._source?.NewField == null) 
    {  
        ctx._source.hf.put('NewField', 'Value');
    }   
    """,
    "lang": "painless"
  },
  "query": {
    "terms": {
        "_id": [
          1 (Replace with Document ID)
        ]
      }
  }
}

在ES 7.3中,新格式为:

POST /myindex/_update/mydocid
{
    "doc" : {
        "myfield": "new value of my field"
    }
}
试试这个

curl -XPOST --header 'Content-Type: application/json' https://search-testarticle-2cwv6oh7wtz3hxowgxv3rprnsa.ap-south-1.es.amazonaws.com/test/_update/4gI4knQB7d5sAgV24YPy -d '{
"doc":  { "name" : "Ankit Singh Raj" }
}'

您还可以使用批量更新部分更新多个文档

POST /foo_v1/_bulk?pretty=true&error_trace=true
{"update":{"_index":"foo_v1","_type":"footype","_id":"397"}}
{"doc":{"Name":"foo","EmailId":"foo@test.com"}}
{"update":{"_index":"foo_v1","_type":"footype","_id":"398"}}
{"doc":{"Name":"foo1","EmailId":"foo1@test.com"}}
如果不确定是更新还是像Upsert一样插入,则可以执行以下操作:

POST /foo_v1/_bulk?pretty=true&error_trace=true
{"update":{"_index":"foo_v1","_type":"footype","_id":"397"}}
{"doc":{"Name":"foo","EmailId":"foo@test.com"}, "doc_as_upsert" : true}
使用“文档作为插入”:true

按查询API更新

更新与指定查询匹配的文档。如果未指定查询,则在不修改源的情况下对数据流或索引中的每个文档执行更新,这对于获取映射更改非常有用

POST http://localhost:9200/INDEX_NAME/_update_by_query
{
  "script": {
    "source": "ctx._source.userName=new_user_name",
    "lang": "painless"
  },
  "query": { 
    "term": {
      "userName": "old_user_name"
    }
  }
}   

在上面的示例中,用户名字段值将更新为new_user_name

是,谢谢您的帮助。我使用了脚本格式,然后继续。您可以看看这个问题吗?我得到的
验证失败:1:脚本或文档丢失
-知道为什么吗?@rahulg没有类型的新语法是POST/yourindexname/_update/youridnumber{“doc”:{“yourProperty”:10000}我需要帮助在elasticsearch中使用Api update,问题是当我想在结构内部进一步更新此级别时,我遇到了什么问题,例如:{“mainLevel”:[{“name”:“maxi”,“SecundaryLevel”:[{“title”:“bamber”}]}}在这种情况下,更新希望在“SecondaryLevel”中执行,但对于某个“MainLevel”,我将我的索引留在下面。Max您可以看到我下面的注释。这样,如果您执行POST/foo_v1/_bulk?pretty=true&error_trace=true{”更新“{u索引”:“foo_v1”,“_类型”:“footype”,“_id”:“397”}{“doc”:{“SecundaryLevel”:{“title”:“已婚”,“EmailId”:”married@gmail.com“}}因此,此查询只会更新文档中的“SecondaryLevel”对象和特定id。我希望这能回答您的问题。很抱歉,回复太晚,我错过了您之前的评论。