Search in sources :

Example 6 with SpanNearQuery

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

the class QueryBuilder method analyzeGraphPhrase.

/**
   * Creates a span near (phrase) query from a graph token stream. The articulation points of the graph are visited in
   * order and the queries created at each point are merged in the returned near query.
   */
protected SpanQuery analyzeGraphPhrase(TokenStream source, String field, int phraseSlop) throws IOException {
    source.reset();
    GraphTokenStreamFiniteStrings graph = new GraphTokenStreamFiniteStrings(source);
    List<SpanQuery> clauses = new ArrayList<>();
    int[] articulationPoints = graph.articulationPoints();
    int lastState = 0;
    for (int i = 0; i <= articulationPoints.length; i++) {
        int start = lastState;
        int end = -1;
        if (i < articulationPoints.length) {
            end = articulationPoints[i];
        }
        lastState = end;
        final SpanQuery queryPos;
        if (graph.hasSidePath(start)) {
            List<SpanQuery> queries = new ArrayList<>();
            Iterator<TokenStream> it = graph.getFiniteStrings(start, end);
            while (it.hasNext()) {
                TokenStream ts = it.next();
                SpanQuery q = createSpanQuery(ts, field);
                if (q != null) {
                    queries.add(q);
                }
            }
            if (queries.size() > 0) {
                queryPos = new SpanOrQuery(queries.toArray(new SpanQuery[0]));
            } else {
                queryPos = null;
            }
        } else {
            Term[] terms = graph.getTerms(field, start);
            assert terms.length > 0;
            if (terms.length == 1) {
                queryPos = new SpanTermQuery(terms[0]);
            } else {
                SpanTermQuery[] orClauses = new SpanTermQuery[terms.length];
                for (int idx = 0; idx < terms.length; idx++) {
                    orClauses[idx] = new SpanTermQuery(terms[idx]);
                }
                queryPos = new SpanOrQuery(orClauses);
            }
        }
        if (queryPos != null) {
            clauses.add(queryPos);
        }
    }
    if (clauses.isEmpty()) {
        return null;
    } else if (clauses.size() == 1) {
        return clauses.get(0);
    } else {
        return new SpanNearQuery(clauses.toArray(new SpanQuery[0]), phraseSlop, true);
    }
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) GraphTokenStreamFiniteStrings(org.apache.lucene.util.graph.GraphTokenStreamFiniteStrings) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery)

Example 7 with SpanNearQuery

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

the class TestSolrCoreParser method testHandySpanQuery.

// test custom query (HandyQueryBuilder) wrapping a SpanQuery
public void testHandySpanQuery() throws IOException, ParserException {
    final String lhsXml = "<SpanOr fieldName='contents'>" + "<SpanTerm>rain</SpanTerm>" + "<SpanTerm>spain</SpanTerm>" + "<SpanTerm>plain</SpanTerm>" + "</SpanOr>";
    final String rhsXml = "<SpanNear fieldName='contents' slop='2' inOrder='true'>" + "<SpanTerm>sunny</SpanTerm>" + "<SpanTerm>sky</SpanTerm>" + "</SpanNear>";
    final Query query = parseHandyQuery(lhsXml, rhsXml);
    final BooleanQuery bq = (BooleanQuery) query;
    assertEquals(2, bq.clauses().size());
    for (int ii = 0; ii < bq.clauses().size(); ++ii) {
        final Query clauseQuery = bq.clauses().get(ii).getQuery();
        switch(ii) {
            case 0:
                assertTrue(unwrapSpanBoostQuery(clauseQuery) instanceof SpanOrQuery);
                break;
            case 1:
                assertTrue(unwrapSpanBoostQuery(clauseQuery) instanceof SpanNearQuery);
                break;
            default:
                fail("unexpected clause index " + ii);
        }
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery)

Example 8 with SpanNearQuery

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

the class ApacheLuceneSolrNearQueryBuilder method getSpanQuery.

public SpanQuery getSpanQuery(Element e) throws ParserException {
    final String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
    final SpanQuery[] spanQueries = new SpanQuery[] { new SpanTermQuery(new Term(fieldName, "Apache")), new SpanTermQuery(new Term(fieldName, "Lucene")), new SpanTermQuery(new Term(fieldName, "Solr")) };
    final int slop = 42;
    final boolean inOrder = false;
    return new SpanNearQuery(spanQueries, slop, inOrder);
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) Term(org.apache.lucene.index.Term) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 9 with SpanNearQuery

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

the class SynonymTokenizer method testSpanHighlighting.

public void testSpanHighlighting() throws Exception {
    Query query1 = new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term(FIELD_NAME, "wordx")), new SpanTermQuery(new Term(FIELD_NAME, "wordy")) }, 1, false);
    Query query2 = new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term(FIELD_NAME, "wordy")), new SpanTermQuery(new Term(FIELD_NAME, "wordc")) }, 1, false);
    BooleanQuery.Builder bquery = new BooleanQuery.Builder();
    bquery.add(query1, Occur.SHOULD);
    bquery.add(query2, Occur.SHOULD);
    doSearching(bquery.build());
    TestHighlightRunner helper = new TestHighlightRunner() {

        @Override
        public void run() throws Exception {
            mode = QUERY;
            doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
        }
    };
    helper.run();
    assertTrue("Failed to find correct number of highlights " + numHighlights + " found", numHighlights == 7);
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TestHighlightRunner(org.apache.lucene.search.highlight.SynonymTokenizer.TestHighlightRunner) 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) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) Builder(org.apache.lucene.search.PhraseQuery.Builder) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Term(org.apache.lucene.index.Term) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery)

Example 10 with SpanNearQuery

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

the class MissesTest method testSpanNearQuery.

public void testSpanNearQuery() throws IOException, InvalidTokenOffsetsException {
    try (Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)) {
        final Query query = new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term("test", "foo")), new SpanTermQuery(new Term("test", "bar")) }, 0, true);
        final Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter(), new QueryScorer(query));
        assertEquals("this is a <B>foo</B> <B>bar</B> example", highlighter.getBestFragment(analyzer, "test", "this is a foo bar example"));
        assertNull(highlighter.getBestFragment(analyzer, "test", "this does not match"));
    }
}
Also used : MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Query(org.apache.lucene.search.Query) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) Term(org.apache.lucene.index.Term) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery)

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