0

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?

6
  • so, what is your question? Commented Feb 27, 2019 at 15:39
  • @christouandr7 I was updating the question right now, thanks. Commented Feb 27, 2019 at 15:40
  • 1
    Do you mean that the results must have California and Nevada on the text? Commented Feb 27, 2019 at 15:47
  • @christouandr7 yes exactly. Commented Feb 28, 2019 at 8:56
  • 1
    check my answer! hope it is what you are looking for! Commented Mar 1, 2019 at 13:31

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.