Example usage for org.apache.lucene.search PhraseQuery.Builder PhraseQuery.Builder

List of usage examples for org.apache.lucene.search PhraseQuery.Builder PhraseQuery.Builder

Introduction

In this page you can find the example usage for org.apache.lucene.search PhraseQuery.Builder PhraseQuery.Builder.

Prototype

PhraseQuery.Builder

Source Link

Usage

From source file:SearcherTest.java

/**
 * PhraseQuery?big car?/*from   ww  w .ja  va2  s  .  c  om*/
 * ?document?"big car"
 * document???????big black car?
 * ????slop
 * slopslop???
 *
 * @throws Exception
 */
@Test
public void testPhraseQuery() throws Exception {
    String searchField = "contents";
    String q1 = "xxxx";
    String q2 = "bbb";
    Term t1 = new Term(searchField, q1);
    Term t2 = new Term(searchField, q2);
    PhraseQuery.Builder builder = new PhraseQuery.Builder();
    builder.add(t1);
    builder.add(t2);
    builder.setSlop(0);
    PhraseQuery query = builder.build();
    TopDocs hits = is.search(query, 10);
    System.out.println("? '" + q1 + q2 + "" + "" + hits.totalHits
            + "");

    for (ScoreDoc scoreDoc : hits.scoreDocs) {
        Document doc = is.doc(scoreDoc.doc);
        System.out.println(doc.get("fullPath"));
    }
}

From source file:uk.ac.ebi.biostudies.efo.EFOQueryExpander.java

License:Apache License

public Query newQueryFromString(String text, String field) {
    if (text.contains(" ")) {
        String[] tokens = text.split("\\s+");
        PhraseQuery.Builder builder = new PhraseQuery.Builder();
        for (String token : tokens) {
            builder.add(new Term(field, token));
        }//from   w w w.  jav  a2s  . co  m
        return builder.build();
    } else {
        return new TermQuery(new Term(field, text));
    }
}

From source file:uk.co.flax.luwak.termextractor.TestExtractors.java

License:Apache License

@Test
public void testPhraseQueryExtractor() {

    PhraseQuery.Builder pq = new PhraseQuery.Builder();
    pq.add(new Term("f", "hello"));
    pq.add(new Term("f", "encyclopedia"));

    assertThat(treeBuilder.collectTerms(pq.build()))
            .containsOnly(new QueryTerm("f", "encyclopedia", QueryTerm.Type.EXACT));

}