Java Code Examples for org.apache.lucene.search.Query#visit()
The following examples show how to use
org.apache.lucene.search.Query#visit() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FuzzyLikeThisQueryTest.java From lucene-solr with Apache License 2.0 | 6 votes |
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")); }
Example 2
Source File: FuzzyLikeThisQueryTest.java From lucene-solr with Apache License 2.0 | 5 votes |
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")); }
Example 3
Source File: FuzzyLikeThisQueryTest.java From lucene-solr with Apache License 2.0 | 5 votes |
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")); }
Example 4
Source File: FuzzyLikeThisQueryTest.java From lucene-solr with Apache License 2.0 | 5 votes |
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")); }
Example 5
Source File: RamUsageEstimator.java From lucene-solr with Apache License 2.0 | 5 votes |
/** * 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); } }
Example 6
Source File: TestFieldMaskingSpanQuery.java From lucene-solr with Apache License 2.0 | 5 votes |
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()); }
Example 7
Source File: MultiTermHighlighting.java From lucene-solr with Apache License 2.0 | 4 votes |
/** * 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]); }
Example 8
Source File: QueryTermExtractor.java From lucene-solr with Apache License 2.0 | 3 votes |
/** * 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]); }
Example 9
Source File: QueryAnalyzer.java From lucene-solr with Apache License 2.0 | 2 votes |
/** * 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); }