本文整理汇总了Java中org.apache.lucene.search.Query.rewrite方法的典型用法代码示例。如果您正苦于以下问题:Java Query.rewrite方法的具体用法?Java Query.rewrite怎么用?Java Query.rewrite使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.lucene.search.Query
的用法示例。
在下文中一共展示了Query.rewrite方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkGeoQueryHighlighting
import org.apache.lucene.search.Query; //导入方法依赖的package包/类
public void checkGeoQueryHighlighting(Query geoQuery) throws IOException, InvalidTokenOffsetsException {
Map analysers = new HashMap<String, Analyzer>();
analysers.put("text", new StandardAnalyzer());
FieldNameAnalyzer fieldNameAnalyzer = new FieldNameAnalyzer(analysers);
Query termQuery = new TermQuery(new Term("text", "failure"));
Query boolQuery = new BooleanQuery.Builder().add(new BooleanClause(geoQuery, BooleanClause.Occur.SHOULD))
.add(new BooleanClause(termQuery, BooleanClause.Occur.SHOULD)).build();
org.apache.lucene.search.highlight.Highlighter highlighter =
new org.apache.lucene.search.highlight.Highlighter(new CustomQueryScorer(boolQuery));
String fragment = highlighter.getBestFragment(fieldNameAnalyzer.tokenStream("text", "Arbitrary text field which should not cause " +
"a failure"), "Arbitrary text field which should not cause a failure");
assertThat(fragment, equalTo("Arbitrary text field which should not cause a <B>failure</B>"));
Query rewritten = boolQuery.rewrite(null);
highlighter = new org.apache.lucene.search.highlight.Highlighter(new CustomQueryScorer(rewritten));
fragment = highlighter.getBestFragment(fieldNameAnalyzer.tokenStream("text", "Arbitrary text field which should not cause " +
"a failure"), "Arbitrary text field which should not cause a failure");
assertThat(fragment, equalTo("Arbitrary text field which should not cause a <B>failure</B>"));
}
示例2: searchIndex
import org.apache.lucene.search.Query; //导入方法依赖的package包/类
/**
* 查询索引
*
* @param keywords
* @return
* @throws Exception
*/
public List<Document> searchIndex(Integer typeId, String keywords) throws Exception {
// 1.init searcher
Analyzer analyzer = new PaodingAnalyzer();
IndexReader reader = IndexReader.open(typeId == appConfig.getGameTypeId() ? appConfig.getGameIndexDir()
: appConfig.getSoftIndexDir());
BooleanClause.Occur[] flags = new BooleanClause.Occur[] { BooleanClause.Occur.SHOULD,
BooleanClause.Occur.SHOULD };
Query query = MultiFieldQueryParser.parse(keywords, appConfig.getQueryFields(), flags, analyzer);
query = query.rewrite(reader);
// 2.search
List<Document> docs = new ArrayList<Document>();
Hits hits = (typeId == appConfig.getGameTypeId() ? gameSearcher.search(query, Sort.RELEVANCE) : softSearcher
.search(query, Sort.RELEVANCE));// searcher.search(query,
// Sort.RELEVANCE);
for (int i = 0; i < hits.length(); i++) {
docs.add(hits.doc(i));
}
// 3.return
reader.close();
return docs;
}
示例3: testRewrite
import org.apache.lucene.search.Query; //导入方法依赖的package包/类
public void testRewrite() throws IOException {
Query q = new ESToParentBlockJoinQuery(
new PhraseQuery("body", "term"), // rewrites to a TermQuery
new QueryBitSetProducer(new TermQuery(new Term("is", "parent"))),
ScoreMode.Avg, "nested");
Query expected = new ESToParentBlockJoinQuery(
new TermQuery(new Term("body", "term")),
new QueryBitSetProducer(new TermQuery(new Term("is", "parent"))),
ScoreMode.Avg, "nested");
Query rewritten = q.rewrite(new MultiReader());
assertEquals(expected, rewritten);
}