本文整理汇总了Java中org.apache.lucene.search.MultiPhraseQuery.Builder方法的典型用法代码示例。如果您正苦于以下问题:Java MultiPhraseQuery.Builder方法的具体用法?Java MultiPhraseQuery.Builder怎么用?Java MultiPhraseQuery.Builder使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.lucene.search.MultiPhraseQuery
的用法示例。
在下文中一共展示了MultiPhraseQuery.Builder方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: rewrite
import org.apache.lucene.search.MultiPhraseQuery; //导入方法依赖的package包/类
@Override
public Query rewrite(IndexReader reader) throws IOException {
Query rewritten = super.rewrite(reader);
if (rewritten != this) {
return rewritten;
}
if (termArrays.isEmpty()) {
return new MatchNoDocsQuery();
}
MultiPhraseQuery.Builder query = new MultiPhraseQuery.Builder();
query.setSlop(slop);
int sizeMinus1 = termArrays.size() - 1;
for (int i = 0; i < sizeMinus1; i++) {
query.add(termArrays.get(i), positions.get(i));
}
Term[] suffixTerms = termArrays.get(sizeMinus1);
int position = positions.get(sizeMinus1);
ObjectHashSet<Term> terms = new ObjectHashSet<>();
for (Term term : suffixTerms) {
getPrefixTerms(terms, term, reader);
if (terms.size() > maxExpansions) {
break;
}
}
if (terms.isEmpty()) {
// if the terms does not exist we could return a MatchNoDocsQuery but this would break the unified highlighter
// which rewrites query with an empty reader.
return new BooleanQuery.Builder()
.add(query.build(), BooleanClause.Occur.MUST)
.add(Queries.newMatchNoDocsQuery("No terms supplied for " + MultiPhrasePrefixQuery.class.getName()),
BooleanClause.Occur.MUST).build();
}
query.add(terms.toArray(Term.class), position);
return query.build();
}
示例2: analyzeMultiPhrase
import org.apache.lucene.search.MultiPhraseQuery; //导入方法依赖的package包/类
/**
* Creates complex phrase query from the cached tokenstream contents
*/
protected Query analyzeMultiPhrase(String field, TokenStream stream, int slop) throws IOException {
MultiPhraseQuery.Builder mpqb = newMultiPhraseQueryBuilder();
mpqb.setSlop(slop);
TermToBytesRefAttribute termAtt = stream.getAttribute(TermToBytesRefAttribute.class);
PositionIncrementAttribute posIncrAtt = stream.getAttribute(PositionIncrementAttribute.class);
int position = -1;
List<Term> multiTerms = new ArrayList<>();
stream.reset();
while (stream.incrementToken()) {
int positionIncrement = posIncrAtt.getPositionIncrement();
if (positionIncrement > 0 && multiTerms.size() > 0) {
if (enablePositionIncrements) {
mpqb.add(multiTerms.toArray(new Term[0]), position);
} else {
mpqb.add(multiTerms.toArray(new Term[0]));
}
multiTerms.clear();
}
position += positionIncrement;
multiTerms.add(new Term(field, termAtt.getBytesRef()));
}
if (enablePositionIncrements) {
mpqb.add(multiTerms.toArray(new Term[0]), position);
} else {
mpqb.add(multiTerms.toArray(new Term[0]));
}
return mpqb.build();
}
示例3: testSynonymsPhrase
import org.apache.lucene.search.MultiPhraseQuery; //导入方法依赖的package包/类
public void testSynonymsPhrase() throws Exception {
MultiPhraseQuery.Builder expectedQ = new MultiPhraseQuery.Builder();
expectedQ.add(new Term(FIELD, "old"));
expectedQ.add(new Term[] { new Term(FIELD, "dogs"), new Term(FIELD, "dog") });
QueryParser qp = new QueryParser(FIELD, new MockSynonymAnalyzer());
assertEquals(expectedQ, qp.parse("\"old dogs\""));
qp.setDefaultOperator(Operator.AND);
assertEquals(expectedQ, qp.parse("\"old dogs\""));
BoostQuery expected = new BoostQuery(expectedQ.build(), 2f);
assertEquals(expected, qp.parse("\"old dogs\"^2"));
expectedQ.setSlop(3);
assertEquals(expected, qp.parse("\"old dogs\"~3^2"));
}
示例4: testCJKSynonymsPhrase
import org.apache.lucene.search.MultiPhraseQuery; //导入方法依赖的package包/类
public void testCJKSynonymsPhrase() throws Exception {
MultiPhraseQuery.Builder expectedQ = new MultiPhraseQuery.Builder();
expectedQ.add(new Term(FIELD, "中"));
expectedQ.add(new Term[] { new Term(FIELD, "国"), new Term(FIELD, "國")});
QueryParser qp = new QueryParser(FIELD, new MockCJKSynonymAnalyzer());
qp.setDefaultOperator(Operator.AND);
assertEquals(expectedQ, qp.parse("\"中国\""));
Query expected = new BoostQuery(expectedQ.build(), 2f);
assertEquals(expected, qp.parse("\"中国\"^2"));
expectedQ.setSlop(3);
assertEquals(expected, qp.parse("\"中国\"~3^2"));
}
示例5: newMultiPhraseQueryBuilder
import org.apache.lucene.search.MultiPhraseQuery; //导入方法依赖的package包/类
/**
* Builds a new MultiPhraseQuery instance.
* <p>
* This is intended for subclasses that wish to customize the generated
* queries.
*
* @return new MultiPhraseQuery instance
*/
protected MultiPhraseQuery.Builder newMultiPhraseQueryBuilder() {
return new MultiPhraseQuery.Builder();
}