Reputation: 16291
In my ElasticSearch document index I have a property type
like
type= LOCATION | PERSON | TIME
and a text
field that represents the whole document.
To search for types like LOCATION
and a specific text like `Mountain View" I do like
doc.text:Mountain View AND doc.type:LOCATION
If I want to do a OR
query I would use instead the query_string
approach like
"query": {
"query_string": {
"query": "entity.text: (Mountain View OR Menlo Park) AND entity.type:LOCATION"
}
}
This works as well. To do AND
queries, like searching for item.text
having both "Mountain View" and "Menlo Park" for a item.type=LOCATION
, it does not work doing like
"query": {
"query_string": {
"query": "entity.text: (California AND Nevada) AND entity.type:LOCATION"
}
}
Other attempts were:
Using bool
clause with should
like:
{
"query": {
"bool": {
"should": [
{ "match": { "text": "Menlo Park" }},
{ "match": { "text": "Mountain View" }}
]
}
}
}
Using cross-fields
with multi_match
"query": {
"multi_match": {
"query": "California Nevada",
"type": "cross_fields",
"operator": "AND",
"fields": [
"text"
]
}
}
Another approach was using must
with the latter (in this case omitting the type
by the way):
{
"query": {
"bool": {
"must": [
{
"multi_match" : {
"query": "Nevada",
"type": "cross_fields",
"fields": [ "text"],
}
},
{
"multi_match" : {
"query": "California",
"type": "cross_fields",
"fields": [ "text" ]
}
}
]
}
}
}
but it returns no results neither. Note that in the last case using should
instead of must
will produce an OR
query that will work ok.
So how to perform an AND
query on the same field text
to match multiple values like California
and Nevada
?
Upvotes: 0
Views: 49
Reputation: 169
If I understood the question right, I would do the following:
{
"query": {
"bool" : {
"must": [
"match" : {
"text" : {
"query" : "California Nevada",
"operator" : "and"
}
}
]
}
}
}
Documentation Hope it helps!
Upvotes: 1