# Ordering Search Result Based On User Context

**URL:** <https://discuss.elastic.co/t/ordering-search-result-based-on-user-context/4841>\
**Category:** Elasticsearch\
**Created:** [July 12, 2011, 8:38pm UTC](https://discuss.elastic.co/t/ordering-search-result-based-on-user-context/4841 "2011-07-12T20:38:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![krisread](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/krisread/32/3172_2.png) [@krisread](https://discuss.elastic.co/u/krisread)\
**Post date:** [July 12, 2011, 8:38pm UTC](https://discuss.elastic.co/t/ordering-search-result-based-on-user-context/4841/1 "2011-07-12T20:38:26Z")

</div>

Hi,

I'm new to ES. We want to search a collection of documents, but then  
order and filter the result-set based on user-context data from  
outside the actual documents themselves.

For example, we want to search for "Rabbit" among all "Animal"  
documents. However, we want to order the results based not just on  
document relevancy to the search term "Rabbit" but also on other  
factors not even contained int he "Animal" documents, such as where  
the searcher is located, what the most popular searched-for Animals  
were that day, what browser the search user is using on the website,  
or what species of Animals that this user has searched for in the  
past.

We don't want to post-process the search results... can we handle all  
of this within ES? Can we have multiple indexes and/or multiple  
types, each of which collects different types of data (one index on  
Animals, another on User Locations, another on User Browsers, etc),  
and then query based on something like a join or intersection?

I realize some things could be passed in with the query as additional  
query parameters... but this doesn't cover when we need to compute  
weights based on complex combinations of aggregate data points.

Suggestions?

---

<div class="post-metadata">

**Author:** ![ppearcy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ppearcy/32/980_2.png) [@ppearcy](https://discuss.elastic.co/u/ppearcy)\
**Post date:** [July 12, 2011, 9:34pm UTC](https://discuss.elastic.co/t/ordering-search-result-based-on-user-context/4841/2 "2011-07-12T21:34:44Z")

</div>

Hey,  
Check out script based sorting:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

You can do a lot of cool stuff with this. It allows you to sort on the  
data contained in the document, as well as, inputs that are passed  
into the sort function.

I think this will meet your needs.

Best Regards,  
Paul

On Jul 12, 2:38 pm, krisread [kris.r...@gmail.com](mailto:kris.r...@gmail.com) wrote:

> Hi,
> 
> I'm new to ES. We want to search a collection of documents, but then  
> order and filter the result-set based on user-context data from  
> outside the actual documents themselves.
> 
> For example, we want to search for "Rabbit" among all "Animal"  
> documents. However, we want to order the results based not just on  
> document relevancy to the search term "Rabbit" but also on other  
> factors not even contained int he "Animal" documents, such as where  
> the searcher is located, what the most popular searched-for Animals  
> were that day, what browser the search user is using on the website,  
> or what species of Animals that this user has searched for in the  
> past.
> 
> We don't want to post-process the search results... can we handle all  
> of this within ES? Can we have multiple indexes and/or multiple  
> types, each of which collects different types of data (one index on  
> Animals, another on User Locations, another on User Browsers, etc),  
> and then query based on something like a join or intersection?
> 
> I realize some things could be passed in with the query as additional  
> query parameters... but this doesn't cover when we need to compute  
> weights based on complex combinations of aggregate data points.
> 
> Suggestions?

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [July 13, 2011, 11:01am UTC](https://discuss.elastic.co/t/ordering-search-result-based-on-user-context/4841/3 "2011-07-13T11:01:30Z")

</div>

you can also use boosts queries:

[http://www.elasticsearch.org/guide/reference/query-dsl/boosting-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/boosting-query.html)

[http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)

E.g. for jetsli.de I'm boosting documents which are in the language of  
the users' browser language:

BoolQueryBuilder bqb = QueryBuilders.boolQuery();  
// here your normal query  
bqb.must(qb);  
// if you omit the following, other languages will be completely  
excluded  
bqb.should(QueryBuilders.matchAllQuery());  
// now the boost  
bqb.should(QueryBuilders.termQuery("lang", userLanguage).boost(1.2f));

But keep in mind this will be slower then the pure "qb" query ...

when using scripting the fastest solution is when you use native  
scripts

Regards,  
Peter.

---

<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, 4:00am UTC](https://discuss.elastic.co/t/ordering-search-result-based-on-user-context/4841/4 "2017-07-06T04:00:49Z")

</div>


