Example usage for org.apache.lucene.search CoveringQuery CoveringQuery

List of usage examples for org.apache.lucene.search CoveringQuery CoveringQuery

Introduction

In this page you can find the example usage for org.apache.lucene.search CoveringQuery CoveringQuery.

Prototype

public CoveringQuery(Collection<Query> queries, LongValuesSource minimumNumberMatch) 

Source Link

Document

Sole constructor.

Usage

From source file:org.apache.solr.handler.component.SubsetQueryPlugin.java

License:Apache License

@Override
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
    return new QParser(qstr, localParams, params, req) {

        @Override/*from   www. j  a va  2  s  . c  o m*/
        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);
        }
    };
}

From source file:org.elasticsearch.index.query.TermsSetQueryBuilder.java

License:Apache License

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    if (values.isEmpty()) {
        return Queries.newMatchNoDocsQuery("No terms supplied for \"" + getName() + "\" query.");
    }/*from  w  w  w .jav a2  s.  c  o m*/
    // Fail before we attempt to create the term queries:
    if (values.size() > BooleanQuery.getMaxClauseCount()) {
        throw new BooleanQuery.TooManyClauses();
    }

    final MappedFieldType fieldType = context.fieldMapper(fieldName);
    final List<Query> queries = new ArrayList<>(values.size());
    for (Object value : values) {
        if (fieldType != null) {
            queries.add(fieldType.termQuery(value, context));
        } else {
            queries.add(new TermQuery(new Term(fieldName, BytesRefs.toBytesRef(value))));
        }
    }
    final LongValuesSource longValuesSource;
    if (minimumShouldMatchField != null) {
        MappedFieldType msmFieldType = context.fieldMapper(minimumShouldMatchField);
        if (msmFieldType == null) {
            throw new QueryShardException(context,
                    "failed to find minimum_should_match field [" + minimumShouldMatchField + "]");
        }

        IndexNumericFieldData fieldData = context.getForField(msmFieldType);
        longValuesSource = new FieldValuesSource(fieldData);
    } else if (minimumShouldMatchScript != null) {
        SearchScript.Factory factory = context.getScriptService().compile(minimumShouldMatchScript,
                SearchScript.CONTEXT);
        Map<String, Object> params = new HashMap<>();
        params.putAll(minimumShouldMatchScript.getParams());
        params.put("num_terms", queries.size());
        SearchScript.LeafFactory leafFactory = factory.newFactory(params, context.lookup());
        longValuesSource = new ScriptLongValueSource(minimumShouldMatchScript, leafFactory);
    } else {
        throw new IllegalStateException("No minimum should match has been specified");
    }
    return new CoveringQuery(queries, longValuesSource);
}