Warren Abdilla
Warren Abdilla

Reputation: 58

Elastic Search - Filtering Complex Logical Queries

I need to find a way on how to process logical queries within a filter context, in order to apply the filter on the result obtained.

Take the example - (("Fox" AND "Wolf") OR ("Hate" AND "Seafood") OR "Fox").

I would like to apply a filter that gives me the documents which correspond to this query, but also where the documents' corresponding value is 175.

The following query is what I'm passing in Kibana

{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "value": {
            "value": 175
          }
        }
      },
      "should": [
            {
              "bool": {
                "must": [
                  {
                    "match": {
                      "desc": "Fox"
                    }
                  },
                  {
                    "match": {
                      "desc": "Wolf"
                    }
                  }
                ]
              }
            },
             {
              "bool": {
                "must": [
                  {
                    "match": {
                      "desc": "Hate"
                    }
                  },
                  {
                    "match": {
                      "desc": "Seafood"
                    }
                  }
                ]
              }
            },
            {
                "match": {"desc": "Fox"}
            }
          ]
    }
  }
}

This provides me with all of the results which correspond only to the filter provided there - Where value == 175. One way I have managed to bypass this is by providing a minimum score requirement of 0.0000001 to the query so it eliminated the redundant values. But I would prefer a cleaner solution.

It would also be more beneficial to integrate the entire query in a filter context in order for it to be cached by Elastic, but I can't seem to find a way.

The following is a list of the results provided when using the above query.

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 2.1192918,
    "hits": [
      {
        "_index": "keywordprocessor",
        "_type": "my_type",
        "_id": "6",
        "_score": 2.1192918,
        "_source": {
          "desc": "I Hate Seafood",
          "value": 175
        }
      },
      {
        "_index": "keywordprocessor",
        "_type": "my_type",
        "_id": "7",
        "_score": 1.4723401,
        "_source": {
          "desc": "I Hate Seafood",
          "value": 175,
          "vendor": "Seafood"
        }
      },
      {
        "_index": "keywordprocessor",
        "_type": "my_type",
        "_id": "8",
        "_score": 0,
        "_source": {
          "desc": "I Hate peanuts",
          "value": 175,
          "vendor": "Seafood"
        }
      },
      {
        "_index": "keywordprocessor",
        "_type": "my_type",
        "_id": "11",
        "_score": 0,
        "_source": {
          "desc": "I Hate peanuts",
          "value": 175
        }
      }
    ]
  }
}

Any help would be appreciated - I have already looked at the documentation for Boolean Queries and the like, and can't seem to make heads or tails about this.

Upvotes: 0

Views: 333

Answers (1)

Praneeth
Praneeth

Reputation: 751

To combine bool queries in filter context, you should use bool clause, like this:

{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "value": 175
              }
            }
          ],
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "match": {
                      "desc": "Fox"
                    }
                  },
                  {
                    "match": {
                      "desc": "Wolf"
                    }
                  }
                ]
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "match": {
                      "desc": "Hate"
                    }
                  },
                  {
                    "match": {
                      "desc": "Seafood"
                    }
                  }
                ]
              }
            },
            {
              "match": {
                "desc": "Fox"
              }
            }
          ]
        }
      }
    }
  }
}

Upvotes: 1

Related Questions