Search in sources :

Example 51 with PrefixQuery

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

the class MyCrazyCustomField method getPrefixQuery.

@Override
public Query getPrefixQuery(QParser parser, SchemaField sf, String termStr) {
    if (termStr.equals("foo")) {
        termStr = "bar";
    } else if (termStr.equals("bar")) {
        termStr = "foo";
    }
    PrefixQuery query = new PrefixQuery(new Term(sf.getName(), termStr));
    query.setRewriteMethod(sf.getType().getRewriteMethod(parser, sf));
    return query;
}
Also used : PrefixQuery(org.apache.lucene.search.PrefixQuery) Term(org.apache.lucene.index.Term)

Example 52 with PrefixQuery

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

the class TestExitableDirectoryReader method testExitableFilterIndexReader.

/**
   * Tests timing out of TermsEnum iterations
   * @throws Exception on error
   */
@Ignore("this test relies on wall clock time and sometimes false fails")
public void testExitableFilterIndexReader() throws Exception {
    Directory directory = newDirectory();
    IndexWriter writer = new IndexWriter(directory, newIndexWriterConfig(new MockAnalyzer(random())));
    Document d1 = new Document();
    d1.add(newTextField("default", "one two", Field.Store.YES));
    writer.addDocument(d1);
    Document d2 = new Document();
    d2.add(newTextField("default", "one three", Field.Store.YES));
    writer.addDocument(d2);
    Document d3 = new Document();
    d3.add(newTextField("default", "ones two four", Field.Store.YES));
    writer.addDocument(d3);
    writer.forceMerge(1);
    writer.commit();
    writer.close();
    DirectoryReader directoryReader;
    DirectoryReader exitableDirectoryReader;
    IndexReader reader;
    IndexSearcher searcher;
    Query query = new PrefixQuery(new Term("default", "o"));
    // Set a fairly high timeout value (1 second) and expect the query to complete in that time frame.
    // Not checking the validity of the result, all we are bothered about in this test is the timing out.
    directoryReader = DirectoryReader.open(directory);
    exitableDirectoryReader = new ExitableDirectoryReader(directoryReader, new QueryTimeoutImpl(1000));
    reader = new TestReader(getOnlyLeafReader(exitableDirectoryReader));
    searcher = new IndexSearcher(reader);
    searcher.search(query, 10);
    reader.close();
    // Set a really low timeout value (1 millisecond) and expect an Exception
    directoryReader = DirectoryReader.open(directory);
    exitableDirectoryReader = new ExitableDirectoryReader(directoryReader, new QueryTimeoutImpl(1));
    reader = new TestReader(getOnlyLeafReader(exitableDirectoryReader));
    IndexSearcher slowSearcher = new IndexSearcher(reader);
    expectThrows(ExitingReaderException.class, () -> {
        slowSearcher.search(query, 10);
    });
    reader.close();
    // Set maximum time out and expect the query to complete. 
    // Not checking the validity of the result, all we are bothered about in this test is the timing out.
    directoryReader = DirectoryReader.open(directory);
    exitableDirectoryReader = new ExitableDirectoryReader(directoryReader, new QueryTimeoutImpl(Long.MAX_VALUE));
    reader = new TestReader(getOnlyLeafReader(exitableDirectoryReader));
    searcher = new IndexSearcher(reader);
    searcher.search(query, 10);
    reader.close();
    // Set a negative time allowed and expect the query to complete (should disable timeouts)
    // Not checking the validity of the result, all we are bothered about in this test is the timing out.
    directoryReader = DirectoryReader.open(directory);
    exitableDirectoryReader = new ExitableDirectoryReader(directoryReader, new QueryTimeoutImpl(-189034L));
    reader = new TestReader(getOnlyLeafReader(exitableDirectoryReader));
    searcher = new IndexSearcher(reader);
    searcher.search(query, 10);
    reader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) PrefixQuery(org.apache.lucene.search.PrefixQuery) Document(org.apache.lucene.document.Document) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) PrefixQuery(org.apache.lucene.search.PrefixQuery) Directory(org.apache.lucene.store.Directory) Ignore(org.junit.Ignore)

Example 53 with PrefixQuery

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

the class TestUnifiedHighlighterStrictPhrases method testRewriteAndMtq.

/**
   * Test it does *not* highlight the same term's not next to the span-near.  "charlie" in this case.
   * This particular example exercises "Rewrite" plus "MTQ" in the same query.
   */
public void testRewriteAndMtq() throws IOException {
    indexWriter.addDocument(newDoc("alpha bravo charlie - charlie bravo alpha"));
    initReaderSearcherHighlighter();
    SpanNearQuery snq = new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term("body", "bravo")), // REWRITES
    new SpanMultiTermQueryWrapper<>(new PrefixQuery(new Term("body", "ch"))) }, 0, true);
    BooleanQuery query = new BooleanQuery.Builder().add(snq, BooleanClause.Occur.MUST).add(new PrefixQuery(new Term("body", "al")), // MTQ
    BooleanClause.Occur.MUST).add(newPhraseQuery("body", "alpha bravo"), BooleanClause.Occur.MUST).add(newPhraseQuery("title", "bravo alpha"), BooleanClause.Occur.SHOULD).build();
    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    String[] snippets = highlighter.highlight("body", query, topDocs);
    assertArrayEquals(new String[] { "<b>alpha</b> <b>bravo</b> <b>charlie</b> - charlie bravo <b>alpha</b>" }, snippets);
    // do again, this time with MTQ disabled.  We should only find "alpha bravo".
    //disable but leave phrase processing enabled
    highlighter.setHandleMultiTermQuery(false);
    topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    snippets = highlighter.highlight("body", query, topDocs);
    assertArrayEquals(new String[] { "<b>alpha</b> <b>bravo</b> charlie - charlie bravo alpha" }, snippets);
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) Term(org.apache.lucene.index.Term) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery)

