Search in sources :

Example 21 with Query

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

the class SpanNotQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    Query includeQuery = this.include.toQuery(context);
    assert includeQuery instanceof SpanQuery;
    Query excludeQuery = this.exclude.toQuery(context);
    assert excludeQuery instanceof SpanQuery;
    return new SpanNotQuery((SpanQuery) includeQuery, (SpanQuery) excludeQuery, pre, post);
}
Also used : Query(org.apache.lucene.search.Query) SpanNotQuery(org.apache.lucene.search.spans.SpanNotQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNotQuery(org.apache.lucene.search.spans.SpanNotQuery)

Example 22 with Query

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

the class SpanTermQueryBuilder method doToQuery.

@Override
protected SpanQuery doToQuery(QueryShardContext context) throws IOException {
    MappedFieldType mapper = context.fieldMapper(fieldName);
    Term term;
    if (mapper == null) {
        term = new Term(fieldName, BytesRefs.toBytesRef(value));
    } else {
        Query termQuery = mapper.termQuery(value, context);
        term = MappedFieldType.extractTerm(termQuery);
    }
    return new SpanTermQuery(term);
}
Also used : Query(org.apache.lucene.search.Query) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Term(org.apache.lucene.index.Term)

Example 23 with Query

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

the class CustomUnifiedHighlighterTests method testSimple.

public void testSimple() throws Exception {
    final String[] inputs = { "This is a test. Just a test1 highlighting from unified highlighter.", "This is the second highlighting value to perform highlighting on a longer text that gets scored lower.", "This is highlighting the third short highlighting value.", "Just a test4 highlighting from unified highlighter." };
    String[] expectedPassages = { "Just a test1 <b>highlighting</b> from unified highlighter.", "This is the second <b>highlighting</b> value to perform <b>highlighting</b> on a" + " longer text that gets scored lower.", "This is <b>highlighting</b> the third short <b>highlighting</b> value.", "Just a test4 <b>highlighting</b> from unified highlighter." };
    Query query = new TermQuery(new Term("text", "highlighting"));
    assertHighlightOneDoc("text", inputs, new StandardAnalyzer(), query, Locale.ROOT, BreakIterator.getSentenceInstance(Locale.ROOT), 0, expectedPassages);
}
Also used : AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) CommonTermsQuery(org.apache.lucene.queries.CommonTermsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Term(org.apache.lucene.index.Term)

Example 24 with Query

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

the class CustomUnifiedHighlighterTests method testRepeat.

public void testRepeat() throws Exception {
    final String[] inputs = { "Fun  fun fun  fun  fun  fun  fun  fun  fun  fun" };
    final String[] outputs = { "<b>Fun</b>  <b>fun</b> <b>fun</b>", "<b>fun</b>  <b>fun</b>", "<b>fun</b>  <b>fun</b>  <b>fun</b>", "<b>fun</b>  <b>fun</b>" };
    Query query = new TermQuery(new Term("text", "fun"));
    assertHighlightOneDoc("text", inputs, new StandardAnalyzer(), query, Locale.ROOT, BoundedBreakIteratorScanner.getSentence(Locale.ROOT, 10), 0, outputs);
    query = new PhraseQuery.Builder().add(new Term("text", "fun")).add(new Term("text", "fun")).build();
    assertHighlightOneDoc("text", inputs, new StandardAnalyzer(), query, Locale.ROOT, BoundedBreakIteratorScanner.getSentence(Locale.ROOT, 10), 0, outputs);
}
Also used : AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) CommonTermsQuery(org.apache.lucene.queries.CommonTermsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Term(org.apache.lucene.index.Term)

Example 25 with Query

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

the class CustomPostingsHighlighterTests method testCustomPostingsHighlighter.

public void testCustomPostingsHighlighter() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()));
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);
    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
    //good position but only one match
    final String firstValue = "This is a test. Just a test1 highlighting from postings highlighter.";
    Field body = new Field("body", "", offsetsType);
    Document doc = new Document();
    doc.add(body);
    body.setStringValue(firstValue);
    //two matches, not the best snippet due to its length though
    final String secondValue = "This is the second highlighting value to perform highlighting on a longer text that gets scored lower.";
    Field body2 = new Field("body", "", offsetsType);
    doc.add(body2);
    body2.setStringValue(secondValue);
    //two matches and short, will be scored highest
    final String thirdValue = "This is highlighting the third short highlighting value.";
    Field body3 = new Field("body", "", offsetsType);
    doc.add(body3);
    body3.setStringValue(thirdValue);
    //one match, same as first but at the end, will be scored lower due to its position
    final String fourthValue = "Just a test4 highlighting from postings highlighter.";
    Field body4 = new Field("body", "", offsetsType);
    doc.add(body4);
    body4.setStringValue(fourthValue);
    iw.addDocument(doc);
    IndexReader ir = iw.getReader();
    iw.close();
    String firstHlValue = "Just a test1 <b>highlighting</b> from postings highlighter.";
    String secondHlValue = "This is the second <b>highlighting</b> value to perform <b>highlighting</b> on a longer text that gets scored lower.";
    String thirdHlValue = "This is <b>highlighting</b> the third short <b>highlighting</b> value.";
    String fourthHlValue = "Just a test4 <b>highlighting</b> from postings highlighter.";
    IndexSearcher searcher = newSearcher(ir);
    Query query = new TermQuery(new Term("body", "highlighting"));
    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    assertThat(topDocs.totalHits, equalTo(1));
    int docId = topDocs.scoreDocs[0].doc;
    String fieldValue = firstValue + HighlightUtils.PARAGRAPH_SEPARATOR + secondValue + HighlightUtils.PARAGRAPH_SEPARATOR + thirdValue + HighlightUtils.PARAGRAPH_SEPARATOR + fourthValue;
    CustomPostingsHighlighter highlighter = new CustomPostingsHighlighter(null, new CustomPassageFormatter("<b>", "</b>", new DefaultEncoder()), fieldValue, false);
    Snippet[] snippets = highlighter.highlightField("body", query, searcher, docId, 5);
    assertThat(snippets.length, equalTo(4));
    assertThat(snippets[0].getText(), equalTo(firstHlValue));
    assertThat(snippets[1].getText(), equalTo(secondHlValue));
    assertThat(snippets[2].getText(), equalTo(thirdHlValue));
    assertThat(snippets[3].getText(), equalTo(fourthHlValue));
    ir.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) Snippet(org.apache.lucene.search.highlight.Snippet) Document(org.apache.lucene.document.Document) FieldType(org.apache.lucene.document.FieldType) TopDocs(org.apache.lucene.search.TopDocs) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) DefaultEncoder(org.apache.lucene.search.highlight.DefaultEncoder) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

Query (org.apache.lucene.search.Query)1371 BooleanQuery (org.apache.lucene.search.BooleanQuery)663 TermQuery (org.apache.lucene.search.TermQuery)633 Term (org.apache.lucene.index.Term)395 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)359 BoostQuery (org.apache.lucene.search.BoostQuery)284 IndexSearcher (org.apache.lucene.search.IndexSearcher)283 Test (org.junit.Test)258 PhraseQuery (org.apache.lucene.search.PhraseQuery)247 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)234 TopDocs (org.apache.lucene.search.TopDocs)224 Document (org.apache.lucene.document.Document)216 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)205 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)194 PrefixQuery (org.apache.lucene.search.PrefixQuery)188 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)174 IndexReader (org.apache.lucene.index.IndexReader)167 RegexpQuery (org.apache.lucene.search.RegexpQuery)159 WildcardQuery (org.apache.lucene.search.WildcardQuery)141 ArrayList (java.util.ArrayList)139