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

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

Introduction

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

Prototype

PhraseQuery

Source Link

Usage

From source file:analysis.SynonymAnalyzerTest.java

License:Apache License

public void testSearchByAPI() throws Exception {

    TermQuery tq = new TermQuery(new Term("content", "hops")); //#1
    assertEquals(1, TestUtil.hitCount(searcher, tq));

    PhraseQuery pq = new PhraseQuery(); //#2
    pq.add(new Term("content", "fox")); //#2
    pq.add(new Term("content", "hops")); //#2
    assertEquals(1, TestUtil.hitCount(searcher, pq));
}

From source file:aos.lucene.search.advanced.MultiPhraseQueryTest.java

License:Apache License

public void testAgainstOR() throws Exception {
    PhraseQuery quickFox = new PhraseQuery();
    quickFox.setSlop(1);/*  w w  w . j a  v a2s  .c o m*/
    quickFox.add(new Term("field", "quick"));
    quickFox.add(new Term("field", "fox"));

    PhraseQuery fastFox = new PhraseQuery();
    fastFox.add(new Term("field", "fast"));
    fastFox.add(new Term("field", "fox"));

    BooleanQuery query = new BooleanQuery();
    query.add(quickFox, BooleanClause.Occur.SHOULD);
    query.add(fastFox, BooleanClause.Occur.SHOULD);
    TopDocs hits = searcher.search(query, 10);
    assertEquals(2, hits.totalHits);
}

From source file:aos.lucene.search.advanced.SpanQueryTest.java

License:Apache License

public void testSpanNearQuery() throws Exception {
      SpanQuery[] quick_brown_dog = new SpanQuery[] { quick, brown, dog };
      SpanNearQuery snq = new SpanNearQuery(quick_brown_dog, 0, true); //
      assertNoMatches(snq);//from  www .j av a  2 s.  c  o m
      dumpSpans(snq);

      snq = new SpanNearQuery(quick_brown_dog, 4, true); //
      assertNoMatches(snq);
      dumpSpans(snq);

      snq = new SpanNearQuery(quick_brown_dog, 5, true); //
      assertOnlyBrownFox(snq);
      dumpSpans(snq);

      // interesting - even a sloppy phrase query would require
      // more slop to match
      snq = new SpanNearQuery(new SpanQuery[] { lazy, fox }, 3, false);// #4
      assertOnlyBrownFox(snq);
      dumpSpans(snq);

      PhraseQuery pq = new PhraseQuery(); //
      pq.add(new Term("f", "lazy")); //
      pq.add(new Term("f", "fox")); //
      pq.setSlop(4); //
      assertNoMatches(pq);

      pq.setSlop(5); //
      assertOnlyBrownFox(pq); //
  }

From source file:aos.lucene.search.msc.PhraseQueryTest.java

License:Apache License

private boolean matched(String[] phrase, int slop) throws IOException {
    PhraseQuery query = new PhraseQuery();
    query.setSlop(slop);//from   www. jav  a 2 s  . c o  m

    for (String word : phrase) {
        query.add(new Term("field", word));
    }

    TopDocs matches = searcher.search(query, 10);
    return matches.totalHits > 0;
}

From source file:at.ac.univie.mminf.luceneSKOS.analysis.SKOSLabelFilterTest.java

License:Apache License

@Test
public void phraseQuerySearch() throws CorruptIndexException, IOException {

    Document doc = new Document();
    doc.add(new Field("content", "The quick brown fox jumps over the lazy dog", Field.Store.YES,
            Field.Index.ANALYZED));

    writer.addDocument(doc);/*www  .j  a  va 2 s .  com*/

    searcher = new IndexSearcher(IndexReader.open(writer, false));

    PhraseQuery pq = new PhraseQuery();
    pq.add(new Term("content", "fox"));
    pq.add(new Term("content", "hops"));

    Assert.assertEquals(1, TestUtil.hitCount(searcher, pq));

}

From source file:com.amalto.core.storage.hibernate.LuceneQueryGenerator.java

License:Open Source License

