Showing posts with label solr function query search indexing. Show all posts
Showing posts with label solr function query search indexing. Show all posts

Wednesday, July 15, 2009

An application of Function Queries in Solr

Suppose you have a set of Solr documents. You wish to push the subset of documents having a particular field with value X to the top of the search results, effectively superseding Solr's default scoring mechanism. For example, your index contains a collection of computers for sale and the particular field's name is "operatingsystemtype" . Possible values for this field are: 1 (Windows XP), 2 (Vista), 5 (Mac OS 10), and 7 (Debian). You wish to push all the documents with operatingsystemtype = 5 to the top.

One way to implement this is to embed a _val_ hook right after the q parameter in the HTTP query string you send to Solr. For example: http://search.buycomputers.com/select/?q=(price:[1000 TO 1500])+_val_="map(operatingsystemtype,5,5,1000)"&fl=id,name,price,details.
  • Note the '+' instead of the usual '&'.
  • The map function will map all values between 5 and 5 inclusive to 1000. (See here for more details.) Although this 1000 is normalized to a smaller value, it is added to the document's score. Basically, all documents with operatingsystemtype = 5 will have a very large constant added to their score, effectively pushing all these documents to the top.
  • Please note that this method relies on Solr's default sort (i.e. score desc). If you provide a sort parameter, thus overriding the default order, this method won't work.
  • This is called a Function Query in Solr parlance.