Java Code Examples for org.apache.lucene.queries.BlendedTermQuery

The following examples show how to use org.apache.lucene.queries.BlendedTermQuery. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Elasticsearch   Source File: MultiMatchQueryBuilder.java    License: Apache License 2.0 6 votes vote down vote up
public Query blendTerm(Term term, MappedFieldType fieldType) {
    if (blendedFields == null) {
        return super.blendTerm(term, fieldType);
    }
    final Term[] terms = new Term[blendedFields.length];
    float[] blendedBoost = new float[blendedFields.length];
    for (int i = 0; i < blendedFields.length; i++) {
        terms[i] = blendedFields[i].newTerm(term.text());
        blendedBoost[i] = blendedFields[i].boost;
    }
    if (options.commonTermsCutoff() != null) {
        return BlendedTermQuery.commonTermsBlendedQuery(
            terms, blendedBoost, false, options.commonTermsCutoff());
    }

    if (tieBreaker == 1.0f) {
        return BlendedTermQuery.booleanBlendedQuery(terms, blendedBoost, false);
    }
    return BlendedTermQuery.dismaxBlendedQuery(terms, blendedBoost, tieBreaker);
}
 
Example 2
Source Project: Elasticsearch   Source File: CustomFieldQuery.java    License: Apache License 2.0 6 votes vote down vote up
@Override
void flatten(Query sourceQuery, IndexReader reader, Collection<Query> flatQueries, float boost) throws IOException {
    if (sourceQuery instanceof SpanTermQuery) {
        super.flatten(new TermQuery(((SpanTermQuery) sourceQuery).getTerm()), reader, flatQueries, boost);
    } else if (sourceQuery instanceof ConstantScoreQuery) {
        flatten(((ConstantScoreQuery) sourceQuery).getQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof FunctionScoreQuery) {
        flatten(((FunctionScoreQuery) sourceQuery).getSubQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof MultiPhrasePrefixQuery) {
        flatten(sourceQuery.rewrite(reader), reader, flatQueries, boost);
    } else if (sourceQuery instanceof FiltersFunctionScoreQuery) {
        flatten(((FiltersFunctionScoreQuery) sourceQuery).getSubQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof MultiPhraseQuery) {
        MultiPhraseQuery q = ((MultiPhraseQuery) sourceQuery);
        convertMultiPhraseQuery(0, new int[q.getTermArrays().size()], q, q.getTermArrays(), q.getPositions(), reader, flatQueries);
    } else if (sourceQuery instanceof BlendedTermQuery) {
        final BlendedTermQuery blendedTermQuery = (BlendedTermQuery) sourceQuery;
        flatten(blendedTermQuery.rewrite(reader), reader, flatQueries, boost);
    } else {
        super.flatten(sourceQuery, reader, flatQueries, boost);
    }
}
 
Example 3
Source Project: Elasticsearch   Source File: MultiMatchQuery.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Query blendTerm(Term term, MappedFieldType fieldType) {
    if (blendedFields == null) {
        return super.blendTerm(term, fieldType);
    }
    final Term[] terms = new Term[blendedFields.length];
    float[] blendedBoost = new float[blendedFields.length];
    for (int i = 0; i < blendedFields.length; i++) {
        terms[i] = blendedFields[i].newTerm(term.text());
        blendedBoost[i] = blendedFields[i].boost;
    }
    if (commonTermsCutoff != null) {
        return BlendedTermQuery.commonTermsBlendedQuery(terms, blendedBoost, false, commonTermsCutoff);
    }

    if (tieBreaker == 1.0f) {
        return BlendedTermQuery.booleanBlendedQuery(terms, blendedBoost, false);
    }
    return BlendedTermQuery.dismaxBlendedQuery(terms, blendedBoost, tieBreaker);
}
 
Example 4
public void testNoMatchQueries() throws IOException {
    String userQuery = "brown cow";

    Term[] termsToBlend = new Term[]{new Term("field",  userQuery.split(" ")[0])};

    Query blended = BlendedTermQuery.dismaxBlendedQuery(termsToBlend, 1f);
    List<PrebuiltFeature> features = Arrays.asList(
            new PrebuiltFeature(null, new TermQuery(new Term("field",  "missingterm"))),
            new PrebuiltFeature(null, blended));

    checkModelWithFeatures(features, null, null);
}
 
Example 5
public void testOnRewrittenQueries() throws IOException {
    String userQuery = "brown cow";

    Term[] termsToBlend = new Term[]{new Term("field",  userQuery.split(" ")[0])};

    Query blended = BlendedTermQuery.dismaxBlendedQuery(termsToBlend, 1f);
    List<Query> features = Arrays.asList(new TermQuery(new Term("field",  userQuery.split(" ")[0])), blended);

    checkModelWithFeatures(toPrebuildFeatureWithNoName(features), null, null);
}