# Search with exact match comes first?

**URL:** https://discuss.elastic.co/t/search-with-exact-match-comes-first/175485
**Category:** Elasticsearch
**Created:** [April 4, 2019, 8:57pm UTC](https://discuss.elastic.co/t/search-with-exact-match-comes-first/175485 "2019-04-04T20:57:24Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![flash](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@flash](https://discuss.elastic.co/u/flash)
#### Post date: [April 4, 2019, 8:57pm UTC](https://discuss.elastic.co/t/search-with-exact-match-comes-first/175485/1 "2019-04-04T20:57:24Z")

</div>

I have some index settings like this

> ```
> DELETE index
> PUT index/_doc/Neighbour
> {
> "id": "id1"
> "name": "Downtown San Francisco"
> "indexed_at": "2019-03-01"
> }
> PUT index/_doc/State
> {
> "id": "id2"
> "name": "Nevada"
> "indexed_at": "2019-03-01"
> }
> PUT index/_doc/State
> {
> "id": "id3"
> "name": "California"
> "indexed_at": "2019-03-01"
> }
> PUT index/_doc/City
> {
> "id": "id4"
> "name": "San francisco"
> "indexed_at": "2019-03-01"
> }
> 
> ```

When I do the search with querystringquery, e.g.

> ```
> GET index/_search
> {
> "query": {
> "query_string": {
> "query": "San Francisco"
> "fuzziness": "AUTO"
> "fuzzy_prefix_length": 3
> "fuzzy_max_expansions": 10
> }
> }
> }
> 
> ```

it does not return San Francisco in the first place.

Do I need to do something specific to get the exact match comes first in the return results?

Thanks!

---

<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: [April 4, 2019, 9:10pm UTC](https://discuss.elastic.co/t/search-with-exact-match-comes-first/175485/2 "2019-04-04T21:10:43Z")

</div>

You can combine multiple criteria within a bool query inside should clauses. And use exact match then.

I wrote an example here:

> <https://gist.github.com/dadoonet/5179ee72ecbf08f12f53d4bda1b76bab>

---

<div class="post-metadata">

### Author: ![flash](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@flash](https://discuss.elastic.co/u/flash)
#### Post date: [April 4, 2019, 9:17pm UTC](https://discuss.elastic.co/t/search-with-exact-match-comes-first/175485/3 "2019-04-04T21:17:01Z")

</div>

Just curious to see why the querystring is not working as the bool query?

because I would like the short words as well as phrases to be acceptable as input. Therefore I think querystring is a quick way to solve?

---

<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: [April 4, 2019, 9:29pm UTC](https://discuss.elastic.co/t/search-with-exact-match-comes-first/175485/4 "2019-04-04T21:29:44Z")

</div>

Did you try with one single shard?

---

<div class="post-metadata">

### Author: ![flash](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@flash](https://discuss.elastic.co/u/flash)
#### Post date: [April 4, 2019, 11:35pm UTC](https://discuss.elastic.co/t/search-with-exact-match-comes-first/175485/5 "2019-04-04T23:35:19Z")

</div>

I index the content based on type like "City", "State", "Neighborhood". Are they falling into the single shard or different index will fall into different shards?

---

<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: [April 5, 2019, 12:58am UTC](https://discuss.elastic.co/t/search-with-exact-match-comes-first/175485/6 "2019-04-05T00:58:17Z")

</div>

Try with one single shard or add the following option when calling the search API:

```
?search_type=dfs_query_then_fetch

```

See [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-search-type.html#dfs-query-then-fetch](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-search-type.html#dfs-query-then-fetch)

---

<div class="post-metadata">

### Author: ![flash](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@flash](https://discuss.elastic.co/u/flash)
#### Post date: [April 5, 2019, 1:13am UTC](https://discuss.elastic.co/t/search-with-exact-match-comes-first/175485/7 "2019-04-05T01:13:37Z")

</div>

dfs\_query\_then\_fetch would add another round to the elasticsearch, which might add up to the latency?

Does multiple indexes end up in different shards? I am still confused why it would happen if it is across different shards?

---

<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: [April 5, 2019, 4:10am UTC](https://discuss.elastic.co/t/search-with-exact-match-comes-first/175485/8 "2019-04-05T04:10:13Z")

</div>

Sad that you don't want to try and check if that's solving or not the problem you described.

Anyway. In ES prior to 7.0, there are 5 shards by default. A document might go to one of those. Unless you are using the same routing key. The default routing key is the document id.

---

<div class="post-metadata">

### Author: ![flash](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@flash](https://discuss.elastic.co/u/flash)
#### Post date: [April 5, 2019, 5:21pm UTC](https://discuss.elastic.co/t/search-with-exact-match-comes-first/175485/9 "2019-04-05T17:21:30Z")

</div>

Oh I did try dfs\_query\_then\_fetch before, this did make the results ranked more reasonably. Thought still for some use cases, exact match are not in the first place.

That's why I am eager to learn more behind the scenes and how can I get the better results 🙂

Are you suggesting that I am storing all of the things into one document? Or stop using querystring?

Btw, just curious , is there a query builder that suits for both short text matches as well as phrases/paragraphs?

---

<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: [April 6, 2019, 10:50am UTC](https://discuss.elastic.co/t/search-with-exact-match-comes-first/175485/10 "2019-04-06T10:50:58Z")

</div>

> Are you suggesting that I am storing all of the things into one document? Or stop using querystring?

No. I'm suggesting that you either run your query on a big dataset and not on few documents or/and that you use only one shard for your index.

> Btw, just curious , is there a query builder that suits for both short text matches as well as phrases/paragraphs?

No. You can use a `bool` query with multiple `should` clauses. One would be a `match_phrase` query, the other one a `match` query.

---

<div class="post-metadata">

### Author: ![flash](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@flash](https://discuss.elastic.co/u/flash)
#### Post date: [April 8, 2019, 6:30pm UTC](https://discuss.elastic.co/t/search-with-exact-match-comes-first/175485/11 "2019-04-08T18:30:33Z")

</div>

Thanks, that is helpful!!

---

<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: [May 6, 2019, 6:42pm UTC](https://discuss.elastic.co/t/search-with-exact-match-comes-first/175485/12 "2019-05-06T18:42:48Z")

</div>

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