Search in sources :

Example 36 with PhraseQuery

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

the class TestQPHelper method testCJKSloppyPhrase.

public void testCJKSloppyPhrase() throws Exception {
    // individual CJK chars as terms
    SimpleCJKAnalyzer analyzer = new SimpleCJKAnalyzer();
    PhraseQuery expected = new PhraseQuery(3, "field", "中", "国");
    assertEquals(expected, getQuery("\"中国\"~3", analyzer));
}
Also used : PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery)

Example 37 with PhraseQuery

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

the class TestSimpleQueryParser method testORPhrase.

/** test some OR'd phrases using '|' operator */
public void testORPhrase() throws Exception {
    PhraseQuery phrase1 = new PhraseQuery("field", "foo", "bar");
    PhraseQuery phrase2 = new PhraseQuery("field", "star", "wars");
    BooleanQuery.Builder expected = new BooleanQuery.Builder();
    expected.add(phrase1, Occur.SHOULD);
    expected.add(phrase2, Occur.SHOULD);
    assertEquals(expected.build(), parse("\"foo bar\"|\"star wars\""));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery)

Example 38 with PhraseQuery

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

the class TestSimpleQueryParser method testANDPhrase.

/** test some AND'd phrases using '+' operator */
public void testANDPhrase() throws Exception {
    PhraseQuery phrase1 = new PhraseQuery("field", "foo", "bar");
    PhraseQuery phrase2 = new PhraseQuery("field", "star", "wars");
    BooleanQuery.Builder expected = new BooleanQuery.Builder();
    expected.add(phrase1, Occur.MUST);
    expected.add(phrase2, Occur.MUST);
    assertEquals(expected.build(), parse("\"foo bar\"+\"star wars\""));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery)

Example 39 with PhraseQuery

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

the class TestSimpleQueryParser method testDisableSlop.

public void testDisableSlop() {
    PhraseQuery expectedPhrase = new PhraseQuery("field", "foo", "bar");
    BooleanQuery.Builder expected = new BooleanQuery.Builder();
    expected.add(expectedPhrase, Occur.MUST);
    expected.add(new TermQuery(new Term("field", "~2")), Occur.MUST);
    assertEquals(expected.build(), parse("\"foo bar\"~2", ~NEAR_OPERATOR));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) Term(org.apache.lucene.index.Term)

Example 40 with PhraseQuery

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

the class ShingleAnalyzerWrapperTest method testShingleAnalyzerWrapperPhraseQuery.

/*
   * This shows how to construct a phrase query containing shingles.
   */
public void testShingleAnalyzerWrapperPhraseQuery() throws Exception {
    PhraseQuery.Builder builder = new PhraseQuery.Builder();
    try (TokenStream ts = analyzer.tokenStream("content", "this sentence")) {
        int j = -1;
        PositionIncrementAttribute posIncrAtt = ts.addAttribute(PositionIncrementAttribute.class);
        CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
        ts.reset();
        while (ts.incrementToken()) {
            j += posIncrAtt.getPositionIncrement();
            String termText = termAtt.toString();
            builder.add(new Term("content", termText), j);
        }
        ts.end();
    }
    PhraseQuery q = builder.build();
    ScoreDoc[] hits = searcher.search(q, 1000).scoreDocs;
    int[] ranks = new int[] { 0 };
    compareRanks(hits, ranks);
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute) PhraseQuery(org.apache.lucene.search.PhraseQuery) Term(org.apache.lucene.index.Term) PositionIncrementAttribute(org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Aggregations

PhraseQuery (org.apache.lucene.search.PhraseQuery)97 Term (org.apache.lucene.index.Term)51 TermQuery (org.apache.lucene.search.TermQuery)39 BooleanQuery (org.apache.lucene.search.BooleanQuery)36 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)36 Document (org.apache.lucene.document.Document)34 Query (org.apache.lucene.search.Query)26 Directory (org.apache.lucene.store.Directory)26 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)25 IndexSearcher (org.apache.lucene.search.IndexSearcher)22 IndexReader (org.apache.lucene.index.IndexReader)20 Field (org.apache.lucene.document.Field)17 TextField (org.apache.lucene.document.TextField)16 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)16 TokenStream (org.apache.lucene.analysis.TokenStream)15 TopDocs (org.apache.lucene.search.TopDocs)14 IndexWriter (org.apache.lucene.index.IndexWriter)13 BoostQuery (org.apache.lucene.search.BoostQuery)13 ArrayList (java.util.ArrayList)10 CannedTokenStream (org.apache.lucene.analysis.CannedTokenStream)10