FunctionQuery

FunctionQuery allows one to use the actual value of a numeric field and functions of those fields in a relevancy score.

  1. Using FunctionQuery
  2. Function Query Syntax
  3. Available Functions
      1. constant
      2. fieldvalue
      3. ord
      4. rord
      5. sum
      6. product
      7. div
      8. pow
      9. abs
      10. log
      11. sqrt
      12. map
      13. scale
      14. query
      15. linear
      16. recip
      17. max
      18. top
      19. General Example

Using FunctionQuery

There are a few ways to use FunctionQuery from Solr's HTTP interface:

  1. Embed a FunctionQuery in a regular query expressed in SolrQuerySyntax via the _val_ hook

  2. Use the FunctionQParserPlugin, ie: q={!func}log(foo)

  3. Use a parameter that has an explicit type of FunctionQuery, such as DisMaxRequestHandler's bf (boost function) parameter.

    • NOTE: the bf parameter actually takes a list of function queries separated by whitespace and each with an optional boost. Make sure to eliminate any internal whitespace in single function queries when using bf. Example: q=dismax&bf="ord(popularity)^0.5 recip(rord(price),1,1000,1000)^0.3"

Function Query Syntax

There is currently no infix parser - functions must be expressed as function calls (e.g. sum(a,b) instead of a+b)

Available Functions

constant

<!> Solr1.3 Floating point constants.

fieldvalue

This function returns the numeric field value of an indexed field with a maximum of one value per document (not multiValued). The syntax is simply the field name by itself. 0 is returned for documents without a value in the field.

ord

ord(myfield) returns the ordinal of the indexed field value within the indexed list of terms for that field in lucene index order (lexicographically ordered by unicode value), starting at 1. In other words, for a given field, all values are ordered lexicographically; this function then returns the offset of a particular value in that ordering. The field must have a maximum of one value per document (not multiValued). 0 is returned for documents without a value in the field.

WARNING: ord() depends on the position in an index and can thus change when other documents are inserted or deleted, or if a MultiSearcher is used.

rord

The reverse ordering of what ord provides.

sum

<!> Solr1.3 sum(x,y,...) returns the sum of multiple functions.

product

<!> Solr1.3 product(x,y,...) returns the product of multiple functions.

div

<!> Solr1.3 div(x,y) divides the function x by the function y.

pow

<!> Solr1.3 pow(x,y) raises the base x to the power y.

abs

<!> Solr1.3 abs(x) returns the absolute value of a function.

log

<!> Solr1.3 log(x) returns log base 10 of the function x.

sqrt

<!> Solr1.3 sqrt(x) returns the square root of the function x

map

<!> Solr1.3 map(x,min,max,target) maps any values of the function x that fall within min and max inclusive to target. min,max,target are constants. It outputs the field's value if it does not fall between min and max.

scale

<!> Solr1.3 scale(x,minTarget,maxTarget) scales values of the function x such that they fall between minTarget and maxTarget inclusive.

query

<!> Solr1.4 query(subquery, default) returns the score for the given subquery, or the default value for documents not matching the query. Any type of subquery is supported through either parameter dereferencing $otherparam or direct specification of the query string in the LocalParams via "v".

/!\ :TODO: /!\ need to define "via v"

linear

linear(x,m,c) implements m*x+c where m and c are constants and x is an arbitrary function. This is equivalent to sum(product(m,x),c), but slightly more efficient as it is implemented as a single function.

recip

A reciprocal function with recip(myfield,m,a,b) implementing a/(m*x+b). m,a,b are constants, x is any arbitrarily complex function.

When a and b are equal, and x>=0, this function has a maximum value of 1 that drops as x increases. Increasing the value of a and b together results in a movement of the entire function to a flatter part of the curve. These properties can make this an ideal function for boosting more recent documents when x is rord(datefield).

max

max(x,c) returns the max of another function and a constant. Useful for "bottoming out" another function at some constant.

top

<!> Solr1.4 Causes it's function query argument to derive it's values from the top-level IndexReader containing all parts of an index. For example, the ordinal of a value in a single segment will be different from the ordinal of that same value in the complete index. The ord() and rord() functions implicitly use top() and hence ord(foo) is equivalent to top(ord(foo)).

General Example

last edited 2009-07-02 18:20:31 by HossMan