Search in sources :

Example 1 with SpanNearQuery

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

the class CustomUnifiedHighlighter method rewriteCustomQuery.

/**
     * Translate custom queries in queries that are supported by the unified highlighter.
     */
private Collection<Query> rewriteCustomQuery(Query query) {
    if (query instanceof MultiPhrasePrefixQuery) {
        MultiPhrasePrefixQuery mpq = (MultiPhrasePrefixQuery) query;
        Term[][] terms = mpq.getTerms();
        int[] positions = mpq.getPositions();
        SpanQuery[] positionSpanQueries = new SpanQuery[positions.length];
        int sizeMinus1 = terms.length - 1;
        for (int i = 0; i < positions.length; i++) {
            SpanQuery[] innerQueries = new SpanQuery[terms[i].length];
            for (int j = 0; j < terms[i].length; j++) {
                if (i == sizeMinus1) {
                    innerQueries[j] = new SpanMultiTermQueryWrapper(new PrefixQuery(terms[i][j]));
                } else {
                    innerQueries[j] = new SpanTermQuery(terms[i][j]);
                }
            }
            if (innerQueries.length > 1) {
                positionSpanQueries[i] = new SpanOrQuery(innerQueries);
            } else {
                positionSpanQueries[i] = innerQueries[0];
            }
        }
        // sum position increments beyond 1
        int positionGaps = 0;
        if (positions.length >= 2) {
            // positions are in increasing order.   max(0,...) is just a safeguard.
            positionGaps = Math.max(0, positions[positions.length - 1] - positions[0] - positions.length + 1);
        }
        //if original slop is 0 then require inOrder
        boolean inorder = (mpq.getSlop() == 0);
        return Collections.singletonList(new SpanNearQuery(positionSpanQueries, mpq.getSlop() + positionGaps, inorder));
    } else if (query instanceof CommonTermsQuery) {
        CommonTermsQuery ctq = (CommonTermsQuery) query;
        List<Query> tqs = new ArrayList<>();
        for (Term term : ctq.getTerms()) {
            tqs.add(new TermQuery(term));
        }
        return tqs;
    } else if (query instanceof AllTermQuery) {
        AllTermQuery atq = (AllTermQuery) query;
        return Collections.singletonList(new TermQuery(atq.getTerm()));
    } else if (query instanceof FunctionScoreQuery) {
        return Collections.singletonList(((FunctionScoreQuery) query).getSubQuery());
    } else if (query instanceof FiltersFunctionScoreQuery) {
        return Collections.singletonList(((FiltersFunctionScoreQuery) query).getSubQuery());
    } else {
        return null;
    }
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) TermQuery(org.apache.lucene.search.TermQuery) SpanMultiTermQueryWrapper(org.apache.lucene.search.spans.SpanMultiTermQueryWrapper) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) Term(org.apache.lucene.index.Term) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) CommonTermsQuery(org.apache.lucene.queries.CommonTermsQuery) FiltersFunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) ArrayList(java.util.ArrayList) List(java.util.List) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery)

Example 2 with SpanNearQuery

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

the class SpanNearQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    SpanQuery[] spanQueries = new SpanQuery[clauses.size()];
    for (int i = 0; i < clauses.size(); i++) {
        Query query = clauses.get(i).toQuery(context);
        assert query instanceof SpanQuery;
        spanQueries[i] = (SpanQuery) query;
    }
    return new SpanNearQuery(spanQueries, slop, inOrder);
}
Also used : Query(org.apache.lucene.search.Query) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 3 with SpanNearQuery

use of org.apache.lucene.search.spans.SpanNearQuery in project languagetool by languagetool-org.

the class PatternRuleQueryBuilder method makeQuery.

private BooleanClause makeQuery(PatternToken patternToken) throws UnsupportedPatternRuleException {
    checkUnsupportedElement(patternToken);
    String termStr = patternToken.getString();
    String pos = patternToken.getPOStag();
    BooleanClause termQuery = getTermQueryOrNull(patternToken, termStr);
    BooleanClause posQuery = getPosQueryOrNull(patternToken, pos);
    if (termQuery != null && posQuery != null) {
        // if both term and POS are set, we create a query where both are at the same position
        if (mustOccur(termQuery) && mustOccur(posQuery)) {
            SpanQuery spanQueryForTerm = asSpanQuery(termQuery);
            SpanQuery spanQueryForPos = asSpanQuery(posQuery);
            SpanQuery[] spanClauses = { spanQueryForTerm, spanQueryForPos };
            return new BooleanClause(new SpanNearQuery(spanClauses, 0, false), BooleanClause.Occur.MUST);
        } else {
            // should not happen, we always use Occur.MUST:
            throw new UnsupportedPatternRuleException("Term/POS combination not supported yet: " + patternToken);
        }
    } else if (termQuery != null) {
        return termQuery;
    } else if (posQuery != null) {
        return posQuery;
    }
    throw new UnsupportedPatternRuleException("Neither POS tag nor term set for element: " + patternToken);
}
Also used : SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 4 with SpanNearQuery

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

