Example usage for org.apache.lucene.search FuzzyQuery FuzzyQuery

List of usage examples for org.apache.lucene.search FuzzyQuery FuzzyQuery

Introduction

In this page you can find the example usage for org.apache.lucene.search FuzzyQuery FuzzyQuery.

Prototype

public FuzzyQuery(Term term) 

Source Link

Document

Calls #FuzzyQuery(Term,int) FuzzyQuery(term, defaultMaxEdits) .

Usage

From source file:SearcherTest.java

/**
 * ??FuzzyQuery/*  w  w w  .  j  a  va2  s . c  om*/
 * FuzzyQuery????
 *
 * @throws Exception
 */
@Test
public void testFuzzyQuery() throws Exception {
    String searchField = "contents";
    String q = "ljlxx";
    Term t = new Term(searchField, q);
    Query query = new FuzzyQuery(t);
    TopDocs hits = is.search(query, 10);
    System.out.println("? '" + q + "'" + hits.totalHits + "");

    for (ScoreDoc scoreDoc : hits.scoreDocs) {
        Document doc = is.doc(scoreDoc.doc);
        System.out.println(doc.get("fullPath"));
    }
}

From source file:aos.lucene.search.msc.QueryParserTest.java

License:Apache License

public void testToString() throws Exception {
    BooleanQuery query = new BooleanQuery();
    query.add(new FuzzyQuery(new Term("field", "kountry")), BooleanClause.Occur.MUST);
    query.add(new TermQuery(new Term("title", "western")), BooleanClause.Occur.SHOULD);
    assertEquals("both kinds", "+kountry~0.5 title:western", query.toString("field"));
}

From source file:aos.lucene.search.msc.ScoreTest.java

License:Apache License

public void testFuzzy() throws Exception {
    indexSingleFieldDocs(new Field[] { new Field("contents", "fuzzy", Field.Store.YES, Field.Index.ANALYZED),
            new Field("contents", "wuzzy", Field.Store.YES, Field.Index.ANALYZED) });

    IndexSearcher searcher = new IndexSearcher(directory);
    Query query = new FuzzyQuery(new Term("contents", "wuzza"));
    TopDocs matches = searcher.search(query, 10);
    assertEquals("both close enough", 2, matches.totalHits);

    assertTrue("wuzzy closer than fuzzy", matches.scoreDocs[0].score != matches.scoreDocs[1].score);

    Document doc = searcher.doc(matches.scoreDocs[0].doc);
    assertEquals("wuzza bear", "wuzzy", doc.get("contents"));
    searcher.close();//w  ww.  j  av  a2 s. c om
}

From source file:br.ufmt.harmonizacao.implementer.PatenteeSearcher.java

