Joe
Joe

Reputation: 65

Boosting fields in SOLR using Solrj

I'm using the solrj API to query my SOLR 3.6 index. I have multiple text fields, which I would like to weight differently. From what I've read, I should be able to do this using the dismax or edismax query types. I've tried the following:

SolrQuery query = new SolrQuery();
query.setQuery( "title:apples oranges content:apples oranges");
query.setQueryType("edismax");
query.set("qf", "title^10.0 content^1.0");
QueryResponse rsp = m_Server.query( query );

But this doesn't work. I've tried the following variations to set the query type, but it doesn't seem to make a difference.

query.setQueryType("dismax");
query.set("qt","dismax");
query.set("type","edismax");
query.set("qt","edismax");
query.set("type","dismax");

I'd like to retain the full Lucene query syntax, so I prefer ExtendedDisMax to DisMax. Boosting individual terms in the query (as shown below) does work, but is not a valid solution, since the queries are automatically generated and can get arbitrarily complex is syntax.

query.setQuery( "title:apples^10.0 oranges^10.0 content:apples oranges");

Any help would be much appreciated.

Upvotes: 2

Views: 7322

Answers (2)

Jasper Floor
Jasper Floor

Reputation: 4852

You should be able to this programatically.

If you have a handler defined :

<requestHandler name="dismax_nl" class="solr.SearchHandler"> <lst name="defaults"> <str name="defType">dismax</str> 

and the following code

solrQuery.put("defType", "dismax"); 
solrQuery.put("qf", "comments_nl^20 id^1 name_nl^1 description_nl^0.2 url_nl^0.5 text^0.1"); 

Keep your query simple. qf defines the (weighted) fields. You should also be able to set qt=dismax_nl instead of deftype=dismax in the above example (which may be better but the one I have shown is something that I know works)

Upvotes: 3

Jayendra
Jayendra

Reputation: 52809

Best way would be to define a request handler in your solrconfig.xml like -

<requestHandler name="search" class="solr.SearchHandler" default="true">
 <lst name="defaults">
   <str name="echoParams">explicit</str>
   <str name="defType">dismax</str>
   <str name="qf">
      title^1 content^0.8
   </str>
   <str name="q.alt">*:*</str>
   <str name="rows">10</str>
   <str name="fl">*,score</str>
 </lst>
</requestHandler>

And use qt parameter to define the request handler -

query.set("qt","search");

You can fine tune the boost configuration just by changing in the solr config xml configurations and reloading the cores.

Upvotes: 7

Related Questions