# Terms on nested object and scoring

**URL:** <https://discuss.elastic.co/t/terms-on-nested-object-and-scoring/9394>\
**Category:** Elasticsearch\
**Created:** [October 17, 2012, 3:17pm UTC](https://discuss.elastic.co/t/terms-on-nested-object-and-scoring/9394 "2012-10-17T15:17:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![samuel\_merlet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/samuel_merlet/32/2656_2.png) [@samuel\_merlet](https://discuss.elastic.co/u/samuel_merlet)\
**Post date:** [October 17, 2012, 3:17pm UTC](https://discuss.elastic.co/t/terms-on-nested-object-and-scoring/9394/1 "2012-10-17T15:17:26Z")

</div>

Hi,

I use a filter on a nested object to check idf the document get a least  
one value like this :

```
"custom_filters_score": {
 "query" : { "term" : { "entity_type_id": 1 } },

  "filters" : [
   {
     "filter" : {
       "bool" :
       {
         "should" : [
           { "term" : { "country_iso3": "usa" } }
         ]
       }
     },
     "boost" : 3.0
   },

    {
     "filter" : {
       "nested" :
       {
         "path" : "profile_type.profile_roles",
         "query" : {
           "filtered" : {
             "query": { "match_all": {}},
             "filter" :
             {
               "bool" :
               {
                 "must" :
                 [
                   {"terms": {"profile_type.profile_roles.role_id": [20,

```

23,5]}},  
{"terms": {  
"profile\_type.profile\_roles.skills.skill\_id": [32,34,25,6,38]}}  
]  
}  
}  
}  
}  
}  
},  
"boost" : 2.0  
}

```
  ],
 "score_mode" : "total"

```

}

Here "profile\_roles" is a nested object containing an array of "roles"

It works great , but in my results some document match all  
profile\_type.profile\_roles.role\_id ( 20 , 23 and 5 ) and all  
profile\_type.profile\_roles.skills.skill\_id while some other match only one  
role.  
So i 'm looking for a way of giving a highest score to the document that  
have more match , the one have 20 , 23 and 5 should have higher score than  
the one who only have role\_id 5.  
Is there any solution for that ?

Thanks

--

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [October 17, 2012, 3:46pm UTC](https://discuss.elastic.co/t/terms-on-nested-object-and-scoring/9394/2 "2012-10-17T15:46:07Z")

</div>

Hello Samuel,

If you want to have higher score on the role\_id, you can specify a  
boost for that terms query in particular. Something like:

"bool" : {  
"must" : [  
{  
"terms": {  
"profile\_type.profile\_roles.role\_id": [20,23,5],  
"boost" : 5.0  
}  
},  
{"terms": {"profile\_type.profile\_roles.skills.skill\_id":  
[32,34,25,6,38]}}  
]  
}

## Best regards, Radu

[http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

On Wed, Oct 17, 2012 at 6:17 PM, samuel merlet [samuel.merlet@gmail.com](mailto:samuel.merlet@gmail.com) wrote:

> Hi,
> 
> I use a filter on a nested object to check idf the document get a least one  
> value like this :
> 
> ```
> "custom_filters_score": {
> "query" : { "term" : { "entity_type_id": 1 } },
> 
> "filters" : [
> {
> "filter" : {
> "bool" :
> {
> "should" : [
> { "term" : { "country_iso3": "usa" } }
> ]
> }
> },
> "boost" : 3.0
> },
> 
> {
> "filter" : {
> "nested" :
> {
> "path" : "profile_type.profile_roles",
> "query" : {
> "filtered" : {
> "query": { "match_all": {}},
> "filter" :
> {
> "bool" :
> {
> "must" :
> [
> {"terms": {"profile_type.profile_roles.role_id":
> 
> ```
> 
> [20,23,5]}},  
> {"terms":  
> {"profile\_type.profile\_roles.skills.skill\_id": [32,34,25,6,38]}}  
> ]  
> }  
> }  
> }  
> }  
> }  
> },  
> "boost" : 2.0  
> }
> 
> ```
> ],
> "score_mode" : "total"
> 
> ```
> 
> }
> 
> Here "profile\_roles" is a nested object containing an array of "roles"
> 
> It works great , but in my results some document match all  
> profile\_type.profile\_roles.role\_id ( 20 , 23 and 5 ) and all  
> profile\_type.profile\_roles.skills.skill\_id while some other match only one  
> role.  
> So i 'm looking for a way of giving a highest score to the document that  
> have more match , the one have 20 , 23 and 5 should have higher score than  
> the one who only have role\_id 5.  
> Is there any solution for that ?
> 
> Thanks
> 
> --

