# Prefix query with any number

**URL:** <https://discuss.elastic.co/t/prefix-query-with-any-number/11140>\
**Category:** Elasticsearch\
**Created:** [March 13, 2013, 6:41pm UTC](https://discuss.elastic.co/t/prefix-query-with-any-number/11140 "2013-03-13T18:41:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BobbyTables](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bobbytables/32/2444_2.png) [@BobbyTables](https://discuss.elastic.co/u/BobbyTables)\
**Post date:** [March 13, 2013, 6:41pm UTC](https://discuss.elastic.co/t/prefix-query-with-any-number/11140/1 "2013-03-13T18:41:51Z")

</div>

So I'm attempting to generate a query that will retrieve anything that  
starts with a number. How would one accomplish this with ElasticSearch? I  
tried passing an array to the prefix query but that seems to have failed  
miserably. How would one do this?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [March 14, 2013, 10:54am UTC](https://discuss.elastic.co/t/prefix-query-with-any-number/11140/2 "2013-03-14T10:54:06Z")

</div>

On Wed, 2013-03-13 at 11:41 -0700, Bobby Tables wrote:

> So I'm attempting to generate a query that will retrieve anything that  
> starts with a number. How would one accomplish this with  
> Elasticsearch? I tried passing an array to the prefix query but that  
> seems to have failed miserably. How would one do this?

Before version 0.90 you will have to do something like this:

curl -XGET '[http://127.0.0.1:9200/\_all/\_search?pretty=1](http://127.0.0.1:9200/_all/_search?pretty=1)' -d '  
{  
"query": {  
"filtered": {  
"query": { "match": { "title": "whatever" }}  
"filter": {  
"bool" : {  
"should" : [  
{ "prefix" : { "my\_field" : 0 }},  
{ "prefix" : { "my\_field" : 1 }},  
{ "prefix" : { "my\_field" : 2 }},  
{ "prefix" : { "my\_field" : 3 }},  
{ "prefix" : { "my\_field" : 4 }},  
{ "prefix" : { "my\_field" : 5 }},  
{ "prefix" : { "my\_field" : 6 }},  
{ "prefix" : { "my\_field" : 7 }},  
{ "prefix" : { "my\_field" : 8 }},  
{ "prefix" : { "my\_field" : 9 }}  
]  
}  
}  
}  
}  
}  
'  
From 0.90 onwards, you can use the regexp filter:

curl -XGET '[http://127.0.0.1:9200/\_all/\_search?pretty=1](http://127.0.0.1:9200/_all/_search?pretty=1)' -d '  
{  
"query" : {  
"filtered" : {  
"filter" : {  
"regexp" : {  
"my\_field" : "[0-9].\*"  
}  
},  
"query" : {  
"match" : {  
"title" : "whatever"  
}  
}  
}  
}  
}  
'

Beware though - these are very heavy searches. They need to examine  
every term in "my\_field" to see if they match.

More efficient would be to store this information in your doc at index  
time, eg {"starts\_with\_number": true}

clint

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:46am UTC](https://discuss.elastic.co/t/prefix-query-with-any-number/11140/3 "2017-07-06T02:46:30Z")

</div>


