本文整理汇总了Java中org.apache.lucene.search.FuzzyQuery类的典型用法代码示例。如果您正苦于以下问题:Java FuzzyQuery类的具体用法?Java FuzzyQuery怎么用?Java FuzzyQuery使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
FuzzyQuery类属于org.apache.lucene.search包,在下文中一共展示了FuzzyQuery类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addSearchTermQueries
import org.apache.lucene.search.FuzzyQuery; //导入依赖的package包/类
/**
* Adds for every field in {@link IIndexTypeConf} a {@link TermQuery}, {@link PrefixQuery} and
* a {@link FuzzyQuery} with a suitable boost relative to the given base boost.
*
* @param indexType the type in which should be searched.
* @param search the search text.
* @param subQuery the {@link BooleanQuery} to add the sub queries.
* @param baseBoost highest possible boost of the query. The more the match is exact
* than a bigger boost will be used.
*/
private static void addSearchTermQueries(final IIndexTypeConf indexType, final String search,
final BooleanQuery subQuery, final float baseBoost) {
for(final IIndexFieldConf<?> field : indexType.getFields()) {
final Term term = new Term(field.getName(), search);
final TermQuery exactQuery = new TermQuery(term);
exactQuery.setBoost(baseBoost);
subQuery.add(exactQuery, Occur.SHOULD);
final PrefixQuery pfQuery = new PrefixQuery(term);
pfQuery.setBoost(0.7f*baseBoost);
subQuery.add(pfQuery, Occur.SHOULD);
final FuzzyQuery fuzzyQuery = new FuzzyQuery(term);
fuzzyQuery.setBoost(0.5f*baseBoost);
subQuery.add(fuzzyQuery, Occur.SHOULD);
}
}
示例2: getFuzzyQuerySingle
import org.apache.lucene.search.FuzzyQuery; //导入依赖的package包/类
private Query getFuzzyQuerySingle(String field, String termStr, String minSimilarity) throws ParseException {
currentFieldType = context.fieldMapper(field);
if (currentFieldType != null) {
try {
BytesRef term = termStr == null ? null : getAnalyzer().normalize(field, termStr);
return currentFieldType.fuzzyQuery(term, Fuzziness.build(minSimilarity),
getFuzzyPrefixLength(), settings.fuzzyMaxExpansions(), FuzzyQuery.defaultTranspositions);
} catch (RuntimeException e) {
if (settings.lenient()) {
return null;
}
throw e;
}
}
return super.getFuzzyQuery(field, termStr, Float.parseFloat(minSimilarity));
}
示例3: newFuzzyQuery
import org.apache.lucene.search.FuzzyQuery; //导入依赖的package包/类
/**
* Dispatches to Lucene's SimpleQueryParser's newFuzzyQuery, optionally
* lowercasing the term first
*/
@Override
public Query newFuzzyQuery(String text, int fuzziness) {
BooleanQuery.Builder bq = new BooleanQuery.Builder();
bq.setDisableCoord(true);
for (Map.Entry<String,Float> entry : weights.entrySet()) {
final String fieldName = entry.getKey();
try {
final BytesRef term = getAnalyzer().normalize(fieldName, text);
Query query = new FuzzyQuery(new Term(fieldName, term), fuzziness);
bq.add(wrapWithBoost(query, entry.getValue()), BooleanClause.Occur.SHOULD);
} catch (RuntimeException e) {
rethrowUnlessLenient(e);
}
}
return super.simplify(bq.build());
}
示例4: testToQueryWithStringField
import org.apache.lucene.search.FuzzyQuery; //导入依赖的package包/类
public void testToQueryWithStringField() throws IOException {
assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
String query = "{\n" +
" \"fuzzy\":{\n" +
" \"" + STRING_FIELD_NAME + "\":{\n" +
" \"value\":\"sh\",\n" +
" \"fuzziness\": \"AUTO\",\n" +
" \"prefix_length\":1,\n" +
" \"boost\":2.0\n" +
" }\n" +
" }\n" +
"}";
Query parsedQuery = parseQuery(query).toQuery(createShardContext());
assertThat(parsedQuery, instanceOf(BoostQuery.class));
BoostQuery boostQuery = (BoostQuery) parsedQuery;
assertThat(boostQuery.getBoost(), equalTo(2.0f));
assertThat(boostQuery.getQuery(), instanceOf(FuzzyQuery.class));
FuzzyQuery fuzzyQuery = (FuzzyQuery) boostQuery.getQuery();
assertThat(fuzzyQuery.getTerm(), equalTo(new Term(STRING_FIELD_NAME, "sh")));
assertThat(fuzzyQuery.getMaxEdits(), equalTo(Fuzziness.AUTO.asDistance("sh")));
assertThat(fuzzyQuery.getPrefixLength(), equalTo(1));
}
示例5: isFuzzyPhraseBanned
import org.apache.lucene.search.FuzzyQuery; //导入依赖的package包/类
/**
* Checks if the given phrase is banned with fuzzy matching.
*
* @param phrase
* phrase to check
* @return <code>true</code> if phrase is banned
*/
public boolean isFuzzyPhraseBanned(String phrase) {
if (phrase != null) {
String[] parts = phrase.split(" ");
if (parts != null) {
for (String part : parts) {
FuzzyQuery query = new FuzzyQuery(new Term(LUCENE_FIELD_NAME, part), 2);
try {
ScoreDoc[] hits = indexSearcher.search(query, null, 1).scoreDocs;
if (hits.length > 0) {
return true;
}
} catch (Exception ex) {
LOG.error("Error running query", ex);
}
}
}
}
return false;
}
示例6: testFuzzy
import org.apache.lucene.search.FuzzyQuery; //导入依赖的package包/类
/** test a fuzzy query */
public void testFuzzy() throws Exception {
Query regular = new TermQuery(new Term("field", "foobar"));
Query expected = new FuzzyQuery(new Term("field", "foobar"), 2);
assertEquals(expected, parse("foobar~2"));
assertEquals(regular, parse("foobar~"));
assertEquals(regular, parse("foobar~a"));
assertEquals(regular, parse("foobar~1a"));
BooleanQuery bool = new BooleanQuery();
FuzzyQuery fuzzy = new FuzzyQuery(new Term("field", "foo"), LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE);
bool.add(fuzzy, Occur.MUST);
bool.add(new TermQuery(new Term("field", "bar")), Occur.MUST);
assertEquals(bool, parse("foo~" + LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE + 1 + " bar"));
}
示例7: testNoSuchMultiTermsInSpanFirst
import org.apache.lucene.search.FuzzyQuery; //导入依赖的package包/类
public void testNoSuchMultiTermsInSpanFirst() throws Exception {
//this hasn't been a problem
FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<>(fuzzyNoSuch);
SpanQuery spanFirst = new SpanFirstQuery(spanNoSuch, 10);
assertEquals(0, searcher.search(spanFirst, 10).totalHits);
WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<>(wcNoSuch);
spanFirst = new SpanFirstQuery(spanWCNoSuch, 10);
assertEquals(0, searcher.search(spanFirst, 10).totalHits);
RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<>(rgxNoSuch);
spanFirst = new SpanFirstQuery(spanRgxNoSuch, 10);
assertEquals(0, searcher.search(spanFirst, 10).totalHits);
PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<>(prfxNoSuch);
spanFirst = new SpanFirstQuery(spanPrfxNoSuch, 10);
assertEquals(0, searcher.search(spanFirst, 10).totalHits);
}
示例8: containsClause
import org.apache.lucene.search.FuzzyQuery; //导入依赖的package包/类
private boolean containsClause(Query query, String field, String value,
int boost, boolean fuzzy) {
if(query instanceof BooleanQuery) {
return containsClause((BooleanQuery)query, field, value, boost, fuzzy);
}
if(query instanceof DisjunctionMaxQuery) {
return containsClause((DisjunctionMaxQuery)query, field, value, boost, fuzzy);
}
if(query instanceof TermQuery && !fuzzy) {
return containsClause((TermQuery)query, field, value, boost);
}
if(query instanceof FuzzyQuery && fuzzy) {
return containsClause((FuzzyQuery)query, field, value, boost);
}
return false;
}
示例9: toString
import org.apache.lucene.search.FuzzyQuery; //导入依赖的package包/类
protected String toString(FuzzyQuery fuzzyQuery, String field) {
final StringBuilder buffer = new StringBuilder();
Term term = fuzzyQuery.getTerm();
int maxEdits = fuzzyQuery.getMaxEdits();
if (!term.field().equals(field)) {
buffer.append(term.field());
buffer.append(":");
}
buffer.append(term.text());
buffer.append('~');
buffer.append(Integer.toString(maxEdits));
return buffer.toString();
}
示例10: testFuzzyQuery
import org.apache.lucene.search.FuzzyQuery; //导入依赖的package包/类
@Test
public void testFuzzyQuery() throws Exception {
buildIndex(fts);
try {
String userInput = "title";
FuzzyQuery luceneQuery = new FuzzyQuery(new Term("title", userInput), 0.4f);
log.debug("Query=" + luceneQuery.toString());
FullTextQuery ftq = fts.createFullTextQuery(luceneQuery, Dvd.class);
List<Dvd> results = ftq.list();
Assertions.assertThat(results.size()).isEqualTo(5);
// Assertions.assertThat(results.get(0).getTitle()).isEqualTo(titles[0]);
for (Dvd dvd : results) {
log.debug("Title=" + dvd.getTitle());
}
} finally {
for (Object element : fts.createQuery("from " + Dvd.class.getName()).list()) {
fts.delete(element);
}
}
}
示例11: testFuzzy
import org.apache.lucene.search.FuzzyQuery; //导入依赖的package包/类
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);
StoredDocument doc = searcher.doc(matches.scoreDocs[0].doc);
assertEquals("wuzza bear", "wuzzy", doc.get("contents"));
searcher.close();
}
示例12: findSimilarWordsTo
import org.apache.lucene.search.FuzzyQuery; //导入依赖的package包/类
private void findSimilarWordsTo(DirectoryReader reader, IndexSearcher searcher, String word) throws IOException {
FuzzyQuery query = new FuzzyQuery(new Term("word", word), 2); // a missing char counts as a distance of 2
TopDocs topDocs = searcher.search(query, 10);
//System.out.println(topDocs.totalHits + " hits for " + word);
List<SimWord> simWords = findSimilarWordsFor(reader, word, topDocs);
//System.out.println(word + " -> " + String.join(", ", simWords));
for (SimWord simWord : simWords) {
if (word.length() == simWord.word.length()) {
int firstDiffPos = getDiffPos(simWord.word.toLowerCase(), word.toLowerCase());
try {
float dist = keyDistance.getDistance(word.charAt(firstDiffPos), simWord.word.charAt(firstDiffPos));
System.out.println(dist + "; " + word + "; " + simWord);
} catch (Exception e) {
System.err.println("Could not get distance between '" + word + "' and '" + simWord + "':");
e.printStackTrace();
}
} else {
// TODO: these need to be handled, too
//System.out.println("-; " + word + "; " + simWord.word);
}
}
}
示例13: newFuzzyQuery
import org.apache.lucene.search.FuzzyQuery; //导入依赖的package包/类
@Override
protected Query newFuzzyQuery(Term term, float minimumSimilarity, int prefixLength) {
String text = term.text();
int numEdits = FuzzyQuery.floatToEdits(minimumSimilarity, text.codePointCount(0, text.length()));
FuzzyQuery query = new FuzzyQuery(term, numEdits, prefixLength,
settings.fuzzyMaxExpansions(), FuzzyQuery.defaultTranspositions);
QueryParsers.setRewriteMethod(query, settings.fuzzyRewriteMethod());
return query;
}
示例14: fuzzyQuery
import org.apache.lucene.search.FuzzyQuery; //导入依赖的package包/类
@Override
public final Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions,
boolean transpositions) {
failIfNotIndexed();
return new FuzzyQuery(new Term(name(), indexedValueForSearch(value)),
fuzziness.asDistance(BytesRefs.toString(value)), prefixLength, maxExpansions, transpositions);
}
示例15: doAssertLuceneQuery
import org.apache.lucene.search.FuzzyQuery; //导入依赖的package包/类
@Override
protected void doAssertLuceneQuery(MultiMatchQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
// we rely on integration tests for deeper checks here
assertThat(query, either(instanceOf(BoostQuery.class)).or(instanceOf(TermQuery.class)).or(instanceOf(AllTermQuery.class))
.or(instanceOf(BooleanQuery.class)).or(instanceOf(DisjunctionMaxQuery.class))
.or(instanceOf(FuzzyQuery.class)).or(instanceOf(MultiPhrasePrefixQuery.class))
.or(instanceOf(MatchAllDocsQuery.class)).or(instanceOf(ExtendedCommonTermsQuery.class))
.or(instanceOf(MatchNoDocsQuery.class)).or(instanceOf(PhraseQuery.class))
.or(instanceOf(LegacyNumericRangeQuery.class))
.or(instanceOf(PointRangeQuery.class)).or(instanceOf(IndexOrDocValuesQuery.class)));
}