You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@lucene.apache.org by mcb <th...@gmail.com> on 2011/08/27 20:05:08 UTC

Using a function for sort

Hi there, 
Relatively new solr user with an unfortunately not-so-newb question. I'm
trying to create a function that I can use as the sort field that compares
what fields match. So basically, say I have a database of cars and I want to
simply select all of them (the sort shouldn't remove any results), I'm just
going to write:

*/cars/?select=*:**

Then I want to return a number between 0 and 1 based on 3 fields: Color,
Type, and Transmission. So the function basically says: 

*If Color=Red, add .25 to the result
If Type=SUV, add .5 to the result
if Transmission=Auto, add .25 to the result*

A match on all fields returns *1*, a match on the 1 and 3 only returns *.5*,
etc.

I think the following approach I have might be a step in the right
direction:

*/cars/?select=*:*
&sort={!function}
add(product(tf(Color,Red),.25),product(tf(Type,SUV),.5),product(tf(Transmission,.Auto),.25))*

but not sure if tf is really the best too to use for this, thanks for the
help!

--
View this message in context: http://lucene.472066.n3.nabble.com/Using-a-function-for-sort-tp3289411p3289411.html
Sent from the Lucene - General mailing list archive at Nabble.com.

Re: Using a function for sort

Posted by mcb <th...@gmail.com>.
Oh, and for starters, tf() is probably not exactly what I want because it can
return 0....n and I'm just looking for a 0 or 1. Also I need to get this
custom score back in the fl with each result to say "according to the custom
function, this guy's score is .5!" or whatnot. 



--
View this message in context: http://lucene.472066.n3.nabble.com/Using-a-function-for-sort-tp3289411p3289436.html
Sent from the Lucene - General mailing list archive at Nabble.com.

Re: Using a function for sort

Posted by Chris Hostetter <ho...@fucit.org>.
: Then I want to return a number between 0 and 1 based on 3 fields: Color,
: Type, and Transmission. So the function basically says: 
	...
: I think the following approach I have might be a step in the right
: direction:
: 
: */cars/?select=*:*
: &sort={!function}
: add(product(tf(Color,Red),.25),product(tf(Type,SUV),.5),product(tf(Transmission,.Auto),.25))*
: 
: but not sure if tf is really the best too to use for this, thanks for the
: help!

...that should work, but if you want the value of the 
function to be included in the response you'll either need to change your 
"q" to be the function query (and use "fq" to constrain only the docs you 
want to match, -- if you really want *:* then you can ignore this) or try 
out the new "fl" "psuedo fields" syntax to include the results of a 
function in the response for each doc returned.

If you don't want the true "tf" value (ie: 0..infinity) and you just want 
o or 1 if it contains the term, you can either omitTf on those fields when 
indexing, or use the "map" function to map all values larger then 1 to 1.


FYI: in the future, questions like this are better suited for the 
solr-user@lucene mailing list, which exists explicitly for asking 
questions about using solr, and has a much larger subscriber base.  This 
list is for general discussion about hte broader lucene project.




-Hoss