# Sorting the data based on frequency of term existing in all documents

**URL:** <https://discuss.elastic.co/t/sorting-the-data-based-on-frequency-of-term-existing-in-all-documents/8878>\
**Category:** Elasticsearch\
**Created:** [August 29, 2012, 11:36am UTC](https://discuss.elastic.co/t/sorting-the-data-based-on-frequency-of-term-existing-in-all-documents/8878 "2012-08-29T11:36:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![narinder\_izap](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/narinder_izap/32/803_2.png) [@narinder\_izap](https://discuss.elastic.co/u/narinder_izap)\
**Post date:** [August 29, 2012, 11:36am UTC](https://discuss.elastic.co/t/sorting-the-data-based-on-frequency-of-term-existing-in-all-documents/8878/1 "2012-08-29T11:36:34Z")

</div>

Hi Shay,

```
        I am facing some problem in SORTING. I need to know whether my 

```

requirements in the sorting are implementable in ES or not???  
The scenario is :

I have an index as relations which is storing the the relationship between  
two entities

The structure of the document is :

{  
guid\_1:11,  
relationship:"fan\_of",  
guid\_2:"12"  
}

{  
guid\_1:11,  
relationship:"follower\_of",  
guid\_2:"12"  
}

{  
guid\_1:11,  
relationship:"fan\_of",  
guid\_2:"13"  
}

{  
guid\_1:22,  
relationship:"fan\_of",  
guid\_2:"12"  
}

{  
guid\_1:22,  
relationship:"fan\_of",  
guid\_2:"14"  
}

These document stores the relations. The first document represents that  
guid\_1 is a fan of guid\_2. similarly, the second doc presents the guid\_1 is  
follower of guid\_2.

So Now I need to find all the fan\_of relations doc of guid\_1 = 11 so that  
are documents with guid\_2 = 12,13 but sorting the document with guid\_2 = 12  
should be sorted first and then guid\_2= 13.

So I need to search the fan\_of relations of sorted on number of fan the  
guid\_2 is having. Is there any way to solve this query???

Please give your suggestion.

Narinder Kaur

--

---

<div class="post-metadata">

**Author:** ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://discuss.elastic.co/u/phill)\
**Post date:** [August 30, 2012, 12:16am UTC](https://discuss.elastic.co/t/sorting-the-data-based-on-frequency-of-term-existing-in-all-documents/8878/2 "2012-08-30T00:16:20Z")

</div>

This is really a type of n:n cross linking table which is great in a  
database to join and do counts etc., but in a document retrieval system  
it is not a perfect fit, since there is no ability to have two 'foreign  
keys' in an ES document, documents are most often structured (hence the  
use of Json) or made up many fields with various tokenizing and parsing  
characteristics, so I think you'll have to do any such merging in the  
client that calls ES.

This is another example where some de-normalizing is probably part of  
the solution.

Do you really want to know who is the most followed and who has the most  
fans?

If so, this is not a sort it is a count, but in SQL you would do sort  
and grouping and a count, but that's not very much in the style of  
document retrieval.

-Paul

--

---

<div class="post-metadata">

**Author:** ![narinder\_izap](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/narinder_izap/32/803_2.png) [@narinder\_izap](https://discuss.elastic.co/u/narinder_izap)\
**Post date:** [August 30, 2012, 5:12am UTC](https://discuss.elastic.co/t/sorting-the-data-based-on-frequency-of-term-existing-in-all-documents/8878/3 "2012-08-30T05:12:11Z")

</div>

I need to find the entities whom I have made fan and that result set should  
be sorted by number of fan in descending order.

On Thursday, August 30, 2012 5:46:04 AM UTC+5:30, P Hill wrote:

> This is really a type of n:n cross linking table which is great in a  
> database to join and do counts etc., but in a document retrieval system  
> it is not a perfect fit, since there is no ability to have two 'foreign  
> keys' in an ES document, documents are most often structured (hence the  
> use of Json) or made up many fields with various tokenizing and parsing  
> characteristics, so I think you'll have to do any such merging in the  
> client that calls ES.
> 
> This is another example where some de-normalizing is probably part of  
> the solution.
> 
> Do you really want to know who is the most followed and who has the most  
> fans?
> 
> If so, this is not a sort it is a count, but in SQL you would do sort  
> and grouping and a count, but that's not very much in the style of  
> document retrieval.
> 
> -Paul

--

---

<div class="post-metadata">

**Author:** ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://discuss.elastic.co/u/phill)\
**Post date:** [September 5, 2012, 3:54am UTC](https://discuss.elastic.co/t/sorting-the-data-based-on-frequency-of-term-existing-in-all-documents/8878/4 "2012-09-05T03:54:23Z")

</div>

Another case were a working example, or at least an example of results  
you expect would be useful.  
For example, you should use your original "relation" documents and  
invent some "people" to go with them.  
I assume the guid\_1 etc refers to some other type of document (aka  
another table) like "people".  
Then describe all the results you need, regardless of how you get them.

But I'd look into denormalizing across a bunch of children documents  
with different children documents for  
fan and its opposite fan\_of (there is that demormalizing!), and others  
for follower and its opposite follower\_of. Then I could imagine one  
query to get some person docs with the most fan children. Then in  
another query which uses the parent IDs found in query 1 and the  
opposite relationship, fan\_of, find all the people documents who are the  
fans.

But I'd have to test if the queries were fast enough for the job.

-Paul

On 8/29/2012 10:12 PM, Narinder Kaur wrote:

> I need to find the entities whom I have made fan and that result set  
> should be sorted by number of fan in descending order.

--

---

<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:14am UTC](https://discuss.elastic.co/t/sorting-the-data-based-on-frequency-of-term-existing-in-all-documents/8878/5 "2017-07-06T03:14:07Z")

</div>