--

---

<div class="post-metadata">

**Author:** ![samuel\_merlet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/samuel_merlet/32/2656_2.png) [@samuel\_merlet](https://discuss.elastic.co/u/samuel_merlet)\
**Post date:** [October 17, 2012, 4:04pm UTC](https://discuss.elastic.co/t/terms-on-nested-object-and-scoring/9394/3 "2012-10-17T16:04:26Z")

</div>

Hi,

No this doesn't works, "terms" filter doesn't support boost. And i don't  
want to boost a filter , i'm looking for a way to give a higher score to  
the document that have more match over the terms list

thanks

On Wednesday, October 17, 2012 5:46:13 PM UTC+2, Radu Gheorghe wrote:

> Hello Samuel,
> 
> If you want to have higher score on the role\_id, you can specify a  
> boost for that terms query in particular. Something like:
> 
> "bool" : {  
> "must" : [  
> {  
> "terms": {  
> "profile\_type.profile\_roles.role\_id": [20,23,5],  
> "boost" : 5.0  
> }  
> },  
> {"terms": {"profile\_type.profile\_roles.skills.skill\_id":  
> [32,34,25,6,38]}}  
> ]  
> }
> 
> ## Best regards, Radu
> 
> [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> 
> On Wed, Oct 17, 2012 at 6:17 PM, samuel merlet \<[samuel...@gmail.com](mailto:samuel...@gmail.com)\<javascript:\>\>  
> wrote:
> 
> > Hi,
> > 
> > I use a filter on a nested object to check idf the document get a least  
> > one  
> > value like this :
> > 
> > ```
> > "custom_filters_score": { 
> > "query" : { "term" : { "entity_type_id": 1 } }, 
> > 
> > "filters" : [ 
> > { 
> > "filter" : { 
> > "bool" : 
> > { 
> > "should" : [ 
> > { "term" : { "country_iso3": "usa" } } 
> > ] 
> > } 
> > }, 
> > "boost" : 3.0 
> > }, 
> > 
> > { 
> > "filter" : { 
> > "nested" : 
> > { 
> > "path" : "profile_type.profile_roles", 
> > "query" : { 
> > "filtered" : { 
> > "query": { "match_all": {}}, 
> > "filter" : 
> > { 
> > "bool" : 
> > { 
> > "must" : 
> > [ 
> > {"terms": {"profile_type.profile_roles.role_id": 
> > 
> > ```
> > 
> > [20,23,5]}},  
> > {"terms":  
> > {"profile\_type.profile\_roles.skills.skill\_id": [32,34,25,6,38]}}  
> > ]  
> > }  
> > }  
> > }  
> > }  
> > }  
> > },  
> > "boost" : 2.0  
> > }
> > 
> > ```
> > ], 
> > "score_mode" : "total" 
> > 
> > ```
> > 
> > }
> > 
> > Here "profile\_roles" is a nested object containing an array of "roles"
> > 
> > It works great , but in my results some document match all  
> > profile\_type.profile\_roles.role\_id ( 20 , 23 and 5 ) and all  
> > profile\_type.profile\_roles.skills.skill\_id while some other match only  
> > one  
> > role.  
> > So i 'm looking for a way of giving a highest score to the document that  
> > have more match , the one have 20 , 23 and 5 should have higher score  
> > than  
> > the one who only have role\_id 5.  
> > Is there any solution for that ?
> > 
> > Thanks
> > 
> > --

