# Best Match & Exact Match

**URL:** https://discuss.elastic.co/t/best-match-exact-match/58675
**Category:** Logstash
**Created:** [August 23, 2016, 11:56am UTC](https://discuss.elastic.co/t/best-match-exact-match/58675 "2016-08-23T11:56:54Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![uppaljs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/uppaljs/32/11535_2.png) [@uppaljs](https://discuss.elastic.co/u/uppaljs)
#### Post date: [August 23, 2016, 11:56am UTC](https://discuss.elastic.co/t/best-match-exact-match/58675/1 "2016-08-23T11:56:54Z")

</div>

Hi,

I have input coming in from a database , specifically call detail records , I need to parse the calling party number to identify the country from a dictionary file.

The problem I am facing is that unless the input field is exactly equal to the value in dictionary , it doesn't match , For example:

The input field is "calling\_mut":937066

dictionary file is :

"9370": "Afghanistan"  
"93706": "Afghanistan - AWC"

The filter I am using is :

```
mutate {
             convert => {
                    calling_mut => "integer"
                    }
            }
    translate {
            field => "calling_mut"
            dictionary_path => "/etc/logstash/conf.d/dialcode-country.yaml"
   		add_field => ["country","%{translation}"]
            fallback => "no match"
   		}

```

The expected result is that it should match 93706 - Afghanistan - AWC as best match , but it doesn't , as the full number 937066 is not in dictionary , Is it possible to force it to use best match instead of exact match , or is there any other way I can match against a dictionary using best match?

---

<div class="post-metadata">

### Author: ![gerdesj](https://avatars.discourse-cdn.com/v4/letter/g/258eb7/32.png) [@gerdesj](https://discuss.elastic.co/u/gerdesj)
#### Post date: [August 23, 2016, 12:28pm UTC](https://discuss.elastic.co/t/best-match-exact-match/58675/2 "2016-08-23T12:28:24Z")

</div>

> **[Translate filter plugin | Logstash Reference \[8.11\] | Elastic](https://www.elastic.co/guide/en/logstash/current/plugins-filters-translate.html)**

> Operationally, if the event field specified in the field configuration matches the EXACT contents of a dictionary entry key (or matches a regex if regex configuration item has been enabled), the field’s value will be substituted with the matched key’s value from the dictionary.

Depending on how your caller ids are coming in then you may be able use a regex.

In the case above, if you only have five significant characters in your dictionary then you could mutate the calling\_mut to five chars and search on that.

---

<div class="post-metadata">

### Author: ![uppaljs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/uppaljs/32/11535_2.png) [@uppaljs](https://discuss.elastic.co/u/uppaljs)
#### Post date: [August 23, 2016, 12:32pm UTC](https://discuss.elastic.co/t/best-match-exact-match/58675/3 "2016-08-23T12:32:15Z")

</div>

Thanks a lot for the reply , The problem is that the dictionary has some countries with detailed in-country operator breakdown as well and some of them are just country codes.

I am looking up the first 5 digits , if i used exact =\> false , It does match it but iterates through each character in the input field separately and matches so

937066 results in AfghanistanRussia0Thailand  
93 - afghanistan  
7 - Russia  
0 - Not in dictionary  
66 - Thailand

Is there any way to trigger this match , as its pretty critical to what i am trying to do.

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [August 23, 2016, 1:10pm UTC](https://discuss.elastic.co/t/best-match-exact-match/58675/4 "2016-08-23T13:10:28Z")

</div>

Have you tried anchoring your regexes at the start of the string, e.g. using `^66` instead of `66`?

---

<div class="post-metadata">

### Author: ![uppaljs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/uppaljs/32/11535_2.png) [@uppaljs](https://discuss.elastic.co/u/uppaljs)
#### Post date: [August 23, 2016, 5:14pm UTC](https://discuss.elastic.co/t/best-match-exact-match/58675/5 "2016-08-23T17:14:07Z")

</div>

I am using the translate filter and not regexes.

How can i achieve this with regexes without writing one for the whole dictionary ( 45000 entries ).

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [August 23, 2016, 5:40pm UTC](https://discuss.elastic.co/t/best-match-exact-match/58675/6 "2016-08-23T17:40:41Z")

</div>

The translate filter supports regex matching, so you can enable regex mode and store regexes as key. With that many regex entries it could however possibly be slow, but you will need to benchmark it.

---

<div class="post-metadata">

### Author: ![uppaljs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/uppaljs/32/11535_2.png) [@uppaljs](https://discuss.elastic.co/u/uppaljs)
#### Post date: [August 24, 2016, 8:07pm UTC](https://discuss.elastic.co/t/best-match-exact-match/58675/7 "2016-08-24T20:07:26Z")

</div>

Hi,

Just reverting back here , for my specific use - case , identifying country code from number , I achieved it by using custom ruby code and phonelib , which is based on libphonenumber from google.

Thanks a lot for replying everyone!

---

<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, 4:41am UTC](https://discuss.elastic.co/t/best-match-exact-match/58675/8 "2017-07-06T04:41:46Z")

</div>


