Matteo Depalo
Matteo Depalo

Reputation: 13

Solr minimum match results ranking

In my Rails application I have a Question model, setup with sunspot solr, with a field "text" and I'd like to search in that field doing a logical OR between words. I've found that setting minimum_match to 1 solves my problem, however I'd like also to order the results by boosting questions that have more than 1 word matching. Is there a way to do this with Solr? The documentation isn't really helpful about ranking functions.

Edit: this is the full query I'm performing in the controller

@questions = Question.solr_search do
  fulltext params[:query], :minimum_match => 1
end.results

Upvotes: 0

Views: 1350

Answers (2)

konyak
konyak

Reputation: 11706

According to http://wiki.apache.org/solr/SchemaXml,

The default operator used by Solr's query parser (SolrQueryParser) can be configured with

<solrQueryParser defaultOperator="AND|OR"/>. 

The default operator is "OR" if unspecified. It is preferable to not use or rely on this setting; instead the request handler or query LocalParams should specify the default operator. This setting here can be omitted and it is being considered for deprecation.

You can change your defaultOperator in solr/conf/schema.xml or you could use LocalParams to specify OR via syntax like https://github.com/sunspot/sunspot/wiki/Building-queries-by-hand

It is true Sunspot's default operator is "AND", as referenced in https://github.com/sunspot/sunspot/blob/master/sunspot_solr/solr/solr/conf/schema.xml

Upvotes: 1

Peter Dixon-Moses
Peter Dixon-Moses

Reputation: 3209

Logical OR is the default behavior of the Dismax request handler used in Sunspot.

Plus, the more words match, the higher the document's score (which sounds like what you want)

Question.search do
  fulltext 'best pizza'
end

...should return results that match one or both words (returning the ones that match both first):

  1. "Joe's has the best pizza by the slice in NYC"
  2. "It's hard to say which pizza place is the best"
  3. "Pizza isn't the best food for you"
  4. "I don't care whether pizza is bad for you!"
  5. "What do you think the best type of fast food is?"

minimum_match is useful only if you want to filter out low relevance results (where only a certain low number or percentage of terms were actually matched). This doesn't affect scoring or logical OR/AND behavior.

Upvotes: 0

Related Questions