Example 54 with PrefixQuery

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

the class TestUnifiedHighlighterStrictPhrases method testRewrite.

/**
   * Like {@link #testRewriteAndMtq} but no freestanding MTQ
   */
public void testRewrite() throws IOException {
    indexWriter.addDocument(newDoc("alpha bravo charlie - charlie bravo alpha"));
    initReaderSearcherHighlighter();
    SpanNearQuery snq = new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term("body", "bravo")), // REWRITES
    new SpanMultiTermQueryWrapper<>(new PrefixQuery(new Term("body", "ch"))) }, 0, true);
    BooleanQuery query = new BooleanQuery.Builder().add(snq, BooleanClause.Occur.MUST).add(newPhraseQuery("body", "alpha bravo"), BooleanClause.Occur.MUST).add(newPhraseQuery("title", "bravo alpha"), BooleanClause.Occur.SHOULD).build();
    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    String[] snippets = highlighter.highlight("body", query, topDocs);
    assertArrayEquals(new String[] { "<b>alpha</b> <b>bravo</b> <b>charlie</b> - charlie bravo alpha" }, snippets);
    // do again, this time with MTQ disabled.  We should only find "alpha bravo".
    //disable but leave phrase processing enabled
    highlighter.setHandleMultiTermQuery(false);
    topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    snippets = highlighter.highlight("body", query, topDocs);
    assertArrayEquals(new String[] { "<b>alpha</b> <b>bravo</b> charlie - charlie bravo alpha" }, snippets);
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) QueryBuilder(org.apache.lucene.util.QueryBuilder) Term(org.apache.lucene.index.Term) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery)

Example 55 with PrefixQuery

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

the class TestUnifiedHighlighterStrictPhrases method testMultiValued.

public void testMultiValued() throws IOException {
    indexWriter.addDocument(newDoc("one bravo three", "four bravo six"));
    initReaderSearcherHighlighter();
    BooleanQuery query = new BooleanQuery.Builder().add(newPhraseQuery("body", "one bravo"), BooleanClause.Occur.MUST).add(newPhraseQuery("body", "four bravo"), BooleanClause.Occur.MUST).add(new PrefixQuery(new Term("body", "br")), BooleanClause.Occur.MUST).build();
    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    String[] snippets = highlighter.highlight("body", query, topDocs, 2);
    assertArrayEquals(new String[] { "<b>one</b> <b>bravo</b> three... <b>four</b> <b>bravo</b> six" }, snippets);
    // now test phraseQuery won't span across values
    assert indexAnalyzer.getPositionIncrementGap("body") > 0;
    PhraseQuery phraseQuery = newPhraseQuery("body", "three four");
    // 1 too little; won't span
    phraseQuery = setSlop(phraseQuery, indexAnalyzer.getPositionIncrementGap("body") - 1);
    query = new BooleanQuery.Builder().add(new TermQuery(new Term("body", "bravo")), BooleanClause.Occur.MUST).add(phraseQuery, BooleanClause.Occur.SHOULD).build();
    topDocs = searcher.search(query, 10);
    snippets = highlighter.highlight("body", query, topDocs, 2);
    assertEquals("one <b>bravo</b> three... four <b>bravo</b> six", snippets[0]);
    // and add just enough slop to cross the values:
    phraseQuery = newPhraseQuery("body", "three four");
    // just enough to span
    phraseQuery = setSlop(phraseQuery, indexAnalyzer.getPositionIncrementGap("body"));
    query = new BooleanQuery.Builder().add(new TermQuery(new Term("body", "bravo")), BooleanClause.Occur.MUST).add(phraseQuery, // must match and it will
    BooleanClause.Occur.MUST).build();
    topDocs = searcher.search(query, 10);
    assertEquals(1, topDocs.totalHits);
    snippets = highlighter.highlight("body", query, topDocs, 2);
    assertEquals("one <b>bravo</b> <b>three</b>... <b>four</b> <b>bravo</b> six", snippets[0]);
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) QueryBuilder(org.apache.lucene.util.QueryBuilder) Term(org.apache.lucene.index.Term)

Aggregations

PrefixQuery (org.apache.lucene.search.PrefixQuery)68 Term (org.apache.lucene.index.Term)62 BooleanQuery (org.apache.lucene.search.BooleanQuery)34 Query (org.apache.lucene.search.Query)30 TermQuery (org.apache.lucene.search.TermQuery)29 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)27 WildcardQuery (org.apache.lucene.search.WildcardQuery)23 BoostQuery (org.apache.lucene.search.BoostQuery)20 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)19 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)15 Document (org.apache.lucene.document.Document)14 IndexSearcher (org.apache.lucene.search.IndexSearcher)14 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)14 PhraseQuery (org.apache.lucene.search.PhraseQuery)14 RegexpQuery (org.apache.lucene.search.RegexpQuery)13 TopDocs (org.apache.lucene.search.TopDocs)13 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)12 IndexReader (org.apache.lucene.index.IndexReader)11 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)11 Field (org.apache.lucene.document.Field)10