# Reindex w/ a regex

**URL:** <https://discuss.elastic.co/t/reindex-w-a-regex/326348>\
**Category:** Elasticsearch\
**Created:** [February 23, 2023, 4:14pm UTC](https://discuss.elastic.co/t/reindex-w-a-regex/326348 "2023-02-23T16:14:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vfeydel](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@vfeydel](https://discuss.elastic.co/u/vfeydel)\
**Post date:** [February 23, 2023, 4:14pm UTC](https://discuss.elastic.co/t/reindex-w-a-regex/326348/1 "2023-02-23T16:14:37Z")

</div>

I have lot of indices with same prefix. In those indices, I need to keep only document that have for example the field "abc" with 7 numbers only. My indices are already index in Elastic.  
It is possible , with a query or other, to reindex directly with dev tools the documents in there index that have 7 numbers in the field "abc" ?

Thanks by advance,  
Viviane

---

<div class="post-metadata">

**Author:** ![eMitch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emitch/32/93607_2.png) [@eMitch](https://discuss.elastic.co/u/eMitch)\
**Post date:** [February 23, 2023, 5:44pm UTC](https://discuss.elastic.co/t/reindex-w-a-regex/326348/2 "2023-02-23T17:44:51Z")

</div>

Hi @vfeydel

There are a couple of ways you can handle this. One way is to use the reindex command and specify a source query with a painless script. Something like this:

```auto
POST _reindex
{
  "source": {
    "index": "source-index-name",
    "query": {
      "bool": {
        "filter": {
          "script": {
            "script": {
              "lang": "painless",
              "source": "if (doc['abc'].size() == 0) { return false;} else {return doc['abc'].value.length() == 7;}"
            }
          }
        }
      }
    }
  },
  "dest": {
    "index": "dest-index-name"
  }
}

```

Another way could be to use an Ingest Pipeline to do this work and call the ingest pipeline in the DEST of the reindex.

---

<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 23, 2023, 5:45pm UTC](https://discuss.elastic.co/t/reindex-w-a-regex/326348/3 "2023-03-23T17:45:26Z")

</div>

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