# Sort Results by category and occurrences in other indexes

**URL:** <https://discuss.elastic.co/t/sort-results-by-category-and-occurrences-in-other-indexes/23376>\
**Category:** Elasticsearch\
**Created:** [April 23, 2015, 1:40pm UTC](https://discuss.elastic.co/t/sort-results-by-category-and-occurrences-in-other-indexes/23376 "2015-04-23T13:40:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Radke](https://avatars.discourse-cdn.com/v4/letter/j/b3f665/32.png) [@James\_Radke](https://discuss.elastic.co/u/James_Radke)\
**Post date:** [April 23, 2015, 1:40pm UTC](https://discuss.elastic.co/t/sort-results-by-category-and-occurrences-in-other-indexes/23376/1 "2015-04-23T13:40:30Z")

</div>

Good morning,

I am new to ElasticSearch, and have the basics of querying, sorting, etc.  
working with some test data.

I have a concept that I would like to solve, but am not really sure how to  
do it, so I thought I would post here and see if you all can help me figure  
this out.

I have the following indexes (note, not all fields in the indexes are  
identified:

Categories Index (we have approximately 4 main category groupings, and 94  
different sub-category groupings)

- Id (int, identifies the category)
- Heading (character varying, heading for the category)
- ParentId (int, set to zero if a main category item, otherwise set to  
which item it is a subcategory item of)

Products Index (approximately 60,000 in test elasticsearch, with a 3 node  
cluster)

- Sku (character varying, identifies the product)
- Level1CatId (int, identifies which main category the product belongs to)
- Level1CatHeading (character varying, main category heading)
- Level2CatId (int, identfies which sub-category the product belongs to)
- Level2CatHeading (character varying, sub-category heading)

LogEvents Index

- DateTime (date, date and time the activity occurred)
- Type (character varying, can be either 'OfferClickThrough',  
'SearchResultClickThrough', 'AdClickThrough' amongst others like 'Login',  
'Logout', 'Errors')
- Sku (product identifier)
- SubCategoryId (sub categoryId the part belongs to)

What I would like to do is create results that look like:

Results {  
Category {  
Id,  
Heading,  
Products  
Product {  
Sku  
Description,  
Price,  
etc.  
}  
Product {  
Sku,  
Destination,  
Price.  
etc.  
}  
....  
}  
}  
Category {  
...  
}  
.....  
}

where, in English, it works like:

1.) Results are first split into categories.

2.) Within each category, products should first be sorted by sku based on  
the the most recent LogEvents Sku with types of 'OfferClickThrough',  
'SearchResultsClickThrough', and 'AdClickThrough'). After that, products  
should be randomized so the same items are not always presented the same  
every time the query has been run.

3.) Category groupings in the Results should first be sorted by the most  
recent LogEvents SubCategoryId with the types of 'OfferClickThrough',  
'SearchResultsClickThrough', and 'AdClickThrough' . After that, the  
remaining categories should be randomized so they do not always come back  
in the same order every time the query has been run.

Is this doable with ElasticSearch?

Thanks,

Jim

--  
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/16e004b2-be86-4a27-b43e-f00ad8d056be%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/16e004b2-be86-4a27-b43e-f00ad8d056be%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:18am UTC](https://discuss.elastic.co/t/sort-results-by-category-and-occurrences-in-other-indexes/23376/2 "2017-07-06T00:18:03Z")

</div>


