Example usage for org.apache.lucene.queryparser.simple SimpleQueryParser SimpleQueryParser

List of usage examples for org.apache.lucene.queryparser.simple SimpleQueryParser SimpleQueryParser

Introduction

In this page you can find the example usage for org.apache.lucene.queryparser.simple SimpleQueryParser SimpleQueryParser.

Prototype

public SimpleQueryParser(Analyzer analyzer, Map<String, Float> weights) 

Source Link

Document

Creates a new parser searching over multiple fields with different weights.

Usage

From source file:io.github.msurdi.redeye.core.lucene.AbstractIndex.java

License:Apache License

/**
 * Retrieve a list of documents matching given query. The query must be a valid lucene query or '*'
 * for matching all documents. If the query is not valid, a best effort search is done.
 *
 * @param q a query string//from   www. j a v a  2  s  .com
 * @return a list of the {@link io.github.msurdi.redeye.api.Indexable} documents matching.
 * @throws IOException
 */
@Override
public List<T> query(String q) throws IOException {
    ensureOpened();

    ArrayList<T> results = Lists.newArrayList();
    Query query;
    try {
        if (MATCH_ALL.equals(q)) {
            query = new MatchAllDocsQuery();
        } else {
            query = new QueryParser(LUCENE_VERSION, DEFAULT_FIELD, analyzer).parse(q);
        }
    } catch (ParseException e) {
        query = new SimpleQueryParser(analyzer, DEFAULT_FIELD).parse(q);
    }

    IndexSearcher searcher = null;

    try {
        searcherManager.maybeRefresh();
        searcher = searcherManager.acquire();
        TopDocs docs = searcher.search(query, Math.max(1, searcher.getIndexReader().maxDoc()));
        for (ScoreDoc scoreDoc : docs.scoreDocs) {
            Document document = searcher.doc(scoreDoc.doc);
            results.add(buildEntity(document));
        }
    } finally {
        searcherManager.release(searcher);
    }

    return results;
}