Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position of document in result set in Solr

I need to know position of the document in Solr results. Let me explain why. We want to show to user position of his item in search (so that user can buy promotion and move it up). Now I query over all pages until I find the document, but it generates a lot of calls to Solr. Is there are way to get by id and query position of the element? Preferably by list of ids so that I can query it in one go.

Sample query:

/search?sort=sort_time%20desc&fq=category_parent_id:2003&rows=15&q=&start=0

Sample document:

{
    sort_time: 1421747930,
    id: 146002852,
    catlevel2: "Volvo",
    catlevel1: "Auto's",
    description: "Volvo XC90",
    score: 6.4758344
}
like image 865
Andrey Avatar asked Jan 16 '15 15:01

Andrey


1 Answers

You are sorting by "sort_time" which looks like a timestamp in milis.

Let's do the following: query all results that have a "sort_time" between 1421747930 and NOW with a range query. This will give you a "count" number of results even in the first resultset. If you order by "sort_time desc" your desired position is "count" as your result will be the last one in that range query. Try that same query adding a "sort_time:[1421747930 TO NOW]" for instance (this might not be the exact syntax, check it out). If you get a total of 137 results the position of 1421747930 will be 137. Myeb you need the max "sort_time" instead of NOW etc... I hope you get the idea.

Now you issue a second query with the exact id to get the rest of the data.

You get what you want with 2 queries though. I would suggest you play with the idea of ranges to get what you need.

like image 111
Bereng Avatar answered Oct 17 '22 16:10

Bereng