Search in sources :

Example 26 with FuzzyQuery

use of org.apache.lucene.search.FuzzyQuery in project lucene-solr by apache.

the class TestSpanMultiTermQueryWrapper method testNoSuchMultiTermsInOr.

public void testNoSuchMultiTermsInOr() throws Exception {
    //test to make sure non existent multiterms aren't throwing null pointer exceptions  
    FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
    SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<>(fuzzyNoSuch);
    SpanQuery term = new SpanTermQuery(new Term("field", "brown"));
    SpanOrQuery near = new SpanOrQuery(new SpanQuery[] { term, spanNoSuch });
    assertEquals(1, searcher.search(near, 10).totalHits);
    //flip
    near = new SpanOrQuery(new SpanQuery[] { spanNoSuch, term });
    assertEquals(1, searcher.search(near, 10).totalHits);
    WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
    SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<>(wcNoSuch);
    near = new SpanOrQuery(new SpanQuery[] { term, spanWCNoSuch });
    assertEquals(1, searcher.search(near, 10).totalHits);
    RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
    SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<>(rgxNoSuch);
    near = new SpanOrQuery(new SpanQuery[] { term, spanRgxNoSuch });
    assertEquals(1, searcher.search(near, 10).totalHits);
    PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
    SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<>(prfxNoSuch);
    near = new SpanOrQuery(new SpanQuery[] { term, spanPrfxNoSuch });
    assertEquals(1, searcher.search(near, 10).totalHits);
    near = new SpanOrQuery(new SpanQuery[] { spanPrfxNoSuch });
    assertEquals(0, searcher.search(near, 10).totalHits);
    near = new SpanOrQuery(new SpanQuery[] { spanPrfxNoSuch, spanPrfxNoSuch });
    assertEquals(0, searcher.search(near, 10).totalHits);
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) Term(org.apache.lucene.index.Term) RegexpQuery(org.apache.lucene.search.RegexpQuery)

Example 27 with FuzzyQuery

use of org.apache.lucene.search.FuzzyQuery in project lucene-solr by apache.

the class TestSpanMultiTermQueryWrapper method testFuzzy2.

public void testFuzzy2() throws Exception {
    // maximum of 1 term expansion
    FuzzyQuery fq = new FuzzyQuery(new Term("field", "broan"), 1, 0, 1, false);
    SpanQuery sfq = new SpanMultiTermQueryWrapper<>(fq);
    // will only match jumps over lazy broun dog
    SpanPositionRangeQuery sprq = new SpanPositionRangeQuery(sfq, 0, 100);
    assertEquals(1, searcher.search(sprq, 10).totalHits);
}
Also used : FuzzyQuery(org.apache.lucene.search.FuzzyQuery) Term(org.apache.lucene.index.Term)

Example 28 with FuzzyQuery

use of org.apache.lucene.search.FuzzyQuery in project lucene-solr by apache.

the class TestSpanMultiTermQueryWrapper method testNoSuchMultiTermsInNear.

public void testNoSuchMultiTermsInNear() throws Exception {
    //test to make sure non existent multiterms aren't throwing null pointer exceptions  
    FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
    SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<>(fuzzyNoSuch);
    SpanQuery term = new SpanTermQuery(new Term("field", "brown"));
    SpanQuery near = new SpanNearQuery(new SpanQuery[] { term, spanNoSuch }, 1, true);
    assertEquals(0, searcher.search(near, 10).totalHits);
    //flip order
    near = new SpanNearQuery(new SpanQuery[] { spanNoSuch, term }, 1, true);
    assertEquals(0, searcher.search(near, 10).totalHits);
    WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
    SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<>(wcNoSuch);
    near = new SpanNearQuery(new SpanQuery[] { term, spanWCNoSuch }, 1, true);
    assertEquals(0, searcher.search(near, 10).totalHits);
    RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
    SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<>(rgxNoSuch);
    near = new SpanNearQuery(new SpanQuery[] { term, spanRgxNoSuch }, 1, true);
    assertEquals(0, searcher.search(near, 10).totalHits);
    PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
    SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<>(prfxNoSuch);
    near = new SpanNearQuery(new SpanQuery[] { term, spanPrfxNoSuch }, 1, true);
    assertEquals(0, searcher.search(near, 10).totalHits);
    //test single noSuch
    near = new SpanNearQuery(new SpanQuery[] { spanPrfxNoSuch }, 1, true);
    assertEquals(0, searcher.search(near, 10).totalHits);
    //test double noSuch
    near = new SpanNearQuery(new SpanQuery[] { spanPrfxNoSuch, spanPrfxNoSuch }, 1, true);
    assertEquals(0, searcher.search(near, 10).totalHits);
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) Term(org.apache.lucene.index.Term) RegexpQuery(org.apache.lucene.search.RegexpQuery)

Example 29 with FuzzyQuery

use of org.apache.lucene.search.FuzzyQuery in project lucene-solr by apache.

the class FuzzyQueryNodeBuilder method build.

@Override
public FuzzyQuery build(QueryNode queryNode) throws QueryNodeException {
    FuzzyQueryNode fuzzyNode = (FuzzyQueryNode) queryNode;
    String text = fuzzyNode.getTextAsString();
    int numEdits = FuzzyQuery.floatToEdits(fuzzyNode.getSimilarity(), text.codePointCount(0, text.length()));
    return new FuzzyQuery(new Term(fuzzyNode.getFieldAsString(), fuzzyNode.getTextAsString()), numEdits, fuzzyNode.getPrefixLength());
}
Also used : FuzzyQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FuzzyQueryNode) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) Term(org.apache.lucene.index.Term)

Example 30 with FuzzyQuery

use of org.apache.lucene.search.FuzzyQuery in project lucene-solr by apache.

the class SynonymTokenizer method testGetFuzzyFragments.

public void testGetFuzzyFragments() throws Exception {
    TestHighlightRunner helper = new TestHighlightRunner() {

        @Override
        public void run() throws Exception {
            numHighlights = 0;
            FuzzyQuery fuzzyQuery = new FuzzyQuery(new Term(FIELD_NAME, "kinnedy"), 2);
            fuzzyQuery.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_REWRITE);
            doSearching(fuzzyQuery);
            doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this, true);
            assertTrue("Failed to find correct number of highlights " + numHighlights + " found", numHighlights == 4);
        }
    };
    helper.start();
}
Also used : TestHighlightRunner(org.apache.lucene.search.highlight.SynonymTokenizer.TestHighlightRunner) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) Term(org.apache.lucene.index.Term)

Aggregations

FuzzyQuery (org.apache.lucene.search.FuzzyQuery)34 Term (org.apache.lucene.index.Term)26 PrefixQuery (org.apache.lucene.search.PrefixQuery)20 BooleanQuery (org.apache.lucene.search.BooleanQuery)17 BoostQuery (org.apache.lucene.search.BoostQuery)16 Query (org.apache.lucene.search.Query)16 TermQuery (org.apache.lucene.search.TermQuery)12 WildcardQuery (org.apache.lucene.search.WildcardQuery)12 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)8 PhraseQuery (org.apache.lucene.search.PhraseQuery)8 RegexpQuery (org.apache.lucene.search.RegexpQuery)7 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)6 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)6 BooleanClause (org.apache.lucene.search.BooleanClause)5 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)5 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)5 TopDocs (org.apache.lucene.search.TopDocs)5 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)5 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)5 Map (java.util.Map)4