# How to do both word by word search and exact matching search?

**URL:** https://discuss.elastic.co/t/how-to-do-both-word-by-word-search-and-exact-matching-search/11632
**Category:** Elasticsearch
**Created:** [April 18, 2013, 2:23pm UTC](https://discuss.elastic.co/t/how-to-do-both-word-by-word-search-and-exact-matching-search/11632 "2013-04-18T14:23:41Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![damien\_2](https://avatars.discourse-cdn.com/v4/letter/d/f08c70/32.png) [@damien\_2](https://discuss.elastic.co/u/damien_2)
#### Post date: [April 18, 2013, 2:23pm UTC](https://discuss.elastic.co/t/how-to-do-both-word-by-word-search-and-exact-matching-search/11632/1 "2013-04-18T14:23:41Z")

</div>

Hello,

I'm using elasticsearch with the FOQElastica bundle in a PHP project and  
it's been a few days I'm trying to make elasticsearch the way I want.  
I would like to make two differents search, one which search the exact  
terms in the query and another one which search query terms one by one. And  
I don't want to just make one search and have both. I need to make two  
differents search to treat data after that.  
Here is an example:  
If my search is 'conan and andy', the first result should come up with

1. Documents with the exact name 'conan and andy'
2. Documents with name containing 'conan and andy'

The second search with the same query should come up with:

- Documents containing 'conan' or 'andy'

I think it is about mapping and custom analyzer but I don't know how to  
figure this out.  
Thank you for your help!

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [April 18, 2013, 8:25pm UTC](https://discuss.elastic.co/t/how-to-do-both-word-by-word-search-and-exact-matching-search/11632/2 "2013-04-18T20:25:32Z")

</div>

On a field level, this is quite easy - you just need to index the same  
field twice using multi\_field[http://www.elasticsearch.org/guide/reference/mapping/multi-field-type/](http://www.elasticsearch.org/guide/reference/mapping/multi-field-type/)type. Search 1) would be term search against not\_analyzed version, search  
2) would be phrase search against analyzed version and search 3) would be  
match search against analyze version. If you need to do it for all fields  
in the document it's more complicated but possible. Could you tell us a  
little bit more about your document structure?

On Thursday, April 18, 2013 10:23:41 AM UTC-4, Damien wrote:

> Hello,
> 
> I'm using elasticsearch with the FOQElastica bundle in a PHP project and  
> it's been a few days I'm trying to make elasticsearch the way I want.  
> I would like to make two differents search, one which search the exact  
> terms in the query and another one which search query terms one by one. And  
> I don't want to just make one search and have both. I need to make two  
> differents search to treat data after that.  
> Here is an example:  
> If my search is 'conan and andy', the first result should come up with
> 
> 1. Documents with the exact name 'conan and andy'
> 2. Documents with name containing 'conan and andy'
> 
> The second search with the same query should come up with:
> 
> - Documents containing 'conan' or 'andy'
> 
> I think it is about mapping and custom analyzer but I don't know how to  
> figure this out.  
> Thank you for your help!

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![damien\_2](https://avatars.discourse-cdn.com/v4/letter/d/f08c70/32.png) [@damien\_2](https://discuss.elastic.co/u/damien_2)
#### Post date: [April 18, 2013, 8:32pm UTC](https://discuss.elastic.co/t/how-to-do-both-word-by-word-search-and-exact-matching-search/11632/3 "2013-04-18T20:32:22Z")

</div>

Thank you for your answer.  
And yeah you're right, I found that few hours later after trying hard.  
Well, I don't need multi\_field, apparently phrase search is doing both 1)  
and 2) so that is cool. And an anlyze version works too for 3)!  
But now I have a problem to calculate a custom query score. I just made  
another post, if you know few things in custom query too, some help would  
be very appreciated! Here is my other post:  
[https://groups.google.com/forum/?fromgroups=#!topic/elasticsearch/uII3y8ysn2I](https://groups.google.com/forum/?fromgroups=#!topic/elasticsearch/uII3y8ysn2I)

Thanks.

Le jeudi 18 avril 2013 16:25:32 UTC-4, Igor Motov a écrit :

> On a field level, this is quite easy - you just need to index the same  
> field twice using multi\_field[http://www.elasticsearch.org/guide/reference/mapping/multi-field-type/](http://www.elasticsearch.org/guide/reference/mapping/multi-field-type/)type. Search 1) would be term search against not\_analyzed version, search  
> 2) would be phrase search against analyzed version and search 3) would be  
> match search against analyze version. If you need to do it for all fields  
> in the document it's more complicated but possible. Could you tell us a  
> little bit more about your document structure?
> 
> On Thursday, April 18, 2013 10:23:41 AM UTC-4, Damien wrote:
> 
> > Hello,
> > 
> > I'm using elasticsearch with the FOQElastica bundle in a PHP project and  
> > it's been a few days I'm trying to make elasticsearch the way I want.  
> > I would like to make two differents search, one which search the exact  
> > terms in the query and another one which search query terms one by one. And  
> > I don't want to just make one search and have both. I need to make two  
> > differents search to treat data after that.  
> > Here is an example:  
> > If my search is 'conan and andy', the first result should come up with
> > 
> > 1. Documents with the exact name 'conan and andy'
> > 2. Documents with name containing 'conan and andy'
> > 
> > The second search with the same query should come up with:
> > 
> > - Documents containing 'conan' or 'andy'
> > 
> > I think it is about mapping and custom analyzer but I don't know how to  
> > figure this out.  
> > Thank you for your help!

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:40am UTC](https://discuss.elastic.co/t/how-to-do-both-word-by-word-search-and-exact-matching-search/11632/4 "2017-07-06T02:40:35Z")

</div>


