# Need Help - with \_update\_by\_query query several indexes for two fields (one is optional)

**URL:** <https://discuss.elastic.co/t/need-help-with-update-by-query-query-several-indexes-for-two-fields-one-is-optional/326786>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [March 1, 2023, 4:37pm UTC](https://discuss.elastic.co/t/need-help-with-update-by-query-query-several-indexes-for-two-fields-one-is-optional/326786 "2023-03-01T16:37:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![brunofl](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@brunofl](https://discuss.elastic.co/u/brunofl)\
**Post date:** [March 1, 2023, 4:37pm UTC](https://discuss.elastic.co/t/need-help-with-update-by-query-query-several-indexes-for-two-fields-one-is-optional/326786/1 "2023-03-01T16:37:49Z")

</div>

Need help- with **\_update\_by\_query** to query several indexes for two fields (one is optional, for the indexes where the field exists).

I was doing two separate update by query, one when the index' document has a field common to all documents, and the other when need to update the same field, but in the query I need to update only the fields where second optional field exists and have a certain value.

The query I was using

```auto
GET /Index_pattern_*/_update_by_query
{
	"script": {
	    "source": "ctx._source.pm_data_source.hw_alias = 'newValue'",
	    "lang": "painless"
	  },
   "query" : { 
     "bool": {
        "must": [
        {"match" : {"pm_data_source.hw_alias": "oldValue"}},
        { "match": {"FieldThatSomeIndicesHave": "CertainValue"
                                                        } }
        ]
    }}
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 29, 2023, 4:38pm UTC](https://discuss.elastic.co/t/need-help-with-update-by-query-query-several-indexes-for-two-fields-one-is-optional/326786/2 "2023-03-29T16:38:05Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
