# Is it possible to use the AND operator for multiple fields match?

**URL:** https://discuss.elastic.co/t/is-it-possible-to-use-the-and-operator-for-multiple-fields-match/12514
**Category:** Elasticsearch
**Created:** [June 21, 2013, 7:46pm UTC](https://discuss.elastic.co/t/is-it-possible-to-use-the-and-operator-for-multiple-fields-match/12514 "2013-06-21T19:46:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Bruno\_Miranda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bruno_miranda/32/746_2.png) [@Bruno\_Miranda](https://discuss.elastic.co/u/Bruno_Miranda)
#### Post date: [June 21, 2013, 7:46pm UTC](https://discuss.elastic.co/t/is-it-possible-to-use-the-and-operator-for-multiple-fields-match/12514/1 "2013-06-21T19:46:22Z")

</div>

In this case the first word is found on the first field and the second on  
the second. I know I can do this with string() but I'd like to stay way  
from it because it's susceptible to parse errors depending on what the user  
inputs.

I get no results using AND.

## INDEX

curl -X POST "[http://localhost:9200/search/document/](http://localhost:9200/search/document/)" -d '{  
"first":"Barbara","last":"Smith"  
}'

## SEARCH

curl -X GET '[http://localhost:9200/search/\_search?pretty](http://localhost:9200/search/_search?pretty)' -d '{  
"query": {  
"multi\_match": {  
"query": "Barbara Smith",  
"operator": "AND",  
"fields": [  
"first",  
"last"  
]  
}  
}  
}'

## RESULT

{  
"took": 1,  
"timed\_out": false,  
"\_shards": {  
"total": 1,  
"successful": 1,  
"failed": 0  
},  
"hits": {  
"total": 0,  
"max\_score": null,  
"hits": []  
}  
}

I get a result using OR.

## INDEX

curl -X POST "[http://localhost:9200/search/document/](http://localhost:9200/search/document/)" -d '{  
"first":"Barbara","last":"Smith"  
}'

## SEARCH

curl -X GET '[http://localhost:9200/search/\_search?pretty](http://localhost:9200/search/_search?pretty)' -d '{  
"query": {  
"multi\_match": {  
"query": "Barbara Smith",  
"operator": "OR",  
"fields": [  
"first",  
"last"  
]  
}  
}  
}'

## RESULT

{  
"took": 3,  
"timed\_out": false,  
"\_shards": {  
"total": 1,  
"successful": 1,  
"failed": 0  
},  
"hits": {  
"total": 1,  
"max\_score": 0.4898842,  
"hits": [  
{  
"\_index": "search",  
"\_type": "document",  
"\_id": "Z0nxJ5H6R1ONDwU1xfOKIA",  
"\_score": 0.4898842,  
"\_source": {  
"first": "Barbara",  
"last": "Smith"  
}  
}  
]  
}  
}

What are my options?

Thank you

--  
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: ![cthoma](https://avatars.discourse-cdn.com/v4/letter/c/b487fb/32.png) [@cthoma](https://discuss.elastic.co/u/cthoma)
#### Post date: [June 21, 2013, 8:20pm UTC](https://discuss.elastic.co/t/is-it-possible-to-use-the-and-operator-for-multiple-fields-match/12514/2 "2013-06-21T20:20:47Z")

</div>

Hello,

one option is to use the \_all field, if you don't need explicit fields.

multi\_match sample:  
{  
"query": {  
"multi\_match": {  
"query": "Barbara Smith",  
"operator": "AND",  
"fields": [  
"\_all"  
]  
}  
}  
}

1. Bool sample:  
{  
"query": {  
"bool": {  
"must": [  
{  
"query\_string": {  
"default\_field": "\_all",  
"query": "Barbara Smith"  
}  
}  
]  
}  
},  
"from": 0,  
"size": 50  
}

--  
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: ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)
#### Post date: [June 25, 2013, 6:56pm UTC](https://discuss.elastic.co/t/is-it-possible-to-use-the-and-operator-for-multiple-fields-match/12514/3 "2013-06-25T18:56:16Z")

</div>

Bruno,

I wrote a Java driver that emits the queries in JSON and can emit the  
response documents in JSON, or in a number of other formats. So these are  
curl-able but not issued via curl. (Constructing queries in Java?  
Wonderful! Constructing queries in JSON? No! But I digress...)

A single-word query that matches two records:

FULL QUERY:  
{  
"from" : 0,  
"size" : 20,  
"query" : {  
"match" : {  
"text" : {  
"query" : "lives",  
"type" : "boolean"  
}  
}  
},  
"version" : true,  
"explain" : false,  
"fields" : ["\_ttl", "\_source"]  
}

