# Documents with Regex fields

**URL:** https://discuss.elastic.co/t/documents-with-regex-fields/195833
**Category:** Elasticsearch
**Created:** [August 20, 2019, 2:21am UTC](https://discuss.elastic.co/t/documents-with-regex-fields/195833 "2019-08-20T02:21:41Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![krish3](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@krish3](https://discuss.elastic.co/u/krish3)
#### Post date: [August 20, 2019, 2:21am UTC](https://discuss.elastic.co/t/documents-with-regex-fields/195833/1 "2019-08-20T02:21:41Z")

</div>

I have documents with fields themselves being a regular expression.  
For example  
doc1: regexp:1111.\*01011  
doc2: regexp: 111.\*01011

So if I give a query with regexp:1111010111101011 should return doc1 and doc2, while a query with regexp:111011101011 should return only doc2. Is this type of query possible with Elastic? If not any alternate way of using Elastic in achieving this?

Thanks

---

<div class="post-metadata">

### Author: ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)
#### Post date: [August 20, 2019, 11:21am UTC](https://discuss.elastic.co/t/documents-with-regex-fields/195833/2 "2019-08-20T11:21:52Z")

</div>

Yes, this is possible! You can use [the percolator](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-percolate-query.html) for that (one of my favorite Elasticsearch features!). The percolator allows you to index queries, and then later ask Elasticsearch if a given document matches those indexed queries.

To use the percolator, first you need to define a field of type `percolator` in the index' mapping. Here I'm defining a field `my_query` of that type, as well as a field `my_expression` that you can match the regular expressions against:

```auto
PUT my_index
{
  "mappings": {
    "properties": {
      "my_expression": {
        "type": "keyword"
      },
      "my_query": {
        "type": "percolator"
      }
    }
  }
}

```

Now, you can index your regular expressions. Here I do so in the form of `regexp` queries that I index into the `my_query` field:

```auto
PUT /my_index/_doc/1
{
  "my_query": {
    "regexp": {
      "my_expression": "1111.*01011"
    }
  }
}

PUT /my_index/_doc/2
{
  "my_query": {
    "regexp": {
      "my_expression": "111.*01011"
    }
  }
}

```

Finally, you can now test a given pattern using the `percolate` query:

```auto
GET /my_index/_search
{
  "query": {
    "percolate": {
      "field": "my_query",
      "document": {
        "my_expression": "111011101011"
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![krish3](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@krish3](https://discuss.elastic.co/u/krish3)
#### Post date: [August 20, 2019, 3:51pm UTC](https://discuss.elastic.co/t/documents-with-regex-fields/195833/3 "2019-08-20T15:51:11Z")

</div>

Andon, Thanks for the reply. I think this should solve my use case. So what is the performance if we have say a million documents each having a percolator field?

- Krish

---

<div class="post-metadata">

### Author: ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)
#### Post date: [August 21, 2019, 6:50am UTC](https://discuss.elastic.co/t/documents-with-regex-fields/195833/4 "2019-08-21T06:50:02Z")

</div>

Good question. The Percolator doesn't quite scale the same as the other queries in Elasticsearch. The response time will basically be linear with the amount of stored percolator queries (although there are some optimizations, as detailed in [the documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-percolate-query.html#how-it-works)).

The Percolator is one of the few examples of when it may be better to have more shards. That's because each of these shards will hold a subset of the stored percolator queries. If you have multiple shards then those will be able to percolate a document in parallel.

You probably need to do some testing with different numbers of documents and shards to see what an optimum for your cluster would be.

---

<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: [September 18, 2019, 6:50am UTC](https://discuss.elastic.co/t/documents-with-regex-fields/195833/5 "2019-09-18T06:50:03Z")

</div>

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