the class EnwikiQueryMaker method getPrebuiltQueries.

private static Query[] getPrebuiltQueries(String field) {
    WildcardQuery wcq = new WildcardQuery(new Term(field, "fo*"));
    wcq.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE);
    // be wary of unanalyzed text
    return new Query[] { new SpanFirstQuery(new SpanTermQuery(new Term(field, "ford")), 5), new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term(field, "night")), new SpanTermQuery(new Term(field, "trading")) }, 4, false), new SpanNearQuery(new SpanQuery[] { new SpanFirstQuery(new SpanTermQuery(new Term(field, "ford")), 10), new SpanTermQuery(new Term(field, "credit")) }, 10, false), wcq };
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) Query(org.apache.lucene.search.Query) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanFirstQuery(org.apache.lucene.search.spans.SpanFirstQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanFirstQuery(org.apache.lucene.search.spans.SpanFirstQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) Term(org.apache.lucene.index.Term) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery)

Example 5 with SpanNearQuery

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

the class TestQueryRescorer method testBasic.

public void testBasic() throws Exception {
    Directory dir = newDirectory();
    RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig());
    Document doc = new Document();
    doc.add(newStringField("id", "0", Field.Store.YES));
    doc.add(newTextField("field", "wizard the the the the the oz", Field.Store.NO));
    w.addDocument(doc);
    doc = new Document();
    doc.add(newStringField("id", "1", Field.Store.YES));
    // 1 extra token, but wizard and oz are close;
    doc.add(newTextField("field", "wizard oz the the the the the the", Field.Store.NO));
    w.addDocument(doc);
    IndexReader r = w.getReader();
    w.close();
    // Do ordinary BooleanQuery:
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.add(new TermQuery(new Term("field", "wizard")), Occur.SHOULD);
    bq.add(new TermQuery(new Term("field", "oz")), Occur.SHOULD);
    IndexSearcher searcher = getSearcher(r);
    searcher.setSimilarity(new ClassicSimilarity());
    TopDocs hits = searcher.search(bq.build(), 10);
    assertEquals(2, hits.totalHits);
    assertEquals("0", searcher.doc(hits.scoreDocs[0].doc).get("id"));
    assertEquals("1", searcher.doc(hits.scoreDocs[1].doc).get("id"));
    // Now, resort using PhraseQuery:
    PhraseQuery pq = new PhraseQuery(5, "field", "wizard", "oz");
    TopDocs hits2 = QueryRescorer.rescore(searcher, hits, pq, 2.0, 10);
    // Resorting changed the order:
    assertEquals(2, hits2.totalHits);
    assertEquals("1", searcher.doc(hits2.scoreDocs[0].doc).get("id"));
    assertEquals("0", searcher.doc(hits2.scoreDocs[1].doc).get("id"));
    // Resort using SpanNearQuery:
    SpanTermQuery t1 = new SpanTermQuery(new Term("field", "wizard"));
    SpanTermQuery t2 = new SpanTermQuery(new Term("field", "oz"));
    SpanNearQuery snq = new SpanNearQuery(new SpanQuery[] { t1, t2 }, 0, true);
    TopDocs hits3 = QueryRescorer.rescore(searcher, hits, snq, 2.0, 10);
    // Resorting changed the order:
    assertEquals(2, hits3.totalHits);
    assertEquals("1", searcher.doc(hits3.scoreDocs[0].doc).get("id"));
    assertEquals("0", searcher.doc(hits3.scoreDocs[1].doc).get("id"));
    r.close();
    dir.close();
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) IndexReader(org.apache.lucene.index.IndexReader) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Aggregations

SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)55 Term (org.apache.lucene.index.Term)49 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)49 SpanQuery (org.apache.lucene.search.spans.SpanQuery)38 BooleanQuery (org.apache.lucene.search.BooleanQuery)14 Query (org.apache.lucene.search.Query)14 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)14 Document (org.apache.lucene.document.Document)13 IndexReader (org.apache.lucene.index.IndexReader)13 ArrayList (java.util.ArrayList)12 IndexSearcher (org.apache.lucene.search.IndexSearcher)12 TopDocs (org.apache.lucene.search.TopDocs)11 Directory (org.apache.lucene.store.Directory)11 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)10 TextField (org.apache.lucene.document.TextField)9 PhraseQuery (org.apache.lucene.search.PhraseQuery)8 PrefixQuery (org.apache.lucene.search.PrefixQuery)8 TermQuery (org.apache.lucene.search.TermQuery)8 BytesRef (org.apache.lucene.util.BytesRef)8 SpanNotQuery (org.apache.lucene.search.spans.SpanNotQuery)7