Java Code Examples for org.apache.lucene.queryparser.complexPhrase.ComplexPhraseQueryParser

The following examples show how to use org.apache.lucene.queryparser.complexPhrase.ComplexPhraseQueryParser. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Stargraph   Source File: LuceneSearchQueryGenerator.java    License: MIT License 6 votes vote down vote up
private static Query fuzzyPhraseSearch(String field, String searchTerm, int maxEdits) {
    StringBuilder queryStr = new StringBuilder();
    queryStr.append(field).append(":(");

    String words[] = searchTerm.split("\\s+");
    for (int i = 0; i < words.length; i++) {
        if (i > 0) {
            queryStr.append(" AND ");
        }
        queryStr.append(ComplexPhraseQueryParser.escape(words[i])).append("~").append(maxEdits);
    }
    queryStr.append(")");

    try {
        return new ComplexPhraseQueryParser("value", new StandardAnalyzer()).parse(queryStr.toString());
    } catch (ParseException e) {
        throw new StarGraphException(e);
    }
}
 
Example 2
Source Project: Quelea   Source File: BibleSearchIndex.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Search for bible chapters that match the given filter.
 *
 * @param queryString the query string to filter.
 * @param type ignored - may be null.
 * @return a list of all bible chapters that match the given filter.
 */
@Override
public BibleChapter[] filter(String queryString, FilterType type) {
    String sanctifyQueryString = SearchIndexUtils.makeLuceneQuery(queryString);
    if(chapters.isEmpty() || sanctifyQueryString.isEmpty()) {
        return chapters.values().toArray(new BibleChapter[chapters.size()]);
    }
    List<BibleChapter> ret;
    try (DirectoryReader dr = DirectoryReader.open(index)) {
        IndexSearcher searcher = new IndexSearcher(dr);
        BooleanQuery.setMaxClauseCount(Integer.MAX_VALUE);
        Query q = new ComplexPhraseQueryParser("text", analyzer).parse(sanctifyQueryString);
        TopScoreDocCollector collector = TopScoreDocCollector.create(10000,10000);
        searcher.search(q, collector);
        ScoreDoc[] hits = collector.topDocs().scoreDocs;
        ret = new ArrayList<>();
        for(int i = 0; i < hits.length; ++i) {
            int docId = hits[i].doc;
            Document d = searcher.doc(docId);
            BibleChapter chapter = chapters.get(Integer.parseInt(d.get("number")));
            ret.add(chapter);
        }
        return ret.toArray(new BibleChapter[ret.size()]);
    }
    catch (ParseException | IOException ex) {
        LOGGER.log(Level.WARNING, "Invalid query string: " + sanctifyQueryString, ex);
        return new BibleChapter[0];
    }
}
 
Example 3
Source Project: lucene-solr   Source File: TestHighlightingMatcher.java    License: Apache License 2.0 5 votes vote down vote up
public void testComplexPhraseQueryParser() throws Exception {

    ComplexPhraseQueryParser cpqp = new ComplexPhraseQueryParser(FIELD, new StandardAnalyzer());
    Query query = cpqp.parse("\"x b\"");
    try (Monitor monitor = newMonitor()) {
      monitor.register(new MonitorQuery("1", query));
      Document doc = buildDoc("x b c");
      MatchingQueries<HighlightsMatch> matches = monitor.match(doc, HighlightsMatch.MATCHER);
      HighlightsMatch m = matches.matches("1");
      assertNotNull(m);
      assertEquals(2, m.getHitCount());
      assertTrue(m.getFields().contains(FIELD));
    }

  }