How to use "score" field for creating custom scoring
Asked Answered
Q

3

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)

Quinonez answered 25/4, 2016 at 10:54 Comment(3)
Can you post your full query to your solr database? Would be useful what else you're sending with your query.Contrarious
@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.Quinonez
You may find this link illuminating if you haven't seen it already: opensourceconnections.com/blog/2014/01/20/…. 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.Slosh
R
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
Rodrick answered 3/5, 2016 at 6:26 Comment(1)
When I use "bf" to boost my results, I see a following error - ""can not use FieldCache on multivalued field: rank"Clavicytherium
L
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.

Lachance answered 23/1, 2017 at 23:19 Comment(1)
Is there a way to show this updated score - product(query($q),popularity) in the score field?Clavicytherium
T
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.

Toehold answered 4/5, 2016 at 9:53 Comment(1)
I don't think I am looking for static boost (index time). That would have been straightforward.Quinonez

© 2022 - 2024 — McMap. All rights reserved.