--

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [October 17, 2012, 4:12pm UTC](https://discuss.elastic.co/t/terms-on-nested-object-and-scoring/9394/4 "2012-10-17T16:12:59Z")

</div>

Hello Samuel,

Sorry, I completely missed that you had a filter and not a query  
there. The only solution I see here is to move those terms filters as  
term queries, so you can boost on them.

## Best regards, Radu

[http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

On Wed, Oct 17, 2012 at 7:04 PM, samuel merlet [samuel.merlet@gmail.com](mailto:samuel.merlet@gmail.com) wrote:

> Hi,
> 
> No this doesn't works, "terms" filter doesn't support boost. And i don't  
> want to boost a filter , i'm looking for a way to give a higher score to  
> the document that have more match over the terms list
> 
> thanks
> 
> On Wednesday, October 17, 2012 5:46:13 PM UTC+2, Radu Gheorghe wrote:
> 
> > Hello Samuel,
> > 
> > If you want to have higher score on the role\_id, you can specify a  
> > boost for that terms query in particular. Something like:
> > 
> > "bool" : {  
> > "must" : [  
> > {  
> > "terms": {  
> > "profile\_type.profile\_roles.role\_id": [20,23,5],  
> > "boost" : 5.0  
> > }  
> > },  
> > {"terms": {"profile\_type.profile\_roles.skills.skill\_id":  
> > [32,34,25,6,38]}}  
> > ]  
> > }
> > 
> > ## Best regards, Radu
> > 
> > [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> > 
> > On Wed, Oct 17, 2012 at 6:17 PM, samuel merlet [samuel...@gmail.com](mailto:samuel...@gmail.com)  
> > wrote:
> > 
> > > Hi,
> > > 
> > > I use a filter on a nested object to check idf the document get a least  
> > > one  
> > > value like this :
> > > 
> > > ```
> > > "custom_filters_score": {
> > > "query" : { "term" : { "entity_type_id": 1 } },
> > > 
> > > "filters" : [
> > > {
> > > "filter" : {
> > > "bool" :
> > > {
> > > "should" : [
> > > { "term" : { "country_iso3": "usa" } }
> > > ]
> > > }
> > > },
> > > "boost" : 3.0
> > > },
> > > 
> > > {
> > > "filter" : {
> > > "nested" :
> > > {
> > > "path" : "profile_type.profile_roles",
> > > "query" : {
> > > "filtered" : {
> > > "query": { "match_all": {}},
> > > "filter" :
> > > {
> > > "bool" :
> > > {
> > > "must" :
> > > [
> > > {"terms": {"profile_type.profile_roles.role_id":
> > > 
> > > ```
> > > 
> > > [20,23,5]}},  
> > > {"terms":  
> > > {"profile\_type.profile\_roles.skills.skill\_id": [32,34,25,6,38]}}  
> > > ]  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > },  
> > > "boost" : 2.0  
> > > }
> > > 
> > > ```
> > > ],
> > > "score_mode" : "total"
> > > 
> > > ```
> > > 
> > > }
> > > 
> > > Here "profile\_roles" is a nested object containing an array of "roles"
> > > 
> > > It works great , but in my results some document match all  
> > > profile\_type.profile\_roles.role\_id ( 20 , 23 and 5 ) and all  
> > > profile\_type.profile\_roles.skills.skill\_id while some other match only  
> > > one  
> > > role.  
> > > So i 'm looking for a way of giving a highest score to the document that  
> > > have more match , the one have 20 , 23 and 5 should have higher score  
> > > than  
> > > the one who only have role\_id 5.  
> > > Is there any solution for that ?
> > > 
> > > Thanks
> > > 
> > > --
> 
> --

--

---

<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:08am UTC](https://discuss.elastic.co/t/terms-on-nested-object-and-scoring/9394/5 "2017-07-06T03:08:15Z")

</div>


