Solr Function Query : How to use “score” field for

2020-03-12 08:38发布

问题:

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)

回答1:

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


回答2:

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.



回答3:

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.