0

I am fairly new to Elasticsearch. I am trying to map basic logical queries to elasticsearch query DSL.

I understand that bool is used to make boolean(logical) queries to ES. I can map queries like if (cond1 && cond2), but I cannot understand how to make OR queries like if(cond1 || cond2)

Logical Query

if(attr1==val1 && attr2=val2){}

Elastic Search DSL

"bool" : {
    "must" : {
        ["term":{"attr1":"val1"}, {"term":"attr2":"val2"}]
    }
}

What do I write for if(attr1==val1 || attr2==val2) ?

shiladitya
  • 2,290
  • 1
  • 23
  • 36

1 Answers1

0

You can do it like this by using the | (or) operator (see docs):

s = search.Search()
s = s.filter(Q('term', attr1='val1') | Q('term', attr2='val2'))

This is equivalent to

"bool" : {
    "should" : {
        ["term":{"attr1":"val1"}, {"term":"attr2":"val2"}]
    },
    "minimum_should_match": 1
}
Val
  • 207,596
  • 13
  • 358
  • 360
  • I am not using python. Can you provide me a corresponding JSON query DSL for the same? – shiladitya May 27 '16 at 06:47
  • Thanks, I also got the idea from here - https://www.elastic.co/guide/en/elasticsearch/guide/current/combining-filters.html Should have gone through this first! – shiladitya May 27 '16 at 06:58