# Searching Integer with wild cards

**URL:** <https://discuss.elastic.co/t/searching-integer-with-wild-cards/114262>\
**Category:** Elasticsearch\
**Created:** [January 5, 2018, 9:55am UTC](https://discuss.elastic.co/t/searching-integer-with-wild-cards/114262 "2018-01-05T09:55:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prashant.kasodariya](https://avatars.discourse-cdn.com/v4/letter/p/5fc32e/32.png) [@Prashant.kasodariya](https://discuss.elastic.co/u/Prashant.kasodariya)\
**Post date:** [January 5, 2018, 9:55am UTC](https://discuss.elastic.co/t/searching-integer-with-wild-cards/114262/1 "2018-01-05T09:55:54Z")

</div>

Can anyone please help me identifying how to use wild cards for integer searches? I tried using the inline script but no luck. This is what I have tried (I have also tried with different regex pattern which works for javascript but here with the query, it does not work)..

```
"query": {
        "bool": {
            "must": {
            	"script" : {
            		"script" : {
            			"inline" : "doc['statCode'].value.toString().match('/9.*/')",
            			"lang" : "javascript"
            		}
            	}   
            }
        }
    }
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 5, 2018, 10:13am UTC](https://discuss.elastic.co/t/searching-integer-with-wild-cards/114262/2 "2018-01-05T10:13:37Z")

</div>

Why do you want to do something so complex and so bad in term of performance?

What are the different values you can have for `statCode`? Can't you use a range query instead?

---

<div class="post-metadata">

**Author:** ![Prashant.kasodariya](https://avatars.discourse-cdn.com/v4/letter/p/5fc32e/32.png) [@Prashant.kasodariya](https://discuss.elastic.co/u/Prashant.kasodariya)\
**Post date:** [January 5, 2018, 10:34am UTC](https://discuss.elastic.co/t/searching-integer-with-wild-cards/114262/3 "2018-01-05T10:34:10Z")

</div>

My requirement is to support wild card search for integer fields? Do you suggest any other way to achieve? how can we use range query for this? If the field has the value say 00123789 and if I want to search with \*7 how do I use range query for it?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 5, 2018, 11:15am UTC](https://discuss.elastic.co/t/searching-integer-with-wild-cards/114262/4 "2018-01-05T11:15:36Z")

</div>

Some comments:

- `00123789` is not an `integer` but a `text`. `123789` might be a number indeed. So what is your mapping?
- If it's indexed as a text, then the wildcard query might work. See [https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html)
- Note that it will be extremely slow. Specifically with `*whatever` form as mentioned in documentation.
- You can use ngrams if you want to be able to search for substrings. See [https://www.elastic.co/guide/en/elasticsearch/reference/6.1/analysis-ngram-tokenizer.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.1/analysis-ngram-tokenizer.html). But it will use more space on disk and will take more time at index time. In which case wildcards won't be required as you will be able to search for something like `123` in you example.

---

<div class="post-metadata">

**Author:** ![Prashant.kasodariya](https://avatars.discourse-cdn.com/v4/letter/p/5fc32e/32.png) [@Prashant.kasodariya](https://discuss.elastic.co/u/Prashant.kasodariya)\
**Post date:** [January 5, 2018, 1:00pm UTC](https://discuss.elastic.co/t/searching-integer-with-wild-cards/114262/5 "2018-01-05T13:00:09Z")

</div>

Yes I have indexed those as String and those are working as well but sorting is an issue currently. if I index them as string then it will not sort it as number (sorting order will not be proper). so I am trying to see if we have wildcard support for integer fields or there is an easy way to achieve it.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 5, 2018, 1:16pm UTC](https://discuss.elastic.co/t/searching-integer-with-wild-cards/114262/6 "2018-01-05T13:16:26Z")

</div>

You can index the same field as both text and number. Then sort on the later.

---

<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:** [February 2, 2018, 1:16pm UTC](https://discuss.elastic.co/t/searching-integer-with-wild-cards/114262/7 "2018-02-02T13:16:41Z")

</div>

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