2

i want to sort the top 100documents of a solr using a specific field but it sort the whole result set and then display result the following is my code.

    query1.setQuery(" Natural Language ");
    query1.setStart(0);
    query1.setRows(100);
    int i=0;
    query1.set("df","Text");
    query1.setFields("PaperID","TotalPageRank");
    query1.setSort("customrank", SolrQuery.ORDER.desc);

Is it possible using solr query to sort the top 100 documents using customrank field?

Shah Khalid
  • 31
  • 1
  • 10
  • So the top 100 documents should be retrieved based on score, then those 100 should again be sorted based on customrank? – MatsLindh Nov 06 '17 at 07:30

2 Answers2

3

You can try to use the Query Re-Ranking feature. This allows you to issue one query, retrieve the top N entries and then re-rank these entries according to a second query. This is usually used to have one simple query limit the total number of documents before applying an expensive query to those entries for re-ranking, but the use case seems similar enough.

An adapted version of the example on the documentation page would probably be something like (I haven't been able to test this, so add a comment with adjustments for your use case):

rq={!rerank reRankQuery=$rqq reRankDocs=100 reRankWeight=3}&rqq=_val_=customrank

You might have to tune the rerankweight to get your customrank to contribute more to the final score, as I don't think you can sort explicitly

MatsLindh
  • 49,529
  • 4
  • 53
  • 84
  • @ MatsLindh, thanks for reply but it is not a second query it is a field value, i want to re rank the retrieved result on a field value. i can sort the retrieved result by by own method using java, but i want to know is there any way in solr to re-rank the top N documents on a specific field value. where can we put this "rq" in my code. – Shah Khalid Nov 07 '17 at 11:32
  • 1
    That's exactly what the rerank query does. `_val_` is a special key that will add the value returned to the score - i.e. your custom rank. That's why I wrote that you'll have to tune the value (multiply it, etc. if necessary). The `rq` is part of the regular query string when accessing Solr. If you're using a library to access Solr, check the documentation for the library for how to add a custom parameter if rerank isn't supported out of the box. – MatsLindh Nov 07 '17 at 14:06
  • I re-rank it by my own method using Python. – Shah Khalid Nov 09 '17 at 01:56
1

It is very simple..

SolrQuery query1 = new SolrQuery();
    CommonsHttpSolrServer server = new CommonsHttpSolrServer("Your server url");
    server.getHttpClient().getParams();
    query1.setQuery("Natural Language");
    query1.setFields("PaperID", "TotalPageRank");
    query1.setStart(0);
    query1.setRows(100);
    query1.setSort("customrank", SolrQuery.ORDER.desc);
    QueryResponse solrresponse = server.query(query1);
    SolrDocumentList results = solrresponse.getResults();
    for (int i = 0; i < results.size(); ++i) {
        String resultsolr = results.get(i).toString();
    }

Note: The customrank field shoud be integer, better to have customrank_i

Hope this will help!! Happy coding :)

Harisudhan. A
  • 662
  • 1
  • 6
  • 20
  • 1
    This will sort the complete data set (and not just the top 100 documents), which sounds like what the asker had already tried? – MatsLindh Nov 06 '17 at 07:31
  • @MatsLindh offcourse it will sort the complete data set, still we can able get the data out with top 100 and then next...next set easily and continously – Harisudhan. A Nov 06 '17 at 09:03
  • But that's not the same - this will get the _top 100_ sorted by `customerrank`. If the user wants to sort the _top 100 results_ _by_ `customerrank`, the result will be different. Imagine a query retrieving the top 100 users by a criteria from two million users, _then_ sorting those 100 users retrieved by `customerrank`. – MatsLindh Nov 06 '17 at 09:08
  • Yes @MatsLindh you are right.. I need to sort only the top 100 documents by customrank, not the entire result set. – Shah Khalid Nov 07 '17 at 00:32
  • @Harisudhan.A dear i already tried that but it sort the entire result set. I want to sort out only the top 100 document by customrank. is there any way? – Shah Khalid Nov 07 '17 at 00:32