# Using must and falling to fuzzy if no match in queries

**URL:** https://discuss.elastic.co/t/using-must-and-falling-to-fuzzy-if-no-match-in-queries/114894
**Category:** Elasticsearch
**Created:** [January 10, 2018, 2:25pm UTC](https://discuss.elastic.co/t/using-must-and-falling-to-fuzzy-if-no-match-in-queries/114894 "2018-01-10T14:25:30Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ericohtake](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ericohtake/32/24539_2.png) [@ericohtake](https://discuss.elastic.co/u/ericohtake)
#### Post date: [January 10, 2018, 2:25pm UTC](https://discuss.elastic.co/t/using-must-and-falling-to-fuzzy-if-no-match-in-queries/114894/1 "2018-01-10T14:25:31Z")

</div>

Hi,

I'm trying to match some documents in a query where my dataset contains exact matches and others that don't exactly match, if ever. I'd like to query ES for the exact match, and if it doesn't exists exactly, it falls to the "should" so I don't get empty handed if the exact match is not present.

I just need the first hit of the dataset.

My original data is in Japanese, so I will try to reproduce some of them here in Roman chars:

Data sample:

```
> Prefecture Municipality Street
> Tokyo Shinaga wa ABCDEFHI
> Tokyo Shina gawa EFGHJK
> Tokyo Shinagawa ABC DEF
> Tokyo Shinagawa AB CDEF
> Tokyo Shinaga wa ABCDEF

```

Query:

```
{
  "query":{
     "bool":{
        "must":[
           { "match": { "prefecture.keyword": "prefecture" } },
           { "fuzzy": { "municipality.keyword": { "value": "shinagawa"} } }
        ],
        "should":[
           { "fuzzy": { "street": { "value": "ABCDEF"} } }
        ]
     }
  },
  "from":0, "size" : 1,
  "_source": ["prefecture", "municipality", "street"]
}

```

Results:

```
> Prefecture Municipality Street
> Tokyo Shinaga wa ABCDEFHI
> Tokyo Shina gawa EFGHJK
> Tokyo Shinagawa ABC DEF
> Tokyo Shinagawa AB CDEF

```

Expected results:

```
> Prefecture Municipality Street
> Tokyo Shinagawa ABCDEF
> Tokyo Shinagawa ABC DEF
> Tokyo Shinagawa AB CDEF

```

I end up getting everything, while I want only what matches, exactly or via fuzzy. How can I do this query properly?

---

<div class="post-metadata">

### Author: ![hubo3085632](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hubo3085632/32/23724_2.png) [@hubo3085632](https://discuss.elastic.co/u/hubo3085632)
#### Post date: [January 16, 2018, 7:40am UTC](https://discuss.elastic.co/t/using-must-and-falling-to-fuzzy-if-no-match-in-queries/114894/2 "2018-01-16T07:40:32Z")

</div>

hi , u need print the explain to see the detail scores,even though u used fuzzy query it is not the important part in scoring algorithm.  
here is the algorithm:  
[https://www.elastic.co/guide/en/elasticsearch/guide/current/practical-scoring-function.html#query-norm](https://www.elastic.co/guide/en/elasticsearch/guide/current/practical-scoring-function.html#query-norm)

---

<div class="post-metadata">

### Author: ![ericohtake](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ericohtake/32/24539_2.png) [@ericohtake](https://discuss.elastic.co/u/ericohtake)
#### Post date: [January 18, 2018, 6:17am UTC](https://discuss.elastic.co/t/using-must-and-falling-to-fuzzy-if-no-match-in-queries/114894/3 "2018-01-18T06:17:48Z")

</div>

Thanks.  
I couldn't accomplish in that way, so I asked for a more normalized data, like a municipality field without that crazy spaces, and made a term match on the first two fields. Then used should/fuzzy on the street. It brings back the things I need.

---

<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 15, 2018, 6:18am UTC](https://discuss.elastic.co/t/using-must-and-falling-to-fuzzy-if-no-match-in-queries/114894/4 "2018-02-15T06:18:04Z")

</div>

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