Solve the SOLR search multi-word matching and sorting scheme

Source: Internet
Author: User
Tags solr solr query

Reprint please indicate source: http://blog.csdn.net/hu948162999/article/details/47727159


In this paper, we mainly introduce the phrase, sentence, multi-word query, SOLR in the control of the number of query hits, and then order the result set .

in SOLR, the default is an or query, that is, if you search for more words in Q, the number of matches is more.  such as: The search phrase "China Lenovo Notebook", participle results: China, Lenovo, notebook.

Overwrite result set: Returns if the document contains these 3 words.

< Span style= "LINE-HEIGHT:25PX; Background-color:rgb (245,245,245); Font-family:helvetica,tahoma,arial,sans-serif "> sort result: According to SOLR's scoring formula. The default matching document with the highest correlation is placed first: To put it simply. is the document, and contains   China, Lenovo, notebook score highest.

< Span style= "LINE-HEIGHT:25PX; Background-color:rgb (245,245,245); Font-family:helvetica,tahoma,arial,sans-serif "> however: if custom sorting is required, the problem is gradually exposed.

by RequestHandler Queryparser Edismax in DF QF, the ordering is obtained after the weight of the field is configured and the integral model of each dimension. is not necessarily in accordance with the Chinese, Lenovo, notebook priority ranking. Some of the words that only contain China are highly prioritized and possible. This sort of results is clearly not understandable and fits the user's meaning.


How to reasonably control the number and quality of the hits of SOLR query???

In the previous article, we mentioned two search accuracy solutions for SOLR's phrases, short sentences (non-keywords), and SOLR controls the number of multiple-word federated query hits.

However, the above solves the problem of return accuracy. But set the MM match precision or the whole word match defaultoperator= "and". The sorting of DF and QF customizations will not work.


By default, SOLR query syntax has only two forms: keywords or key phrases separated by spaces. When querying English, English itself is a space to distinguish the word, so SOLR can directly obtain English words and assemble query, but there is no space in the middle of the Chinese sentence, SOLR query the entire sentence to query, and then by the query followed by field to participle, query. In this way, the benefits of QF in Dismax can be lost.

So: Thinking about such a way of thinking. After the word " Chinese Lenovo Notebook ". In the middle of each word unit by a space, you can meet the control of the search term matching degree under the premise of providing a custom sort. This is the time to rewrite Lucene's default queryparser.

Version: solr4.10.3

Solrconfig.xml Code

<span style= "FONT-SIZE:14PX;" ><str name= "Deftype" >myparser</str></span>

<span style= "FONT-SIZE:14PX;" > <!--Custom Queryparser--  <queryparser name= "Myparser" class= "Com.lubanec.util.MyQParserPlugin"/ ></span>

Rewrite Qparserplugin and Dismaxqparser

<span style= "FONT-SIZE:14PX;" >package Com.lubanec.util;import Org.apache.solr.common.params.solrparams;import Org.apache.solr.common.util.namedlist;import Org.apache.solr.request.solrqueryrequest;import Org.apache.solr.search.qparser;import Org.apache.solr.search.qparserplugin;public class MyQParserPlugin extends Qparserplugin {public void init (Namedlist args) {}public qparser createparser (String qstr, Solrparams localparams, Solrparams params, solrqueryrequest req) {return new Myqparser (Qstr, Localparams, params, req);}} </span>

<span style= "FONT-SIZE:14PX;" >package Com.lubanec.util;import Java.io.stringreader;import Org.apache.lucene.analysis.analyzer;import Org.apache.lucene.analysis.tokenstream;import Org.apache.lucene.analysis.tokenattributes.CharTermAttribute; Import Org.apache.solr.common.params.solrparams;import Org.apache.solr.request.solrqueryrequest;import Org.apache.solr.search.dismaxqparser;import Org.slf4j.logger;import Org.slf4j.loggerfactory;public class MyQParser Extends Dismaxqparser {private static Logger log = Loggerfactory.getlogger (myqparser.class);p ublic myqparser (String Qstr, Solrparams localparams, Solrparams params,solrqueryrequest req) {super (qstr, Localparams, params, req); Analyzer Analyzer = Req.getschema (). Getqueryanalyzer (); if (null = = Analyzer) return; StringBuilder norm = new StringBuilder ()//log.info ("Before analyzer, qstr=" + this.qstr); try {tokenstream ts = analyzer.t Okenstream (Req.getschema (). Getdefaultsearchfieldname (), New StringReader (THIS.QSTR)), Ts.reset (); while (TS.Incrementtoken ()) {Chartermattribute Termattribute = Ts.getattribute (Chartermattribute.class);//System.out              . println (Termattribute.tostring ());  Norm.append (New String (Termattribute.tostring ())). Append (""); }ts.end (); Ts.close ();} catch (Exception ex) {log.info ("ex=" + Ex);} if (norm.length () > 0) this.qstr = norm.tostring ()//log.info ("After analyzer, qstr=" + This.qstr);}} </span>


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Solve the SOLR search multi-word matching and sorting scheme

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.