# Search multiple fields with “and” operator (but use fields' own analyzers)

**URL:** https://discuss.elastic.co/t/search-multiple-fields-with-and-operator-but-use-fields-own-analyzers/13113
**Category:** Elasticsearch
**Created:** [August 7, 2013, 10:08pm UTC](https://discuss.elastic.co/t/search-multiple-fields-with-and-operator-but-use-fields-own-analyzers/13113 "2013-08-07T22:08:38Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Max\_Ivanov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/max_ivanov/32/1684_2.png) [@Max\_Ivanov](https://discuss.elastic.co/u/Max_Ivanov)
#### Post date: [August 7, 2013, 10:08pm UTC](https://discuss.elastic.co/t/search-multiple-fields-with-and-operator-but-use-fields-own-analyzers/13113/1 "2013-08-07T22:08:38Z")

</div>

ElasticSearch Version: 0.90.2

Here's the problem: I want to find documents in the index so that they:

1. match _all_ query tokens across multiple fields
2. fields _own_ analyzers are used

So if there are 4 documents:

```
{ "_id" : 1, "name" : "Joe Doe", "mark" : "1", "message" : "Message 

```

First" }  
{ "\_id" : 2, "name" : "Ann", "mark" : "3", "message" : "Yesterday  
Joe Doe got 1 for the message First"}  
{ "\_id" : 3, "name" : "Joe Doe", "mark" : "2", "message" : "Message  
Second" }  
{ "\_id" : 4, "name" : "Dan Spencer", "mark" : "2", "message" : "Message  
Third" }

And the query is "Joe First 1" it should find ids 1 and 2. I.e., it should  
find documents which contain all the tokens from search query, no matter in  
which fields they are (maybe all tokens are in one field, or maybe each  
token is in its own field).

One solution would be to use elasticsearch "\_all" field functionality: that  
way it will merge all the fields I need (name, mark, message) into one and  
I'll be able to query it with something like

```
"match": {
  "_all": {
    "query": "Joe First 1",
    "operator": "and"
  }
}

```

But this way I can specify analyzer for the "\_all" field only. And I need  
"name" and "message" fields to have different set of tokenizers/token  
filters (let's say name will have phonetic analyzer and message will have  
some stemming token filter).

Is there a way to do this?  
Thanks.

--  
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: ![Max\_Ivanov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/max_ivanov/32/1684_2.png) [@Max\_Ivanov](https://discuss.elastic.co/u/Max_Ivanov)
#### Post date: [August 8, 2013, 10:07am UTC](https://discuss.elastic.co/t/search-multiple-fields-with-and-operator-but-use-fields-own-analyzers/13113/2 "2013-08-08T10:07:20Z")

</div>

The only thing I have on my mind at the moment is to split query to terms  
clientside (I know tokenizer will do it better but I can bear with some  
inaccuracy) and make a query like this:  
"query\_string": {  
"fields": [  
"name",  
"mark",  
"message"  
],  
"query": "Joe AND First AND 1"  
}

On Thursday, August 8, 2013 2:08:38 AM UTC+4, Max Ivanov wrote:

> Elasticsearch Version: 0.90.2
> 
> Here's the problem: I want to find documents in the index so that they:
> 
> 1. match _all_ query tokens across multiple fields
> 2. fields _own_ analyzers are used
> 
> So if there are 4 documents:
> 
> ```
> { "_id" : 1, "name" : "Joe Doe", "mark" : "1", "message" : "Message 
> 
> ```
> 
> First" }  
> { "\_id" : 2, "name" : "Ann", "mark" : "3", "message" : "Yesterday  
> Joe Doe got 1 for the message First"}  
> { "\_id" : 3, "name" : "Joe Doe", "mark" : "2", "message" : "Message  
> Second" }  
> { "\_id" : 4, "name" : "Dan Spencer", "mark" : "2", "message" : "Message  
> Third" }
> 
> And the query is "Joe First 1" it should find ids 1 and 2. I.e., it should  
> find documents which contain all the tokens from search query, no matter in  
> which fields they are (maybe all tokens are in one field, or maybe each  
> token is in its own field).
> 
> One solution would be to use elasticsearch "\_all" field functionality:  
> that way it will merge all the fields I need (name, mark, message) into one  
> and I'll be able to query it with something like
> 
> ```
> "match": {
> "_all": {
> "query": "Joe First 1",
> "operator": "and"
> }
> }
> 
> ```
> 
> But this way I can specify analyzer for the "\_all" field only. And I need  
> "name" and "message" fields to have different set of tokenizers/token  
> filters (let's say name will have phonetic analyzer and message will have  
> some stemming token filter).
> 
> Is there a way to do this?  
> Thanks.

--  
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: ![roytmana](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roytmana/32/44855_2.png) [@roytmana](https://discuss.elastic.co/u/roytmana)
#### Post date: [August 8, 2013, 12:10pm UTC](https://discuss.elastic.co/t/search-multiple-fields-with-and-operator-but-use-fields-own-analyzers/13113/3 "2013-08-08T12:10:57Z")

</div>

Query\_string has option to specify default lofical operator to be used netween tokens default is OR but you can specify AND

--  
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\_Ferey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/damien_ferey/32/536_2.png) [@Damien\_Ferey](https://discuss.elastic.co/u/Damien_Ferey)
#### Post date: [August 8, 2013, 12:15pm UTC](https://discuss.elastic.co/t/search-multiple-fields-with-and-operator-but-use-fields-own-analyzers/13113/4 "2013-08-08T12:15:12Z")

</div>

use multi match query:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Le jeudi 8 août 2013 00:08:38 UTC+2, Max Ivanov a écrit :

> Elasticsearch Version: 0.90.2
> 
> Here's the problem: I want to find documents in the index so that they:
> 
> 1. match _all_ query tokens across multiple fields
> 2. fields _own_ analyzers are used
> 
> So if there are 4 documents:
> 
> ```
> { "_id" : 1, "name" : "Joe Doe", "mark" : "1", "message" : "Message 
> 
> ```
> 
> First" }  
> { "\_id" : 2, "name" : "Ann", "mark" : "3", "message" : "Yesterday  
> Joe Doe got 1 for the message First"}  
> { "\_id" : 3, "name" : "Joe Doe", "mark" : "2", "message" : "Message  
> Second" }  
> { "\_id" : 4, "name" : "Dan Spencer", "mark" : "2", "message" : "Message  
> Third" }
> 
> And the query is "Joe First 1" it should find ids 1 and 2. I.e., it should  
> find documents which contain all the tokens from search query, no matter in  
> which fields they are (maybe all tokens are in one field, or maybe each  
> token is in its own field).
> 
> One solution would be to use elasticsearch "\_all" field functionality:  
> that way it will merge all the fields I need (name, mark, message) into one  
> and I'll be able to query it with something like
> 
> ```
> "match": {
> "_all": {
> "query": "Joe First 1",
> "operator": "and"
> }
> }
> 
> ```
> 
> But this way I can specify analyzer for the "\_all" field only. And I need  
> "name" and "message" fields to have different set of tokenizers/token  
> filters (let's say name will have phonetic analyzer and message will have  
> some stemming token filter).
> 
> Is there a way to do this?  
> Thanks.

--  
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: ![Max\_Ivanov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/max_ivanov/32/1684_2.png) [@Max\_Ivanov](https://discuss.elastic.co/u/Max_Ivanov)
#### Post date: [August 8, 2013, 1:44pm UTC](https://discuss.elastic.co/t/search-multiple-fields-with-and-operator-but-use-fields-own-analyzers/13113/5 "2013-08-08T13:44:46Z")

</div>

AlexR, I guess you meant one of the match or multi match queries, they both  
have configurable logical operator, but it won't help (see below).

Damien, multi match query won't work, because it will produce a query to  
match _all_ tokens within _each_ field (if AND logical operator is used),  
or a query to match _any_ token withinn _any_ field (with OR logical  
operator set).  
So in my example, if the query is "Joe First 1" and multi match with AND  
setting used,

```
{ "_id" : 2, "name" : "Ann", "mark" : "3", "message" : "Yesterday 

```

Joe Doe got 1 for the message First"} will match  
while  
{ "\_id" : 1, "name" : "Joe Doe", "mark" : "1", "message" : "Message  
First" } won't

On Thursday, August 8, 2013 4:15:12 PM UTC+4, Damien Ferey wrote:

> use multi match query:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query/)
> 
> Le jeudi 8 août 2013 00:08:38 UTC+2, Max Ivanov a écrit :
> 
> > Elasticsearch Version: 0.90.2
> > 
> > Here's the problem: I want to find documents in the index so that they:
> > 
> > 1. match _all_ query tokens across multiple fields
> > 2. fields _own_ analyzers are used
> > 
> > So if there are 4 documents:
> > 
> > ```
> > { "_id" : 1, "name" : "Joe Doe", "mark" : "1", "message" : "Message 
> > 
> > ```
> > 
> > First" }  
> > { "\_id" : 2, "name" : "Ann", "mark" : "3", "message" : "Yesterday  
> > Joe Doe got 1 for the message First"}  
> > { "\_id" : 3, "name" : "Joe Doe", "mark" : "2", "message" : "Message  
> > Second" }  
> > { "\_id" : 4, "name" : "Dan Spencer", "mark" : "2", "message" : "Message  
> > Third" }
> > 
> > And the query is "Joe First 1" it should find ids 1 and 2. I.e., it  
> > should find documents which contain all the tokens from search query, no  
> > matter in which fields they are (maybe all tokens are in one field, or  
> > maybe each token is in its own field).
> > 
> > One solution would be to use elasticsearch "\_all" field functionality:  
> > that way it will merge all the fields I need (name, mark, message) into one  
> > and I'll be able to query it with something like
> > 
> > ```
> > "match": {
> > "_all": {
> > "query": "Joe First 1",
> > "operator": "and"
> > }
> > }
> > 
> > ```
> > 
> > But this way I can specify analyzer for the "\_all" field only. And I need  
> > "name" and "message" fields to have different set of tokenizers/token  
> > filters (let's say name will have phonetic analyzer and message will have  
> > some stemming token filter).
> > 
> > Is there a way to do this?  
> > Thanks.

--  
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: ![roytmana](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roytmana/32/44855_2.png) [@roytmana](https://discuss.elastic.co/u/roytmana)
#### Post date: [August 8, 2013, 7:03pm UTC](https://discuss.elastic.co/t/search-multiple-fields-with-and-operator-but-use-fields-own-analyzers/13113/6 "2013-08-08T19:03:10Z")

</div>

I meant query\_string see default\_operator in [http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query/](http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query/)

--  
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: ![Max\_Ivanov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/max_ivanov/32/1684_2.png) [@Max\_Ivanov](https://discuss.elastic.co/u/Max_Ivanov)
#### Post date: [August 8, 2013, 8:39pm UTC](https://discuss.elastic.co/t/search-multiple-fields-with-and-operator-but-use-fields-own-analyzers/13113/7 "2013-08-08T20:39:56Z")

</div>

AlexR you're absolutely right, I overlooked default\_operator parameter.  
Problem solved 🙂  
Thank you!

On Thursday, August 8, 2013 11:03:10 PM UTC+4, AlexR wrote:

> I meant query\_string see default\_operator in  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query/)

--  
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:22am UTC](https://discuss.elastic.co/t/search-multiple-fields-with-and-operator-but-use-fields-own-analyzers/13113/8 "2017-07-06T02:22:01Z")

</div>


