Bool query

Hi, I'm trying to write a bool query that will act such as (event_time in range) AND (at least one of the should condition is correct)

I implemented it as:

{
"query": {
"bool": {
"must":
{
"range": {
"events.event_time": {
"from": "2013-06-01",
"to": "2013-06-03",
"include_lower": true,
"include_upper": true
}
}
},
"should": {
"term": {
"events.products.product": "1772166"
}
}
,"minimum_should_match" : 1
}
},
"path": "events"
}

But it seems like the condition between the MUST and the SHOULD conditions is an OR instead of an AND, so I get a lot more results than I should.

I read in the link below that:"In the query after this, a boolean query gets created, and in this query, "mushrooms" must be present, but "steak" should be present. If "steak" is not present but "mushrooms" is present for a document, it will be still returned as a hit."
http://www.avajava.com/tutorials/lessons/how-do-i-combine-queries-with-a-boolean-query.html

So I understand that these 2 operations are having an OR between them. Any idea how can I make them combined under an AND condition?

Please not that the should condition should contain more then one product(but at least one should match).

Why not simply convert the should clause into a must clause? It appears
that is what you want.

Here is another good writeup on boolean queries:
http://searchhub.org/2011/12/28/why-not-and-or-and-not/

--
Ivan

On Tue, Jul 23, 2013 at 2:09 AM, oreno oreno@exelate.com wrote:

Hi, I'm trying to write a bool query that will act such as (event_time in
range) AND (at least one of the should condition is correct)

I implemented it as:

{
"query": {
"bool": {
"must":
{
"range": {

"events.event_time": {

  "from": "2013-06-01",

  "to": "2013-06-03",

  "include_lower": true,

  "include_upper": true
                                                                    }
                                                            }
                                                    },
                                                    "should": {
                                                            "term": {

"events.products.product": "1772166"
}
}

,"minimum_should_match" : 1
}
},
"path": "events"
}

But it seems like the condition between the MUST and the SHOULD conditions
is an OR instead of an AND, so I get a lot more results than I should.

I read in the link below that:"In the query after this, a boolean query
gets
created, and in this query, "mushrooms" must be present, but "steak" should
be present. If "steak" is not present but "mushrooms" is present for a
document, it will be still returned as a hit."

http://www.avajava.com/tutorials/lessons/how-do-i-combine-queries-with-a-boolean-query.html

So I understand that these 2 operations are having an OR between them. Any
idea how can I make them combined under an AND condition?

--
View this message in context:
http://elasticsearch-users.115913.n3.nabble.com/Bool-query-tp4038495.html
Sent from the ElasticSearch Users mailing list archive at Nabble.com.

--
You received this message because you are subscribed to the Google Groups
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Hi Ivan, I needed the range to be a must, but I needed another should
condition that will have an AND relation with the range.
I was able to solve this using the "terms" condition(since it's like
"should" that can work inside a must).

"must" : [ {
"range" : {
"events.event_time" : {
"from" : "2013-06-01",
"to" : "2013-06-03",
"include_lower" : true,
"include_upper" : true
}
}
}, {
"terms" : {
"events.segments.segment" : [ "80870","80871" ],
"minimum_should_match" : "1"
}
} ]

Thanks,

[image: Inline image 1]
*
*
Oren Orgad | Software Engineer | eXelate http://www.exelate.com/

p: +972- 72-2221329 | e: oreno@exelate.com

On Tue, Jul 23, 2013 at 7:44 PM, Ivan Brusic [via ElasticSearch Users] <
ml-node+s115913n4038549h54@n3.nabble.com> wrote:

Why not simply convert the should clause into a must clause? It appears
that is what you want.

Here is another good writeup on boolean queries:
http://searchhub.org/2011/12/28/why-not-and-or-and-not/

--
Ivan

On Tue, Jul 23, 2013 at 2:09 AM, oreno <[hidden email]http://user/SendEmail.jtp?type=node&node=4038549&i=0

wrote:

Hi, I'm trying to write a bool query that will act such as (event_time in
range) AND (at least one of the should condition is correct)

I implemented it as:

{
"query": {
"bool": {
"must":
{
"range": {

"events.event_time": {

  "from": "2013-06-01",

  "to": "2013-06-03",

  "include_lower": true,

  "include_upper": true
                                                                    }
                                                            }
                                                    },
                                                    "should": {
                                                            "term": {

"events.products.product": "1772166"
}
}

,"minimum_should_match" : 1
}
},
"path": "events"
}

But it seems like the condition between the MUST and the SHOULD conditions
is an OR instead of an AND, so I get a lot more results than I should.

I read in the link below that:"In the query after this, a boolean query
gets
created, and in this query, "mushrooms" must be present, but "steak"
should
be present. If "steak" is not present but "mushrooms" is present for a
document, it will be still returned as a hit."

http://www.avajava.com/tutorials/lessons/how-do-i-combine-queries-with-a-boolean-query.html

So I understand that these 2 operations are having an OR between them. Any
idea how can I make them combined under an AND condition?

--
View this message in context:
http://elasticsearch-users.115913.n3.nabble.com/Bool-query-tp4038495.html
Sent from the ElasticSearch Users mailing list archive at Nabble.com.

--
You received this message because you are subscribed to the Google Groups
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to [hidden email]http://user/SendEmail.jtp?type=node&node=4038549&i=1
.
For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to [hidden email]http://user/SendEmail.jtp?type=node&node=4038549&i=2
.
For more options, visit https://groups.google.com/groups/opt_out.


If you reply to this email, your message will be added to the discussion
below:

http://elasticsearch-users.115913.n3.nabble.com/Bool-query-tp4038495p4038549.html
To unsubscribe from Bool query, click herehttp://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4038495&code=b3Jlbm9AZXhlbGF0ZS5jb218NDAzODQ5NXw4ODk1Mjk2Nzg=
.
NAMLhttp://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html!nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers!nabble%3Aemail.naml-instant_emails!nabble%3Aemail.naml-send_instant_email!nabble%3Aemail.naml