# Update By Query | Alias

**URL:** <https://discuss.elastic.co/t/update-by-query-alias/342834>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [September 12, 2023, 12:52pm UTC](https://discuss.elastic.co/t/update-by-query-alias/342834 "2023-09-12T12:52:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankitpandoh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankitpandoh/32/125219_2.png) [@ankitpandoh](https://discuss.elastic.co/u/ankitpandoh)\
**Post date:** [September 12, 2023, 12:52pm UTC](https://discuss.elastic.co/t/update-by-query-alias/342834/1 "2023-09-12T12:52:06Z")

</div>

I am able to add a document to an index say my-main-index having some alias my-alias-index. When I did a search like below, I was able to get the documents.

```auto
GET /my-main-index/_search
{
  "query":{
    "match_all": {}
  }
}

or

GET /my-alias-index/_search
{
  "query":{
    "match_all": {}
  }
}

```

But when I try to do an update\_by\_query, none of the below is working. I am getting 200 response with no record is being updated.

```auto
POST /my-main-index/_update_by_query
{
  "script": {
    "source": "ctx._source['customers.company'] = params.company",
    "lang": "painless",
    "params": {
      "company": "apple"
    }
  },
  "query": {
    "term": {
      "customers.handle": "search-user815"
    }
  }
}

OR 

POST /my-alias-index/_update_by_query
{
  "script": {
    "source": "ctx._source['customers.company'] = params.company",
    "lang": "painless",
    "params": {
      "company": "apple"
    }
  },
  "query": {
    "term": {
      "customers.handle": "search-user815"
    }
  }
}

```

Could someone please help me identifying the issue?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 12, 2023, 1:06pm UTC](https://discuss.elastic.co/t/update-by-query-alias/342834/2 "2023-09-12T13:06:45Z")

</div>

What's the mapping, specifically for the field `customers.handle`?

---

<div class="post-metadata">

**Author:** ![ankitpandoh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankitpandoh/32/125219_2.png) [@ankitpandoh](https://discuss.elastic.co/u/ankitpandoh)\
**Post date:** [September 12, 2023, 1:14pm UTC](https://discuss.elastic.co/t/update-by-query-alias/342834/3 "2023-09-12T13:14:54Z")

</div>

```auto
"handle": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword"
                }
              },
              "norms": false,
              "analyzer": "custom_analyzer"

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 12, 2023, 1:20pm UTC](https://discuss.elastic.co/t/update-by-query-alias/342834/4 "2023-09-12T13:20:55Z")

</div>

That's why it's failing. Try on `customers.handle.keyword` instead:

```auto
POST /my-alias-index/_update_by_query
{
  "script": {
    "source": "ctx._source['customers.company'] = params.company",
    "lang": "painless",
    "params": {
      "company": "apple"
    }
  },
  "query": {
    "term": {
      "customers.handle.keyword": "search-user815"
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![ankitpandoh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankitpandoh/32/125219_2.png) [@ankitpandoh](https://discuss.elastic.co/u/ankitpandoh)\
**Post date:** [September 12, 2023, 1:30pm UTC](https://discuss.elastic.co/t/update-by-query-alias/342834/5 "2023-09-12T13:30:25Z")

</div>

Yeah, it is working now. Thank you so much for pointing it out.  
But why does term query not working?

```auto
"query": {
    "term": {
      "customers.handle": "search-user815"
    }
  }

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 12, 2023, 1:51pm UTC](https://discuss.elastic.co/t/update-by-query-alias/342834/6 "2023-09-12T13:51:26Z")

</div>

Because you are using a `text` type for this field. Which means that the content is analyzed.  
And basically, your `"search-user815"` is indexed as `["search", "user815"]`.  
When searching with a `term` query, the searched text is not analyzed so you are trying to compare `search-user815` with `search` or `user815`, which obviously does not match.

Read more about this from: [Text analysis | Elasticsearch Guide [8.9] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis.html)

---

<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:** [October 10, 2023, 1:51pm UTC](https://discuss.elastic.co/t/update-by-query-alias/342834/7 "2023-10-10T13:51:56Z")

</div>

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