Search in sources :

Example 6 with MultiTermQuery

use of org.apache.lucene.search.MultiTermQuery in project lucene-solr by apache.

the class FieldQuery method flatten.

void flatten(Query sourceQuery, IndexReader reader, Collection<Query> flatQueries, float boost) throws IOException {
    while (sourceQuery instanceof BoostQuery) {
        BoostQuery bq = (BoostQuery) sourceQuery;
        sourceQuery = bq.getQuery();
        boost *= bq.getBoost();
    }
    if (sourceQuery instanceof BooleanQuery) {
        BooleanQuery bq = (BooleanQuery) sourceQuery;
        for (BooleanClause clause : bq) {
            if (!clause.isProhibited()) {
                flatten(clause.getQuery(), reader, flatQueries, boost);
            }
        }
    } else if (sourceQuery instanceof DisjunctionMaxQuery) {
        DisjunctionMaxQuery dmq = (DisjunctionMaxQuery) sourceQuery;
        for (Query query : dmq) {
            flatten(query, reader, flatQueries, boost);
        }
    } else if (sourceQuery instanceof TermQuery) {
        if (boost != 1f) {
            sourceQuery = new BoostQuery(sourceQuery, boost);
        }
        if (!flatQueries.contains(sourceQuery))
            flatQueries.add(sourceQuery);
    } else if (sourceQuery instanceof SynonymQuery) {
        SynonymQuery synQuery = (SynonymQuery) sourceQuery;
        for (Term term : synQuery.getTerms()) {
            flatten(new TermQuery(term), reader, flatQueries, boost);
        }
    } else if (sourceQuery instanceof PhraseQuery) {
        PhraseQuery pq = (PhraseQuery) sourceQuery;
        if (pq.getTerms().length == 1)
            sourceQuery = new TermQuery(pq.getTerms()[0]);
        if (boost != 1f) {
            sourceQuery = new BoostQuery(sourceQuery, boost);
        }
        flatQueries.add(sourceQuery);
    } else if (sourceQuery instanceof ConstantScoreQuery) {
        final Query q = ((ConstantScoreQuery) sourceQuery).getQuery();
        if (q != null) {
            flatten(q, reader, flatQueries, boost);
        }
    } else if (sourceQuery instanceof CustomScoreQuery) {
        final Query q = ((CustomScoreQuery) sourceQuery).getSubQuery();
        if (q != null) {
            flatten(q, reader, flatQueries, boost);
        }
    } else if (sourceQuery instanceof ToParentBlockJoinQuery) {
        Query childQuery = ((ToParentBlockJoinQuery) sourceQuery).getChildQuery();
        if (childQuery != null) {
            flatten(childQuery, reader, flatQueries, boost);
        }
    } else if (reader != null) {
        Query query = sourceQuery;
        Query rewritten;
        if (sourceQuery instanceof MultiTermQuery) {
            rewritten = new MultiTermQuery.TopTermsScoringBooleanQueryRewrite(MAX_MTQ_TERMS).rewrite(reader, (MultiTermQuery) query);
        } else {
            rewritten = query.rewrite(reader);
        }
        if (rewritten != query) {
            // only rewrite once and then flatten again - the rewritten query could have a speacial treatment
            // if this method is overwritten in a subclass.
            flatten(rewritten, reader, flatQueries, boost);
        }
    // if the query is already rewritten we discard it
    }
// else discard queries
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) BoostQuery(org.apache.lucene.search.BoostQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery) BooleanClause(org.apache.lucene.search.BooleanClause) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery)

Example 7 with MultiTermQuery

use of org.apache.lucene.search.MultiTermQuery in project lucene-solr by apache.

the class TestQPHelper method testConstantScoreAutoRewrite.

public void testConstantScoreAutoRewrite() throws Exception {
    StandardQueryParser qp = new StandardQueryParser(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false));
    Query q = qp.parse("foo*bar", "field");
    assertTrue(q instanceof WildcardQuery);
    assertEquals(MultiTermQuery.CONSTANT_SCORE_REWRITE, ((MultiTermQuery) q).getRewriteMethod());
    q = qp.parse("foo*", "field");
    assertTrue(q instanceof PrefixQuery);
    assertEquals(MultiTermQuery.CONSTANT_SCORE_REWRITE, ((MultiTermQuery) q).getRewriteMethod());
    q = qp.parse("[a TO z]", "field");
    assertTrue(q instanceof TermRangeQuery);
    assertEquals(MultiTermQuery.CONSTANT_SCORE_REWRITE, ((MultiTermQuery) q).getRewriteMethod());
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery)

Example 8 with MultiTermQuery

use of org.apache.lucene.search.MultiTermQuery in project elasticsearch by elastic.

