Sometimes the default field score does not meet our needs, such as we want to put the keyword in the title of the priority display.
tested on:SOLR 4.5.1, Jdk 1.6.0_45, Tomcat 6.0.37 | CentOS 5.7
There are 3 implementation methods:
- Set boost when indexing is in progress, this value will be written to the file, there is an additional performance overhead when building the index;
- When you specify boost at query time, this value is not written to the file, and the boost value of any field is specified at query time, which is applicable to multiple query scenarios with an index library, but with additional performance overhead when querying;
- (presumption) The former two combinations, such as the majority of the need for title priority, then the index can be specified when the title boost is 2, other scenarios can be specified in the query when the title boost is 0.5, and the setting when the index is built.
Only the second method is shown here:
Id |
Title |
Content |
Default Score |
Specify the title boost value is 2 after score |
108 |
Test |
Development Notes Development Notes Development Notes Development Notes Development notes |
1.6093808 |
1.0178617 |
107 |
Development Notes |
|
1.439474 |
1.8208065 |
Specify the method:
The Doc 107 is now in the top row:
Debug Query Effect:
Finding the method, we are not difficult to get the implementation in SOLRJ:
Package demo.service;
Import Java.util.Iterator;
Import Org.apache.log4j.Logger;
Import Org.apache.solr.client.solrj.SolrQuery;
Import org.apache.solr.client.solrj.SolrServerException;
Import Org.apache.solr.client.solrj.impl.HttpSolrServer;
Import Org.apache.solr.client.solrj.response.QueryResponse;
Import org.apache.solr.common.SolrDocument;
Import org.apache.solr.common.SolrDocumentList;
Import Base.util.ConfigUtil;
public class Solrservice {
private static Logger log = Logger.getlogger (Solrservice.class);
private static Httpsolrserver Solrserver;
static {
Solrserver = new Httpsolrserver (Configutil.getvalue ("Solr.url"));
Solrserver.setconnectiontimeout (5000);
}
public static void Main (string[] args) {
Solrquery query = new Solrquery ();
Query.setquery ("title: Development Note ^2 OR content: Development note");
Queryresponse RSP = null;
try {
RSP = solrserver.query (query);
} catch (Solrserverexception e) {
Log.error ("The Boost query encountered an error:", e);
}
Solrdocumentlist docs = Rsp.getresults ();
Iterator<solrdocument> iter = Docs.iterator ();
while (Iter.hasnext ()) {
Solrdocument doc = Iter.next ();
String idstr = doc.getfieldvalue ("id"). toString ();
int id = integer.parseint (IDSTR);
String title = Doc.getfieldvalue ("title"). ToString ();
String content = Doc.getfieldvalue ("content"). ToString ();
SYSTEM.OUT.PRINTLN ("ID:" +id+ "title:" +title+ "Content:" +content ");
}
}
}
Note: Because we want to set the boost for a field, the Copy field is no longer available at the time of the query.
In fact, there is a way to use the Copy field can be sorted, for your reference:
SOLR Copyfield usage scenarios and corresponding changes to the search ordering method
SOLR features three: Boost (change the default scoring sort)