# Prioritize document

**URL:** https://discuss.elastic.co/t/prioritize-document/23414
**Category:** Elasticsearch
**Created:** [April 27, 2015, 11:23am UTC](https://discuss.elastic.co/t/prioritize-document/23414 "2015-04-27T11:23:32Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Zsolt](https://avatars.discourse-cdn.com/v4/letter/z/58956e/32.png) [@Zsolt](https://discuss.elastic.co/u/Zsolt)
#### Post date: [April 27, 2015, 11:23am UTC](https://discuss.elastic.co/t/prioritize-document/23414/1 "2015-04-27T11:23:32Z")

</div>

Hi,

I have the following 3 documents:

{  
"product" : "D1",  
"owner" : "global"  
},  
{  
"product" : "D1",  
"owner" : "john"  
},  
{  
"product" : "D2",  
"owner" : "global"  
}

There is always a "product" with "owner": "global" and only sometimes the  
same product with different owner (basically a product is customized for a  
user). I would like to return all products but only the customized version  
for a product where it exists (where "owner" = "a-user-name"). So for the  
my example I would like returned the following two docs:

{  
"product" : "D1",  
"owner" : "john"  
},  
{  
"product" : "D2",  
"owner" : "global"  
}

I would appreciate some help on how I could solve this.

Thanks

--  
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/1298ddd6-3cad-4219-89cd-67c5f2642c78%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1298ddd6-3cad-4219-89cd-67c5f2642c78%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Zsolt](https://avatars.discourse-cdn.com/v4/letter/z/58956e/32.png) [@Zsolt](https://discuss.elastic.co/u/Zsolt)
#### Post date: [April 29, 2015, 7:54am UTC](https://discuss.elastic.co/t/prioritize-document/23414/2 "2015-04-29T07:54:48Z")

</div>

Nobody?

--  
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/4749653c-5af2-4505-a502-39934d418566%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4749653c-5af2-4505-a502-39934d418566%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [April 29, 2015, 8:24am UTC](https://discuss.elastic.co/t/prioritize-document/23414/3 "2015-04-29T08:24:03Z")

</div>

> I would like to return all products but only the customized version for a  
> product where it exists (where "owner" = "a-user-name").

Search for:  
user-name:(global OR john)  
Combine this with a terms aggregation to break down by "product" field and  
below that nest a "top\_hits" aggregation with a size of 1.

The search should naturally rank a match on a user name higher than a match  
on "global" (assuming global is very common and per-user customizations are  
rare).  
Otherwise look into boosts.  
The top\_hits agg should then select the best-scoring option for each  
product.

Obviously if you have millions of products this could present a memory  
issue.

Cheers  
Mark

--  
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/dead03ff-3f0f-4217-b8de-2cde93ab4fdb%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/dead03ff-3f0f-4217-b8de-2cde93ab4fdb%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![deepak\_daffodil](https://avatars.discourse-cdn.com/v4/letter/d/ebca7d/32.png) [@deepak\_daffodil](https://discuss.elastic.co/u/deepak_daffodil)
#### Post date: [April 29, 2015, 9:27am UTC](https://discuss.elastic.co/t/prioritize-document/23414/4 "2015-04-29T09:27:28Z")

</div>

i think , query string query may be useful with applying operator

On Wed, Apr 29, 2015 at 1:54 PM, [mark@elastic.co](mailto:mark@elastic.co) wrote:

> > I would like to return all products but only the customized version for a  
> > product where it exists (where "owner" = "a-user-name").
> 
> Search for:  
> user-name:(global OR john)  
> Combine this with a terms aggregation to break down by "product" field and  
> below that nest a "top\_hits" aggregation with a size of 1.
> 
> The search should naturally rank a match on a user name higher than a  
> match on "global" (assuming global is very common and per-user  
> customizations are rare).  
> Otherwise look into boosts.  
> The top\_hits agg should then select the best-scoring option for each  
> product.
> 
> Obviously if you have millions of products this could present a memory  
> issue.
> 
> Cheers  
> Mark
> 
> --  
> 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/dead03ff-3f0f-4217-b8de-2cde93ab4fdb%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/dead03ff-3f0f-4217-b8de-2cde93ab4fdb%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/dead03ff-3f0f-4217-b8de-2cde93ab4fdb%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/dead03ff-3f0f-4217-b8de-2cde93ab4fdb%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAHzCMpoHyHj9EdARDDzBFGy\_aa%3DwYSmXmut1GevqgvBA4y9uQA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHzCMpoHyHj9EdARDDzBFGy_aa%3DwYSmXmut1GevqgvBA4y9uQA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Zsolt](https://avatars.discourse-cdn.com/v4/letter/z/58956e/32.png) [@Zsolt](https://discuss.elastic.co/u/Zsolt)
#### Post date: [April 29, 2015, 11:34am UTC](https://discuss.elastic.co/t/prioritize-document/23414/5 "2015-04-29T11:34:13Z")

</div>

Thanks Mark, this returns the desired results.

Unfortunately it seems to have introduced a new issue. I don't have  
millions of products but enough so I use paging and as I can see, it is not  
possible to page/sort the returned buckets (issue 4915  
[https://github.com/elastic/elasticsearch/issues/4915](https://github.com/elastic/elasticsearch/issues/4915))...

\Zsolt

On Wednesday, 29 April 2015 10:24:04 UTC+2, [ma...@elastic.co](mailto:ma...@elastic.co) wrote:

> > I would like to return all products but only the customized version for a  
> > product where it exists (where "owner" = "a-user-name").
> 
> Search for:  
> user-name:(global OR john)  
> Combine this with a terms aggregation to break down by "product" field and  
> below that nest a "top\_hits" aggregation with a size of 1.
> 
> The search should naturally rank a match on a user name higher than a  
> match on "global" (assuming global is very common and per-user  
> customizations are rare).  
> Otherwise look into boosts.  
> The top\_hits agg should then select the best-scoring option for each  
> product.
> 
> Obviously if you have millions of products this could present a memory  
> issue.
> 
> Cheers  
> Mark

--  
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/fb71b53b-11d0-466a-ac4f-9bf2ec1c7194%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/fb71b53b-11d0-466a-ac4f-9bf2ec1c7194%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [April 29, 2015, 11:49am UTC](https://discuss.elastic.co/t/prioritize-document/23414/6 "2015-04-29T11:49:57Z")

</div>

You could break the products into an arbitrary number of batches as follows  
(in this case page 5 of 100):

{  
"aggs":{  
"productsPage":{  
"terms":{

"script":"p=doc['product'].value;if(p.hashCode()%100==5)return p;return  
null;"  
}  
}  
}  
}

On Wednesday, April 29, 2015 at 12:34:13 PM UTC+1, Zsolt wrote:

> Thanks Mark, this returns the desired results.
> 
> Unfortunately it seems to have introduced a new issue. I don't have  
> millions of products but enough so I use paging and as I can see, it is not  
> possible to page/sort the returned buckets (issue 4915  
> [https://github.com/elastic/elasticsearch/issues/4915](https://github.com/elastic/elasticsearch/issues/4915))...
> 
> \Zsolt
> 
> On Wednesday, 29 April 2015 10:24:04 UTC+2, [ma...@elastic.co](mailto:ma...@elastic.co) wrote:
> 
> > > I would like to return all products but only the customized version for  
> > > a product where it exists (where "owner" = "a-user-name").
> > 
> > Search for:  
> > user-name:(global OR john)  
> > Combine this with a terms aggregation to break down by "product" field  
> > and below that nest a "top\_hits" aggregation with a size of 1.
> > 
> > The search should naturally rank a match on a user name higher than a  
> > match on "global" (assuming global is very common and per-user  
> > customizations are rare).  
> > Otherwise look into boosts.  
> > The top\_hits agg should then select the best-scoring option for each  
> > product.
> > 
> > Obviously if you have millions of products this could present a memory  
> > issue.
> > 
> > Cheers  
> > Mark

--  
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/a995a89d-fab0-49f7-a378-5f13a3554a10%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a995a89d-fab0-49f7-a378-5f13a3554a10%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Zsolt](https://avatars.discourse-cdn.com/v4/letter/z/58956e/32.png) [@Zsolt](https://discuss.elastic.co/u/Zsolt)
#### Post date: [April 29, 2015, 1:58pm UTC](https://discuss.elastic.co/t/prioritize-document/23414/7 "2015-04-29T13:58:52Z")

</div>

Is there a way of getting the total results without actually returning and  
counting the elements of the bucket array?

On Wednesday, 29 April 2015 13:49:57 UTC+2, [ma...@elastic.co](mailto:ma...@elastic.co) wrote:

> You could break the products into an arbitrary number of batches as  
> follows (in this case page 5 of 100):
> 
> {  
> "aggs":{  
> "productsPage":{  
> "terms":{
> 
> "script":"p=doc['product'].value;if(p.hashCode()%100==5)return p;return  
> null;"  
> }  
> }  
> }  
> }

--  
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/e842a236-1b49-4db7-ac17-72dce7517771%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e842a236-1b49-4db7-ac17-72dce7517771%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:17am UTC](https://discuss.elastic.co/t/prioritize-document/23414/8 "2017-07-06T00:17:01Z")

</div>


