Boolean filter: A and ((C or D) and (X or Y))


(pcdinh) #1

Hi all,

In ES, I can write a query that represents the boolean expression: A and (C
or D)

"filter" : {

        "bool" : {

            "must" : {

                "term" : { "tag" : "A" }

            },

            "should" : [

                {

                    "term" : { "tag" : "C" }

                },

                {

                    "term" : { "tag" : "D" }

                }

            ]

        }

}

However how can I write a query that represents the expression: A and ((C or
D) and (X or Y))? I plan to use a Python dictionary and convert it into a
JSON object.

Thanks


(Lukáš Vlček) #2

Hi,

the example that you have in your email does not in fact corresponds to A
and (C or D). Both C and D are optional in this case and only A is
mandatory. It translates to +A C D, where + denotes terms that must be found
in the document, and C and D are only optional. It seems that documentation
for boolean filter (
http://www.elasticsearch.org/guide/reference/query-dsl/bool-filter.html) has
similar example and can be little confusing. I think in your particular
example the optional (should) clauses have no effect on the result data set
since it will filter all documents having A clause present. If it were a
boolean query and not filter then the effect of optional (should) clauses
would be reflected in document relevancy. Check boolean query doc
http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html for
further info.

Note that both boolean queries and boolean filters can contain nested
queries (or filters) thus creating more complex queries is possible (think
of using bool query instead of term queries in your particular example).

Regards,
Lukas

On Wed, Oct 19, 2011 at 6:46 PM, pcdinh pcdinh@gmail.com wrote:

Hi all,

In ES, I can write a query that represents the boolean expression: A and
(C or D)

"filter" : {

        "bool" : {

            "must" : {

                "term" : { "tag" : "A" }

            },

            "should" : [

                {

                    "term" : { "tag" : "C" }

                },

                {

                    "term" : { "tag" : "D" }

                }

            ]

        }

}

However how can I write a query that represents the expression: A and ((C
or D) and (X or Y))? I plan to use a Python dictionary and convert it into a
JSON object.

Thanks


(system) #3