# A little help in a "Complex query"

**URL:** <https://discuss.elastic.co/t/a-little-help-in-a-complex-query/9969>\
**Category:** Elasticsearch\
**Created:** [December 6, 2012, 9:42am UTC](https://discuss.elastic.co/t/a-little-help-in-a-complex-query/9969 "2012-12-06T09:42:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roman\_Kournjaev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roman_kournjaev/32/2552_2.png) [@Roman\_Kournjaev](https://discuss.elastic.co/u/Roman_Kournjaev)\
**Post date:** [December 6, 2012, 9:42am UTC](https://discuss.elastic.co/t/a-little-help-in-a-complex-query/9969/1 "2012-12-06T09:42:07Z")

</div>

Hi dear ES community

I am trying to achive some query functionallity from ES , and not sure how  
to do that right.

So i have a large product index.

And I would like to query for :

1. Query for products that have "sharp televisions" in "name"
2. Then i would like lucene to order the results from 1 by having also  
"sharp televisions" in their "manufacturer" field

I looks to me like a nested query , get all the results from 1 and apply  
the function from 2 on them.

So for instance here is the clause from 1 :

{  
"query": {  
"query\_string": {  
"query": "sharp televisions",  
"default\_field": "name",  
"default\_operator": "and"  
}  
}  
}

And that clause would be from 2 :  
{  
"custom\_boost\_factor": {  
"query": {  
"query\_string": {  
"query": "sharp televisions",  
"fields": [  
"seller",  
"manufacturer"  
],  
"default\_operator": "or"  
}  
},  
"boost\_factor": 3.0  
}  
}

Now how the hell i create a nested query with these 2 clauses ?

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 6, 2012, 10:12am UTC](https://discuss.elastic.co/t/a-little-help-in-a-complex-query/9969/2 "2012-12-06T10:12:25Z")

</div>

Hi Roman,

What not using a MatchQuery on each field and use a boost factor for the Match  
query on manufacturer?

Does it help?  
David

Le 6 décembre 2012 à 10:42, Roman Kournjaev [kournjaev@gmail.com](mailto:kournjaev@gmail.com) a écrit :

> Hi dear ES community
> 
> I am trying to achive some query functionallity from ES , and not sure how to  
> do that right.
> 
> So i have a large product index.
> 
> And I would like to query for :
> 
> ```
> 1. Query for products that have "sharp televisions" in "name"
> 2. Then i would like lucene to order the results from 1 by having also
> 
> ```
> 
> "sharp televisions" in their "manufacturer" field
> 
> I looks to me like a nested query , get all the results from 1 and apply the  
> function from 2 on them.
> 
> So for instance here is the clause from 1 :
> 
> {  
> "query": {  
> "query\_string": {  
> "query": "sharp televisions",  
> "default\_field": "name",  
> "default\_operator": "and"  
> }  
> }  
> }
> 
> And that clause would be from 2 :  
> {  
> "custom\_boost\_factor" : {  
> "query" : {  
> "query\_string" : {  
> "query" : "sharp televisions" ,  
> "fields" : [  
> "seller" ,  
> "manufacturer"  
> ],  
> "default\_operator" : "or"  
> }  
> },  
> "boost\_factor" : 3.0  
> }  
> }
> 
> Now how the hell i create a nested query with these 2 clauses ?
> 
> --

--  
David Pilato  
[http://www.scrutmydocs.org/](http://www.scrutmydocs.org/)  
[http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

--

---

<div class="post-metadata">

**Author:** ![Roman\_Kournjaev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roman_kournjaev/32/2552_2.png) [@Roman\_Kournjaev](https://discuss.elastic.co/u/Roman_Kournjaev)\
**Post date:** [December 6, 2012, 10:18am UTC](https://discuss.elastic.co/t/a-little-help-in-a-complex-query/9969/3 "2012-12-06T10:18:11Z")

</div>

Hi David

Not sure it will return me the results I need.

If the 2 clauses are in the same level , and in the second level i have the  
operator set to "OR". I will get results , (even if they are not high  
rated) , that have for instance "sharp" in the manufacturer but are not  
televisions.  
Is there an opiton to use a nested query ?

Roman

On Thursday, December 6, 2012 12:12:25 PM UTC+2, David Pilato wrote:

> Hi Roman,
> 
> What not using a MatchQuery on each field and use a boost factor for the  
> Match query on manufacturer?
> 
> Does it help?  
> David
> 
> Le 6 décembre 2012 à 10:42, Roman Kournjaev \<[kour...@gmail.com](mailto:kour...@gmail.com)\<javascript:\>\>  
> a écrit :
> 
> Hi dear ES community
> 
> I am trying to achive some query functionallity from ES , and not sure  
> how to do that right.
> 
> So i have a large product index.
> 
> And I would like to query for :
> 
> 1. Query for products that have "sharp televisions" in "name"
> 2. Then i would like lucene to order the results from 1 by having also  
> "sharp televisions" in their "manufacturer" field
> 
> I looks to me like a nested query , get all the results from 1 and apply  
> the function from 2 on them.
> 
> So for instance here is the clause from 1 :
> 
> {  
> "query": {  
> "query\_string": {  
> "query": "sharp televisions",  
> "default\_field": "name",  
> "default\_operator": "and"  
> }  
> }  
> }
> 
> And that clause would be from 2 :  
> {  
> "custom\_boost\_factor" : {  
> "query" : {  
> "query\_string" : {  
> "query" : "sharp televisions" ,  
> "fields" : [  
> "seller" ,  
> "manufacturer"  
> ],  
> "default\_operator" : "or"  
> }  
> },  
> "boost\_factor" : 3.0  
> }  
> }
> 
> Now how the hell i create a nested query with these 2 clauses ?
> 
> --
> 
> --  
> David Pilato  
> [http://www.scrutmydocs.org/](http://www.scrutmydocs.org/)  
> [http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [December 7, 2012, 4:35am UTC](https://discuss.elastic.co/t/a-little-help-in-a-complex-query/9969/4 "2012-12-07T04:35:27Z")

</div>

If I understood you requirements correctly, you should be able to build  
such query by using the custom\_filters\_score[http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html)query with the first clause as a query and the second clause as one of the  
filters.

On Thursday, December 6, 2012 5:18:11 AM UTC-5, Roman Kournjaev wrote:

> Hi David
> 
> Not sure it will return me the results I need.
> 
> If the 2 clauses are in the same level , and in the second level i have  
> the operator set to "OR". I will get results , (even if they are not high  
> rated) , that have for instance "sharp" in the manufacturer but are not  
> televisions.  
> Is there an opiton to use a nested query ?
> 
> Roman
> 
> On Thursday, December 6, 2012 12:12:25 PM UTC+2, David Pilato wrote:
> 
> > Hi Roman,
> > 
> > What not using a MatchQuery on each field and use a boost factor for the  
> > Match query on manufacturer?
> > 
> > Does it help?  
> > David
> > 
> > Le 6 décembre 2012 à 10:42, Roman Kournjaev [kour...@gmail.com](mailto:kour...@gmail.com) a  
> > écrit :
> > 
> > Hi dear ES community
> > 
> > I am trying to achive some query functionallity from ES , and not sure  
> > how to do that right.
> > 
> > So i have a large product index.
> > 
> > And I would like to query for :
> > 
> > 1. Query for products that have "sharp televisions" in "name"
> > 2. Then i would like lucene to order the results from 1 by having  
> > also "sharp televisions" in their "manufacturer" field
> > 
> > I looks to me like a nested query , get all the results from 1 and  
> > apply the function from 2 on them.
> > 
> > So for instance here is the clause from 1 :
> > 
> > {  
> > "query": {  
> > "query\_string": {  
> > "query": "sharp televisions",  
> > "default\_field": "name",  
> > "default\_operator": "and"  
> > }  
> > }  
> > }
> > 
> > And that clause would be from 2 :  
> > {  
> > "custom\_boost\_factor" : {  
> > "query" : {  
> > "query\_string" : {  
> > "query" : "sharp televisions" ,  
> > "fields" : [  
> > "seller" ,  
> > "manufacturer"  
> > ],  
> > "default\_operator" : "or"  
> > }  
> > },  
> > "boost\_factor" : 3.0  
> > }  
> > }
> > 
> > Now how the hell i create a nested query with these 2 clauses ?
> > 
> > --
> > 
> > --  
> > David Pilato  
> > [http://www.scrutmydocs.org/](http://www.scrutmydocs.org/)  
> > [http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

--

---

<div class="post-metadata">

**Author:** ![Roman\_Kournjaev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roman_kournjaev/32/2552_2.png) [@Roman\_Kournjaev](https://discuss.elastic.co/u/Roman_Kournjaev)\
**Post date:** [December 8, 2012, 10:58am UTC](https://discuss.elastic.co/t/a-little-help-in-a-complex-query/9969/5 "2012-12-08T10:58:27Z")

</div>

Thanks Igor , thats exactly what i need.

On Friday, December 7, 2012 6:35:27 AM UTC+2, Igor Motov wrote:

> If I understood you requirements correctly, you should be able to build  
> such query by using the custom\_filters\_score[http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html)query with the first clause as a query and the second clause as one of the  
> filters.
> 
> On Thursday, December 6, 2012 5:18:11 AM UTC-5, Roman Kournjaev wrote:
> 
> > Hi David
> > 
> > Not sure it will return me the results I need.
> > 
> > If the 2 clauses are in the same level , and in the second level i have  
> > the operator set to "OR". I will get results , (even if they are not high  
> > rated) , that have for instance "sharp" in the manufacturer but are not  
> > televisions.  
> > Is there an opiton to use a nested query ?
> > 
> > Roman
> > 
> > On Thursday, December 6, 2012 12:12:25 PM UTC+2, David Pilato wrote:
> > 
> > > Hi Roman,
> > > 
> > > What not using a MatchQuery on each field and use a boost factor for  
> > > the Match query on manufacturer?
> > > 
> > > Does it help?  
> > > David
> > > 
> > > Le 6 décembre 2012 à 10:42, Roman Kournjaev [kour...@gmail.com](mailto:kour...@gmail.com) a  
> > > écrit :
> > > 
> > > Hi dear ES community
> > > 
> > > I am trying to achive some query functionallity from ES , and not sure  
> > > how to do that right.
> > > 
> > > So i have a large product index.
> > > 
> > > And I would like to query for :
> > > 
> > > 1. Query for products that have "sharp televisions" in "name"
> > > 2. Then i would like lucene to order the results from 1 by having  
> > > also "sharp televisions" in their "manufacturer" field
> > > 
> > > I looks to me like a nested query , get all the results from 1 and  
> > > apply the function from 2 on them.
> > > 
> > > So for instance here is the clause from 1 :
> > > 
> > > {  
> > > "query": {  
> > > "query\_string": {  
> > > "query": "sharp televisions",  
> > > "default\_field": "name",  
> > > "default\_operator": "and"  
> > > }  
> > > }  
> > > }
> > > 
> > > And that clause would be from 2 :  
> > > {  
> > > "custom\_boost\_factor" : {  
> > > "query" : {  
> > > "query\_string" : {  
> > > "query" : "sharp televisions" ,  
> > > "fields" : [  
> > > "seller" ,  
> > > "manufacturer"  
> > > ],  
> > > "default\_operator" : "or"  
> > > }  
> > > },  
> > > "boost\_factor" : 3.0  
> > > }  
> > > }
> > > 
> > > Now how the hell i create a nested query with these 2 clauses ?
> > > 
> > > --
> > > 
> > > --  
> > > David Pilato  
> > > [http://www.scrutmydocs.org/](http://www.scrutmydocs.org/)  
> > > [http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
> > > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

--

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 3:00am UTC](https://discuss.elastic.co/t/a-little-help-in-a-complex-query/9969/6 "2017-07-06T03:00:45Z")

</div>


