本文整理汇总了Java中org.apache.lucene.queryParser.standard.StandardQueryParser类的典型用法代码示例。如果您正苦于以下问题:Java StandardQueryParser类的具体用法?Java StandardQueryParser怎么用?Java StandardQueryParser使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
StandardQueryParser类属于org.apache.lucene.queryParser.standard包,在下文中一共展示了StandardQueryParser类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: search
import org.apache.lucene.queryParser.standard.StandardQueryParser; //导入依赖的package包/类
@Override
public List<String> search(String text, String typeName, int hitsPerPage) {
Query q;
try {
if (typeName != null) {
BooleanQuery bq = new BooleanQuery();
Query query = new StandardQueryParser(this.analyzer).parse(text, LuceneInformation.DATA);
bq.add(query, BooleanClause.Occur.MUST);
bq.add(new TermQuery(new Term(LuceneInformation.TYPE, typeName)), BooleanClause.Occur.MUST);
q = bq;
} else {
q = new StandardQueryParser(this.analyzer).parse(text, LuceneInformation.DATA);
}
} catch (QueryNodeException e) {
throw new RuntimeException(e.getMessage());
}
List<String> results = new ArrayList<String>();
try {
@SuppressWarnings("deprecation")
IndexSearcher searcher = new IndexSearcher(this.index);
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
searcher.search(q, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
LuceneInformation.logger.info("Found " + hits.length + " hits.");
for (int i = 0; i < hits.length; ++i) {
int docId = hits[i].doc;
Document d = searcher.doc(docId);
LuceneInformation.logger.debug((i + 1) + ". " + d.get(LuceneInformation.NAME));
@SuppressWarnings("deprecation")
String stringValue = d.getField(LuceneInformation.DATA).stringValue();
LuceneInformation.logger.debug(stringValue);
results.add(d.get(LuceneInformation.NAME));
}
searcher.close();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
return results;
}