下面列出了org.apache.lucene.search.Query#visit ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void testNonExistingField() throws Throwable {
FuzzyLikeThisQuery flt = new FuzzyLikeThisQuery(10, analyzer);
flt.addTerms("jonathin smoth", "name", 2, 1);
flt.addTerms("jonathin smoth", "this field does not exist", 2, 1);
// don't fail here just because the field doesn't exits
Query q = flt.rewrite(searcher.getIndexReader());
HashSet<Term> queryTerms = new HashSet<>();
q.visit(QueryVisitor.termCollector(queryTerms));
assertTrue("Should have variant jonathan", queryTerms.contains(new Term("name", "jonathan")));
assertTrue("Should have variant smith", queryTerms.contains(new Term("name", "smith")));
TopDocs topDocs = searcher.search(flt, 1);
ScoreDoc[] sd = topDocs.scoreDocs;
assertTrue("score docs must match 1 doc", (sd != null) && (sd.length > 0));
Document doc = searcher.doc(sd[0].doc);
assertEquals("Should match most similar when using 2 words", "2", doc.get("id"));
}
public void testClosestEditDistanceMatchComesFirst() throws Throwable {
FuzzyLikeThisQuery flt = new FuzzyLikeThisQuery(10, analyzer);
flt.addTerms("smith", "name", 2, 1);
Query q = flt.rewrite(searcher.getIndexReader());
HashSet<Term> queryTerms = new HashSet<>();
q.visit(QueryVisitor.termCollector(queryTerms));
assertTrue("Should have variant smythe", queryTerms.contains(new Term("name", "smythe")));
assertTrue("Should have variant smith", queryTerms.contains(new Term("name", "smith")));
assertTrue("Should have variant smyth", queryTerms.contains(new Term("name", "smyth")));
TopDocs topDocs = searcher.search(flt, 1);
ScoreDoc[] sd = topDocs.scoreDocs;
assertTrue("score docs must match 1 doc", (sd != null) && (sd.length > 0));
Document doc = searcher.doc(sd[0].doc);
assertEquals("Should match most similar not most rare variant", "2", doc.get("id"));
}
public void testMultiWord() throws Throwable {
FuzzyLikeThisQuery flt = new FuzzyLikeThisQuery(10, analyzer);
flt.addTerms("jonathin smoth", "name", 2, 1);
Query q = flt.rewrite(searcher.getIndexReader());
HashSet<Term> queryTerms = new HashSet<>();
q.visit(QueryVisitor.termCollector(queryTerms));
assertTrue("Should have variant jonathan", queryTerms.contains(new Term("name", "jonathan")));
assertTrue("Should have variant smith", queryTerms.contains(new Term("name", "smith")));
TopDocs topDocs = searcher.search(flt, 1);
ScoreDoc[] sd = topDocs.scoreDocs;
assertTrue("score docs must match 1 doc", (sd != null) && (sd.length > 0));
Document doc = searcher.doc(sd[0].doc);
assertEquals("Should match most similar when using 2 words", "2", doc.get("id"));
}
public void testNoMatchFirstWordBug() throws Throwable {
FuzzyLikeThisQuery flt = new FuzzyLikeThisQuery(10, analyzer);
flt.addTerms("fernando smith", "name", 2, 1);
Query q = flt.rewrite(searcher.getIndexReader());
HashSet<Term> queryTerms = new HashSet<>();
q.visit(QueryVisitor.termCollector(queryTerms));
assertTrue("Should have variant smith", queryTerms.contains(new Term("name", "smith")));
TopDocs topDocs = searcher.search(flt, 1);
ScoreDoc[] sd = topDocs.scoreDocs;
assertTrue("score docs must match 1 doc", (sd != null) && (sd.length > 0));
Document doc = searcher.doc(sd[0].doc);
assertEquals("Should match most similar when using 2 words", "2", doc.get("id"));
}
/**
* Returns the size in bytes of a Query object. Unknown query types will be estimated
* using {@link #shallowSizeOf(Object)}, or using the supplied <code>defSize</code> parameter
* if its value is greater than 0.
*/
public static long sizeOf(Query q, long defSize) {
if (q instanceof Accountable) {
return ((Accountable)q).ramBytesUsed();
} else {
RamUsageQueryVisitor visitor = new RamUsageQueryVisitor(q, defSize);
q.visit(visitor);
return alignObjectSize(visitor.total);
}
}
public void testRewrite2() throws Exception {
SpanQuery q1 = new SpanTermQuery(new Term("last", "smith"));
SpanQuery q2 = new SpanTermQuery(new Term("last", "jones"));
SpanQuery q = new SpanNearQuery(new SpanQuery[]
{ q1, new FieldMaskingSpanQuery(q2, "last")}, 1, true );
Query qr = searcher.rewrite(q);
QueryUtils.checkEqual(q, qr);
HashSet<Term> set = new HashSet<>();
qr.visit(QueryVisitor.termCollector(set));
assertEquals(2, set.size());
}
/**
* Extracts MultiTermQueries that match the provided field predicate.
* Returns equivalent automata that will match terms.
*/
static LabelledCharArrayMatcher[] extractAutomata(Query query, Predicate<String> fieldMatcher, boolean lookInSpan) {
AutomataCollector collector = new AutomataCollector(lookInSpan, fieldMatcher);
query.visit(collector);
return collector.runAutomata.toArray(new LabelledCharArrayMatcher[0]);
}
/**
* Extracts all terms texts of a given Query into an array of WeightedTerms
*
* @param query Query to extract term texts from
* @param prohibited <code>true</code> to extract "prohibited" terms, too
* @param fieldName The fieldName used to filter query terms
* @return an array of the terms used in a query, plus their weights.
*/
public static WeightedTerm[] getTerms(Query query, boolean prohibited, String fieldName) {
HashSet<WeightedTerm> terms = new HashSet<>();
Predicate<String> fieldSelector = fieldName == null ? f -> true : fieldName::equals;
query.visit(new BoostedTermExtractor(1, terms, prohibited, fieldSelector));
return terms.toArray(new WeightedTerm[0]);
}
/**
* Create a {@link QueryTree} from a passed in Query or Filter
*
* @param luceneQuery the query to analyze
* @return a QueryTree describing the analyzed query
*/
QueryTree buildTree(Query luceneQuery, TermWeightor weightor) {
QueryBuilder builder = new QueryBuilder();
luceneQuery.visit(builder);
return builder.apply(weightor);
}