RESULTS:  
{ "\_index" : "mortal" , "\_type" : "elf" , "\_id" : "1" , "\_version" : 1 ,  
"\_score" : 1.4914339780807495 , "\_source" : { "cn" : "Celeborn" , "text" :  
"Lives forever" } }  
{ "\_index" : "mortal" , "\_type" : "elf" , "\_id" : "2" , "\_version" : 1 ,  
"\_score" : 1.4914339780807495 , "\_source" : { "cn" : "Galadriel" , "text" :  
"Lives forever" } }

And now for an AND across two fields. But a BoolQuery and not a MultiMatch  
(not sure what's up with the MultiMatch):

FULL QUERY:  
{  
"from" : 0,  
"size" : 20,  
"query" : {  
"bool" : {  
"must" : [ {  
"match" : {  
"text" : {  
"query" : "lives",  
"type" : "boolean"  
}  
}  
}, {  
"match" : {  
"cn" : {  
"query" : "galadriel",  
"type" : "boolean"  
}  
}  
} ]  
}  
},  
"version" : true,  
"explain" : false,  
"fields" : ["\_ttl", "\_source"]  
}

RESULTS:  
{ "\_index" : "mortal" , "\_type" : "elf" , "\_id" : "2" , "\_version" : 1 ,  
"\_score" : 1.4914339780807495 , "\_source" : { "cn" : "Galadriel" , "text" :  
"Lives forever" } }

But also an AND of a phrase match and a trucated (that is to say, prefix)  
match:

FULL QUERY:  
{  
"from" : 0,  
"size" : 20,  
"query" : {  
"bool" : {  
"must" : [ {  
"match" : {  
"text" : {  
"query" : "lives forever",  
"type" : "phrase",  
"slop" : 0  
}  
}  
}, {  
"prefix" : {  
"cn" : "gala"  
}  
} ]  
}  
},  
"version" : true,  
"explain" : false,  
"fields" : ["\_ttl", "\_source"]  
}

RESULTS:  
{ "\_index" : "mortal" , "\_type" : "elf" , "\_id" : "2" , "\_version" : 1 ,  
"\_score" : 1.4914339780807495 , "\_source" : { "cn" : "Galadriel" , "text" :  
"Lives forever" } }

Brian

On Friday, June 21, 2013 3:46:22 PM UTC-4, Bruno Miranda wrote:

> In this case the first word is found on the first field and the second on  
> the second. I know I can do this with string() but I'd like to stay way  
> from it because it's susceptible to parse errors depending on what the user  
> inputs.
> 
> I get no results using AND.
> 
> ## INDEX
> 
> curl -X POST "[http://localhost:9200/search/document/](http://localhost:9200/search/document/)" -d '{  
> "first":"Barbara","last":"Smith"  
> }'
> 
> ## SEARCH
> 
> curl -X GET '[http://localhost:9200/search/\_search?pretty](http://localhost:9200/search/_search?pretty)' -d '{  
> "query": {  
> "multi\_match": {  
> "query": "Barbara Smith",  
> "operator": "AND",  
> "fields": [  
> "first",  
> "last"  
> ]  
> }  
> }  
> }'
> 
> ## RESULT
> 
> {  
> "took": 1,  
> "timed\_out": false,  
> "\_shards": {  
> "total": 1,  
> "successful": 1,  
> "failed": 0  
> },  
> "hits": {  
> "total": 0,  
> "max\_score": null,  
> "hits":   
> }  
> }
> 
> I get a result using OR.
> 
> ## INDEX
> 
> curl -X POST "[http://localhost:9200/search/document/](http://localhost:9200/search/document/)" -d '{  
> "first":"Barbara","last":"Smith"  
> }'
> 
> ## SEARCH
> 
> curl -X GET '[http://localhost:9200/search/\_search?pretty](http://localhost:9200/search/_search?pretty)' -d '{  
> "query": {  
> "multi\_match": {  
> "query": "Barbara Smith",  
> "operator": "OR",  
> "fields": [  
> "first",  
> "last"  
> ]  
> }  
> }  
> }'
> 
> ## RESULT
> 
> {  
> "took": 3,  
> "timed\_out": false,  
> "\_shards": {  
> "total": 1,  
> "successful": 1,  
> "failed": 0  
> },  
> "hits": {  
> "total": 1,  
> "max\_score": 0.4898842,  
> "hits": [  
> {  
> "\_index": "search",  
> "\_type": "document",  
> "\_id": "Z0nxJ5H6R1ONDwU1xfOKIA",  
> "\_score": 0.4898842,  
> "\_source": {  
> "first": "Barbara",  
> "last": "Smith"  
> }  
> }  
> ]  
> }  
> }
> 
> What are my options?
> 
> Thank you

--  
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:29am UTC](https://discuss.elastic.co/t/is-it-possible-to-use-the-and-operator-for-multiple-fields-match/12514/4 "2017-07-06T02:29:32Z")

</div>


