Last active
January 2, 2016 20:29
-
-
Save vsdev1/8357542 to your computer and use it in GitHub Desktop.
Common terms query parser plugin for Apache Solr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Solr query parser login for the lucene common terms query. Must be used like q={!commonTermsQueryParser}WeightThe weight... | |
* where commonTermsQueryParser is configured in solrconfig.xml. | |
* | |
* The query field must be given as query parameter "qf". The maxTermFrequency can be configured in solrconfig.xml | |
* (default is 0.01). The query is executed against the field given in the request parameter qf while | |
* using the query analyzers of this field. | |
* | |
* see http://java.dzone.com/articles/create-custom-solr-queryparser | |
*/ | |
public class CommonTermsQParserPlugin extends QParserPlugin { | |
private static final String MAX_TERM_FREQUENCY_CONFIG_PARAM_NAME = "maxTermFrequency"; | |
private static final float DEFAULT_MAX_TERM_FREQUENCY = 0.01F; | |
private float maxTermFrequency = DEFAULT_MAX_TERM_FREQUENCY; | |
@Override | |
public void init(NamedList args) { | |
SolrParams params = SolrParams.toSolrParams(args); | |
// handle configuration parameters passed through solrconfig.xml | |
final Float maxTermFrequency = params.getFloat(MAX_TERM_FREQUENCY_CONFIG_PARAM_NAME); | |
if (maxTermFrequency != null) { | |
this.maxTermFrequency = maxTermFrequency; | |
} | |
} | |
@Override | |
public QParser createParser(final String queryString, SolrParams localParams, SolrParams params, SolrQueryRequest req) { | |
return new QParser(queryString, localParams, params, req) { | |
@Override | |
public Query parse() throws SyntaxError { | |
String queryField = params.get("qf"); | |
final Analyzer analyzer = req.getSchema().getQueryAnalyzer(); | |
final CommonTermsQuery commonTermsQuery = new CommonTermsQuery(BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD, maxTermFrequency); | |
try { | |
final TokenStream tokenStream = analyzer.tokenStream(queryField, new StringReader(queryString)); | |
tokenStream.reset(); | |
final CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class); | |
while (tokenStream.incrementToken()) { | |
commonTermsQuery.add(new Term(queryField, charTermAttribute.toString())); | |
} | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
return commonTermsQuery; | |
} | |
}; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<queryParser class="....CommonTermsQParserPlugin" name="commonTermsQueryParser"> | |
<float name="maxTermFrequency">0.00001</float> | |
</queryParser> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment