I have a solr index accessed using SolrNet, and I would like to retrieve the index (position) of a particular document in the results, without returning the whole result set.

In more detail... the query returns ~30,000 documents and is ordered by an integer field. The unique key field contains a Guid, and I would like to find where in the results a particular document is, based on the unique key, while only returning the first 10 results.

This index was originally implemented in plain old Lucene, and this task was achieved with two queries, one to get the lucene doc id of the document I want to know about, then a second that returns the whole result set. I can then use the doc id to find where the document appears in the full result set, but then only enumerate the documents for the first 10.

Is there a way to achieve what I'm after with Solr, without returning all 30000 results (even limiting this to the Guid only seems too slow)?


  • 1
    What do you want to achieve with this? What's the real underlying problem? – Mauricio Scheffer Jan 4 '11 at 14:22
  • Lucene doc IDs are an implementation detail, Solr does not expose them. – Mauricio Scheffer Jan 4 '11 at 14:23
  • The results represent a paginated list of users, each having a numeric "points" field. The results should be ordered by this "points" field, and I then need to know at what position user X appears in the results. So above the paginated list, I can display "You're in 3rd place" or "You're in 321st place". If the current user is in the first page of results great, but the problem comes if they're not. Thanks. – robinbetts Jan 6 '11 at 13:25

I think you can do with a range query, including your user point's as lower level you can get the number of users above. You can do an explicit query or get that info using the facet range approach.

so, if you know that you user point is 10.000, you could do a query: game:tetris points[MaxInt TO 10000], and if the result is 375 you would know that your user is in rank 375.

  • Genius! That's exactly what I needed. I'd given up on this, then coming back to the problem a month later, my question came up on google and I saw your response which I hadn't seen before. Thanks! – robinbetts Feb 18 '11 at 16:35

The only reliable way I can think of is building the ranking at index-time, i.e. have a "rank" integer field and populate it when you build the index. The downside of this is that every update requires rebuilding the whole index.

Lucene doc ids are not stable, I wouldn't recommend using them for this (see this, this, this), and Solr does not expose them anyway.

Your Answer

