Search in sources :

Example 1 with SpanFirstQuery

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

the class QueryAnalyzerTests method testExtractQueryMetadata_spanFirstQuery.

public void testExtractQueryMetadata_spanFirstQuery() {
    SpanTermQuery spanTermQuery1 = new SpanTermQuery(new Term("_field", "_short_term"));
    SpanFirstQuery spanFirstQuery = new SpanFirstQuery(spanTermQuery1, 20);
    Result result = analyze(spanFirstQuery);
    assertThat(result.verified, is(false));
    assertTermsEqual(result.terms, spanTermQuery1.getTerm());
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanFirstQuery(org.apache.lucene.search.spans.SpanFirstQuery) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm) Result(org.elasticsearch.percolator.QueryAnalyzer.Result)

Example 2 with SpanFirstQuery

use of org.apache.lucene.search.spans.SpanFirstQuery 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 3 with SpanFirstQuery

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

the class TestPayloadSpans method testSpanFirst.

public void testSpanFirst() throws IOException {
    SpanQuery match;
    SpanFirstQuery sfq;
    match = new SpanTermQuery(new Term(PayloadHelper.FIELD, "one"));
    sfq = new SpanFirstQuery(match, 2);
    Spans spans = sfq.createWeight(searcher, false, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.PAYLOADS);
    checkSpans(spans, 109, 1, 1, 1);
    //Test more complicated subclause
    SpanQuery[] clauses = new SpanQuery[2];
    clauses[0] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "one"));
    clauses[1] = new SpanTermQuery(new Term(PayloadHelper.FIELD, "hundred"));
    match = new SpanNearQuery(clauses, 0, true);
    sfq = new SpanFirstQuery(match, 2);
    checkSpans(sfq.createWeight(searcher, false, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.PAYLOADS), 100, 2, 1, 1);
    match = new SpanNearQuery(clauses, 0, false);
    sfq = new SpanFirstQuery(match, 2);
    checkSpans(sfq.createWeight(searcher, false, 1f).getSpans(searcher.getIndexReader().leaves().get(0), SpanWeight.Postings.PAYLOADS), 100, 2, 1, 1);
}
Also used : 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) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Spans(org.apache.lucene.search.spans.Spans)

Example 4 with SpanFirstQuery

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

the class SpanFirstBuilder method getSpanQuery.

@Override
public SpanQuery getSpanQuery(Element e) throws ParserException {
    int end = DOMUtils.getAttribute(e, "end", 1);
    Element child = DOMUtils.getFirstChildElement(e);
    SpanQuery q = factory.getSpanQuery(child);
    SpanFirstQuery sfq = new SpanFirstQuery(q, end);
    float boost = DOMUtils.getAttribute(e, "boost", 1.0f);
    return new SpanBoostQuery(sfq, boost);
}
Also used : SpanFirstQuery(org.apache.lucene.search.spans.SpanFirstQuery) Element(org.w3c.dom.Element) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 5 with SpanFirstQuery

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

the class TestUnifiedHighlighterMTQ method testSpanPositionCheck.

public void testSpanPositionCheck() throws Exception {
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);
    Field body = new Field("body", "", fieldType);
    Document doc = new Document();
    doc.add(body);
    body.setStringValue("This is a test.");
    iw.addDocument(doc);
    body.setStringValue("Test a one sentence document.");
    iw.addDocument(doc);
    IndexReader ir = iw.getReader();
    iw.close();
    IndexSearcher searcher = newSearcher(ir);
    UnifiedHighlighter highlighter = new UnifiedHighlighter(searcher, indexAnalyzer);
    SpanQuery childQuery = new SpanMultiTermQueryWrapper<>(new WildcardQuery(new Term("body", "te*")));
    Query query = new SpanFirstQuery(childQuery, 1000000);
    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    assertEquals(2, topDocs.totalHits);
    String[] snippets = highlighter.highlight("body", query, topDocs);
    assertEquals(2, snippets.length);
    assertEquals("This is a <b>test</b>.", snippets[0]);
    assertEquals("<b>Test</b> a one sentence document.", snippets[1]);
    ir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) WildcardQuery(org.apache.lucene.search.WildcardQuery) SpanMultiTermQueryWrapper(org.apache.lucene.search.spans.SpanMultiTermQueryWrapper) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) SpanFirstQuery(org.apache.lucene.search.spans.SpanFirstQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) SpanNotQuery(org.apache.lucene.search.spans.SpanNotQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanFirstQuery(org.apache.lucene.search.spans.SpanFirstQuery) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) SpanQuery(org.apache.lucene.search.spans.SpanQuery) TopDocs(org.apache.lucene.search.TopDocs) Field(org.apache.lucene.document.Field) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Aggregations

SpanFirstQuery (org.apache.lucene.search.spans.SpanFirstQuery)5 Term (org.apache.lucene.index.Term)4 SpanQuery (org.apache.lucene.search.spans.SpanQuery)4 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)4 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)3 Query (org.apache.lucene.search.Query)2 WildcardQuery (org.apache.lucene.search.WildcardQuery)2 SpanBoostQuery (org.apache.lucene.search.spans.SpanBoostQuery)2 Document (org.apache.lucene.document.Document)1 Field (org.apache.lucene.document.Field)1 IndexReader (org.apache.lucene.index.IndexReader)1 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 BoostQuery (org.apache.lucene.search.BoostQuery)1 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)1 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)1 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)1