# Sort the basic of Number of documents

**URL:** <https://discuss.elastic.co/t/sort-the-basic-of-number-of-documents/8717>\
**Category:** Elasticsearch\
**Created:** [August 11, 2012, 12:39pm UTC](https://discuss.elastic.co/t/sort-the-basic-of-number-of-documents/8717 "2012-08-11T12:39:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carl\_Sharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carl_sharma/32/2767_2.png) [@Carl\_Sharma](https://discuss.elastic.co/u/Carl_Sharma)\
**Post date:** [August 11, 2012, 12:39pm UTC](https://discuss.elastic.co/t/sort-the-basic-of-number-of-documents/8717/1 "2012-08-11T12:39:23Z")

</div>

I am saving user relations in ES Index

i.e

{'id' =\> 1, 'User\_id\_1' =\> '2001', 'relation' =\> 'friend', 'User\_id\_2' =\>  
'1002'}  
{'id' =\> 2, 'User\_id\_1' =\> '2002', 'relation' =\> 'friend', 'User\_id\_2' =\>  
'1002'}

{'id' =\> 3, 'User\_id\_1' =\> '2002', 'relation' =\> 'friend', 'User\_id\_2' =\>  
'1001'}  
{'id' =\> 4, 'User\_id\_1' =\> '2003', 'relation' =\> 'friend', 'User\_id\_2' =\>  
'1003'}

no suppose i want to get the user\_id\_2 who has most friends,

in above case its 1002 as 2001, and 2002 are its friends. (Count = 2)

I just can't figure out the query

Thanks.

--

---

<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:** [August 11, 2012, 12:58pm UTC](https://discuss.elastic.co/t/sort-the-basic-of-number-of-documents/8717/2 "2012-08-11T12:58:55Z")

</div>

What about TermFacet on field user\_id\_2?

--

Le 11 août 2012 à 14:39, Chetan Sharma [carl@izap.in](mailto:carl@izap.in) a écrit :

> I am saving user relations in ES Index
> 
> i.e
> 
> {'id' =\> 1, 'User\_id\_1' =\> '2001', 'relation' =\> 'friend', 'User\_id\_2' =\> '1002'}  
> {'id' =\> 2, 'User\_id\_1' =\> '2002', 'relation' =\> 'friend', 'User\_id\_2' =\> '1002'}
> 
> {'id' =\> 3, 'User\_id\_1' =\> '2002', 'relation' =\> 'friend', 'User\_id\_2' =\> '1001'}  
> {'id' =\> 4, 'User\_id\_1' =\> '2003', 'relation' =\> 'friend', 'User\_id\_2' =\> '1003'}
> 
> no suppose i want to get the user\_id\_2 who has most friends,
> 
> in above case its 1002 as 2001, and 2002 are its friends. (Count = 2)
> 
> I just can't figure out the query
> 
> Thanks.
> 
> --

--

---

<div class="post-metadata">

**Author:** ![Chetan\_Sharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chetan_sharma/32/2767_2.png) [@Chetan\_Sharma](https://discuss.elastic.co/u/Chetan_Sharma)\
**Post date:** [August 11, 2012, 3:25pm UTC](https://discuss.elastic.co/t/sort-the-basic-of-number-of-documents/8717/3 "2012-08-11T15:25:30Z")

</div>

I am trying that, thanks 4 idea will post back the results soon.  
-----Original Message-----  
From: David Pilato [david@pilato.fr](mailto:david@pilato.fr)  
Sender: [elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)  
Date: Sat, 11 Aug 2012 14:58:55  
To: [elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)[elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)  
Reply-To: [elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)  
Subject: Re: Sort the basic of Number of documents

What about TermFacet on field user\_id\_2?

--

Le 11 août 2012 à 14:39, Chetan Sharma [carl@izap.in](mailto:carl@izap.in) a écrit :

> I am saving user relations in ES Index
> 
> i.e
> 
> {'id' =\> 1, 'User\_id\_1' =\> '2001', 'relation' =\> 'friend', 'User\_id\_2' =\> '1002'}  
> {'id' =\> 2, 'User\_id\_1' =\> '2002', 'relation' =\> 'friend', 'User\_id\_2' =\> '1002'}
> 
> {'id' =\> 3, 'User\_id\_1' =\> '2002', 'relation' =\> 'friend', 'User\_id\_2' =\> '1001'}  
> {'id' =\> 4, 'User\_id\_1' =\> '2003', 'relation' =\> 'friend', 'User\_id\_2' =\> '1003'}
> 
> no suppose i want to get the user\_id\_2 who has most friends,
> 
> in above case its 1002 as 2001, and 2002 are its friends. (Count = 2)
> 
> I just can't figure out the query
> 
> Thanks.
> 
> --

--

--

---

<div class="post-metadata">

**Author:** ![Carl\_Sharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carl_sharma/32/2767_2.png) [@Carl\_Sharma](https://discuss.elastic.co/u/Carl_Sharma)\
**Post date:** [August 13, 2012, 7:37am UTC](https://discuss.elastic.co/t/sort-the-basic-of-number-of-documents/8717/4 "2012-08-13T07:37:33Z")

</div>

Well "term facets" is very good choice and it almost solved my problem, but

The problem is I have is 2 Indexes

1st index is for saving the main docs and 2nd index for saving the relations

now problem is

Suppose I have 100 USER Docs in my main index, only 50 of them has made  
relations, so I'll have only 50 USER Docs in my relationship index

So when i implement the "term facet", it sorts the results and gives the  
correct output i want, but I am missing those left 50 users who don't have  
any relations yet, i need them in my final output after the 50 sorted users.

On Saturday, August 11, 2012 6:28:55 PM UTC+5:30, David Pilato wrote:

> What about TermFacet on field user\_id\_2?
> 
> --
> 
> Le 11 août 2012 à 14:39, Chetan Sharma \<ca...@izap.in \<javascript:\>\> a  
> écrit :
> 
> I am saving user relations in ES Index
> 
> i.e
> 
> {'id' =\> 1, 'User\_id\_1' =\> '2001', 'relation' =\> 'friend', 'User\_id\_2' =\>  
> '1002'}  
> {'id' =\> 2, 'User\_id\_1' =\> '2002', 'relation' =\> 'friend', 'User\_id\_2' =\>  
> '1002'}
> 
> {'id' =\> 3, 'User\_id\_1' =\> '2002', 'relation' =\> 'friend', 'User\_id\_2' =\>  
> '1001'}  
> {'id' =\> 4, 'User\_id\_1' =\> '2003', 'relation' =\> 'friend', 'User\_id\_2' =\>  
> '1003'}
> 
> no suppose i want to get the user\_id\_2 who has most friends,
> 
> in above case its 1002 as 2001, and 2002 are its friends. (Count = 2)
> 
> I just can't figure out the query
> 
> Thanks.
> 
> --

--

---

<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:** [August 13, 2012, 7:46am UTC](https://discuss.elastic.co/t/sort-the-basic-of-number-of-documents/8717/5 "2012-08-13T07:46:39Z")

</div>

What I would do for your use case is to think my documents in another way.

I mean that you want to store users. Users have relations. So store relations as a field of your user document.

Then it will be easier to query for users or relations.

If you need to filter by number of relations, why not adding a numberofrelation property in your user doc?

David

--

Le 13 août 2012 à 09:37, Chetan Sharma [carl@izap.in](mailto:carl@izap.in) a écrit :

> Well "term facets" is very good choice and it almost solved my problem, but
> 
> The problem is I have is 2 Indexes
> 
> 1st index is for saving the main docs and 2nd index for saving the relations
> 
> now problem is
> 
> Suppose I have 100 USER Docs in my main index, only 50 of them has made relations, so I'll have only 50 USER Docs in my relationship index
> 
> So when i implement the "term facet", it sorts the results and gives the correct output i want, but I am missing those left 50 users who don't have any relations yet, i need them in my final output after the 50 sorted users.
> 
> On Saturday, August 11, 2012 6:28:55 PM UTC+5:30, David Pilato wrote:  
> What about TermFacet on field user\_id\_2?
> 
> --
> 
> Le 11 août 2012 à 14:39, Chetan Sharma [ca...@izap.in](mailto:ca...@izap.in) a écrit :
> 
> > I am saving user relations in ES Index
> > 
> > i.e
> > 
> > {'id' =\> 1, 'User\_id\_1' =\> '2001', 'relation' =\> 'friend', 'User\_id\_2' =\> '1002'}  
> > {'id' =\> 2, 'User\_id\_1' =\> '2002', 'relation' =\> 'friend', 'User\_id\_2' =\> '1002'}
> > 
> > {'id' =\> 3, 'User\_id\_1' =\> '2002', 'relation' =\> 'friend', 'User\_id\_2' =\> '1001'}  
> > {'id' =\> 4, 'User\_id\_1' =\> '2003', 'relation' =\> 'friend', 'User\_id\_2' =\> '1003'}
> > 
> > no suppose i want to get the user\_id\_2 who has most friends,
> > 
> > in above case its 1002 as 2001, and 2002 are its friends. (Count = 2)
> > 
> > I just can't figure out the query
> > 
> > Thanks.
> > 
> > --
> 
> --

--

---

<div class="post-metadata">

**Author:** ![Chetan\_Sharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chetan_sharma/32/2767_2.png) [@Chetan\_Sharma](https://discuss.elastic.co/u/Chetan_Sharma)\
**Post date:** [August 13, 2012, 7:53am UTC](https://discuss.elastic.co/t/sort-the-basic-of-number-of-documents/8717/6 "2012-08-13T07:53:48Z")

</div>

If we add relations to the same DOC, the Document will increase to very  
large size.

Suppose I save

friend\_of : [array of ids],  
fan\_of : [array of ids]

if user have 10,000 fans and 1000 friends, then the size of document will  
be very large

So it modular to keep it in different index.

On Mon, Aug 13, 2012 at 1:16 PM, David Pilato [david@pilato.fr](mailto:david@pilato.fr) wrote:

> What I would do for your use case is to think my documents in another way.
> 
> I mean that you want to store users. Users have relations. So store  
> relations as a field of your user document.
> 
> Then it will be easier to query for users or relations.
> 
> If you need to filter by number of relations, why not adding a  
> numberofrelation property in your user doc?
> 
> David
> 
> --
> 
> Le 13 août 2012 à 09:37, Chetan Sharma [carl@izap.in](mailto:carl@izap.in) a écrit :
> 
> Well "term facets" is very good choice and it almost solved my problem, but
> 
> The problem is I have is 2 Indexes
> 
> 1st index is for saving the main docs and 2nd index for saving the  
> relations
> 
> now problem is
> 
> Suppose I have 100 USER Docs in my main index, only 50 of them has made  
> relations, so I'll have only 50 USER Docs in my relationship index
> 
> So when i implement the "term facet", it sorts the results and gives the  
> correct output i want, but I am missing those left 50 users who don't have  
> any relations yet, i need them in my final output after the 50 sorted users.
> 
> On Saturday, August 11, 2012 6:28:55 PM UTC+5:30, David Pilato wrote:
> 
> > What about TermFacet on field user\_id\_2?
> > 
> > --
> > 
> > Le 11 août 2012 à 14:39, Chetan Sharma [ca...@izap.in](mailto:ca...@izap.in) a écrit :
> > 
> > I am saving user relations in ES Index
> > 
> > i.e
> > 
> > {'id' =\> 1, 'User\_id\_1' =\> '2001', 'relation' =\> 'friend', 'User\_id\_2' =\>  
> > '1002'}  
> > {'id' =\> 2, 'User\_id\_1' =\> '2002', 'relation' =\> 'friend', 'User\_id\_2' =\>  
> > '1002'}
> > 
> > {'id' =\> 3, 'User\_id\_1' =\> '2002', 'relation' =\> 'friend', 'User\_id\_2' =\>  
> > '1001'}  
> > {'id' =\> 4, 'User\_id\_1' =\> '2003', 'relation' =\> 'friend', 'User\_id\_2' =\>  
> > '1003'}
> > 
> > no suppose i want to get the user\_id\_2 who has most friends,
> > 
> > in above case its 1002 as 2001, and 2002 are its friends. (Count = 2)
> > 
> > I just can't figure out the query
> > 
> > Thanks.
> > 
> > --
> > 
> > --
> 
> --

--

Chetan Sharma  
iZAP

--

---

<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:** [August 13, 2012, 7:56am UTC](https://discuss.elastic.co/t/sort-the-basic-of-number-of-documents/8717/7 "2012-08-13T07:56:50Z")

</div>

I see. But my last comment can do the job. "adding a numberofrelation property in your user doc?"

--

Le 13 août 2012 à 09:53, Chetan Sharma [chetan@izap.in](mailto:chetan@izap.in) a écrit :

> If we add relations to the same DOC, the Document will increase to very large size.
> 
> Suppose I save
> 
> friend\_of : [array of ids],  
> fan\_of : [array of ids]
> 
> if user have 10,000 fans and 1000 friends, then the size of document will be very large
> 
> So it modular to keep it in different index.
> 
> On Mon, Aug 13, 2012 at 1:16 PM, David Pilato [david@pilato.fr](mailto:david@pilato.fr) wrote:  
> What I would do for your use case is to think my documents in another way.
> 
> I mean that you want to store users. Users have relations. So store relations as a field of your user document.
> 
> Then it will be easier to query for users or relations.
> 
> If you need to filter by number of relations, why not adding a numberofrelation property in your user doc?
> 
> David
> 
> --
> 
> Le 13 août 2012 à 09:37, Chetan Sharma [carl@izap.in](mailto:carl@izap.in) a écrit :
> 
> > Well "term facets" is very good choice and it almost solved my problem, but
> > 
> > The problem is I have is 2 Indexes
> > 
> > 1st index is for saving the main docs and 2nd index for saving the relations
> > 
> > now problem is
> > 
> > Suppose I have 100 USER Docs in my main index, only 50 of them has made relations, so I'll have only 50 USER Docs in my relationship index
> > 
> > So when i implement the "term facet", it sorts the results and gives the correct output i want, but I am missing those left 50 users who don't have any relations yet, i need them in my final output after the 50 sorted users.
> > 
> > On Saturday, August 11, 2012 6:28:55 PM UTC+5:30, David Pilato wrote:  
> > What about TermFacet on field user\_id\_2?
> > 
> > --
> > 
> > Le 11 août 2012 à 14:39, Chetan Sharma [ca...@izap.in](mailto:ca...@izap.in) a écrit :
> > 
> > > I am saving user relations in ES Index
> > > 
> > > i.e
> > > 
> > > {'id' =\> 1, 'User\_id\_1' =\> '2001', 'relation' =\> 'friend', 'User\_id\_2' =\> '1002'}  
> > > {'id' =\> 2, 'User\_id\_1' =\> '2002', 'relation' =\> 'friend', 'User\_id\_2' =\> '1002'}
> > > 
> > > {'id' =\> 3, 'User\_id\_1' =\> '2002', 'relation' =\> 'friend', 'User\_id\_2' =\> '1001'}  
> > > {'id' =\> 4, 'User\_id\_1' =\> '2003', 'relation' =\> 'friend', 'User\_id\_2' =\> '1003'}
> > > 
> > > no suppose i want to get the user\_id\_2 who has most friends,
> > > 
> > > in above case its 1002 as 2001, and 2002 are its friends. (Count = 2)
> > > 
> > > I just can't figure out the query
> > > 
> > > Thanks.
> > > 
> > > --
> > 
> > --
> 
> --
> 
> --
> 
> Chetan Sharma  
> iZAP
> 
> --

--

---

<div class="post-metadata">

**Author:** ![Chetan\_Sharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chetan_sharma/32/2767_2.png) [@Chetan\_Sharma](https://discuss.elastic.co/u/Chetan_Sharma)\
**Post date:** [August 13, 2012, 8:03am UTC](https://discuss.elastic.co/t/sort-the-basic-of-number-of-documents/8717/8 "2012-08-13T08:03:14Z")

</div>

Yes you are right, but according to our current conventions (Project  
management), we are not allowed to keep the "COUNTERS".

Isn't there any solution around this in Elasticsearch Queries ?

On Mon, Aug 13, 2012 at 1:26 PM, David Pilato [david@pilato.fr](mailto:david@pilato.fr) wrote:

> I see. But my last comment can do the job. "adding a numberofrelation  
> property in your user doc?"
> 
> --
> 
> Le 13 août 2012 à 09:53, Chetan Sharma [chetan@izap.in](mailto:chetan@izap.in) a écrit :
> 
> If we add relations to the same DOC, the Document will increase to very  
> large size.
> 
> Suppose I save
> 
> friend\_of : [array of ids],  
> fan\_of : [array of ids]
> 
> if user have 10,000 fans and 1000 friends, then the size of document will  
> be very large
> 
> So it modular to keep it in different index.
> 
> On Mon, Aug 13, 2012 at 1:16 PM, David Pilato [david@pilato.fr](mailto:david@pilato.fr) wrote:
> 
> > What I would do for your use case is to think my documents in another way.
> > 
> > I mean that you want to store users. Users have relations. So store  
> > relations as a field of your user document.
> > 
> > Then it will be easier to query for users or relations.
> > 
> > If you need to filter by number of relations, why not adding a  
> > numberofrelation property in your user doc?
> > 
> > David
> > 
> > --
> > 
> > Le 13 août 2012 à 09:37, Chetan Sharma [carl@izap.in](mailto:carl@izap.in) a écrit :
> > 
> > Well "term facets" is very good choice and it almost solved my problem,  
> > but
> > 
> > The problem is I have is 2 Indexes
> > 
> > 1st index is for saving the main docs and 2nd index for saving the  
> > relations
> > 
> > now problem is
> > 
> > Suppose I have 100 USER Docs in my main index, only 50 of them has made  
> > relations, so I'll have only 50 USER Docs in my relationship index
> > 
> > So when i implement the "term facet", it sorts the results and gives the  
> > correct output i want, but I am missing those left 50 users who don't have  
> > any relations yet, i need them in my final output after the 50 sorted users.
> > 
> > On Saturday, August 11, 2012 6:28:55 PM UTC+5:30, David Pilato wrote:
> > 
> > > What about TermFacet on field user\_id\_2?
> > > 
> > > --
> > > 
> > > Le 11 août 2012 à 14:39, Chetan Sharma [ca...@izap.in](mailto:ca...@izap.in) a écrit :
> > > 
> > > I am saving user relations in ES Index
> > > 
> > > i.e
> > > 
> > > {'id' =\> 1, 'User\_id\_1' =\> '2001', 'relation' =\> 'friend', 'User\_id\_2'  
> > > =\> '1002'}  
> > > {'id' =\> 2, 'User\_id\_1' =\> '2002', 'relation' =\> 'friend', 'User\_id\_2'  
> > > =\> '1002'}
> > > 
> > > {'id' =\> 3, 'User\_id\_1' =\> '2002', 'relation' =\> 'friend', 'User\_id\_2'  
> > > =\> '1001'}  
> > > {'id' =\> 4, 'User\_id\_1' =\> '2003', 'relation' =\> 'friend', 'User\_id\_2'  
> > > =\> '1003'}
> > > 
> > > no suppose i want to get the user\_id\_2 who has most friends,
> > > 
> > > in above case its 1002 as 2001, and 2002 are its friends. (Count = 2)
> > > 
> > > I just can't figure out the query
> > > 
> > > Thanks.
> > > 
> > > --
> > > 
> > > --
> > 
> > --
> 
> --
> 
> Chetan Sharma  
> iZAP
> 
> --
> 
> --

--

Chetan Sharma  
iZAP

--

---

<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:16am UTC](https://discuss.elastic.co/t/sort-the-basic-of-number-of-documents/8717/9 "2017-07-06T03:16:38Z")

</div>


