Michael Kročka
Michael Kročka

Reputation: 655

Search query in elasticsearch that returns result which match either value_1 or either value_2 but not both

I am trying to write an elasticsearch query which returns hits that contain in text field "description" either value "Donald Trump" or value "Joe Biden", but not both. I tried with "should":

"query": {
    "bool": {
        "minimum_should_match": 1,
        "should": [
            {
                "match": {
                    "author.description": {
                        "query": "Joe Biden"
                    }
                }
            },
            {
                "match": {
                    "author.description": {
                        "query": "Donald Trump"
                    }
                }
            }
        ]       
    }
}

But as expected this return hits which contain either one, or both of said strings. Is this possible in elasticsearch? I haven't found a similar question on SO and elasticsearch documentation didn't help me much either.

Upvotes: 1

Views: 100

Answers (1)

Bhavya
Bhavya

Reputation: 16192

There is no direct way to achieve your use case, but you can use a combination of bool query

Adding a working example with index data, mapping, search query, and search result

Index Mapping:

{
  "mappings": {
    "properties": {
      "author": {
        "properties": {
          "description": {
            "type": "text"
          }
        }
      }
    }
  }
}

Index data:

{
  "author": {
    "description": "Joe Biden"
  }
}
{
  "author": {
    "description": "Donald Trump"
  }
}
{
  "author": {
    "description": [
      "Donald Trump",
      "Joe Biden"
    ]
  }
}

Search Query:

{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": {
              "match": {
                "author.description": {
                  "query": "Joe Biden"
                }
              }
            },
            "must_not": {
              "match": {
                "author.description": {
                  "query": "Donald Trump"
                }
              }
            }
          }
        },
        {
          "bool": {
            "must": {
              "match": {
                "author.description": {
                  "query": "Donald Trump"
                }
              }
            },
            "must_not": {
              "match": {
                "author.description": {
                  "query": "Joe Biden"
                }
              }
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "65131464",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0470967,
        "_source": {
          "author": {
            "description": "Joe Biden"
          }
        }
      },
      {
        "_index": "65131464",
        "_type": "_doc",
        "_id": "2",
        "_score": 1.0470967,
        "_source": {
          "author": {
            "description": "Donald Trump"
          }
        }
      }
    ]

Upvotes: 1

Related Questions