Combining Multiple Queries with 'OR' or 'AND'

Hi,
Question

I am trying to combine 2 user search queries using AND and OR as operation.
I am aware of combining queries where we can merge filters, but I want to
merge entire queries like { {BIG Elastic Query1} AND {BIG Elastic Query2} }.
Details

For instance say a user performs a search for "batman in movies type with
filters of Christian and Bale" and another query "Dark Knight in type
tvshows with filters of Christopher Nolan". I want to combine both queries
so I can look for both batman movies and Dark Knight tvshows, but not Dark
knight movies or batman tvshows.

For example, for the given queries
I just want to run Query1 OR Query2 in the elasticsearch.
Query 1:

{
"query": {
"filtered": {
"query": {
"query_string":{
"query":"Batman",
"default_operator":"AND",
"fields":[
"Movies._all"
]
}
},
"filter": {
"bool": {
"must": [
{
"query":{
"filtered":{
"filter":{
"and":[
{
"term":{
"cast.firstName":"Christian"
}
},
{
"term":{
"cast.lastName":"Bale"
}
}
]
}
}
}
}
]
}
}
}
}
}

Query2:

{
"query": {
"filtered": {
"query": {
"query_string":{
"query":"Dark Knight",
"default_operator":"AND",
"fields":[
"tvshows._all"
]
}
},
"filter": {
"bool": {
"must": [
{
"query":{
"filtered":{
"filter":{
"and":[
{
"term":{
"director.firstName":"Christopher"
}
},
{
"term":{
"director.lastName":"Nolan"
}
}
]
}
}
}
}
]
}
}
}
}
}

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/be301214-1b6a-4065-8c8e-f1e5048e9873%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hi,

bool query should work:

{
"query": {
"bool": {
"should": [
{
"filtered": { // Query1
"query": {...},
"filter": {...}
}
},
{
"filtered": { // Query2
"query": {...},
"filter": {...}
}
}
]
}
}
}

Masaru

On Fri, Feb 20, 2015 at 8:29 AM, Debashish Paul shimanbb@gmail.com wrote:

Hi,
Question

I am trying to combine 2 user search queries using AND and OR as
operation. I am aware of combining queries where we can merge filters, but
I want to merge entire queries like { {BIG Elastic Query1} AND {BIG Elastic
Query2} }.
Details

For instance say a user performs a search for "batman in movies type with
filters of Christian and Bale" and another query "Dark Knight in type
tvshows with filters of Christopher Nolan". I want to combine both queries
so I can look for both batman movies and Dark Knight tvshows, but not Dark
knight movies or batman tvshows.

For example, for the given queries
I just want to run Query1 OR Query2 in the elasticsearch.
Query 1:

{
"query": {
"filtered": {
"query": {
"query_string":{
"query":"Batman",
"default_operator":"AND",
"fields":[
"Movies._all"
]
}
},
"filter": {
"bool": {
"must": [
{
"query":{
"filtered":{
"filter":{
"and":[
{
"term":{
"cast.firstName":"Christian"
}
},
{
"term":{
"cast.lastName":"Bale"
}
}
]
}
}
}
}
]
}
}
}
}
}

Query2:

{
"query": {
"filtered": {
"query": {
"query_string":{
"query":"Dark Knight",
"default_operator":"AND",
"fields":[
"tvshows._all"
]
}
},
"filter": {
"bool": {
"must": [
{
"query":{
"filtered":{
"filter":{
"and":[
{
"term":{
"director.firstName":"Christopher"
}
},
{
"term":{
"director.lastName":"Nolan"
}
}
]
}
}
}
}
]
}
}
}
}
}

--
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.
To view this discussion on the web visit
https://groups.google.com/d/msgid/elasticsearch/be301214-1b6a-4065-8c8e-f1e5048e9873%40googlegroups.com
https://groups.google.com/d/msgid/elasticsearch/be301214-1b6a-4065-8c8e-f1e5048e9873%40googlegroups.com?utm_medium=email&utm_source=footer
.
For more options, visit https://groups.google.com/d/optout.

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CAGmu3c1GF4sQX_Pa7RRL2xOFz0gXs2r5-dtx55AsToxCgWtKcQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.