@Override
public Query visit(Compare condition) {
    condition.getLeft().accept(this);
    Expression right = condition.getRight();
    right.accept(this);
    if (condition.getPredicate() == Predicate.EQUALS || condition.getPredicate() == Predicate.CONTAINS
            || condition.getPredicate() == Predicate.STARTS_WITH) {
        String searchValue = String.valueOf(currentValue);
        BooleanQuery termQuery = new BooleanQuery();
        if (searchValue != null && searchValue.startsWith("\'") && searchValue.endsWith("\'")) { //$NON-NLS-1$ //$NON-NLS-2$
            PhraseQuery query = new PhraseQuery();
            StringTokenizer tokenizer = new StringTokenizer(searchValue.substring(1, searchValue.length() - 1));
            while (tokenizer.hasMoreTokens()) {
                query.add(new Term(currentFieldName, tokenizer.nextToken().toLowerCase()));
            }//from   ww w. jav a 2  s .  c om
            termQuery.add(query, BooleanClause.Occur.SHOULD);
        } else {
            StringTokenizer tokenizer = new StringTokenizer(searchValue);
            while (tokenizer.hasMoreTokens()) {
                TermQuery newTermQuery = new TermQuery(
                        new Term(currentFieldName, tokenizer.nextToken().toLowerCase()));
                termQuery.add(newTermQuery,
                        isBuildingNot ? BooleanClause.Occur.MUST_NOT : BooleanClause.Occur.MUST);
                if (condition.getPredicate() == Predicate.STARTS_WITH) {
                    break;
                }
            }
        }
        return termQuery;
    } else if (condition.getPredicate() == Predicate.GREATER_THAN
            || condition.getPredicate() == Predicate.GREATER_THAN_OR_EQUALS
            || condition.getPredicate() == Predicate.LOWER_THAN
            || condition.getPredicate() == Predicate.LOWER_THAN_OR_EQUALS) {
        throw new RuntimeException("Greater than, less than are not supported in full text searches."); //$NON-NLS-1$
    } else {
        throw new NotImplementedException("No support for predicate '" + condition.getPredicate() + "'"); //$NON-NLS-1$ //$NON-NLS-2$
    }
}

From source file:com.appeligo.search.actions.SearchResults.java

License:Apache License

/**
 * This method has to be called sometime after deserialization as well as during initialization.
 * Deserialization will not call the constructor.
 *//* www.j av  a  2s . com*/
private void initializeStatics() {
    if (config == null) {
        config = ConfigUtils.getSystemConfig();
    }

    minimumHits = config.getInt("didYouMeanMinHits", 4);
    minimumScore = config.getFloat("didYouMeanMinScore", 1.0f);
    //relevanceMargin = config.getFloat("relevanceMargin", 0.10f);
    defaultField = "compositeField"; // the only field in the compositeIndex

    if (didYouMeanParser == null) {
        try {
            didYouMeanParser = new CompositeDidYouMeanParser(defaultField,
                    FSDirectory.getDirectory(config.getString("spellIndex")));
        } catch (IOException e) {
            log.error("Error opening spell index", e);
        }
    }
    analyzer = new PorterStemAnalyzer(LuceneIndexer.STOP_WORDS);
    genreQueries = new HashMap<String, Query>();
    String[] queries = config.getStringArray("genreQuery.genre");
    String[] labels = config.getStringArray("genreQuery.genre[@label]");
    PorterStemmer stemmer = new PorterStemmer();
    for (int i = 0; i < labels.length && i < labels.length; i++) {
        if (queries[i].indexOf(' ') > 0) {
            PhraseQuery phraseQuery = new PhraseQuery();
            String[] words = queries[i].split(" ");
            for (String word : words) {
                phraseQuery.add(new Term("genre", stemmer.stem(word)));
            }
            genreQueries.put(labels[i], phraseQuery);
        } else {
            genreQueries.put(labels[i], new TermQuery(new Term("genre", queries[i])));
        }
    }
}

From source file:com.bewsia.script.safe.lucene.SEntity.java

License:Open Source License

public PhraseQuery newPhraseQuery() {
    return new PhraseQuery();
}

From source file:com.bizosys.hsearch.dictionary.DictionaryValues.java

License:Apache License

private ScoreDoc[] searchTop(Directory idx, String query, Analyzer analyzer, List<String> words,
        Set<Term> terms) throws ParseException, CorruptIndexException, IOException {

    fastSplit(words, query, ' ');

    QueryParser parser = new QueryParser(Version.LUCENE_35, "k", analyzer);
    PhraseQuery q = new PhraseQuery();
    int location = 0;
    for (String word : words) {
        Query q1 = parser.parse(word);
        q1.extractTerms(terms);//from   w ww.ja v a 2 s .c o m
        for (Term term : terms) {
            q.add(term, location++);
        }
        terms.clear();
    }
    words.clear();
    q.setSlop(0);

    int hitsPerPage = 1;

    TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
    searcher.search(q, collector);
    ScoreDoc[] hits = collector.topDocs().scoreDocs;
    return hits;
}

From source file:com.digitalpebble.ngrams.NGramCorpus.java

License:Apache License

/** Returns the frequency of a term given a list of its tokens **/
public long getOccurrences(final List<String> tokens, boolean lowercase) throws IOException {
    // queries the Lucene index for the occurrences
    // for the exact sequence of ngrams

    String ngrams = Integer.toString(tokens.size());

    PhraseQuery pq = new PhraseQuery();
    pq.setSlop(0);/*from w  w  w  .j a v  a 2  s.c  o  m*/

    String fieldName = "text";
    if (lowercase)
        fieldName = "lowercase";

    for (String s : tokens) {
        if (lowercase)
            s = s.toLowerCase();
        Term t = new Term(fieldName, s);
        pq.add(t);
    }

    TermQuery tq = new TermQuery(new Term("length", ngrams));

    BooleanQuery bq = new BooleanQuery();
    bq.add(pq, Occur.MUST);
    bq.add(tq, Occur.MUST);

    return getOccurrences(bq);
}