# Score of matched (nested) documents?

**URL:** <https://discuss.elastic.co/t/score-of-matched-nested-documents/8641>\
**Category:** Elasticsearch\
**Created:** [August 5, 2012, 11:52pm UTC](https://discuss.elastic.co/t/score-of-matched-nested-documents/8641 "2012-08-05T23:52:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [August 5, 2012, 11:52pm UTC](https://discuss.elastic.co/t/score-of-matched-nested-documents/8641/1 "2012-08-05T23:52:54Z")

</div>

I believe the answer is "no", but wanted to check:

Is there a way to return the individual score of the nested documents that  
match a query? I'm performing a query on nested docs which returns  
appropriate parents, but I would like to know which nested child scored the  
highest for the query. The nested score\_mode simply tells me that "one of  
the children scored well", but not which one. Is there any way to sort or  
find the score of the nested children?

Barring that, I'm not certain of the best way to structure my data. Should  
I arrange the data in two types and denormalize so that one type has a  
"foreign key" (to use relational db terminology) that points at the other  
type, which would then require several more queries to access the  
individual documents?

Thanks!  
-Zach

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [August 6, 2012, 1:04pm UTC](https://discuss.elastic.co/t/score-of-matched-nested-documents/8641/2 "2012-08-06T13:04:13Z")

</div>

Looks like I found my answer here:

[https://groups.google.com/d/topic/elasticsearch/1\_1zXSeRsxQ/discussion](https://groups.google.com/d/topic/elasticsearch/1_1zXSeRsxQ/discussion)

Guess I'll start down the denormalization path. IF anyone has some  
comments or opinions on the best way to structure relationships in ES, I'd  
love to hear. Is the general sentiment to _not_ model relationships and  
simply retrieve IDs which you use through another, non-ES mechanism to find  
(say, query a database) ?

Related, what's the performance of simply looking up a specific document by  
it's ID? Is it O(n)? Does ES use something like b-trees for document ID  
lookups?

Thanks!  
-Zach

On Sunday, August 5, 2012 7:52:54 PM UTC-4, Zachary Tong wrote:

> I believe the answer is "no", but wanted to check:
> 
> Is there a way to return the individual score of the nested documents that  
> match a query? I'm performing a query on nested docs which returns  
> appropriate parents, but I would like to know which nested child scored the  
> highest for the query. The nested score\_mode simply tells me that "one of  
> the children scored well", but not which one. Is there any way to sort or  
> find the score of the nested children?
> 
> Barring that, I'm not certain of the best way to structure my data.  
> Should I arrange the data in two types and denormalize so that one type  
> has a "foreign key" (to use relational db terminology) that points at the  
> other type, which would then require several more queries to access the  
> individual documents?
> 
> Thanks!  
> -Zach

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [August 7, 2012, 9:40pm UTC](https://discuss.elastic.co/t/score-of-matched-nested-documents/8641/3 "2012-08-07T21:40:09Z")

</div>

There is no option to get score of specific nested doc, though it has been requested to be able to return "only" the nested doc that matched the query, in which case we can possibly return the score with it. This is a very "up in the air" feature currently, not slated (or even designed) yet.

Regarding the "get" API pert, its not a btree, it will probably be a bit faster then a btree. Thats because there is a bloom filter associated with each segment, so getting a doc will almost all times find the relevant segment, and then within that segment (a segment is part of an index) its more similar to an interval based skip list.

On Aug 6, 2012, at 3:04 PM, Zachary Tong [zacharyjtong@gmail.com](mailto:zacharyjtong@gmail.com) wrote:

> Looks like I found my answer here:
> 
> [https://groups.google.com/d/topic/elasticsearch/1\_1zXSeRsxQ/discussion](https://groups.google.com/d/topic/elasticsearch/1_1zXSeRsxQ/discussion)
> 
> Guess I'll start down the denormalization path. IF anyone has some comments or opinions on the best way to structure relationships in ES, I'd love to hear. Is the general sentiment to not model relationships and simply retrieve IDs which you use through another, non-ES mechanism to find (say, query a database) ?
> 
> Related, what's the performance of simply looking up a specific document by it's ID? Is it O(n)? Does ES use something like b-trees for document ID lookups?
> 
> Thanks!  
> -Zach
> 
> On Sunday, August 5, 2012 7:52:54 PM UTC-4, Zachary Tong wrote:  
> I believe the answer is "no", but wanted to check:
> 
> Is there a way to return the individual score of the nested documents that match a query? I'm performing a query on nested docs which returns appropriate parents, but I would like to know which nested child scored the highest for the query. The nested score\_mode simply tells me that "one of the children scored well", but not which one. Is there any way to sort or find the score of the nested children?
> 
> Barring that, I'm not certain of the best way to structure my data. Should I arrange the data in two types and denormalize so that one type has a "foreign key" (to use relational db terminology) that points at the other type, which would then require several more queries to access the individual documents?
> 
> Thanks!  
> -Zach

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [August 7, 2012, 10:20pm UTC](https://discuss.elastic.co/t/score-of-matched-nested-documents/8641/4 "2012-08-07T22:20:59Z")

</div>

Good to know, thanks for the detailed response Shay!

-Zach

On Tuesday, August 7, 2012 5:40:09 PM UTC-4, kimchy wrote:

> There is no option to get score of specific nested doc, though it has been  
> requested to be able to return "only" the nested doc that matched the  
> query, in which case we can possibly return the score with it. This is a  
> very "up in the air" feature currently, not slated (or even designed) yet.
> 
> Regarding the "get" API pert, its not a btree, it will probably be a bit  
> faster then a btree. Thats because there is a bloom filter associated with  
> each segment, so getting a doc will almost all times find the relevant  
> segment, and then within that segment (a segment is part of an index) its  
> more similar to an interval based skip list.
> 
> On Aug 6, 2012, at 3:04 PM, Zachary Tong \<[zachar...@gmail.com](mailto:zachar...@gmail.com)\<javascript:\>\>  
> wrote:
> 
> Looks like I found my answer here:
> 
> [https://groups.google.com/d/topic/elasticsearch/1\_1zXSeRsxQ/discussion](https://groups.google.com/d/topic/elasticsearch/1_1zXSeRsxQ/discussion)
> 
> Guess I'll start down the denormalization path. IF anyone has some  
> comments or opinions on the best way to structure relationships in ES, I'd  
> love to hear. Is the general sentiment to _not_ model relationships and  
> simply retrieve IDs which you use through another, non-ES mechanism to find  
> (say, query a database) ?
> 
> Related, what's the performance of simply looking up a specific document  
> by it's ID? Is it O(n)? Does ES use something like b-trees for document  
> ID lookups?
> 
> Thanks!  
> -Zach
> 
> On Sunday, August 5, 2012 7:52:54 PM UTC-4, Zachary Tong wrote:
> 
> > I believe the answer is "no", but wanted to check:
> > 
> > Is there a way to return the individual score of the nested documents  
> > that match a query? I'm performing a query on nested docs which returns  
> > appropriate parents, but I would like to know which nested child scored the  
> > highest for the query. The nested score\_mode simply tells me that "one of  
> > the children scored well", but not which one. Is there any way to sort or  
> > find the score of the nested children?
> > 
> > Barring that, I'm not certain of the best way to structure my data.  
> > Should I arrange the data in two types and denormalize so that one type  
> > has a "foreign key" (to use relational db terminology) that points at the  
> > other type, which would then require several more queries to access the  
> > individual documents?
> > 
> > Thanks!  
> > -Zach

---

<div class="post-metadata">

**Author:** ![btiernay](https://avatars.discourse-cdn.com/v4/letter/b/bb73d2/32.png) [@btiernay](https://discuss.elastic.co/u/btiernay)\
**Post date:** [January 18, 2013, 2:54pm UTC](https://discuss.elastic.co/t/score-of-matched-nested-documents/8641/5 "2013-01-18T14:54:55Z")

</div>

It's been a little time since this post was created I'm curious to know if  
there are any plans to add this feature (child filtering w/ score) any time  
in the future?

Thanks!

--

---

<div class="post-metadata">

**Author:** ![btiernay](https://avatars.discourse-cdn.com/v4/letter/b/bb73d2/32.png) [@btiernay](https://discuss.elastic.co/u/btiernay)\
**Post date:** [January 18, 2013, 3:01pm UTC](https://discuss.elastic.co/t/score-of-matched-nested-documents/8641/6 "2013-01-18T15:01:19Z")

</div>

I suppose this is the following ticket?

> <https://github.com/elastic/elasticsearch/issues/1383>
>
> Hiya
> 
> I have a use case for nested docs which, currently, can not be supported e…asily.
> 
> For instance, have a look at this doc:
> 
> \`\`\`
> {
> "location" : { "lat" : "8.31667", "lon" : "52.38333" },
> "parent\_ids" : \["2862926", "2921044" \],
> "contexts" : \[ {
> "context" : "/en",
> "rank" : 2,
> "tokens" : \[ 
> { "tokens" : \["steinbrink" \] },
> { "tokens" : \["steinbrink", "germany" \] }
> \],
> "label" : {
> "short" : "Steinbrink",
> "long" : "Steinbrink, Germany"
> }
> },
> {
> "context" : "/de",
> "rank" : 3,
> "tokens" : \[ 
> { "tokens" : \["steinbrink" \] },
> { "tokens" : \["steinbrink", "deutschland" \] }
> \],
> "label" : {
> "short" : "Steinbrink",
> "long" : "Steinbrink, Deutschland"
> }
> }
> \]
> }
> \`\`\`
> 
> \`contexts\` and \`tokens\` are both nested docs.
> 
> I would like to run query like:
> \- filter by \`contexts.context == '/de'\`
> \- sort by:
> - \`contexts.context.rank desc\`
> - \`contexts.label.short asc\`
> 
> but of course I have no way of pulling out the single matching context and using the values in that sub-doc.
> 
> I could use a script sort like the horribly inefficient:
> 
> \`\`\`
> {
> "sort" : \[
> {
> "params" : {
> "name" : "/de"
> },
> "\_script" : {
> "script" : "for (context: contexts) { if (context.context == name) { return context.rank }}",
> "order" : "desc",
> "type" : "number"
> }
> },
> {
> "\_script" : {
> "params" : {
> "name" : "/de"
> },
> "script" : "for (context: contexts) { if (context.context == name) { return context.label.short }}",
> "order" : "asc",
> "type" : "string"
> }
> }
> \]
> }
> \`\`\`
> 
> Or I may want to do:
> \- filter by \`context=='/de'\`
> \- find doc with tokens that best match \`stein\*\`
> \- return the \`tokens\` that match best as a field
> 
> Or have a slightly different doc structure:
> 
> \`\`\`
> "tokens" : \[ 
> { 
> "label": "Steinbrink",
> "tokens" : \["steinbrink" \] 
> },
> { 
> "label": "Steinbrink, Germany",
> "tokens" : \["steinbrink, germany" \] 
> },
> \],
> \`\`\`
> 
> and return the \`label\` associated with the \`tokens\` that best match. 
> 
> Even with a script, I couldn't find the best matching \`tokens\`.
> 
> In this example, I only have two contexts, and so could have changed the doc structure to something like:
> 
> \`\`\`
> contexts: {
> en: {....},
> de: {....}
> }
> \`\`\`
> 
> But I have other use cases where I have hundreds of contexts, which would result in thousands of fields.
> 
> Would it be possible to add functionality to:
> \- expose the best matching child (or children, with the best matching first in an array) in a \`\_scope\` so we could do something like:
>   
> "sort": \[
> { "\_child.myscope\[0\].rank": "desc" }, 
> { "\_child.myscope\[0\].label.short": "asc"}
> \],
> "fields": \["\_child.otherscope\[0\],tokens.label" \]
> \- return the scores for matching children in a \`\_scope\` eg:
>   
> { "children": 
> {
> "myscope": \[5,3,0,2,0\],
> "otherscope": \[0,1,0\]
> }
> }
> 
> Not sure how feasible this is, and if it is feasible, there is probably a much more elegant way of doing it.
> 
> thanks
> 
> clint

On Friday, 18 January 2013 09:54:55 UTC-5, btiernay wrote:

> It's been a little time since this post was created I'm curious to know if  
> there are any plans to add this feature (child filtering w/ score) any time  
> in the future?
> 
> Thanks!

--

---

<div class="post-metadata">

**Author:** ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discuss.elastic.co/u/Nova)\
**Post date:** [November 12, 2013, 10:09am UTC](https://discuss.elastic.co/t/score-of-matched-nested-documents/8641/7 "2013-11-12T10:09:30Z")

</div>

Example: we search an address "Dien Bien Phu", but we get ["Dien Bien Phu", "Hung Vuong", Thanh Khe"], Can we rank score for every nested document in this case?  
I think if so, "Dien Bien Phu" in nested document will have highest score.

How do you think about this ?

---

<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, 2:07am UTC](https://discuss.elastic.co/t/score-of-matched-nested-documents/8641/8 "2017-07-06T02:07:42Z")

</div>


