简体   繁体   中英

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

I am trying to update particular field in document based on some condition. In general sql way, I want to do following.

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

I am using below to update all the documents but I am not able to add any condition.

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

Here is the template, I am using:

{
  "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
              }
            }
          }
        }
      }
    }
  }
}

Could someone guide me how to add condition to it as mentioned above for the source and name.

Thanks, Babu

You can make use of the below query to what you are looking for. I'm assuming name and source are your fields in your index.

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"
            }
          }
        }
      ]
    }
  }
}

You can probably make use of any of the Full Text Queries or Term Queries inside the Bool Query for either searching/updating/deletions.

Do spend sometime in going through them.

Note: Make use of Term Queries only if your field's datatype is keyword

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM