# Java Regex Search

**URL:** https://discuss.elastic.co/t/java-regex-search/9406
**Category:** Elasticsearch
**Created:** [October 18, 2012, 11:55am UTC](https://discuss.elastic.co/t/java-regex-search/9406 "2012-10-18T11:55:36Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dean\_2](https://avatars.discourse-cdn.com/v4/letter/d/ce7236/32.png) [@dean\_2](https://discuss.elastic.co/u/dean_2)
#### Post date: [October 18, 2012, 11:55am UTC](https://discuss.elastic.co/t/java-regex-search/9406/1 "2012-10-18T11:55:36Z")

</div>

Does ElasticSearch support Java Regex search?  
For example,there is a field value"2008-12-03 some information here ....".  
I want use "\d{4}-\d{2}-\d{2}" to search it out.  
Can I do this with ElasticSearch?

--

---

<div class="post-metadata">

### Author: ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)
#### Post date: [October 18, 2012, 12:49pm UTC](https://discuss.elastic.co/t/java-regex-search/9406/2 "2012-10-18T12:49:39Z")

</div>

Hello,

I think I got lost in escaping so I couldn't do your exact expression  
but something like this works:

curl -XPOST localhost:9200/testindex/testtype/\_search?pretty=true -d '{  
"filter" : {  
"script" : {  
"script" : "\_source.fieldname ~= '"'2008.\*'"'"  
}  
}  
}'

However, that would be very slow compared to "normal" search  
operations, because it has to go through all the documents. You might  
not feel the difference with small amounts of data, but with bigger  
datasets it will get inconvenient.

Instead, you might want to structure your documents so that your date  
ends up in a separate field. If some document doesn't contain a date,  
that's fine, don't specify it. In the end you can filter documents  
that have a date using an "exists" filter:

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

Or the other ones with a "missing" filter:

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

That would be hugely faster than doing a regex. Plus, you can do all  
the nice stuff like filter with date ranges, etc.

## Best regards, Radu

[http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

On Thu, Oct 18, 2012 at 2:55 PM, dean [elasticbetter@gmail.com](mailto:elasticbetter@gmail.com) wrote:

> Does Elasticsearch support Java Regex search?  
> For example,there is a field value"2008-12-03 some information here ....".  
> I want use "\d{4}-\d{2}-\d{2}" to search it out.  
> Can I do this with Elasticsearch?
> 
> --

--

---

<div class="post-metadata">

### Author: ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://discuss.elastic.co/u/phill)
#### Post date: [October 19, 2012, 6:07pm UTC](https://discuss.elastic.co/t/java-regex-search/9406/3 "2012-10-19T18:07:41Z")

</div>

On 10/18/2012 5:49 AM, Radu Gheorghe wrote:

> However, that would be very slow compared to "normal" search  
> operations, because it has to go through all the documents. You might  
> not feel the difference with small amounts of data, but with bigger  
> datasets it will get inconvenient.  
> I definitely have to agree with the caveat mentioned.

Doing a RE on a field is to instantiate EVERY value of that field in the  
index and try the RE on it.  
Always pre-calculate and save whatever you can before search time!  
Build something that anticipates the question.

As Radu suggested, do the RE, but do it AS YOU INDEX, put it in another  
field. RE the "some information here"  
put that in another field. Keep the body of the text if you need to  
search that also.  
Using various other fields may allow you to extract other useful  
information or even multiple occurrences of the same date and text  
pattern in your text.

The date(s) can even go in a date type field. Then you can do date range  
operations on it. The alternative to a date field might be a long  
integer representing a typical time stamp.

Once you have various fields with the values you'll be searching, you  
can specify a query which involves the date, the "some information", and  
even the rest of the text. Each will involve all the indexing that ES  
and Lucene can provide instead of a string operation on free-form text  
at the last second.

Good luck,

-Paul

--

---

<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: [July 6, 2017, 3:07am UTC](https://discuss.elastic.co/t/java-regex-search/9406/4 "2017-07-06T03:07:54Z")

</div>


