下面列出了怎么用org.apache.lucene.search.FuzzyQuery的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public List<String> search(final String object) {
if (stopwords.contains(object.toLowerCase())) {
log.debug("\t Stopword detected: |" + object + "|");
return ImmutableList.of();
}
ArrayList<String> uris = Lists.newArrayList();
try {
log.debug("\t start asking index...");
Query q = new FuzzyQuery(new Term(FIELD_NAME_OBJECT, object), 0);
TopScoreDocCollector collector = TopScoreDocCollector.create(numberOfDocsRetrievedFromIndex);
isearcher.search(q, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
for (ScoreDoc hit : hits) {
Document hitDoc = isearcher.doc(hit.doc);
uris.add(hitDoc.get(FIELD_NAME_SUBJECT));
}
log.debug("\t finished asking index...");
} catch (Exception e) {
log.error(e.getLocalizedMessage() + " -> " + object, e);
}
return uris;
}
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();
}
/** 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(expected, parse("foobar~"));
assertEquals(regular, parse("foobar~a"));
assertEquals(regular, parse("foobar~1a"));
BooleanQuery.Builder bool = new BooleanQuery.Builder();
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.build(), parse("foo~" + LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE + 1 + " bar"));
}
public void testSpanNotWithMultiterm() throws Exception {
SpanQuery q = spanNotQuery(
spanTermQuery(field, "r1"),
new SpanMultiTermQueryWrapper<>(new PrefixQuery(new Term(field, "s1"))),3,3);
checkHits(q, new int[] {14});
q = spanNotQuery(
spanTermQuery(field, "r1"),
new SpanMultiTermQueryWrapper<>(new FuzzyQuery(new Term(field, "s12"), 1, 2)),3,3);
checkHits(q, new int[] {14});
q = spanNotQuery(
new SpanMultiTermQueryWrapper<>(new PrefixQuery(new Term(field, "r"))),
spanTermQuery(field, "s21"),3,3);
checkHits(q, new int[] {13});
}
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.count(spanFirst));
WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<>(wcNoSuch);
spanFirst = new SpanFirstQuery(spanWCNoSuch, 10);
assertEquals(0, searcher.count(spanFirst));
RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<>(rgxNoSuch);
spanFirst = new SpanFirstQuery(spanRgxNoSuch, 10);
assertEquals(0, searcher.count(spanFirst));
PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<>(prfxNoSuch);
spanFirst = new SpanFirstQuery(spanPrfxNoSuch, 10);
assertEquals(0, searcher.count(spanFirst));
}
private void addToBooleanQuery(
final List<String> aTermList, final String aFieldName, final BooleanQuery.Builder aQuery, final BooleanClause.Occur aOccour)
throws IOException {
for (final var theTerm : aTermList) {
if (QueryUtils.isWildCard(theTerm)) {
aQuery.add(new WildcardQuery(new Term(aFieldName, theTerm)), aOccour);
} else if (QueryUtils.isFuzzy(theTerm)) {
aQuery.add(new FuzzyQuery(new Term(aFieldName, theTerm)), aOccour);
} else {
final var theTokenizedTerm = toToken(theTerm, aFieldName);
if (!StringUtils.isEmpty(theTokenizedTerm)) {
aQuery.add(new TermQuery(new Term(aFieldName, theTokenizedTerm)), aOccour);
}
}
}
}
protected QueryBuilder convertFuzzyQuery(final QueryContext context, final FuzzyQuery fuzzyQuery, final float boost) {
final Term term = fuzzyQuery.getTerm();
final String field = getSearchField(context, term.field());
// TODO fuzzy value
if (Constants.DEFAULT_FIELD.equals(field)) {
context.addFieldLog(field, term.text());
return buildDefaultQueryBuilder((f, b) -> QueryBuilders.fuzzyQuery(f, term.text())
.fuzziness(Fuzziness.fromEdits(fuzzyQuery.getMaxEdits())).boost(b * boost));
} else if (isSearchField(field)) {
context.addFieldLog(field, term.text());
return QueryBuilders.fuzzyQuery(field, term.text()).boost(boost).fuzziness(Fuzziness.fromEdits(fuzzyQuery.getMaxEdits()));
} else {
final String origQuery = fuzzyQuery.toString();
context.addFieldLog(Constants.DEFAULT_FIELD, origQuery);
context.addHighlightedQuery(origQuery);
return buildDefaultQueryBuilder((f, b) -> QueryBuilders.fuzzyQuery(f, origQuery)
.fuzziness(Fuzziness.fromEdits(fuzzyQuery.getMaxEdits())).boost(b * boost));
}
}
protected Query blendTermQuery(Term term, MappedFieldType fieldType) {
if (fuzziness != null) {
try {
Query query = fieldType.fuzzyQuery(term.text(), fuzziness, fuzzyPrefixLength, maxExpansions, transpositions);
if (query instanceof FuzzyQuery) {
QueryParsers.setRewriteMethod((FuzzyQuery) query, fuzzyRewriteMethod);
}
return query;
} catch (RuntimeException e) {
if (lenient) {
return newLenientFieldQuery(fieldType.name(), e);
} else {
throw e;
}
}
}
return termQuery(fieldType, term.bytes(), lenient);
}
@Test
public void testFuzzyQuery() throws Exception {
// 模糊查询
Query query = new FuzzyQuery(new Term("title", "Stori"));
TopDocs search = searcher.search(query, 1000);
Assert.assertEquals(32, search.totalHits.value);
}
public static ParsedOptions parse(MultiMatchQueryBuilder.Type matchType,
@Nullable Map options) throws IllegalArgumentException {
if (options == null) {
options = Collections.emptyMap();
} else {
// need a copy. Otherwise manipulations on a shared option will lead to strange race conditions.
options = new HashMap(options);
}
ParsedOptions parsedOptions = new ParsedOptions(
floatValue(options, OPTIONS.BOOST, null),
analyzer(options.remove(OPTIONS.ANALYZER)),
zeroTermsQuery(options.remove(OPTIONS.ZERO_TERMS_QUERY)),
intValue(options, OPTIONS.MAX_EXPANSIONS, FuzzyQuery.defaultMaxExpansions),
fuzziness(options.remove(OPTIONS.FUZZINESS)),
intValue(options, OPTIONS.PREFIX_LENGTH, FuzzyQuery.defaultPrefixLength),
transpositions(options.remove(OPTIONS.FUZZY_TRANSPOSITIONS))
);
switch (matchType.matchQueryType()) {
case BOOLEAN:
parsedOptions.commonTermsCutoff(floatValue(options, OPTIONS.CUTOFF_FREQUENCY, null));
parsedOptions.operator(operator(options.remove(OPTIONS.OPERATOR)));
parsedOptions.minimumShouldMatch(minimumShouldMatch(options.remove(OPTIONS.MINIMUM_SHOULD_MATCH)));
break;
case PHRASE:
parsedOptions.phraseSlop(intValue(options, OPTIONS.SLOP, 0));
parsedOptions.tieBreaker(floatValue(options, OPTIONS.TIE_BREAKER, null));
break;
case PHRASE_PREFIX:
parsedOptions.phraseSlop(intValue(options, OPTIONS.SLOP, 0));
parsedOptions.tieBreaker(floatValue(options, OPTIONS.TIE_BREAKER, null));
parsedOptions.rewrite(rewrite(options.remove(OPTIONS.REWRITE)));
break;
}
if (!options.isEmpty()) {
raiseIllegalOptions(matchType, options);
}
return parsedOptions;
}
private Query getFuzzyQuerySingle(String field, String termStr, String minSimilarity) throws ParseException {
currentFieldType = parseContext.fieldMapper(field);
if (currentFieldType != null) {
try {
return currentFieldType.fuzzyQuery(termStr, Fuzziness.build(minSimilarity), fuzzyPrefixLength, settings.fuzzyMaxExpansions(), FuzzyQuery.defaultTranspositions);
} catch (RuntimeException e) {
if (settings.lenient()) {
return null;
}
throw e;
}
}
return super.getFuzzyQuery(field, termStr, Float.parseFloat(minSimilarity));
}
@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;
}
private List<String> getUsingFuzzySearch(String searchQuery) throws IOException {
Query query = new FuzzyQuery(new Term(WORD_FIELD, searchQuery), 2, 0);
List<String> foundWords = new ArrayList<>();
TopDocs rs = searcher.search(query, 2);
for (ScoreDoc docRef : rs.scoreDocs) {
Document docHit = searcher.doc(docRef.doc);
foundWords.add(docHit.get(WORD_FIELD));
}
return foundWords;
}
@Override
public List<String> search(final String object) {
if (stopwords.contains(object.toLowerCase())) {
log.debug("\t Stopword detected: |" + object + "|");
System.out.println("returning immutable empty");
return ImmutableList.of();
}
ArrayList<String> uris = Lists.newArrayList();
try {
log.debug("\t start asking index for |" + object + "|");
Query q = new FuzzyQuery(new Term(FIELD_NAME_OBJECT, object), 0);
TopScoreDocCollector collector = TopScoreDocCollector.create(numberOfDocsRetrievedFromIndex);
isearcher.search(q, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
for (ScoreDoc hit : hits) {
Document hitDoc = isearcher.doc(hit.doc);
log.debug(object + "->" + hitDoc.get(FIELD_NAME_SUBJECT) + ", " + hitDoc.get(FIELD_NAME_OBJECT));
uris.add(hitDoc.get(FIELD_NAME_SUBJECT));
}
log.debug("\t finished asking index...");
} catch (Exception e) {
log.error(e.getLocalizedMessage() + " -> " + object, e);
}
return uris;
}
/**
* Get the minimal similarity for fuzzy queries.
*/
@Override
public float getFuzzyMinSim() {
FuzzyConfig fuzzyConfig = getQueryConfigHandler().get(ConfigurationKeys.FUZZY_CONFIG);
if (fuzzyConfig == null) {
return FuzzyQuery.defaultMaxEdits;
} else {
return fuzzyConfig.getMinSimilarity();
}
}
/**
* Get the prefix length for fuzzy queries.
*
* @return Returns the fuzzyPrefixLength.
*/
@Override
public int getFuzzyPrefixLength() {
FuzzyConfig fuzzyConfig = getQueryConfigHandler().get(ConfigurationKeys.FUZZY_CONFIG);
if (fuzzyConfig == null) {
return FuzzyQuery.defaultPrefixLength;
} else {
return fuzzyConfig.getPrefixLength();
}
}
@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());
}
public void testFuzzy() throws Exception {
FuzzyQuery fq = new FuzzyQuery(new Term("field", "broan"));
SpanQuery sfq = new SpanMultiTermQueryWrapper<>(fq);
// will not match quick brown fox
SpanPositionRangeQuery sprq = new SpanPositionRangeQuery(sfq, 3, 6);
assertEquals(2, searcher.count(sprq));
}
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.count(sprq));
}
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.count(near));
//flip order
near = new SpanNearQuery(new SpanQuery[]{spanNoSuch, term}, 1, true);
assertEquals(0, searcher.count(near));
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.count(near));
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.count(near));
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.count(near));
//test single noSuch
near = new SpanNearQuery(new SpanQuery[]{spanPrfxNoSuch}, 1, true);
assertEquals(0, searcher.count(near));
//test double noSuch
near = new SpanNearQuery(new SpanQuery[]{spanPrfxNoSuch, spanPrfxNoSuch}, 1, true);
assertEquals(0, searcher.count(near));
}
public void testNoSuchMultiTermsInNotNear() throws Exception {
//test to make sure non existent multiterms aren't throwing non-matching field 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"));
SpanNotQuery notNear = new SpanNotQuery(term, spanNoSuch, 0,0);
assertEquals(1, searcher.count(notNear));
//flip
notNear = new SpanNotQuery(spanNoSuch, term, 0,0);
assertEquals(0, searcher.count(notNear));
//both noSuch
notNear = new SpanNotQuery(spanNoSuch, spanNoSuch, 0,0);
assertEquals(0, searcher.count(notNear));
WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<>(wcNoSuch);
notNear = new SpanNotQuery(term, spanWCNoSuch, 0,0);
assertEquals(1, searcher.count(notNear));
RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<>(rgxNoSuch);
notNear = new SpanNotQuery(term, spanRgxNoSuch, 1, 1);
assertEquals(1, searcher.count(notNear));
PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<>(prfxNoSuch);
notNear = new SpanNotQuery(term, spanPrfxNoSuch, 1, 1);
assertEquals(1, searcher.count(notNear));
}
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.count(near));
//flip
near = new SpanOrQuery(new SpanQuery[]{spanNoSuch, term});
assertEquals(1, searcher.count(near));
WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<>(wcNoSuch);
near = new SpanOrQuery(new SpanQuery[]{term, spanWCNoSuch});
assertEquals(1, searcher.count(near));
RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<>(rgxNoSuch);
near = new SpanOrQuery(new SpanQuery[]{term, spanRgxNoSuch});
assertEquals(1, searcher.count(near));
PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<>(prfxNoSuch);
near = new SpanOrQuery(new SpanQuery[]{term, spanPrfxNoSuch});
assertEquals(1, searcher.count(near));
near = new SpanOrQuery(new SpanQuery[]{spanPrfxNoSuch});
assertEquals(0, searcher.count(near));
near = new SpanOrQuery(new SpanQuery[]{spanPrfxNoSuch, spanPrfxNoSuch});
assertEquals(0, searcher.count(near));
}
/**
* Builds a new FuzzyQuery instance
* @param term Term
* @param minimumSimilarity minimum similarity
* @param prefixLength prefix length
* @return new FuzzyQuery Instance
*/
protected Query newFuzzyQuery(Term term, float minimumSimilarity, int prefixLength) {
// FuzzyQuery doesn't yet allow constant score rewrite
String text = term.text();
int numEdits = FuzzyQuery.floatToEdits(minimumSimilarity,
text.codePointCount(0, text.length()));
return new FuzzyQuery(term,numEdits,prefixLength);
}
private boolean containsClause(Query query, String field, String value,
int boost, boolean fuzzy) {
float queryBoost = 1f;
if (query instanceof BoostQuery) {
BoostQuery bq = (BoostQuery) query;
query = bq.getQuery();
queryBoost = bq.getBoost();
}
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 (boost != queryBoost) {
return false;
}
if(query instanceof TermQuery && !fuzzy) {
return containsClause((TermQuery)query, field, value);
}
if(query instanceof FuzzyQuery && fuzzy) {
return containsClause((FuzzyQuery)query, field, value);
}
return false;
}
private boolean containsClause(FuzzyQuery query, String field, String value) {
if(query.getTerm().field().equals(field) &&
query.getTerm().bytes().utf8ToString().equals(value)) {
return true;
}
return false;
}
@Override
public void readFields(DataInput in) throws IOException {
float boost = in.readFloat();
TermWritable termWritable = new TermWritable();
termWritable.readFields(in);
Term term = termWritable.getTerm();
int maxEdits = in.readInt();
int prefixLength = in.readInt();
int maxExpansions = in.readInt();
boolean transpositions = in.readBoolean();
query = new FuzzyQuery(term, maxEdits, prefixLength, maxExpansions, transpositions);
query.setBoost(boost);
}
public static void assertEqualsQuery(Query expected, Query actual) {
assertEquals(expected.getClass(), actual.getClass());
if (expected instanceof BooleanQuery) {
assertEqualsBooleanQuery((BooleanQuery) expected, (BooleanQuery) actual);
} else if (expected instanceof SuperQuery) {
assertEqualsSuperQuery((SuperQuery) expected, (SuperQuery) actual);
} else if (expected instanceof TermQuery) {
assertEqualsTermQuery((TermQuery) expected, (TermQuery) actual);
} else if (expected instanceof PrefixQuery) {
assertEqualsPrefixQuery((PrefixQuery) expected, (PrefixQuery) actual);
} else if (expected instanceof WildcardQuery) {
assertEqualsWildcardQuery((WildcardQuery) expected, (WildcardQuery) actual);
} else if (expected instanceof FuzzyQuery) {
assertEqualsFuzzyQuery((FuzzyQuery) expected, (FuzzyQuery) actual);
} else if (expected instanceof RegexpQuery) {
assertEqualsRegexpQuery((RegexpQuery) expected, (RegexpQuery) actual);
} else if (expected instanceof TermRangeQuery) {
assertEqualsTermRangeQuery((TermRangeQuery) expected, (TermRangeQuery) actual);
} else if (expected instanceof MatchAllDocsQuery) {
assertEqualsMatchAllDocsQuery((MatchAllDocsQuery) expected, (MatchAllDocsQuery) actual);
} else if (expected instanceof MultiPhraseQuery) {
assertEqualsMultiPhraseQuery((MultiPhraseQuery) expected, (MultiPhraseQuery) actual);
} else if (expected instanceof PhraseQuery) {
assertEqualsPhraseQuery((PhraseQuery) expected, (PhraseQuery) actual);
} else if (expected instanceof NumericRangeQuery<?>) {
assertEqualsNumericRangeQuery((NumericRangeQuery<?>) expected, (NumericRangeQuery<?>) actual);
} else {
fail("Type [" + expected.getClass() + "] not supported");
}
}
protected QueryBuilder convertQuery(final QueryContext context, final Query query, final float boost) {
if (query instanceof TermQuery) {
return convertTermQuery(context, (TermQuery) query, boost);
} else if (query instanceof TermRangeQuery) {
return convertTermRangeQuery(context, (TermRangeQuery) query, boost);
} else if (query instanceof PhraseQuery) {
return convertPhraseQuery(context, (PhraseQuery) query, boost);
} else if (query instanceof FuzzyQuery) {
return convertFuzzyQuery(context, (FuzzyQuery) query, boost);
} else if (query instanceof PrefixQuery) {
return convertPrefixQuery(context, (PrefixQuery) query, boost);
} else if (query instanceof WildcardQuery) {
return convertWildcardQuery(context, (WildcardQuery) query, boost);
} else if (query instanceof BooleanQuery) {
final BooleanQuery booleanQuery = (BooleanQuery) query;
return convertBooleanQuery(context, booleanQuery, boost);
} else if (query instanceof MatchAllDocsQuery) {
return QueryBuilders.matchAllQuery();
} else if (query instanceof BoostQuery) {
final BoostQuery boostQuery = (BoostQuery) query;
return convertQuery(context, boostQuery.getQuery(), boostQuery.getBoost());
}
throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnknown(UserMessages.GLOBAL_PROPERTY_KEY), "Unknown q: "
+ query.getClass() + " => " + query);
}
@Test
public void givenFuzzyQueryWhenFetchedDocumentThenCorrect() {
InMemoryLuceneIndex inMemoryLuceneIndex = new InMemoryLuceneIndex(new RAMDirectory(), new StandardAnalyzer());
inMemoryLuceneIndex.indexDocument("article", "Halloween Festival");
inMemoryLuceneIndex.indexDocument("decoration", "Decorations for Halloween");
Term term = new Term("body", "hallowen");
Query query = new FuzzyQuery(term);
List<Document> documents = inMemoryLuceneIndex.searchIndex(query);
Assert.assertEquals(2, documents.size());
}
@Override
public 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);
}