1

I have a problem with figuring out how to formulate a solr query to dynamically elevate certain documents and applying a different sorting mechanism to the rest of the documents. To make it more clear consider following documents.

<doc>
  <field name="docId">1</field>
  <field name="sales">33</field>
  <field name="price">10</field>
</doc>
<doc>
  <field name="docId">2</field>
  <field name="sales">44</field>
  <field name="price">20</field>
</doc>
<doc>
  <field name="docId">3</field>
  <field name="sales">55</field>
  <field name="price">30</field>
</doc>
<doc>
  <field name="docId">4</field>
  <field name="sales">22</field>
  <field name="price">50</field>
</doc>
<doc>
  <field name="docId">5</field>
  <field name="sales">11</field>
  <field name="price">5</field>
</doc>
<doc>
  <field name="docId">6</field>
  <field name="sales">1</field>
  <field name="price">4</field>
</doc>

My goal is to select one product with the best sales and display it as the first product. Then select a product with the lowest price and display it as second and finally sort the rest of the documents by price ascending, so my final result would look like that:

<doc>
  <field name="docId">3</field><!-- Product with the highest amount of sales -->
</doc>
<doc>
  <field name="docId">6</field><!-- Product with the lowest price -->
</doc>
<!-- The rest sorted by price asc -->
<doc>
  <field name="docId">5</field>
</doc>
<doc>
  <field name="docId">4</field>
</doc>
<doc>
  <field name="docId">1</field>
</doc>
<doc>
  <field name="docId">2</field>
</doc>

At the beggining I was considering using QueryElevation component, but it only supports static data and I need to be able to for example change the price range and apply the same sorting logic.

I started looking at the solr function query and particularly "max(sales,0) desc" would solve my problem, but I cannot find a way to limit results in sorting and merge them together.

Maybe I am trying to achieve something that is not possible with just one query at all, but before I decide to give up, I wanted to ask, if you guys have an idea, how to solve this one?

I have also found this answer but it does not go into details about the solution.

Thanks in advance!

Community
  • 1
  • 1
the_disco
  • 112
  • 9
  • Honestly, I wouldn't rely on Solr to do this. I'd get my result set, and pull out the two special cases myself. Or query for them separately. You might be able to write custom sort functionality to work within Solr, but it would be far more trouble than it's worth. – femtoRgon Mar 05 '14 at 18:33
  • That was my other option. Query documents that have to be elevated and merge the results on application level. Too bad it is not entirely possible with solr. – the_disco Mar 06 '14 at 08:22
  • Which version of Solr are you using? – cheffe Mar 06 '14 at 13:40
  • From solr dashboard "4.6.1 1560866 - mark - 2014-01-23 20:21:50", so it is relatively fresh. – the_disco Mar 06 '14 at 17:07
  • We're currently using multiple queries to achieve this, very interested in how to do it in 1 query myself. – Valentin V Feb 03 '15 at 13:46
  • I gave up and solved it using separate queries. It saved me a lot of headache. – the_disco Feb 03 '15 at 18:34

1 Answers1

0

This ticket might be interesting for you: https://issues.apache.org/jira/browse/SOLR-5541

[It] adds the ability to pass in elevateIds and excludeIds through two new http parameters "elevateIds" and "excludeIds". This will allow more sophisticated business logic to be used in selecting which ids to elevate/exclude.

Simon
  • 857
  • 5
  • 14
  • Thanks @Simon, that does look interesting, because you can move merging of the results directly to Solr instead of doing it on the application level. It still doesn't solve the problem of getting the results using one query. – the_disco Feb 08 '15 at 10:24