0

This is not same as the below:

How to sort the top 100 results of solr query based on a specific filed?

Suppose my search criteria title:Building the Nation returns 100k results. I guess Solr already returns data based on the best match i.e best match data will in the top. I am sorting based on some thing. In this case the record at the last position may come at the top.

https://host/solr/booksCore/select?q=(title:"Building the Nation" OR title:Building the Nation OR author:"Building the Nation" OR author:Building the Nation) AND subjects:*&rows=100&sort=sales_a desc&wt=json

So I want to do search at first, then take the 1st 100 records and then sort. How can I do that using Solr select API?

James Z
  • 12,209
  • 10
  • 24
  • 44
Sumit
  • 856
  • 5
  • 18
  • 38

1 Answers1

4

First - the queries you're writing does not do what you probably intend them to do. author:Building the Nation searches for Building in the author field, and the and Nation in the default search field. You probably want author:the author:Nation in that case, or use qf with the edismax query parser (and use ps and pf to boost phrases).

Second, if subjects:* are meant to match any document that has a subject, that syntax would be subjects:[* TO *].

Then third, to re-rank the top N results in a query, you can use rq - a re-rank query. The result of this query will be a new set of scores, which you can boost using the reRankWeight parameter:

In the example below, the top 1000 documents matching the query "greetings" will be re-ranked using the query "(hi hello hey hiya)". The resulting scores for each of those 1000 documents will be 3 times their score from the "(hi hello hey hiya)", plus the score from the original "greetings" query:

q=greetings&rq={!rerank reRankQuery=$rqq reRankDocs=1000 reRankWeight=3}&rqq=(hi+hello+hey+hiya)

You can use _val_ to directly assign a numerical value as the score of the document, effectively reverse sorting by that value.

Community
  • 1
  • 1
MatsLindh
  • 49,529
  • 4
  • 53
  • 84
  • For the FIRST section: Thanks a lot to findout my mistakes. I am new to Solr. I amusing the `edismax` now. – Sumit Aug 21 '18 at 14:18
  • For the SECOND section: I want to do search for any value of subjects. Actually when I have a `subjects` value available I put it in the query and when I dont have it I put `subjects:*` – Sumit Aug 21 '18 at 14:19
  • For the THIRD section: How can I do a sorting based on `sales_a` field using re-ranking? re-ranking is useful if we want to fire a subquery on the subset of 1st query. Please correct me if I am wrong. @MatsLindh – Sumit Aug 21 '18 at 14:22
  • Correct, but that query's score is used for reranking the query. So if you use `_val_`, you should be able to assign the returned value as the score for that query, and get the sort order you want. What is the type of your `sales_a` field, and do you have any examples of values in it? – MatsLindh Aug 21 '18 at 21:26
  • `sales_a` is `pdouble`. Example value is 2.0, 2500.25 – Sumit Aug 22 '18 at 08:44
  • 1
    Try it with just `_val_:sales_a`, as that should assign the value in the `sales_a` field as the score of the document. – MatsLindh Aug 22 '18 at 10:11
  • As per FIRST section I have modified the query using: `https://host/solr/booksCore/select?q=(Building the Nation)&defType=edismax&qf=title author&rows=100&sort=sales_a desc&wt=json ` – Sumit Aug 22 '18 at 10:20
  • I understood your point & it should work. But I am not sure how to use the `edismax` query WITH the re-rank query using _val_. – Sumit Aug 22 '18 at 10:22
  • You'll have to drop the `sort` criteria if you're going to use score for the original search result, then add the rerank query as `rq` to re-order the top hits – MatsLindh Aug 22 '18 at 10:25
  • Can you please tell me whether this query is correct? `q=(Building the Nation)&defType=edismax&qf=title author&rq={!rerank reRankQuery=$rqq reRankDocs=1000 reRankWeight=3}&rqq=(_val_:sales_a)`. Do we need the `reRankWeight` here? – Sumit Aug 22 '18 at 10:39
  • The reRankWeight tells you how much the score returned by the rerank query will be multiplied by before affecting the regular score. You'll have to look at the score values returned by your query to decide if the value given is proper. – MatsLindh Aug 22 '18 at 10:53
  • Posting the final URL to helps others. It will search for "Building The Nation" on title and author and filter with subject: 20_5. The top 100 results will be reranked and the sales_a value will enhance their score. So if any record within the top 100 has more sales_a value the score of it will be enhanced in that ratio. So it will occupy top position. So basically I am sotring with sales_a from the top 100 records(minimum 100. Solr considers it minimum and increase it if necessary automatically). – Sumit Aug 24 '18 at 08:59
  • https://host/solr/booksCore/select?q=(Building the Nation)&defType=edismax&qf=title author&fq=subjects:20_5&rq={!rerank reRankQuery=$rqq reRankDocs=100 reRankWeight=3}&rqq=(_val_:sales_a)&rows=100&wt=json – Sumit Aug 24 '18 at 08:59
  • Thanks @MatsLindh for your help. May god give you a Nobel Prize :) – Sumit Aug 24 '18 at 09:00