# "Contains" query in a numberic/date field

**URL:** <https://discuss.elastic.co/t/contains-query-in-a-numberic-date-field/130298>\
**Category:** Elasticsearch\
**Created:** [May 2, 2018, 4:23pm UTC](https://discuss.elastic.co/t/contains-query-in-a-numberic-date-field/130298 "2018-05-02T16:23:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wifly](https://avatars.discourse-cdn.com/v4/letter/w/c67d28/32.png) [@wifly](https://discuss.elastic.co/u/wifly)\
**Post date:** [May 2, 2018, 4:23pm UTC](https://discuss.elastic.co/t/contains-query-in-a-numberic-date-field/130298/1 "2018-05-02T16:23:01Z")

</div>

Hi all,  
I'm trying to perform a contains query in a field which is numeric, is that possible?  
Let's say for example

`.../_mapping`  
`"index": { "mappings": { "element": { "properties": { "name": { "type": "string }, "code": { "type": "integer" }, "amendmentDate": { "type": "date", "format": "dd/MM/yyyy" } } } } }`

I'm trying things like:  
`.../_search`  
`{ "query":{ "query_string":{ "query":"(code: /.*1313.*/)" } } }`

my objective is to find numbers like 1313, 13135, 5551313555,

Is there a way to cast the field to string before applying the query, or search over a computed "stringized" field ?

It would be great to be able to do it over the date field too, that's my next step 🙂

I'm getting NumberFormatExceptions for numbers, "Invalid format: "20" is too short" for dates... and have tried with regexes, without, with quotes ...

Sorry if it's very obvious or already answered, I come from the sql world.. I've been digging in the documentation and the forums for a few hours and haven't been able to find anything.  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Dan\_Markhasin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dan_markhasin/32/14187_2.png) [@Dan\_Markhasin](https://discuss.elastic.co/u/Dan_Markhasin)\
**Post date:** [May 2, 2018, 4:31pm UTC](https://discuss.elastic.co/t/contains-query-in-a-numberic-date-field/130298/2 "2018-05-02T16:31:07Z")

</div>

You can do it with a script:

> {  
> "from":0,  
> "size":10,  
> "query":{  
> "bool":{  
> "must":[  
> {  
> "script":{  
> "script":"doc['code'].value.toString() =~ /._1313._/"  
> }  
> }  
> ]  
> }  
> }  
> }

---

<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:** [May 30, 2018, 4:31pm UTC](https://discuss.elastic.co/t/contains-query-in-a-numberic-date-field/130298/3 "2018-05-30T16:31:11Z")

</div>

Closed on May 30, 2018, 4:31pm UTC

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