use of org.apache.lucene.search.FuzzyQuery in project elasticsearch by elastic.
the class SimpleQueryParser method newFuzzyQuery.
/**
* 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());
}
use of org.apache.lucene.search.FuzzyQuery in project elasticsearch by elastic.
the class FuzzyQueryBuilder method doToQuery.
@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
Query query = null;
String rewrite = this.rewrite;
if (rewrite == null && context.isFilter()) {
rewrite = QueryParsers.CONSTANT_SCORE.getPreferredName();
}
MappedFieldType fieldType = context.fieldMapper(fieldName);
if (fieldType != null) {
query = fieldType.fuzzyQuery(value, fuzziness, prefixLength, maxExpansions, transpositions);
}
if (query == null) {
int maxEdits = fuzziness.asDistance(BytesRefs.toString(value));
query = new FuzzyQuery(new Term(fieldName, BytesRefs.toBytesRef(value)), maxEdits, prefixLength, maxExpansions, transpositions);
}
if (query instanceof MultiTermQuery) {
MultiTermQuery.RewriteMethod rewriteMethod = QueryParsers.parseRewriteMethod(rewrite, null);
QueryParsers.setRewriteMethod((MultiTermQuery) query, rewriteMethod);
}
return query;
}
use of org.apache.lucene.search.FuzzyQuery in project stargate-core by tuplejump.
the class FuzzyCondition method query.
/**
* {@inheritDoc}
*/
@Override
public Query query(Options schema) {
if (field == null || field.trim().isEmpty()) {
throw new IllegalArgumentException("Field name required");
}
if (value == null || value.trim().isEmpty()) {
throw new IllegalArgumentException("Field value required");
}
if (maxEdits < 0 || maxEdits > 2) {
throw new IllegalArgumentException("max_edits must be between 0 and 2");
}
if (prefixLength < 0) {
throw new IllegalArgumentException("prefix_length must be positive.");
}
if (maxExpansions < 0) {
throw new IllegalArgumentException("max_expansions must be positive.");
}
Properties properties = schema.getProperties(field);
String message;
Type fieldType = properties != null ? properties.getType() : Type.text;
if (fieldType == Type.string || fieldType == Type.text) {
String analyzedValue = analyze(field, value, schema.analyzer);
Term term = new Term(field, analyzedValue);
Query query = new FuzzyQuery(term, maxEdits, prefixLength, maxExpansions, transpositions);
return query;
}
message = String.format("Fuzzy queries cannot be supported for field type %s", fieldType);
throw new UnsupportedOperationException(message);
}
use of org.apache.lucene.search.FuzzyQuery in project lucene-solr by apache.
the class TestExtendedDismaxParser method containsClause.
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;
}
use of org.apache.lucene.search.FuzzyQuery in project lucene-solr by apache.
the class TestQueryWrapperFilter method testBasic.
public void testBasic() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
Document doc = new Document();
doc.add(newTextField("field", "value", Field.Store.NO));
writer.addDocument(doc);
IndexReader reader = writer.getReader();
writer.close();
TermQuery termQuery = new TermQuery(new Term("field", "value"));
// should not throw exception with primitive query
QueryWrapperFilter qwf = new QueryWrapperFilter(termQuery);
IndexSearcher searcher = newSearcher(reader);
TopDocs hits = searcher.search(qwf, 10);
assertEquals(1, hits.totalHits);
hits = searcher.search(new FilterWrapper(qwf), 10);
assertEquals(1, hits.totalHits);
// should not throw exception with complex primitive query
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
booleanQuery.add(termQuery, Occur.MUST);
booleanQuery.add(new TermQuery(new Term("field", "missing")), Occur.MUST_NOT);
qwf = new QueryWrapperFilter(termQuery);
hits = searcher.search(qwf, 10);
assertEquals(1, hits.totalHits);
hits = searcher.search(new FilterWrapper(qwf), 10);
assertEquals(1, hits.totalHits);
// should not throw exception with non primitive Query (doesn't implement
// Query#createWeight)
qwf = new QueryWrapperFilter(new FuzzyQuery(new Term("field", "valu")));
hits = searcher.search(qwf, 10);
assertEquals(1, hits.totalHits);
hits = searcher.search(new FilterWrapper(qwf), 10);
assertEquals(1, hits.totalHits);
// test a query with no hits
termQuery = new TermQuery(new Term("field", "not_exist"));
qwf = new QueryWrapperFilter(termQuery);
hits = searcher.search(qwf, 10);
assertEquals(0, hits.totalHits);
hits = searcher.search(new FilterWrapper(qwf), 10);
assertEquals(0, hits.totalHits);
reader.close();
dir.close();
}
Aggregations