Search in sources :

Example 1 with BlendedTermQuery

use of org.apache.lucene.queries.BlendedTermQuery in project elasticsearch by elastic.

the class QueryAnalyzerTests method testExtractQueryMetadata_blendedTermQuery.

public void testExtractQueryMetadata_blendedTermQuery() {
    Term[] termsArr = new Term[] { new Term("_field", "_term1"), new Term("_field", "_term2") };
    BlendedTermQuery commonTermsQuery = BlendedTermQuery.booleanBlendedQuery(termsArr, false);
    Result result = analyze(commonTermsQuery);
    assertThat(result.verified, is(true));
    List<Term> terms = new ArrayList<>(result.terms);
    Collections.sort(terms);
    assertThat(terms.size(), equalTo(2));
    assertThat(terms.get(0).field(), equalTo("_field"));
    assertThat(terms.get(0).text(), equalTo("_term1"));
    assertThat(terms.get(1).field(), equalTo("_field"));
    assertThat(terms.get(1).text(), equalTo("_term2"));
}
Also used : ArrayList(java.util.ArrayList) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm) Result(org.elasticsearch.percolator.QueryAnalyzer.Result)

Example 2 with BlendedTermQuery

use of org.apache.lucene.queries.BlendedTermQuery in project elasticsearch by elastic.

the class CustomFieldQuery method flatten.

@Override
void flatten(Query sourceQuery, IndexReader reader, Collection<Query> flatQueries, float boost) throws IOException {
    if (sourceQuery instanceof BoostQuery) {
        BoostQuery bq = (BoostQuery) sourceQuery;
        sourceQuery = bq.getQuery();
        boost *= bq.getBoost();
        flatten(sourceQuery, reader, flatQueries, boost);
    } else 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().length], 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 if (sourceQuery instanceof ESToParentBlockJoinQuery) {
        ESToParentBlockJoinQuery blockJoinQuery = (ESToParentBlockJoinQuery) sourceQuery;
        flatten(blockJoinQuery.getChildQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof BoostingQuery) {
        BoostingQuery boostingQuery = (BoostingQuery) sourceQuery;
        //flatten positive query with query boost
        flatten(boostingQuery.getMatch(), reader, flatQueries, boost);
        //flatten negative query with negative boost
        flatten(boostingQuery.getContext(), reader, flatQueries, boostingQuery.getBoost());
    } else if (sourceQuery instanceof SynonymQuery) {
        // SynonymQuery should be handled by the parent class directly.
        // This statement should be removed when https://issues.apache.org/jira/browse/LUCENE-7484 is merged.
        SynonymQuery synQuery = (SynonymQuery) sourceQuery;
        for (Term term : synQuery.getTerms()) {
            flatten(new TermQuery(term), reader, flatQueries, boost);
        }
    } else {
        super.flatten(sourceQuery, reader, flatQueries, boost);
    }
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) ESToParentBlockJoinQuery(org.elasticsearch.index.search.ESToParentBlockJoinQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) BoostingQuery(org.apache.lucene.queries.BoostingQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery)

Example 3 with BlendedTermQuery

use of org.apache.lucene.queries.BlendedTermQuery in project elasticsearch by elastic.

the class CandidateQueryTests method testDuelSpecificQueries.

public void testDuelSpecificQueries() throws Exception {
    List<ParseContext.Document> documents = new ArrayList<>();
    CommonTermsQuery commonTermsQuery = new CommonTermsQuery(BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD, 128);
    commonTermsQuery.add(new Term("field", "quick"));
    commonTermsQuery.add(new Term("field", "brown"));
    commonTermsQuery.add(new Term("field", "fox"));
    addQuery(commonTermsQuery, documents);
    BlendedTermQuery blendedTermQuery = BlendedTermQuery.booleanBlendedQuery(new Term[] { new Term("field", "quick"), new Term("field", "brown"), new Term("field", "fox") }, false);
    addQuery(blendedTermQuery, documents);
    SpanNearQuery spanNearQuery = new SpanNearQuery.Builder("field", true).addClause(new SpanTermQuery(new Term("field", "quick"))).addClause(new SpanTermQuery(new Term("field", "brown"))).addClause(new SpanTermQuery(new Term("field", "fox"))).build();
    addQuery(spanNearQuery, documents);
    SpanNearQuery spanNearQuery2 = new SpanNearQuery.Builder("field", true).addClause(new SpanTermQuery(new Term("field", "the"))).addClause(new SpanTermQuery(new Term("field", "lazy"))).addClause(new SpanTermQuery(new Term("field", "doc"))).build();
    SpanOrQuery spanOrQuery = new SpanOrQuery(spanNearQuery, spanNearQuery2);
    addQuery(spanOrQuery, documents);
    SpanNotQuery spanNotQuery = new SpanNotQuery(spanNearQuery, spanNearQuery);
    addQuery(spanNotQuery, documents);
    long lowerLong = randomIntBetween(0, 256);
    long upperLong = lowerLong + randomIntBetween(0, 32);
    addQuery(LongPoint.newRangeQuery("long_field", lowerLong, upperLong), documents);
    indexWriter.addDocuments(documents);
    indexWriter.close();
    directoryReader = DirectoryReader.open(directory);
    IndexSearcher shardSearcher = newSearcher(directoryReader);
    // Disable query cache, because ControlQuery cannot be cached...
    shardSearcher.setQueryCache(null);
    Document document = new Document();
    document.add(new TextField("field", "the quick brown fox jumps over the lazy dog", Field.Store.NO));
    long randomLong = randomIntBetween((int) lowerLong, (int) upperLong);
    document.add(new LongPoint("long_field", randomLong));
    MemoryIndex memoryIndex = MemoryIndex.fromDocument(document, new WhitespaceAnalyzer());
    duelRun(queryStore, memoryIndex, shardSearcher);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) ArrayList(java.util.ArrayList) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) Term(org.apache.lucene.index.Term) LongPoint(org.apache.lucene.document.LongPoint) Document(org.apache.lucene.document.Document) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) CommonTermsQuery(org.apache.lucene.queries.CommonTermsQuery) SpanNotQuery(org.apache.lucene.search.spans.SpanNotQuery) MemoryIndex(org.apache.lucene.index.memory.MemoryIndex) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TextField(org.apache.lucene.document.TextField) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery)

Aggregations

Term (org.apache.lucene.index.Term)3 BlendedTermQuery (org.apache.lucene.queries.BlendedTermQuery)3 ArrayList (java.util.ArrayList)2 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)2 WhitespaceAnalyzer (org.apache.lucene.analysis.core.WhitespaceAnalyzer)1 Document (org.apache.lucene.document.Document)1 LongPoint (org.apache.lucene.document.LongPoint)1 TextField (org.apache.lucene.document.TextField)1 MemoryIndex (org.apache.lucene.index.memory.MemoryIndex)1 BoostingQuery (org.apache.lucene.queries.BoostingQuery)1 CommonTermsQuery (org.apache.lucene.queries.CommonTermsQuery)1 BoostQuery (org.apache.lucene.search.BoostQuery)1 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)1 SynonymQuery (org.apache.lucene.search.SynonymQuery)1 TermQuery (org.apache.lucene.search.TermQuery)1 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)1 SpanNotQuery (org.apache.lucene.search.spans.SpanNotQuery)1 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)1