Kevin Sylvestre
Kevin Sylvestre

Reputation: 38012

Combining Multiple Should and Must Queries as Subqueries

Is it possible to merge two different bool filters? For example given the following documents:

| Name                | Location |
|=====================|==========|
| Bordeaux Red Wine   | France   |
| Bordeaux Red Wine   | France   |
| Bordeaux White Wine | France   |
| Niagara Red Wine    | Canada   |
| Niagara White Wine  | Canada   |
| Naples Red Wine     | Italy    |
| Naples White Wine   | Italy    |

The following query returns the red wines with a stronger weight for the French red wines:

{
  "bool": {
    "must": [
      { "term": { "name" : "red" } }
    ],
    "should": [
      { "term": { "location": "France" }},
    ],
  }
}

The following query returns the white wines with a stronger weight for Canadian white wines:

{
  "bool": {
    "must": [
      { "term": { "name" : "white" } }
    ],
    "should": [
      { "term": { "location": "Canada" }},
    ],
  }
}

Is it possible to intertwine the results without running multiple queries? I am looking to find a query that scores the French whites and Canadian reds as 1.0, then the other French and Canadian wines as 0.5, and the Italian wines as 0.0.

Upvotes: 1

Views: 1811

Answers (1)

Andrei Stefan
Andrei Stefan

Reputation: 52368

Quite long, but it should order the wines as french whites and canadian reds first and with an equal score, other french and canadian second and with an equal score, italian last with an equal score:

{
  "query": {
    "bool": {
      "should": [
        {
          "constant_score": {
            "query": {
              "bool": {
                "should": [
                  {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "name": "white"
                          }
                        },
                        {
                          "term": {
                            "location": "france"
                          }
                        }
                      ]
                    }
                  },
                  {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "name": "red"
                          }
                        },
                        {
                          "term": {
                            "location": "canada"
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            },
            "boost": 1
          }
        },
        {
          "constant_score": {
            "query": {
              "bool": {
                "should": [
                  {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "location": "france"
                          }
                        }
                      ]
                    }
                  },
                  {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "location": "canada"
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            },
            "boost": 0.5
          }
        },
        {
          "constant_score": {
            "query": {
              "bool": {
                "should": [
                  {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "location": "italy"
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            },
            "boost": 0
          }
        }
      ]
    }
  }
}

Upvotes: 4

Related Questions