9

After searching extensively and coming across answers such as these -

Solr: Sort by score & an int field value

Use function query for boosting score in Solr

I am still unable to solve the following problem :

How do I use the "score" field of a document to create a new scoring function and rank the results accordingly. Something like this -

new_score = score * my_other_field

Current Query -

http://localhost:8984/solr/suggest_new/select?q=tom&wt=json&indent=true&bq=_val_:"product(score,count_of_searches)"

This is something I would have done in Elasticsearch -

"script_score" : {
    "script" : "_score * doc['my_numeric_field'].value"
}

Please help/ point out correct links. Thanks a lot ! (Note : Solr Version : 4.10.4)

Braiam
  • 1
  • 11
  • 47
  • 78
Utsav T
  • 1,515
  • 2
  • 24
  • 42
  • Can you post your full query to your solr database? Would be useful what else you're sending with your query. – megubyte Apr 25 '16 at 11:00
  • @MichealHarker - I've been trying a lot things. None of them seem to work though !:( I had found out the same thing in Elasticsearch, but haven't managed to find out the Solr equivalent. – Utsav T Apr 25 '16 at 11:05
  • You may find this link illuminating if you haven't seen it already: http://opensourceconnections.com/blog/2014/01/20/build-your-own-custom-lucene-query-and-scorer/. I'm not sure Solr can do what you want in the exact way you want it, so you're going to need to be a bit flexible. – TMBT Apr 28 '16 at 14:14

3 Answers3

11

When using Dismax or eDismax you should be able to just use the field bf (Boost Functions) parameter and fill it with the name of your numeric field.

Example

I have an index with documents that contain among other fields a numeric value named first_publication_year. When I run a matchAllQuery *:* against my index, all documents will get a score of 1. This makes the effect of the bf parameter easier to see, as 1 is an easy divisor. The sample would go with any query though.

/select?q=*:*

Result

{
  "responseHeader": {
    "status": 0,
    "QTime": 1
  },
  "response": {
    "numFound": 10007277,
    "start": 0,
    "maxScore": 1,
    "docs": [
      {
        "first_publication_year": 2002,
        "score": 1
      }
    ]
  }
}

Now I want to boost the documents based on that field, so I add that field name as bf parameter

/select?q=*:*&bf=first_publication_year

Result

{
  "responseHeader": {
    "status": 0,
    "QTime": 1
  },
  "response": {
    "numFound": 10007277,
    "start": 0,
    "maxScore": 1425.5273,
    "docs": [
      {
        "first_publication_year": 2015,
        "score": 1425.5273
      }
    ]
  }
}

If you think that the boost is too meagre you may adjust this with function queries. This sample multiplies the first publication year with 10.

/select?q=*:*&bf=product(first_publication_year,10)

Result

{
  "responseHeader": {
    "status": 0,
    "QTime": 465
  },
  "response": {
    "numFound": 10007277,
    "start": 0,
    "maxScore": 14248.908,
    "docs": [
      {
        "first_publication_year": 2015,
        "score": 14248.908
      }
    ]
  }
}

References

This is also documented in the Solr Reference Manual.

The bf (Boost Functions) Parameter

The bf parameter specifies functions (with optional boosts) that will be used to construct FunctionQueries which will be added to the user's main query as optional clauses that will influence the score. Any function supported natively by Solr can be used, along with a boost value. For example:

recip(rord(myfield),1,2,3)^1.5
Community
  • 1
  • 1
cheffe
  • 9,345
  • 2
  • 46
  • 57
  • When I use "bf" to boost my results, I see a following error - ""can not use FieldCache on multivalued field: rank" – jagamot Jul 03 '18 at 17:44
7

Use query() function to get the value of 'score' instead.

So if you're trying for this: new_score = score * popularity , use the following format.

q=searchterm&sort=product(query($q),popularity) desc

query($q) - returns the TF-IDF score for the query. So this is the equivalent of using the 'score' field.

1

I think you should do index time boosting for Solr documents. You need to add an optional boost attribute to your document. If you are using SolrJ, you can use document.setDocumentBoost(x) to boost your documents by a boost factor of x

You can also follow this link for detail description of index and query time boosting of Solr Documents.

Rohan
  • 179
  • 2
  • 3
  • I don't think I am looking for static boost (index time). That would have been straightforward. – Utsav T May 04 '16 at 10:34