浏览代码

Dry up IdFieldMapper implementations (#105586)

The field type is almost the same code across both implementations.
Armin Braun 1 年之前
父节点
当前提交
6a8c1d0480

+ 59 - 0
server/src/main/java/org/elasticsearch/index/mapper/IdFieldMapper.java

@@ -10,9 +10,17 @@ package org.elasticsearch.index.mapper;
 
 import org.apache.lucene.document.Field;
 import org.apache.lucene.document.StringField;
+import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.TermInSetQuery;
+import org.apache.lucene.util.BytesRef;
 import org.elasticsearch.common.lucene.Lucene;
 import org.elasticsearch.index.analysis.NamedAnalyzer;
+import org.elasticsearch.index.query.SearchExecutionContext;
 
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.Map;
 
 /**
@@ -72,4 +80,55 @@ public abstract class IdFieldMapper extends MetadataFieldMapper {
     public static Field standardIdField(String id) {
         return new StringField(NAME, Uid.encodeId(id), Field.Store.YES);
     }
+
+    protected abstract static class AbstractIdFieldType extends TermBasedFieldType {
+
+        public AbstractIdFieldType() {
+            super(NAME, true, true, false, TextSearchInfo.SIMPLE_MATCH_ONLY, Collections.emptyMap());
+        }
+
+        @Override
+        public String typeName() {
+            return CONTENT_TYPE;
+        }
+
+        @Override
+        public boolean isSearchable() {
+            // The _id field is always searchable.
+            return true;
+        }
+
+        @Override
+        public Query termsQuery(Collection<?> values, SearchExecutionContext context) {
+            failIfNotIndexed();
+            BytesRef[] bytesRefs = values.stream().map(v -> {
+                Object idObject = v;
+                if (idObject instanceof BytesRef) {
+                    idObject = ((BytesRef) idObject).utf8ToString();
+                }
+                return Uid.encodeId(idObject.toString());
+            }).toArray(BytesRef[]::new);
+            return new TermInSetQuery(name(), bytesRefs);
+        }
+
+        @Override
+        public Query termQuery(Object value, SearchExecutionContext context) {
+            return termsQuery(Arrays.asList(value), context);
+        }
+
+        @Override
+        public Query existsQuery(SearchExecutionContext context) {
+            return new MatchAllDocsQuery();
+        }
+
+        @Override
+        public BlockLoader blockLoader(BlockLoaderContext blContext) {
+            return new BlockStoredFieldsReader.IdBlockLoader();
+        }
+
+        @Override
+        public ValueFetcher valueFetcher(SearchExecutionContext context, String format) {
+            return new StoredValueFetcher(context.lookup(), NAME);
+        }
+    }
 }

+ 1 - 51
server/src/main/java/org/elasticsearch/index/mapper/ProvidedIdFieldMapper.java

@@ -9,10 +9,7 @@
 package org.elasticsearch.index.mapper;
 
 import org.apache.lucene.index.LeafReaderContext;
-import org.apache.lucene.search.MatchAllDocsQuery;
-import org.apache.lucene.search.Query;
 import org.apache.lucene.search.SortField;
-import org.apache.lucene.search.TermInSetQuery;
 import org.apache.lucene.util.BytesRef;
 import org.elasticsearch.common.logging.DeprecationCategory;
 import org.elasticsearch.common.logging.DeprecationLogger;
@@ -41,8 +38,6 @@ import org.elasticsearch.search.sort.SortOrder;
 
 import java.io.IOException;
 import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
 import java.util.function.BooleanSupplier;
 
 /**
@@ -59,69 +54,24 @@ public class ProvidedIdFieldMapper extends IdFieldMapper {
 
     public static final ProvidedIdFieldMapper NO_FIELD_DATA = new ProvidedIdFieldMapper(() -> false);
 
-    static final class IdFieldType extends TermBasedFieldType {
+    static final class IdFieldType extends AbstractIdFieldType {
 
         private final BooleanSupplier fieldDataEnabled;
 
         IdFieldType(BooleanSupplier fieldDataEnabled) {
-            super(NAME, true, true, false, TextSearchInfo.SIMPLE_MATCH_ONLY, Collections.emptyMap());
             this.fieldDataEnabled = fieldDataEnabled;
         }
 
-        @Override
-        public String typeName() {
-            return CONTENT_TYPE;
-        }
-
-        @Override
-        public boolean isSearchable() {
-            // The _id field is always searchable.
-            return true;
-        }
-
         @Override
         public boolean mayExistInIndex(SearchExecutionContext context) {
             return true;
         }
 
-        @Override
-        public ValueFetcher valueFetcher(SearchExecutionContext context, String format) {
-            return new StoredValueFetcher(context.lookup(), NAME);
-        }
-
         @Override
         public boolean isAggregatable() {
             return fieldDataEnabled.getAsBoolean();
         }
 
-        @Override
-        public Query termQuery(Object value, SearchExecutionContext context) {
-            return termsQuery(Arrays.asList(value), context);
-        }
-
-        @Override
-        public Query existsQuery(SearchExecutionContext context) {
-            return new MatchAllDocsQuery();
-        }
-
-        @Override
-        public Query termsQuery(Collection<?> values, SearchExecutionContext context) {
-            failIfNotIndexed();
-            BytesRef[] bytesRefs = values.stream().map(v -> {
-                Object idObject = v;
-                if (idObject instanceof BytesRef) {
-                    idObject = ((BytesRef) idObject).utf8ToString();
-                }
-                return Uid.encodeId(idObject.toString());
-            }).toArray(BytesRef[]::new);
-            return new TermInSetQuery(name(), bytesRefs);
-        }
-
-        @Override
-        public BlockLoader blockLoader(BlockLoaderContext blContext) {
-            return new BlockStoredFieldsReader.IdBlockLoader();
-        }
-
         @Override
         public IndexFieldData.Builder fielddataBuilder(FieldDataContext fieldDataContext) {
             if (fieldDataEnabled.getAsBoolean() == false) {

+ 6 - 67
server/src/main/java/org/elasticsearch/index/mapper/TsidExtractingIdFieldMapper.java

@@ -11,9 +11,6 @@ package org.elasticsearch.index.mapper;
 import org.apache.lucene.document.Field;
 import org.apache.lucene.document.StringField;
 import org.apache.lucene.index.IndexableField;
-import org.apache.lucene.search.MatchAllDocsQuery;
-import org.apache.lucene.search.Query;
-import org.apache.lucene.search.TermInSetQuery;
 import org.apache.lucene.util.BytesRef;
 import org.elasticsearch.cluster.routing.IndexRouting;
 import org.elasticsearch.common.hash.MurmurHash3;
@@ -21,11 +18,7 @@ import org.elasticsearch.common.hash.MurmurHash3.Hash128;
 import org.elasticsearch.common.util.ByteUtils;
 import org.elasticsearch.index.fielddata.FieldDataContext;
 import org.elasticsearch.index.fielddata.IndexFieldData;
-import org.elasticsearch.index.query.SearchExecutionContext;
 
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
 import java.util.Locale;
 
 /**
@@ -40,67 +33,13 @@ public class TsidExtractingIdFieldMapper extends IdFieldMapper {
 
     public static final TsidExtractingIdFieldMapper INSTANCE = new TsidExtractingIdFieldMapper();
 
-    public static final TypeParser PARSER = new FixedTypeParser(MappingParserContext::idFieldMapper);
-    // NOTE: we use a prefix when hashing the tsid field so to be able later on (for instance for debugging purposes)
-    // to query documents whose tsid has been hashed. Using a prefix allows us to query using the prefix.
-
-    static final class IdFieldType extends TermBasedFieldType {
-        IdFieldType() {
-            super(NAME, true, true, false, TextSearchInfo.SIMPLE_MATCH_ONLY, Collections.emptyMap());
-        }
-
-        @Override
-        public String typeName() {
-            return CONTENT_TYPE;
-        }
-
-        @Override
-        public boolean isSearchable() {
-            // The _id field is always searchable.
-            return true;
-        }
-
-        @Override
-        public ValueFetcher valueFetcher(SearchExecutionContext context, String format) {
-            return new StoredValueFetcher(context.lookup(), NAME);
-        }
-
-        @Override
-        public Query termQuery(Object value, SearchExecutionContext context) {
-            return termsQuery(Arrays.asList(value), context);
-        }
-
-        @Override
-        public Query existsQuery(SearchExecutionContext context) {
-            return new MatchAllDocsQuery();
-        }
-
-        @Override
-        public Query termsQuery(Collection<?> values, SearchExecutionContext context) {
-            failIfNotIndexed();
-            BytesRef[] bytesRefs = values.stream().map(v -> {
-                Object idObject = v;
-                if (idObject instanceof BytesRef) {
-                    idObject = ((BytesRef) idObject).utf8ToString();
-                }
-                return Uid.encodeId(idObject.toString());
-            }).toArray(BytesRef[]::new);
-            return new TermInSetQuery(name(), bytesRefs);
-        }
-
-        @Override
-        public BlockLoader blockLoader(BlockLoaderContext blContext) {
-            return new BlockStoredFieldsReader.IdBlockLoader();
-        }
-
-        @Override
-        public IndexFieldData.Builder fielddataBuilder(FieldDataContext fieldDataContext) {
-            throw new IllegalArgumentException("Fielddata is not supported on [_id] field in [time_series] indices");
-        }
-    }
-
     private TsidExtractingIdFieldMapper() {
-        super(new IdFieldType());
+        super(new AbstractIdFieldType() {
+            @Override
+            public IndexFieldData.Builder fielddataBuilder(FieldDataContext fieldDataContext) {
+                throw new IllegalArgumentException("Fielddata is not supported on [_id] field in [time_series] indices");
+            }
+        });
     }
 
     private static final long SEED = 0;

+ 2 - 3
server/src/test/java/org/elasticsearch/index/mapper/IdFieldTypeTests.java

@@ -23,7 +23,7 @@ public class IdFieldTypeTests extends ESTestCase {
     public void testRangeQuery() {
         MappedFieldType ft = randomBoolean()
             ? new ProvidedIdFieldMapper.IdFieldType(() -> false)
-            : new TsidExtractingIdFieldMapper.IdFieldType();
+            : TsidExtractingIdFieldMapper.INSTANCE.fieldType();
         IllegalArgumentException e = expectThrows(
             IllegalArgumentException.class,
             () -> ft.rangeQuery(null, null, randomBoolean(), randomBoolean(), null, null, null, null)
@@ -58,7 +58,6 @@ public class IdFieldTypeTests extends ESTestCase {
         ft = new ProvidedIdFieldMapper.IdFieldType(() -> true);
         assertTrue(ft.isAggregatable());
 
-        ft = new TsidExtractingIdFieldMapper.IdFieldType();
-        assertFalse(ft.isAggregatable());
+        assertFalse(TsidExtractingIdFieldMapper.INSTANCE.fieldType().isAggregatable());
     }
 }