lucene 4: Upgraded FieldMapper.fuzzyQuery to use new FuzzyQuery API

This commit is contained in:
Chris Male 2012-10-31 12:01:11 +13:00 committed by Shay Banon
parent be424c4564
commit 27481800bc
15 changed files with 35 additions and 30 deletions

View File

@ -397,7 +397,8 @@ public class MapperQueryParser extends QueryParser {
currentMapper = fieldMappers.fieldMappers().mapper(); currentMapper = fieldMappers.fieldMappers().mapper();
if (currentMapper != null) { if (currentMapper != null) {
try { try {
Query fuzzyQuery = currentMapper.fuzzyQuery(termStr, minSimilarity, fuzzyPrefixLength, settings.fuzzyMaxExpansions()); //LUCENE 4 UPGRADE I disabled transpositions here by default - maybe this needs to be changed
Query fuzzyQuery = currentMapper.fuzzyQuery(termStr, minSimilarity, fuzzyPrefixLength, settings.fuzzyMaxExpansions(), false);
return wrapSmartNameQuery(fuzzyQuery, fieldMappers, parseContext); return wrapSmartNameQuery(fuzzyQuery, fieldMappers, parseContext);
} catch (RuntimeException e) { } catch (RuntimeException e) {
if (settings.lenient()) { if (settings.lenient()) {

View File

@ -189,9 +189,9 @@ public interface FieldMapper<T> {
*/ */
Query fieldQuery(String value, @Nullable QueryParseContext context); Query fieldQuery(String value, @Nullable QueryParseContext context);
Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions); Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions, boolean transpositions);
Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions); Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions, boolean transpositions);
Query prefixQuery(String value, @Nullable MultiTermQuery.RewriteMethod method, @Nullable QueryParseContext context); Query prefixQuery(String value, @Nullable MultiTermQuery.RewriteMethod method, @Nullable QueryParseContext context);

View File

@ -409,13 +409,15 @@ public abstract class AbstractFieldMapper<T> implements FieldMapper<T>, Mapper {
} }
@Override @Override
public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions) { public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions, boolean transpositions) {
return new FuzzyQuery(names().createIndexNameTerm(indexedValue(value)), Float.parseFloat(minSim), prefixLength, maxExpansions); int edits = FuzzyQuery.floatToEdits(Float.parseFloat(minSim), value.codePointCount(0, value.length()));
return new FuzzyQuery(names.createIndexNameTerm(indexedValue(value)), edits, prefixLength, maxExpansions, transpositions);
} }
@Override @Override
public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions) { public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions, boolean transpositions) {
return new FuzzyQuery(names().createIndexNameTerm(value), (float) minSim, prefixLength, maxExpansions); int edits = FuzzyQuery.floatToEdits((float) minSim, value.codePointCount(0, value.length()));
return new FuzzyQuery(names.createIndexNameTerm(indexedValue(value)), edits, prefixLength, maxExpansions, transpositions);
} }
@Override @Override

View File

