根据 Kibana/Elasticsearch 中的条件更新文档中的字段

Update field in a document based on the condition in Kibana/Elasticsearch

我正在尝试根据某些条件更新文档中的特定字段。一般来说 sql 方式,我想做以下事情。

Update index indexname
set name =  "XXXXXX"
where source: file and name :  "YYYYYY"

我正在使用下面更新所有文档,但我无法添加任何条件。

POST indexname/_update_by_query
{
  "query": { 
    "term": {
      "name": "XXXXX"
    }
  }
}

这是模板,我正在使用:

{
  "indexname": {
    "mappings": {
      "idxname123": {
        "_all": {
          "enabled": false
        },
        "properties": {
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "date1": {
            "type": "date",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "source": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

谁能指导我如何按照上面提到的来源和名称为其添加条件。

谢谢, 巴布

您可以使用以下查询来查找您要查找的内容。我假设 namesource 是您索引中的字段。

POST <your_index_name>/_update_by_query
{
  "script": {
    "inline": "ctx._source.name = 'XXXXX'",
    "lang": "painless"
  },
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "name": {
              "value": "YYYYY"
            }
          }
        },
        {
          "term": {
            "source": {
              "value": "file"
            }
          }
        }
      ]
    }
  }
}

您可以将 Full Text Queries or Term Queries inside the Bool Query 中的任何一个用于 searching/updating/deletions。

请花些时间仔细阅读它们。

注意: 仅当您的字段的数据类型为 keyword

时才使用 Term Queries

希望对您有所帮助!