# Dealing with nested documents and multiplication

**URL:** <https://discuss.elastic.co/t/dealing-with-nested-documents-and-multiplication/22302>\
**Category:** Elasticsearch\
**Created:** [February 20, 2015, 8:03pm UTC](https://discuss.elastic.co/t/dealing-with-nested-documents-and-multiplication/22302 "2015-02-20T20:03:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matias\_Tealdi](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@Matias\_Tealdi](https://discuss.elastic.co/u/Matias_Tealdi)\
**Post date:** [February 20, 2015, 8:03pm UTC](https://discuss.elastic.co/t/dealing-with-nested-documents-and-multiplication/22302/1 "2015-02-20T20:03:11Z")

</div>

Hello, I need help I want to calculate the score of a document using the  
information in a nested doc of it.

More context, I have the document mapping.

"product": {  
"properties": {  
"name" : {  
"type": "string"  
}  
"offers": {  
"type": "nested",  
"properties" : {  
"id" : {  
"type": "string",  
"index": "not\_analized"  
}  
"price": {  
"type": "double"  
}  
}  
}  
}  
}

A doc example would be :

"product": {  
"name" : "tv",  
"offers" : [  
{"id" : "1", "price": 10},  
{"id" : "2", "price": 11},  
{"id" : "3", "price": 7},  
]  
}

I want to make the following query. Given a set of offers with an id and a  
expected price, for instance

query : [{"id": "1", "price" : 15}, {"id": "3", "price": 15}]

I wanna order the doc according the fallowing algorithm  
the score of a doc should be.

Do the intersection of the query offers with the offers of the doc.  
for each nested offers of the doc in the intersection, apply a linear decay  
function with the corresponding offer in the query  
and finally multiply these value.

I can do almost all this query, but as the offers are nested document, I  
can modify the doc score with  
min, max, sum, avg but not with product.

Here is what i was trying...

{  
"explain": true,  
"query": {  
"bool" : {  
"should" : [{  
"nested" : {  
"query" : {  
"function\_score" : {  
"filter" : {  
"term" : { "id" : "1"}  
},  
"functions" : [{  
"linear" : {  
"price" : {  
"origin" : "15",  
"scale" : "2",  
"offset" : "1"  
}  
}  
}]  
}  
},  
"path" : "offers",  
"score\_mode" : "sum"  
}  
},{  
"nested" : {  
"query" : {  
"function\_score" : {  
"filter" : {  
"term" : { "id" : "3"}  
},  
"functions" : [{  
"linear" : {  
"price" : {  
"origin" : "15",  
"scale" : "2",  
"offset" : "1"  
}  
}  
}]  
}  
},  
"path" : "offers",  
"score\_mode" : "sum"  
}  
}]  
}  
}  
}

But the nested is summing and the should is also summing.  
Another idea is replace the product with the sum of logarithm, so I would  
need in this  
case calculate the log of the linear decay function.

In conclusion,  
I can not figure out a way of multiply in the score of the parent document  
the value calculated in each nested child.  
I can not figure out a way of get the log of the nested query and then use  
the sum of the should.

does Some one have an idea of how to do it?

Thanks and sorry for the long post.  
Matias.

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/a050cd22-6a35-4920-bba0-b90ac2963eb8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a050cd22-6a35-4920-bba0-b90ac2963eb8%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:31am UTC](https://discuss.elastic.co/t/dealing-with-nested-documents-and-multiplication/22302/2 "2017-07-06T00:31:08Z")

</div>


