Example usage for org.apache.lucene.queryparser.flexible.standard StandardQueryParser setAnalyzer

List of usage examples for org.apache.lucene.queryparser.flexible.standard StandardQueryParser setAnalyzer

Introduction

In this page you can find the example usage for org.apache.lucene.queryparser.flexible.standard StandardQueryParser setAnalyzer.

Prototype

public void setAnalyzer(Analyzer analyzer) 

Source Link

Usage

From source file:org.scilab.modules.xcos.palette.PaletteSearcher.java

/**
 * @param str Query/*from w ww  .j a v a  2  s .  c  o  m*/
 * @return paths to the found blocks
 */
public List<Document> search(String str) {
    List<Document> found = new ArrayList<>();
    try (IndexReader reader = DirectoryReader.open(mgr.getDirectory())) {
        IndexSearcher searcher = new IndexSearcher(reader);

        StandardQueryParser queryParserHelper = new StandardQueryParser();
        queryParserHelper.setAllowLeadingWildcard(true);
        queryParserHelper.setLowercaseExpandedTerms(true);
        queryParserHelper.setAnalyzer(mgr.getAnalyzer());
        queryParserHelper.setMultiFields(new String[] { "refname", "refpurpose", "content" });

        Query query = queryParserHelper.parse(str, null);
        TopDocs results = searcher.search(query, XcosConstants.MAX_HITS);
        ScoreDoc[] hits = results.scoreDocs;

        if (hits.length == 0) {
            query = queryParserHelper.parse("*" + str + "*", null);
            results = searcher.search(query, XcosConstants.MAX_HITS);
            hits = results.scoreDocs;
        }

        for (int i = 0; i < hits.length; i++) {
            Document doc = searcher.doc(hits[i].doc);
            found.add(doc);
        }
    } catch (IOException | QueryNodeException e) {
        Logger.getLogger(PaletteSearcher.class.getName()).log(Level.SEVERE, null, e);
    }
    return found;
}