the class SpanMultiTermQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    Query subQuery = multiTermQueryBuilder.toQuery(context);
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    if (subQuery instanceof BoostQuery) {
        BoostQuery boostQuery = (BoostQuery) subQuery;
        subQuery = boostQuery.getQuery();
        boost = boostQuery.getBoost();
    }
    //no MultiTermQuery extends SpanQuery, so SpanBoostQuery is not supported here
    assert subQuery instanceof SpanBoostQuery == false;
    if (subQuery instanceof MultiTermQuery == false) {
        throw new UnsupportedOperationException("unsupported inner query, should be " + MultiTermQuery.class.getName() + " but was " + subQuery.getClass().getName());
    }
    SpanQuery wrapper = new SpanMultiTermQueryWrapper<>((MultiTermQuery) subQuery);
    if (boost != AbstractQueryBuilder.DEFAULT_BOOST) {
        wrapper = new SpanBoostQuery(wrapper, boost);
    }
    return wrapper;
}
Also used : Query(org.apache.lucene.search.Query) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanMultiTermQueryWrapper(org.apache.lucene.search.spans.SpanMultiTermQueryWrapper) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 9 with MultiTermQuery

use of org.apache.lucene.search.MultiTermQuery in project neo4j by neo4j.

the class LuceneDocumentStructureTest method shouldBuildRangeSeekByPrefixQueryForStrings.

@Test
public void shouldBuildRangeSeekByPrefixQueryForStrings() throws Exception {
    // given
    MultiTermQuery prefixQuery = (MultiTermQuery) LuceneDocumentStructure.newRangeSeekByPrefixQuery("Prefix");
    // then
    assertThat("Should contain term value", prefixQuery.toString(), containsString("Prefix"));
}
Also used : MultiTermQuery(org.apache.lucene.search.MultiTermQuery) Test(org.junit.Test)

Example 10 with MultiTermQuery

use of org.apache.lucene.search.MultiTermQuery in project lucene-solr by apache.

the class SynonymTokenizer method testNotRewriteMultiTermQuery.

public void testNotRewriteMultiTermQuery() throws IOException {
    // field "bar": (not the field we ultimately want to extract)
    MultiTermQuery mtq = new TermRangeQuery("bar", new BytesRef("aa"), new BytesRef("zz"), true, true);
    WeightedSpanTermExtractor extractor = new WeightedSpanTermExtractor() {

        @Override
        protected void extract(Query query, float boost, Map<String, WeightedSpanTerm> terms) throws IOException {
            assertEquals(mtq, query);
            super.extract(query, boost, terms);
        }
    };
    extractor.setExpandMultiTermQuery(true);
    extractor.setMaxDocCharsToAnalyze(51200);
    extractor.getWeightedSpanTerms(mtq, 3, new CannedTokenStream(new Token("aa", 0, 2), new Token("bb", 2, 4)), // field "foo"
    "foo");
}
Also used : MultiTermQuery(org.apache.lucene.search.MultiTermQuery) Query(org.apache.lucene.search.Query) SpanPayloadCheckQuery(org.apache.lucene.queries.payloads.SpanPayloadCheckQuery) CommonTermsQuery(org.apache.lucene.queries.CommonTermsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) ToChildBlockJoinQuery(org.apache.lucene.search.join.ToChildBlockJoinQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanNotQuery(org.apache.lucene.search.spans.SpanNotQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) Token(org.apache.lucene.analysis.Token) CannedTokenStream(org.apache.lucene.analysis.CannedTokenStream) Map(java.util.Map) HashMap(java.util.HashMap) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

MultiTermQuery (org.apache.lucene.search.MultiTermQuery)10 Query (org.apache.lucene.search.Query)7 TermQuery (org.apache.lucene.search.TermQuery)6 Term (org.apache.lucene.index.Term)5 BoostQuery (org.apache.lucene.search.BoostQuery)5 BooleanQuery (org.apache.lucene.search.BooleanQuery)4 PhraseQuery (org.apache.lucene.search.PhraseQuery)4 SpanQuery (org.apache.lucene.search.spans.SpanQuery)4 CustomScoreQuery (org.apache.lucene.queries.CustomScoreQuery)3 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)3 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)3 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)3 PrefixQuery (org.apache.lucene.search.PrefixQuery)3 SynonymQuery (org.apache.lucene.search.SynonymQuery)3 ToParentBlockJoinQuery (org.apache.lucene.search.join.ToParentBlockJoinQuery)3 IndexReader (org.apache.lucene.index.IndexReader)2 CommonTermsQuery (org.apache.lucene.queries.CommonTermsQuery)2 BooleanClause (org.apache.lucene.search.BooleanClause)2 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)2 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2