public List<String> search(String field, String value) {
    try {//from  w w w . j  a  va2s. co m
        long start = System.currentTimeMillis();
        TokenStream stream = analyzer.tokenStream(field, new StringReader(value));
        CharTermAttribute attr = stream.getAttribute(CharTermAttribute.class);
        stream.reset();
        String valor = "";
        while (stream.incrementToken()) {
            valor = valor + attr.toString() + ' ';
        }
        BooleanQuery bq = new BooleanQuery();
        BooleanQuery acronymBq = null;
        String query = "";
        BooleanQuery wrapBq = new BooleanQuery();
        String[] tokens = valor.split(" ");
        for (int i = 0; i < tokens.length; i++) {
            if (tokens.length >= 2) {
                acronymBq = new BooleanQuery();
                switch (i) {
                case 0:
                    acronymBq.add(new PrefixQuery(new Term(field, tokens[i])), BooleanClause.Occur.MUST);
                    bq.add(new PrefixQuery(new Term(field, tokens[i])), BooleanClause.Occur.SHOULD);
                    break;
                case 1:
                    acronymBq.add(new FuzzyQuery(new Term(field, tokens[i])), BooleanClause.Occur.MUST_NOT);
                    bq.add(new FuzzyQuery(new Term(field, tokens[i])), BooleanClause.Occur.SHOULD);
                    bq.add(new LengthQuery(field, valor), BooleanClause.Occur.MUST_NOT);
                    break;
                default:
                    break;
                }
            } else {
                if (tokens[i].length() > 3) {
                    bq.add(new FuzzyQuery(new Term(field, tokens[i])), BooleanClause.Occur.MUST);
                } else {
                    bq.add(new TermQuery(new Term(field, tokens[i])), BooleanClause.Occur.MUST);
                }
            }
        }

        stream.end();
        stream.close();
        // Aqui termina
        // Cria uma fuzzyquery, ela que far a busca de aproximao

        wrapBq.add(bq, BooleanClause.Occur.MUST);
        if (acronymBq != null) {
            //new QueryParser(Version.LUCENE_47, field, new StandardAnalyzer(Version.LUCENE_47)).parse(query)
            wrapBq.add(acronymBq, BooleanClause.Occur.MUST_NOT);
        }
        String queryTime = "Tempo para construo da query : " + (System.currentTimeMillis() - start) + "ms";
        // Pegando os documentos encontrado na pesquisa
        start = System.currentTimeMillis();
        ScoreDoc[] hits = searcher.search(wrapBq, 10).scoreDocs;
        String searchTime = "Tempo para busca : " + (System.currentTimeMillis() - start) + "ms";
        List<String> result = new ArrayList<String>();
        result.add(valor);
        if (hits.length > 0) {
            for (int i = 0; i < hits.length; i++) {
                Document hitDoc = searcher.doc(hits[i].doc);
                result.add(hitDoc.get(field));
            }
        }
        result.add(queryTime);
        result.add(searchTime);
        return result;
    } catch (IOException ex) {
        Logger.getLogger(PatenteeSearcher.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:brightsolid.solr.plugins.TestTargetPositionQueryFuzzy.java

License:Apache License

public void testTargetPositionFuzzy() throws Exception {
    FuzzyQuery fq = new FuzzyQuery(new Term("field", "three"));
    SpanQuery stq = new SpanMultiTermQueryWrapper<FuzzyQuery>(fq);
    SpanQuery tpq = new SpanTargetPositionQuery(stq, 1);
    TopDocs td = searcher.search(tpq, 10);

    assertEquals(fieldValue(td, 0), "two threx one");
    assertEquals(3, td.totalHits);//from   w w  w.  j a  v a2 s . co m
}

From source file:com.eclipsesource.connect.search.Searcher.java

License:Open Source License

private void performSearch(LuceneSearchResult searchResult, String query) {
    try (DirectoryReader reader = DirectoryReader.open(directory)) {
        IndexSearcher searcher = new IndexSearcher(reader);
        FuzzyQuery luceneQuery = new FuzzyQuery(new Term(Indexer.CONTENT_KEY, query));
        TopDocs topDocs = searcher.search(luceneQuery, Integer.MAX_VALUE);
        populateSearchResult(searchResult, reader, topDocs);
    } catch (IOException shouldNotHappen) {
        throw new IllegalStateException(shouldNotHappen);
    }//  w w  w  . jav  a  2 s .  com
}

From source file:com.esri.gpt.catalog.lucene.QueryProvider.java

License:Apache License

/**
 * Gets fuzzy query./*  ww  w. ja  va 2s . c  o  m*/
 * @param field field name
 * @param termStr term
 * @param minSimilarity minimal similarity
 * @return query or <code>null</code> if query for the particular field is unavailable
 * @throws ParseException if error creating query
 */
protected Query getFuzzyQuery(String field, String termStr, float minSimilarity) throws ParseException {
    Query q = null;
    PropertyMeaning meaning = resolveMeaning(field);
    if (meaning != null) {
        PropertyComparisonType type = meaning.getComparisonType();

        if (type == PropertyComparisonType.KEYWORD) {
            q = new FuzzyQuery(new Term(field, Val.chkStr(termStr).toLowerCase()));
        }
        if (type == PropertyComparisonType.VALUE) {
            q = new FuzzyQuery(new Term(field, termStr));
        }
    }

    if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.fine("QueryProvider.getFuzzyQuery(" + field + "," + termStr + "," + minSimilarity + ") -> " + q);
    }
    return q;
}

From source file:com.ideabase.dictionary.core.impl.LuceneBasedDictionaryServiceImpl.java

License:Open Source License

private Query buildLuceneQuery(final String pWord) throws ParseException {
    return new FuzzyQuery(new Term(FIELD_WORD, pWord));
}

From source file:com.it.lucene.test.IndexSearch.java

@Test
public void fuzzyQuery() {
    Query query = new FuzzyQuery(new Term("description", "lucene"));
    doSearch(query);/*from   w  w  w  .j ava2 s.co m*/
}

From source file:com.it.lucene.test.IndexSearch.java

@Test
public void testsToString() {
    BooleanQuery query = new BooleanQuery();
    query.add(new FuzzyQuery(new Term("description", "lucene")), Occur.MUST);
    Query q2 = NumericRangeQuery.newFloatRange("price", 50f, 56f, false, true);
    query.add(q2, Occur.MUST);/*from w  w  w. j  ava  2  s  .  co m*/
    System.out.println(query);
}