@ -145,7 +145,7 @@ public class ByteFieldMapper extends NumberFieldMapper<Byte> {
} }
@Override @Override
public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions) { public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions, boolean transpositions) {
byte iValue = Byte.parseByte(value); byte iValue = Byte.parseByte(value);
byte iSim; byte iSim;
try { try {
@ -160,7 +160,7 @@ public class ByteFieldMapper extends NumberFieldMapper<Byte> {
} }
@Override @Override
public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions) { public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions, boolean transpositions) {
byte iValue = Byte.parseByte(value); byte iValue = Byte.parseByte(value);
byte iSim = (byte) (minSim * dFuzzyFactor); byte iSim = (byte) (minSim * dFuzzyFactor);
return NumericRangeQuery.newIntRange(names.indexName(), precisionStep, return NumericRangeQuery.newIntRange(names.indexName(), precisionStep,

View File

@ -218,7 +218,7 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
} }
@Override @Override
public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions) { public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions, boolean transpositions) {
long iValue = dateMathParser.parse(value, System.currentTimeMillis()); long iValue = dateMathParser.parse(value, System.currentTimeMillis());
long iSim; long iSim;
try { try {
@ -234,7 +234,7 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
} }
@Override @Override
public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions) { public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions, boolean transpositions) {
long iValue = dateMathParser.parse(value, System.currentTimeMillis()); long iValue = dateMathParser.parse(value, System.currentTimeMillis());
long iSim = (long) (minSim * dFuzzyFactor); long iSim = (long) (minSim * dFuzzyFactor);
return NumericRangeQuery.newLongRange(names.indexName(), precisionStep, return NumericRangeQuery.newLongRange(names.indexName(), precisionStep,

View File

@ -148,7 +148,7 @@ public class DoubleFieldMapper extends NumberFieldMapper<Double> {
} }
@Override @Override
public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions) { public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions, boolean transpositions) {
double iValue = Double.parseDouble(value); double iValue = Double.parseDouble(value);
double iSim = Double.parseDouble(minSim); double iSim = Double.parseDouble(minSim);
return NumericRangeQuery.newDoubleRange(names.indexName(), precisionStep, return NumericRangeQuery.newDoubleRange(names.indexName(), precisionStep,
@ -158,7 +158,7 @@ public class DoubleFieldMapper extends NumberFieldMapper<Double> {
} }
@Override @Override
public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions) { public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions, boolean transpositions) {
double iValue = Double.parseDouble(value); double iValue = Double.parseDouble(value);
double iSim = minSim * dFuzzyFactor; double iSim = minSim * dFuzzyFactor;
return NumericRangeQuery.newDoubleRange(names.indexName(), precisionStep, return NumericRangeQuery.newDoubleRange(names.indexName(), precisionStep,

View File

@ -149,7 +149,7 @@ public class FloatFieldMapper extends NumberFieldMapper<Float> {
} }
@Override @Override
public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions) { public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions, boolean transpositions) {
float iValue = Float.parseFloat(value); float iValue = Float.parseFloat(value);
float iSim = Float.parseFloat(minSim); float iSim = Float.parseFloat(minSim);
return NumericRangeQuery.newFloatRange(names.indexName(), precisionStep, return NumericRangeQuery.newFloatRange(names.indexName(), precisionStep,
@ -159,7 +159,7 @@ public class FloatFieldMapper extends NumberFieldMapper<Float> {
} }
@Override @Override
public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions) { public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions, boolean transpositions) {
float iValue = Float.parseFloat(value); float iValue = Float.parseFloat(value);
float iSim = (float) (minSim * dFuzzyFactor); float iSim = (float) (minSim * dFuzzyFactor);
return NumericRangeQuery.newFloatRange(names.indexName(), precisionStep, return NumericRangeQuery.newFloatRange(names.indexName(), precisionStep,

View File

@ -148,7 +148,7 @@ public class IntegerFieldMapper extends NumberFieldMapper<Integer> {
} }
@Override @Override
public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions) { public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions, boolean transpositions) {
int iValue = Integer.parseInt(value); int iValue = Integer.parseInt(value);
int iSim; int iSim;
try { try {
@ -163,7 +163,7 @@ public class IntegerFieldMapper extends NumberFieldMapper<Integer> {
} }
@Override @Override
public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions) { public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions, boolean transpositions) {
int iValue = Integer.parseInt(value); int iValue = Integer.parseInt(value);
int iSim = (int) (minSim * dFuzzyFactor); int iSim = (int) (minSim * dFuzzyFactor);
return NumericRangeQuery.newIntRange(names.indexName(), precisionStep, return NumericRangeQuery.newIntRange(names.indexName(), precisionStep,

View File

@ -149,7 +149,7 @@ public class LongFieldMapper extends NumberFieldMapper<Long> {
} }
@Override @Override
public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions) { public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions, boolean transpositions) {
long iValue = Long.parseLong(value); long iValue = Long.parseLong(value);
long iSim; long iSim;
try { try {
@ -164,7 +164,7 @@ public class LongFieldMapper extends NumberFieldMapper<Long> {
} }
@Override @Override
public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions) { public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions, boolean transpositions) {
long iValue = Long.parseLong(value); long iValue = Long.parseLong(value);
long iSim = (long) (minSim * dFuzzyFactor); long iSim = (long) (minSim * dFuzzyFactor);
return NumericRangeQuery.newLongRange(names.indexName(), precisionStep, return NumericRangeQuery.newLongRange(names.indexName(), precisionStep,

View File

@ -216,10 +216,10 @@ public abstract class NumberFieldMapper<T extends Number> extends AbstractFieldM
} }
@Override @Override
public abstract Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions); public abstract Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions, boolean transpositions);
@Override @Override
public abstract Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions); public abstract Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions, boolean transpositions);
/** /**
* Numeric field level filter are basically range queries with same value and included. That's the recommended * Numeric field level filter are basically range queries with same value and included. That's the recommended

View File

@ -148,7 +148,7 @@ public class ShortFieldMapper extends NumberFieldMapper<Short> {
} }
@Override @Override
public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions) { public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions, boolean transpositions) {
short iValue = Short.parseShort(value); short iValue = Short.parseShort(value);
short iSim; short iSim;
try { try {
@ -163,7 +163,7 @@ public class ShortFieldMapper extends NumberFieldMapper<Short> {
} }
@Override @Override
public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions) { public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions, boolean transpositions) {
short iValue = Short.parseShort(value); short iValue = Short.parseShort(value);
short iSim = (short) (minSim * dFuzzyFactor); short iSim = (short) (minSim * dFuzzyFactor);
return NumericRangeQuery.newIntRange(names.indexName(), precisionStep, return NumericRangeQuery.newIntRange(names.indexName(), precisionStep,

View File

@ -152,7 +152,7 @@ public class BoostFieldMapper extends NumberFieldMapper<Float> implements Intern
} }
@Override @Override
public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions) { public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions, boolean transpositions) {
float iValue = Float.parseFloat(value); float iValue = Float.parseFloat(value);
float iSim = Float.parseFloat(minSim); float iSim = Float.parseFloat(minSim);
return NumericRangeQuery.newFloatRange(names.indexName(), precisionStep, return NumericRangeQuery.newFloatRange(names.indexName(), precisionStep,
@ -162,7 +162,7 @@ public class BoostFieldMapper extends NumberFieldMapper<Float> implements Intern
} }
@Override @Override
public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions) { public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions, boolean transpositions) {
float iValue = Float.parseFloat(value); float iValue = Float.parseFloat(value);
float iSim = (float) (minSim * dFuzzyFactor); float iSim = (float) (minSim * dFuzzyFactor);
return NumericRangeQuery.newFloatRange(names.indexName(), precisionStep, return NumericRangeQuery.newFloatRange(names.indexName(), precisionStep,

View File

@ -189,7 +189,7 @@ public class IpFieldMapper extends NumberFieldMapper<Long> {
} }
@Override @Override
public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions) { public Query fuzzyQuery(String value, String minSim, int prefixLength, int maxExpansions, boolean transpositions) {
long iValue = ipToLong(value); long iValue = ipToLong(value);
long iSim; long iSim;
try { try {
@ -208,8 +208,10 @@ public class IpFieldMapper extends NumberFieldMapper<Long> {
} }
@Override @Override
public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions) { public Query fuzzyQuery(String value, double minSim, int prefixLength, int maxExpansions, boolean transpositions) {
return new FuzzyQuery(names().createIndexNameTerm(value), (float) minSim, prefixLength, maxExpansions); // Lucene 4 Upgrade: It's surprising this uses FuzzyQuery instead of NumericRangeQuery
int edits = FuzzyQuery.floatToEdits((float) minSim, value.codePointCount(0, value.length()));
return new FuzzyQuery(names.createIndexNameTerm(indexedValue(value)), edits, prefixLength, maxExpansions, transpositions);
} }
@Override @Override

View File

@ -109,7 +109,7 @@ public class FuzzyQueryParser implements QueryParser {
MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(fieldName); MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(fieldName);
if (smartNameFieldMappers != null) { if (smartNameFieldMappers != null) {
if (smartNameFieldMappers.hasMapper()) { if (smartNameFieldMappers.hasMapper()) {
query = smartNameFieldMappers.mapper().fuzzyQuery(value, minSimilarity, prefixLength, maxExpansions); query = smartNameFieldMappers.mapper().fuzzyQuery(value, minSimilarity, prefixLength, maxExpansions, transpositions);
} }
} }
if (query == null) { if (query == null) {

View File

@ -371,7 +371,7 @@ public class MatchQuery {
private Query newTermQuery(@Nullable FieldMapper mapper, Term term) { private Query newTermQuery(@Nullable FieldMapper mapper, Term term) {
if (fuzziness != null) { if (fuzziness != null) {
if (mapper != null) { if (mapper != null) {
Query query = mapper.fuzzyQuery(term.text(), fuzziness, fuzzyPrefixLength, maxExpansions); Query query = mapper.fuzzyQuery(term.text(), fuzziness, fuzzyPrefixLength, maxExpansions, transpositions);
if (query instanceof FuzzyQuery) { if (query instanceof FuzzyQuery) {
QueryParsers.setRewriteMethod((FuzzyQuery) query, fuzzyRewriteMethod); QueryParsers.setRewriteMethod((FuzzyQuery) query, fuzzyRewriteMethod);
} }