From 92c162d5345a7b3657651c12bca77384f70bb437 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Thu, 15 Dec 2011 21:24:28 +0200 Subject: [PATCH] optimize field query/filter for numerics to only parse the string representation once --- .../index/mapper/core/ByteFieldMapper.java | 15 +++++++++++++++ .../index/mapper/core/DoubleFieldMapper.java | 16 ++++++++++++++++ .../index/mapper/core/FloatFieldMapper.java | 16 ++++++++++++++++ .../index/mapper/core/IntegerFieldMapper.java | 16 ++++++++++++++++ .../index/mapper/core/LongFieldMapper.java | 16 ++++++++++++++++ .../index/mapper/core/ShortFieldMapper.java | 16 ++++++++++++++++ 6 files changed, 95 insertions(+) diff --git a/src/main/java/org/elasticsearch/index/mapper/core/ByteFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/ByteFieldMapper.java index 29eaea76cba..6601558fd4d 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/ByteFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/ByteFieldMapper.java @@ -27,6 +27,7 @@ import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.util.NumericUtils; +import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -35,6 +36,7 @@ import org.elasticsearch.index.analysis.NumericIntegerAnalyzer; import org.elasticsearch.index.cache.field.data.FieldDataCache; import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.mapper.*; +import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.search.NumericRangeFieldDataFilter; import java.io.IOException; @@ -157,6 +159,12 @@ public class ByteFieldMapper extends NumberFieldMapper { true, true); } + @Override + public Query fieldQuery(String value, @Nullable QueryParseContext context) { + int iValue = Integer.parseInt(value); + return NumericRangeQuery.newIntRange(names.indexName(), precisionStep, + iValue, iValue, true, true); + } @Override public Query rangeQuery(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper) { @@ -166,6 +174,13 @@ public class ByteFieldMapper extends NumberFieldMapper { includeLower, includeUpper); } + @Override + public Filter fieldFilter(String value, @Nullable QueryParseContext context) { + int iValue = Integer.parseInt(value); + return NumericRangeFilter.newIntRange(names.indexName(), precisionStep, + iValue, iValue, true, true); + } + @Override public Filter rangeFilter(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper) { return NumericRangeFilter.newIntRange(names.indexName(), precisionStep, diff --git a/src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.java index 80103d406f3..eab5ae5fb3e 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.java @@ -27,6 +27,7 @@ import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.util.NumericUtils; +import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Numbers; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -35,6 +36,7 @@ import org.elasticsearch.index.analysis.NumericDoubleAnalyzer; import org.elasticsearch.index.cache.field.data.FieldDataCache; import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.mapper.*; +import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.search.NumericRangeFieldDataFilter; import java.io.IOException; @@ -154,6 +156,13 @@ public class DoubleFieldMapper extends NumberFieldMapper { true, true); } + @Override + public Query fieldQuery(String value, @Nullable QueryParseContext context) { + double dValue = Double.parseDouble(value); + return NumericRangeQuery.newDoubleRange(names.indexName(), precisionStep, + dValue, dValue, true, true); + } + @Override public Query rangeQuery(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper) { return NumericRangeQuery.newDoubleRange(names.indexName(), precisionStep, @@ -162,6 +171,13 @@ public class DoubleFieldMapper extends NumberFieldMapper { includeLower, includeUpper); } + @Override + public Filter fieldFilter(String value, @Nullable QueryParseContext context) { + double dValue = Double.parseDouble(value); + return NumericRangeFilter.newDoubleRange(names.indexName(), precisionStep, + dValue, dValue, true, true); + } + @Override public Filter rangeFilter(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper) { return NumericRangeFilter.newDoubleRange(names.indexName(), precisionStep, diff --git a/src/main/java/org/elasticsearch/index/mapper/core/FloatFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/FloatFieldMapper.java index e665744299f..0e3daec618c 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/FloatFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/FloatFieldMapper.java @@ -27,6 +27,7 @@ import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.util.NumericUtils; +import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Numbers; import org.elasticsearch.common.Strings; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -36,6 +37,7 @@ import org.elasticsearch.index.analysis.NumericFloatAnalyzer; import org.elasticsearch.index.cache.field.data.FieldDataCache; import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.mapper.*; +import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.search.NumericRangeFieldDataFilter; import java.io.IOException; @@ -153,6 +155,13 @@ public class FloatFieldMapper extends NumberFieldMapper { true, true); } + @Override + public Query fieldQuery(String value, @Nullable QueryParseContext context) { + float fValue = Float.parseFloat(value); + return NumericRangeQuery.newFloatRange(names.indexName(), precisionStep, + fValue, fValue, true, true); + } + @Override public Query rangeQuery(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper) { return NumericRangeQuery.newFloatRange(names.indexName(), precisionStep, @@ -161,6 +170,13 @@ public class FloatFieldMapper extends NumberFieldMapper { includeLower, includeUpper); } + @Override + public Filter fieldFilter(String value, @Nullable QueryParseContext context) { + float fValue = Float.parseFloat(value); + return NumericRangeFilter.newFloatRange(names.indexName(), precisionStep, + fValue, fValue, true, true); + } + @Override public Filter rangeFilter(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper) { return NumericRangeFilter.newFloatRange(names.indexName(), precisionStep, diff --git a/src/main/java/org/elasticsearch/index/mapper/core/IntegerFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/IntegerFieldMapper.java index 50b1d3edc63..6f63d089380 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/IntegerFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/IntegerFieldMapper.java @@ -27,6 +27,7 @@ import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.util.NumericUtils; +import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Numbers; import org.elasticsearch.common.Strings; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -36,6 +37,7 @@ import org.elasticsearch.index.analysis.NumericIntegerAnalyzer; import org.elasticsearch.index.cache.field.data.FieldDataCache; import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.mapper.*; +import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.search.NumericRangeFieldDataFilter; import java.io.IOException; @@ -158,6 +160,13 @@ public class IntegerFieldMapper extends NumberFieldMapper { true, true); } + @Override + public Query fieldQuery(String value, @Nullable QueryParseContext context) { + int iValue = Integer.parseInt(value); + return NumericRangeQuery.newIntRange(names.indexName(), precisionStep, + iValue, iValue, true, true); + } + @Override public Query rangeQuery(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper) { return NumericRangeQuery.newIntRange(names.indexName(), precisionStep, @@ -166,6 +175,13 @@ public class IntegerFieldMapper extends NumberFieldMapper { includeLower, includeUpper); } + @Override + public Filter fieldFilter(String value, @Nullable QueryParseContext context) { + int iValue = Integer.parseInt(value); + return NumericRangeFilter.newIntRange(names.indexName(), precisionStep, + iValue, iValue, true, true); + } + @Override public Filter rangeFilter(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper) { return NumericRangeFilter.newIntRange(names.indexName(), precisionStep, diff --git a/src/main/java/org/elasticsearch/index/mapper/core/LongFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/LongFieldMapper.java index b970500c030..22e83231c65 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/LongFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/LongFieldMapper.java @@ -27,6 +27,7 @@ import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.util.NumericUtils; +import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Numbers; import org.elasticsearch.common.Strings; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -36,6 +37,7 @@ import org.elasticsearch.index.analysis.NumericLongAnalyzer; import org.elasticsearch.index.cache.field.data.FieldDataCache; import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.mapper.*; +import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.search.NumericRangeFieldDataFilter; import java.io.IOException; @@ -158,6 +160,13 @@ public class LongFieldMapper extends NumberFieldMapper { true, true); } + @Override + public Query fieldQuery(String value, @Nullable QueryParseContext context) { + long iValue = Long.parseLong(value); + return NumericRangeQuery.newLongRange(names.indexName(), precisionStep, + iValue, iValue, true, true); + } + @Override public Query rangeQuery(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper) { return NumericRangeQuery.newLongRange(names.indexName(), precisionStep, @@ -166,6 +175,13 @@ public class LongFieldMapper extends NumberFieldMapper { includeLower, includeUpper); } + @Override + public Filter fieldFilter(String value, @Nullable QueryParseContext context) { + long iValue = Long.parseLong(value); + return NumericRangeFilter.newLongRange(names.indexName(), precisionStep, + iValue, iValue, true, true); + } + @Override public Filter rangeFilter(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper) { return NumericRangeFilter.newLongRange(names.indexName(), precisionStep, diff --git a/src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.java index 28b5a66d735..0202b715a1a 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.java @@ -27,6 +27,7 @@ import org.apache.lucene.search.NumericRangeFilter; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.util.NumericUtils; +import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Numbers; import org.elasticsearch.common.Strings; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -36,6 +37,7 @@ import org.elasticsearch.index.analysis.NumericIntegerAnalyzer; import org.elasticsearch.index.cache.field.data.FieldDataCache; import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.mapper.*; +import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.search.NumericRangeFieldDataFilter; import java.io.IOException; @@ -158,6 +160,13 @@ public class ShortFieldMapper extends NumberFieldMapper { true, true); } + @Override + public Query fieldQuery(String value, @Nullable QueryParseContext context) { + int iValue = Integer.parseInt(value); + return NumericRangeQuery.newIntRange(names.indexName(), precisionStep, + iValue, iValue, true, true); + } + @Override public Query rangeQuery(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper) { return NumericRangeQuery.newIntRange(names.indexName(), precisionStep, @@ -166,6 +175,13 @@ public class ShortFieldMapper extends NumberFieldMapper { includeLower, includeUpper); } + @Override + public Filter fieldFilter(String value, @Nullable QueryParseContext context) { + int iValue = Integer.parseInt(value); + return NumericRangeFilter.newIntRange(names.indexName(), precisionStep, + iValue, iValue, true, true); + } + @Override public Filter rangeFilter(String lowerTerm, String upperTerm, boolean includeLower, boolean includeUpper) { return NumericRangeFilter.newIntRange(names.indexName(), precisionStep,