use of org.apache.lucene.search.CoveringQuery in project ranger by apache.
the class SubsetQueryPlugin method createParser.
@Override
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
return new QParser(qstr, localParams, params, req) {
@Override
public Query parse() throws SyntaxError {
String fieldName = Preconditions.checkNotNull(localParams.get(SETVAL_FIELD_NAME));
String countFieldName = Preconditions.checkNotNull(localParams.get(COUNT_FIELD_NAME));
boolean allowMissingValues = Boolean.parseBoolean(Preconditions.checkNotNull(localParams.get(MISSING_VAL_ALLOWED)));
String wildcardToken = localParams.get(WILDCARD_CHAR);
LongValuesSource minimumNumberMatch = LongValuesSource.fromIntField(countFieldName);
Collection<Query> queries = new ArrayList<>();
String fieldVals = Preconditions.checkNotNull(localParams.get(SETVAL_PARAM_NAME));
for (String v : fieldVals.split(",")) {
queries.add(new TermQuery(new Term(fieldName, v)));
}
if (wildcardToken != null && !wildcardToken.equals("")) {
queries.add(new TermQuery(new Term(fieldName, wildcardToken)));
}
if (allowMissingValues) {
// To construct this query we need to do a little trick tho construct a test for an empty field as follows:
// (*:* AND -fieldName:*) ==> parses as: (+*:* -fieldName:*)
// It is a feature of Lucene that pure negative queries are not allowed (although Solr allows them as a top level construct)
// therefore we need to AND with *:*
// We can then pass this BooleanQuery to the CoveringQuery as one of its allowed matches.
BooleanQuery.Builder builder = new BooleanQuery.Builder();
builder.add(new BooleanClause(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD));
builder.add(new BooleanClause(new WildcardQuery(new Term(fieldName, "*")), BooleanClause.Occur.MUST_NOT));
queries.add(builder.build());
}
return new CoveringQuery(queries, minimumNumberMatch);
}
};
}
Aggregations