# Serch operators code from full phone number

**URL:** <https://discuss.elastic.co/t/serch-operators-code-from-full-phone-number/24875>\
**Category:** Elasticsearch\
**Created:** [July 3, 2015, 3:35pm UTC](https://discuss.elastic.co/t/serch-operators-code-from-full-phone-number/24875 "2015-07-03T15:35:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![LegotinK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/legotink/32/3567_2.png) [@LegotinK](https://discuss.elastic.co/u/LegotinK)\
**Post date:** [July 3, 2015, 3:35pm UTC](https://discuss.elastic.co/t/serch-operators-code-from-full-phone-number/24875/1 "2015-07-03T15:35:23Z")

</div>

Hi.  
I have index with two field in documents like  
_direction\_name, direction\_code_  
and data like:  
_Ukraine mobile, 38067_  
_Ukraine mobile, 38063_  
_Ukraine fixed, 380_

How I can search operator nume by full phone number like _380673421233_ ?  
It must return code _Ukraine mobile =\> 38067_  
And if number  
_3801231231212_  
it must return  
_Ukraine fixed =\> 380._

for MySQL I used query:

```
SELECT dc_direction
            FROM direct_codes
            WHERE 3801231231212 LIKE dc_code || '%'
            ORDER BY LENGTH(dc_code) DESC LIMIT 1

```

All data is for example.  
Thanks.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [July 3, 2015, 4:00pm UTC](https://discuss.elastic.co/t/serch-operators-code-from-full-phone-number/24875/2 "2015-07-03T16:00:19Z")

</div>

> [@LegotinK](#):
>
> direction\_name, direction\_codeand data like:Ukraine mobile, 38067Ukraine mobile, 38063Ukraine fixed, 380

Its easier to help if you post the actual JSON.

> [@LegotinK](#):
>
> How I can search operator nume by full phone number like 380673421233 ?It must return code Ukraine mobile =\> 38067And if number 3801231231212 it must return Ukraine fixed =\> 380.

You want the longest matching prefix? You could do it with a term query against the direction\_code field if you set the index\_analyzer to use the keyword tokenizer and the query\_analyzer to use the edgeNGram tokenizer. You'd have to index the length of the direction\_code as integer field and add a sort on it. Its not a particularly strait forward thing to do but its not super hard either.

Under the covers this is implemented super differently than the way MySQL would do it. Expect some overhead on the first query but subsequent queries to be much faster.

Since this kind of list doesn't change much its probably a good idea to \_optimize it to a single segment after loading it to get optimal performance. But only because it doesn't change much.

How many of these prefix codes do you have? How deep do you go in the number list and how many regions is it? NANPA's can be broken down into something like ten million of these if you are willing to go to the "exchange" level and that's just two countries.

Anyway, if you only have tens of thousands of these things I'd suggest going with a single shard. It'll be faster to query.

---

<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, 12:03am UTC](https://discuss.elastic.co/t/serch-operators-code-from-full-phone-number/24875/3 "2017-07-06T00:03:41Z")

</div>


