Can i filter with "and" and "or"?


(mrblue) #1

Hi,

i have a query with 3 "and". I´d like one of them to be "OR".

So results would be for tag=green AND tag=blue AND (name= John OR name
=Dennis). Can´t figure the correct syntax..

Here is my query:

{
"query" : { "match_all" : { } } ,

 "filter" : {
      "and" : [
	   { "term" : { "bla" : "foo" }}
               { "term" : { "foo" : "blabla" }},
               { "term" : { "color" : "blue" }}
	           ]
                   },

"facets" : {
  "uhren-facetten" : { "terms" : {"fields" : ["Hersteller" ,

"Ziffernblattfarbe" , "Anzeige" , "Uhrwerk" , "Armbandfarbe" ,
"Funktionen" , "Armbandmaterial" , "Wasserdichte" ], "size" :
"100" , "order" : "term" } , "all_terms" : true } }
}

NOTE: there might be a typo or two because i build the query with PHP
usually but it works fine so far :slight_smile:

The Goal here would be to return results for color: blue OR color:
yellow as long as all results are either foo-blabla-blue or foo-blabla
yello.

Thx a lot !


(Ivan Brusic) #2

Use "must" and "should" instead of "and" and "or".

http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html
http://www.elasticsearch.org/guide/reference/query-dsl/bool-filter.html

A blog post about the subject just came out when you posted:
http://www.lucidimagination.com/blog/2011/12/28/why-not-and-or-and-not/

Cheers,

Ivan

On Wed, Dec 28, 2011 at 3:25 PM, mrblue john.agricola.27@googlemail.com wrote:

Hi,

i have a query with 3 "and". I´d like one of them to be "OR".

So results would be for tag=green AND tag=blue AND (name= John OR name
=Dennis). Can´t figure the correct syntax..

Here is my query:

{
"query" : { "match_all" : { } } ,

    "filter" : {
         "and" : [
              { "term" : { "bla" : "foo" }}
              { "term" : { "foo" : "blabla" }},
              { "term" : { "color" : "blue" }}
                      ]
                  },

   "facets" : {
 "uhren-facetten" : { "terms" : {"fields" : ["Hersteller" ,

"Ziffernblattfarbe" , "Anzeige" , "Uhrwerk" , "Armbandfarbe" ,
"Funktionen" , "Armbandmaterial" , "Wasserdichte" ], "size" :
"100" , "order" : "term" } , "all_terms" : true } }
}

NOTE: there might be a typo or two because i build the query with PHP
usually but it works fine so far :slight_smile:

The Goal here would be to return results for color: blue OR color:
yellow as long as all results are either foo-blabla-blue or foo-blabla
yello.

Thx a lot !


(mrblue) #3

Very interesting read and it probably fits my needs much better !
However, there is no way i can build the actual query from that doc
inside elasticsearch.. Are you willing to help me out here ?

I need a sample query for X is a must, Y is a must, A or B or C.

In this case valid results would be X-Y-A , X-Y-B , X-Y-C

(I need to dynamically add AND (=should ?) conditions to my query.
(corresponding to the facets used). Then, some will have only one
value to match, others might have up to ~12.)

I start to like this :slight_smile:

On Dec 29, 12:49 am, Ivan Brusic i...@brusic.com wrote:

Use "must" and "should" instead of "and" and "or".

http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.htmlhttp://www.elasticsearch.org/guide/reference/query-dsl/bool-filter.html

A blog post about the subject just came out when you posted:http://www.lucidimagination.com/blog/2011/12/28/why-not-and-or-and-not/

Cheers,

Ivan

On Wed, Dec 28, 2011 at 3:25 PM, mrblue john.agricola...@googlemail.com wrote:

Hi,

i have a query with 3 "and". I´d like one of them to be "OR".

So results would be for tag=green AND tag=blue AND (name= John OR name
=Dennis). Can´t figure the correct syntax..

Here is my query:

{
"query" : { "match_all" : { } } ,

    "filter" : {
         "and" : [
              { "term" : { "bla" : "foo" }}
              { "term" : { "foo" : "blabla" }},
              { "term" : { "color" : "blue" }}
                      ]
                  },
   "facets" : {
 "uhren-facetten" : { "terms" : {"fields" : ["Hersteller" ,

"Ziffernblattfarbe" , "Anzeige" , "Uhrwerk" , "Armbandfarbe" ,
"Funktionen" , "Armbandmaterial" , "Wasserdichte" ], "size" :
"100" , "order" : "term" } , "all_terms" : true } }
}

NOTE: there might be a typo or two because i build the query with PHP
usually but it works fine so far :slight_smile:

The Goal here would be to return results for color: blue OR color:
yellow as long as all results are either foo-blabla-blue or foo-blabla
yello.

Thx a lot !


(Frederic) #4

Hi John,

I'm not sure how to build your query using 'must'/'should' but, with
AND/OR it is pretty simple I guess:

If you want somenthing like "X and Y and (A or B or C), you may have
something like (a 'constant_score' query is equivalent to a
'match_all' one. https://groups.google.com/group/elasticsearch/browse_frm/thread/4dfdb2c2a2b30fde/26913f5863902251):

"query": {
"constant_score" : {
"filter" : {
"and" : [
{ "term" : { "tagX" : "X" }}
{ "term" : { "tagY" : "Y" }},
{
"or" : [
{ "term" : { "tagABC" : "A" }}
{ "term" : { "tagABC" : "B" }}
{ "term" : { "tagABC" : "C" }}
]
}
]
}
}
}

Off course I can't guarantee this is the most performant option, but
it would do the work.

Cheers,

On 29 dic, 00:17, mrblue john.agricola...@googlemail.com wrote:

Very interesting read and it probably fits my needs much better !
However, there is no way i can build the actual query from that doc
inside elasticsearch.. Are you willing to help me out here ?

I need a sample query for X is a must, Y is a must, A or B or C.

In this case valid results would be X-Y-A , X-Y-B , X-Y-C

(I need to dynamically add AND (=should ?) conditions to my query.
(corresponding to the facets used). Then, some will have only one
value to match, others might have up to ~12.)

I start to like this :slight_smile:

On Dec 29, 12:49 am, Ivan Brusic i...@brusic.com wrote:

Use "must" and "should" instead of "and" and "or".

http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.htm...

A blog post about the subject just came out when you posted:http://www.lucidimagination.com/blog/2011/12/28/why-not-and-or-and-not/

Cheers,

Ivan

On Wed, Dec 28, 2011 at 3:25 PM, mrblue john.agricola...@googlemail.com wrote:

Hi,

i have a query with 3 "and". I´d like one of them to be "OR".

So results would be for tag=green AND tag=blue AND (name= John OR name
=Dennis). Can´t figure the correct syntax..

Here is my query:

{
"query" : { "match_all" : { } } ,

    "filter" : {
         "and" : [
              { "term" : { "bla" : "foo" }}
              { "term" : { "foo" : "blabla" }},
              { "term" : { "color" : "blue" }}
                      ]
                  },
   "facets" : {
 "uhren-facetten" : { "terms" : {"fields" : ["Hersteller" ,

"Ziffernblattfarbe" , "Anzeige" , "Uhrwerk" , "Armbandfarbe" ,
"Funktionen" , "Armbandmaterial" , "Wasserdichte" ], "size" :
"100" , "order" : "term" } , "all_terms" : true } }
}

NOTE: there might be a typo or two because i build the query with PHP
usually but it works fine so far :slight_smile:

The Goal here would be to return results for color: blue OR color:
yellow as long as all results are either foo-blabla-blue or foo-blabla
yello.

Thx a lot !


(system) #5