GabrielChu
GabrielChu

Reputation: 6166

Boolean query with AND OR operator

Query: National Public Radio\/TV

That is, match a query National Public Raido or National Public TV. I wondered how to perform such a task in elasticsearch? The task is done in Python.

What I've tried:

Boolean Query "National Public Radio"

from elasticsearch import Elasticsearch

es_conn = Elasticsearch(config.ES_HOSTS)

res = es_conn.search(index = config.INDEX_NAME,
      body={
              "_source": ["filename"],
              "query": {
                "match" : {
                  "text" : {
                    "query" : "National Public Radio",
                    "operator" : "and"
                  }
                }
              }
            }
        )

Boolean Query "Radio OR TV"

res = es_conn.search(index = config.INDEX_NAME,
        body={
              "_source": ["filename"],
              "query": {
                "match" : {
                  "text" : {
                    "query" : "Radio TV",
                  }
                }
              }
            }
        )

But how to combine these two, I have no clue, could anyone help?

Upvotes: 1

Views: 453

Answers (1)

sourabh1024
sourabh1024

Reputation: 657

You can use the following query to check if the query text matches the National Public TV or national Public Radio. It used should operator and minimum number should match equals to one ensures that the text should either match one of these two.

{
  "query": {
     "bool": {
        "should": [
           {

                 "match": {
                    "text": {
                       "query": "National Public Radio"
                    }

              }
           },
           {

                 "match": {
                    "text": {
                       "query": "National Public TV"
                    }
                 }

           }
        ],
        "minimum_should_match" : "1"
     }
  }
}

Upvotes: 1

Related Questions