# Is there a query besides query\_string that can do "and" search across multiple fields?

**URL:** <https://discuss.elastic.co/t/is-there-a-query-besides-query-string-that-can-do-and-search-across-multiple-fields/11380>\
**Category:** Elasticsearch\
**Created:** [March 29, 2013, 1:06pm UTC](https://discuss.elastic.co/t/is-there-a-query-besides-query-string-that-can-do-and-search-across-multiple-fields/11380 "2013-03-29T13:06:06Z")\
**Posts on this page:** 7\
**Page:** 1

<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:** [March 29, 2013, 1:06pm UTC](https://discuss.elastic.co/t/is-there-a-query-besides-query-string-that-can-do-and-search-across-multiple-fields/11380/1 "2013-03-29T13:06:06Z")

</div>

is there a query besides query\_string that can do "and" search across  
multiple fields?

--  
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:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [March 29, 2013, 1:38pm UTC](https://discuss.elastic.co/t/is-there-a-query-besides-query-string-that-can-do-and-search-across-multiple-fields/11380/2 "2013-03-29T13:38:18Z")

</div>

On Fri, 2013-03-29 at 06:06 -0700, AlexR wrote:

> is there a query besides query\_string that can do "and" search across  
> multiple fields?

multi\_match

but the behaviour differs. query\_string looks for each search term in  
each field. multi\_match looks for all terms in a single field.

have a look at:

> <https://stackoverflow.com/questions/15423033/multi-field-multi-word-match-without-query-string/15430238#15430238>

clint

--  
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:** [March 29, 2013, 1:48pm UTC](https://discuss.elastic.co/t/is-there-a-query-besides-query-string-that-can-do-and-search-across-multiple-fields/11380/3 "2013-03-29T13:48:01Z")

</div>

Hi Clint,

I should have qualify it better. I am not interested in multi\_match as it  
does not search across fields but within (as you said just execute the same  
query against all the fields so a doc with one query term in field A and  
the other in B won't be retrieved)

As far as I can say there is no way to achieve searching across multiple  
fields with any query but query\_string. I am wondering why there is on such  
query in ES as it seems to be an pretty common use case?

My very naive thinking is that it could be implemented by finding doc IDs  
for each term in each field and then reducing them to those that have every  
field covered (and scoring them)

Thank you,  
Alex

On Fri, Mar 29, 2013 at 9:38 AM, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)wrote:

> On Fri, 2013-03-29 at 06:06 -0700, AlexR wrote:
> 
> > is there a query besides query\_string that can do "and" search across  
> > multiple fields?
> 
> multi\_match
> 
> but the behaviour differs. query\_string looks for each search term in  
> each field. multi\_match looks for all terms in a single field.
> 
> have a look at:
> 
> [search - Multi-field, multi-word, match without query\_string - Stack Overflow](http://stackoverflow.com/questions/15423033/multi-field-multi-word-match-without-query-string/15430238#15430238)
> 
> clint
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/Vt\_WiHgKspw/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/Vt_WiHgKspw/unsubscribe?hl=en-US)  
> .  
> To unsubscribe from this group and all its topics, 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).

--  
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:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [March 30, 2013, 8:55am UTC](https://discuss.elastic.co/t/is-there-a-query-besides-query-string-that-can-do-and-search-across-multiple-fields/11380/4 "2013-03-30T08:55:02Z")

</div>

Hi Alex

> As far as I can say there is no way to achieve searching across  
> multiple fields with any query but query\_string. I am wondering why  
> there is on such query in ES as it seems to be an pretty common use  
> case?

In order to do this, you have to preparse the query string, eg if you  
want to do an AND search in field\_1 and field\_2, a query for "foo bar"  
becomes:

```
+(field_1:foo | field_2:foo) +(field_1:bar | field_2:bar)

```

But what happens if "foo bar" is a synonym for "foobar" in field\_1? Then  
this query won't work. What happens if field\_1 is analyzed differently  
from field\_2? Again, unexpected results, because your query terms have  
already been preparsed.

It's a minefield.

If you want to query multiple fields as if they were a single field,  
then create your own custom \_all field which contains the data from all  
the involved fields, as I described here:

> <https://stackoverflow.com/questions/15423033/multi-field-multi-word-match-without-query-string/15430238#15430238>

clint

--  
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:** [March 30, 2013, 3:49pm UTC](https://discuss.elastic.co/t/is-there-a-query-besides-query-string-that-can-do-and-search-across-multiple-fields/11380/5 "2013-03-30T15:49:25Z")

</div>

I would not try to do it via a complex query as you said it is a mindfield  
and unless you cover all the permutations is not reliable ot needs to be  
done algorithmically like i described above

I do useultifield for the job but it has its own problems such as its  
analysisay not be suitable for all fields that contrinuye to it (say you  
have person name and a quote in a multifield and you want the quote stemmed  
but not person name) another is highlighting of individual fields of the  
multifield if they are not analized like the multifield they would not be  
highlighted

My trouble is that we have couple fundreds of fields to search across and I  
need to have good relevanve and highlight every individual field that  
matched  
On Mar 30, 2013 4:55 AM, "Clinton Gormley" [clint@traveljury.com](mailto:clint@traveljury.com) wrote:

> Hi Alex
> 
> > As far as I can say there is no way to achieve searching across  
> > multiple fields with any query but query\_string. I am wondering why  
> > there is on such query in ES as it seems to be an pretty common use  
> > case?
> 
> In order to do this, you have to preparse the query string, eg if you  
> want to do an AND search in field\_1 and field\_2, a query for "foo bar"  
> becomes:
> 
> ```
> +(field_1:foo | field_2:foo) +(field_1:bar | field_2:bar)
> 
> ```
> 
> But what happens if "foo bar" is a synonym for "foobar" in field\_1? Then  
> this query won't work. What happens if field\_1 is analyzed differently  
> from field\_2? Again, unexpected results, because your query terms have  
> already been preparsed.
> 
> It's a minefield.
> 
> If you want to query multiple fields as if they were a single field,  
> then create your own custom \_all field which contains the data from all  
> the involved fields, as I described here:
> 
> [search - Multi-field, multi-word, match without query\_string - Stack Overflow](http://stackoverflow.com/questions/15423033/multi-field-multi-word-match-without-query-string/15430238#15430238)
> 
> clint
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/Vt\_WiHgKspw/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/Vt_WiHgKspw/unsubscribe?hl=en-US)  
> .  
> To unsubscribe from this group and all its topics, 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).

--  
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:** ![Loic\_Bertron](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loic_bertron/32/1794_2.png) [@Loic\_Bertron](https://discuss.elastic.co/u/Loic_Bertron)\
**Post date:** [April 3, 2013, 7:48pm UTC](https://discuss.elastic.co/t/is-there-a-query-besides-query-string-that-can-do-and-search-across-multiple-fields/11380/6 "2013-04-03T19:48:13Z")

</div>

Multi\_field  
: [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/multi-field-type/)

```
"tweet" : {
    "properties": {
        "first_name": {
            "type": "multi_field",
            "path": "just_name",
            "fields": {
                "first_name": {"type": "string", "index": "analyzed"},
                "any_name": {"type": "string","index": "analyzed"}
            }
        },
        "last_name": {
            "type": "multi_field",
            "path": "just_name",
            "fields": {
                "last_name": {"type": "string", "index": "analyzed"},
                "any_name": {"type": "string","index": "analyzed"}
            }
        }
    }
}

```

}

With this mapping you can either search individually for first\_name,  
last\_name, or for a virtual field : any\_name combining first\_name and  
last\_name.  
But you have to specify it in your mapping, not a query time !

Le samedi 30 mars 2013 11:49:25 UTC-4, AlexR a écrit :

> I would not try to do it via a complex query as you said it is a mindfield  
> and unless you cover all the permutations is not reliable ot needs to be  
> done algorithmically like i described above
> 
> I do useultifield for the job but it has its own problems such as its  
> analysisay not be suitable for all fields that contrinuye to it (say you  
> have person name and a quote in a multifield and you want the quote stemmed  
> but not person name) another is highlighting of individual fields of the  
> multifield if they are not analized like the multifield they would not be  
> highlighted
> 
> My trouble is that we have couple fundreds of fields to search across and  
> I need to have good relevanve and highlight every individual field that  
> matched  
> On Mar 30, 2013 4:55 AM, "Clinton Gormley" \<[cl...@traveljury.com](mailto:cl...@traveljury.com)\<javascript:\>\>  
> wrote:
> 
> > Hi Alex
> > 
> > > As far as I can say there is no way to achieve searching across  
> > > multiple fields with any query but query\_string. I am wondering why  
> > > there is on such query in ES as it seems to be an pretty common use  
> > > case?
> > 
> > In order to do this, you have to preparse the query string, eg if you  
> > want to do an AND search in field\_1 and field\_2, a query for "foo bar"  
> > becomes:
> > 
> > ```
> > +(field_1:foo | field_2:foo) +(field_1:bar | field_2:bar)
> > 
> > ```
> > 
> > But what happens if "foo bar" is a synonym for "foobar" in field\_1? Then  
> > this query won't work. What happens if field\_1 is analyzed differently  
> > from field\_2? Again, unexpected results, because your query terms have  
> > already been preparsed.
> > 
> > It's a minefield.
> > 
> > If you want to query multiple fields as if they were a single field,  
> > then create your own custom \_all field which contains the data from all  
> > the involved fields, as I described here:
> > 
> > [search - Multi-field, multi-word, match without query\_string - Stack Overflow](http://stackoverflow.com/questions/15423033/multi-field-multi-word-match-without-query-string/15430238#15430238)
> > 
> > clint
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/Vt\_WiHgKspw/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/Vt_WiHgKspw/unsubscribe?hl=en-US)  
> > .  
> > To unsubscribe from this group and all its topics, send an email to  
> > [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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:42am UTC](https://discuss.elastic.co/t/is-there-a-query-besides-query-string-that-can-do-and-search-across-multiple-fields/11380/7 "2017-07-06T02:42:57Z")

</div>


