Skip to content

Apache Lucene open-source search software

SOLR-9579: SchemaField should implement lucene.IndexableFieldType to avoid repeated creation

yonik98191225eb3e
diff --git a/lucene/core/src/java/org/apache/lucene/document/BinaryPoint.java b/lucene/core/src/java/org/apache/lucene/document/BinaryPoint.java
index 338817081d1..81ef0920295 100644
--- a/lucene/core/src/java/org/apache/lucene/document/BinaryPoint.java
+++ b/lucene/core/src/java/org/apache/lucene/document/BinaryPoint.java
@@ -19,6 +19,7 @@ package org.apache.lucene.document;
import java.util.Arrays;
import java.util.Comparator;
+import org.apache.lucene.index.IndexableFieldType;
import org.apache.lucene.index.PointValues;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.PointInSetQuery;
@@ -121,7 +122,7 @@ public final class BinaryPoint extends Field {
}
/** Expert API */
- public BinaryPoint(String name, byte[] packedPoint, FieldType type) {
+ public BinaryPoint(String name, byte[] packedPoint, IndexableFieldType type) {
super(name, packedPoint, type);
if (packedPoint.length != type.pointDimensionCount() * type.pointNumBytes()) {
throw new IllegalArgumentException("packedPoint is length=" + packedPoint.length + " but type.pointDimensionCount()=" + type.pointDimensionCount() + " and type.pointNumBytes()=" + type.pointNumBytes());
diff --git a/lucene/core/src/java/org/apache/lucene/document/Field.java b/lucene/core/src/java/org/apache/lucene/document/Field.java
index 8f5f8692d66..fa3f5a73d98 100644
--- a/lucene/core/src/java/org/apache/lucene/document/Field.java
+++ b/lucene/core/src/java/org/apache/lucene/document/Field.java
@@ -64,7 +64,7 @@ public class Field implements IndexableField {
/**
* Field's type
*/
- protected final FieldType type;
+ protected final IndexableFieldType type;
/**
* Field's name
@@ -94,7 +94,7 @@ public class Field implements IndexableField {
* @throws IllegalArgumentException if either the name or type
* is null.
*/
- protected Field(String name, FieldType type) {
+ protected Field(String name, IndexableFieldType type) {
if (name == null) {
throw new IllegalArgumentException("name must not be null");
}
@@ -115,7 +115,7 @@ public class Field implements IndexableField {
* if tokenized() is false.
* @throws NullPointerException if the reader is null
*/
- public Field(String name, Reader reader, FieldType type) {
+ public Field(String name, Reader reader, IndexableFieldType type) {
if (name == null) {
throw new IllegalArgumentException("name must not be null");
}
@@ -147,7 +147,7 @@ public class Field implements IndexableField {
* if tokenized() is false, or if indexed() is false.
* @throws NullPointerException if the tokenStream is null
*/
- public Field(String name, TokenStream tokenStream, FieldType type) {
+ public Field(String name, TokenStream tokenStream, IndexableFieldType type) {
if (name == null) {
throw new IllegalArgumentException("name must not be null");
}
@@ -179,7 +179,7 @@ public class Field implements IndexableField {
* or the field's type is indexed()
* @throws NullPointerException if the type is null
*/
- public Field(String name, byte[] value, FieldType type) {
+ public Field(String name, byte[] value, IndexableFieldType type) {
this(name, value, 0, value.length, type);
}
@@ -197,7 +197,7 @@ public class Field implements IndexableField {
* or the field's type is indexed()
* @throws NullPointerException if the type is null
*/
- public Field(String name, byte[] value, int offset, int length, FieldType type) {
+ public Field(String name, byte[] value, int offset, int length, IndexableFieldType type) {
this(name, new BytesRef(value, offset, length), type);
}
@@ -213,7 +213,7 @@ public class Field implements IndexableField {
* or the field's type is indexed()
* @throws NullPointerException if the type is null
*/
- public Field(String name, BytesRef bytes, FieldType type) {
+ public Field(String name, BytesRef bytes, IndexableFieldType type) {
if (name == null) {
throw new IllegalArgumentException("name must not be null");
}
@@ -237,7 +237,7 @@ public class Field implements IndexableField {
* or if indexed() is false but storeTermVectors() is true.
* @throws NullPointerException if the type is null
*/
- public Field(String name, String value, FieldType type) {
+ public Field(String name, String value, IndexableFieldType type) {
if (name == null) {
throw new IllegalArgumentException("name must not be null");
}
@@ -496,7 +496,7 @@ public class Field implements IndexableField {
/** Returns the {@link FieldType} for this field. */
@Override
- public FieldType fieldType() {
+ public IndexableFieldType fieldType() {
return type;
}
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestBagOfPositions.java b/lucene/core/src/test/org/apache/lucene/index/TestBagOfPositions.java
index 164840885a5..6214e4cde1d 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestBagOfPositions.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestBagOfPositions.java
@@ -28,6 +28,7 @@ import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
+import org.apache.lucene.document.TextField;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.LuceneTestCase.SuppressCodecs;
@@ -77,8 +78,7 @@ public class TestBagOfPositions extends LuceneTestCase {
System.out.println("threadCount=" + threadCount);
}
- Field prototype = newTextField("field", "", Field.Store.NO);
- FieldType fieldType = new FieldType(prototype.fieldType());
+ FieldType fieldType = new FieldType(TextField.TYPE_NOT_STORED);
if (random().nextBoolean()) {
fieldType.setOmitNorms(true);
}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/analysis/BaseTokenStreamTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/analysis/BaseTokenStreamTestCase.java
index 0bb623fe36d..5fd2fef189c 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/analysis/BaseTokenStreamTestCase.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/analysis/BaseTokenStreamTestCase.java
@@ -34,6 +34,7 @@ import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexOptions;
+import org.apache.lucene.index.IndexableFieldType;
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.Attribute;
@@ -628,7 +629,7 @@ public abstract class BaseTokenStreamTestCase extends LuceneTestCase {
if (iw != null) {
if (random.nextInt(7) == 0) {
// pile up a multivalued field
- FieldType ft = field.fieldType();
+ IndexableFieldType ft = field.fieldType();
currentField = new Field("dummy", bogus, ft);
doc.add(currentField);
} else {
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 74851a71b0d..271cec48cf4 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -53,6 +53,10 @@ Optimizations
check on every request and move connection lifecycle management towards the client.
(Ryan Zezeski, Mark Miller, Shawn Heisey, Steve Davids)
+* SOLR-9579: Make Solr's SchemaField implement Lucene's IndexableFieldType, removing the
+ creation of a Lucene FieldType every time a field is indexed. (John Call, yonik)
+
+
================== 6.3.0 ==================
Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release.
diff --git a/solr/core/src/java/org/apache/solr/schema/CurrencyField.java b/solr/core/src/java/org/apache/solr/schema/CurrencyField.java
index 787599a1ccb..7ad285b9ac8 100644
--- a/solr/core/src/java/org/apache/solr/schema/CurrencyField.java
+++ b/solr/core/src/java/org/apache/solr/schema/CurrencyField.java
@@ -35,6 +35,7 @@ import javax.xml.xpath.XPathFactory;
import org.apache.lucene.analysis.util.ResourceLoader;
import org.apache.lucene.analysis.util.ResourceLoaderAware;
+import org.apache.lucene.document.StoredField;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.queries.function.FunctionValues;
@@ -180,14 +181,11 @@ public class CurrencyField extends FieldType implements SchemaAware, ResourceLoa
f.add(currencyField.createField(value.getCurrencyCode(), currencyField.indexed() && !currencyField.omitNorms() ? boost : 1F));
if (field.stored()) {
- org.apache.lucene.document.FieldType customType = new org.apache.lucene.document.FieldType();
- assert !customType.omitNorms();
- customType.setStored(true);
String storedValue = externalVal.toString().trim();
if (storedValue.indexOf(",") < 0) {
storedValue += "," + defaultCurrency;
}
- f.add(createField(field.getName(), storedValue, customType, 1F));
+ f.add(createField(field.getName(), storedValue, StoredField.TYPE, 1F));
}
return f;
diff --git a/solr/core/src/java/org/apache/solr/schema/EnumField.java b/solr/core/src/java/org/apache/solr/schema/EnumField.java
index 4820e77bb11..16c69593197 100644
--- a/solr/core/src/java/org/apache/solr/schema/EnumField.java
+++ b/solr/core/src/java/org/apache/solr/schema/EnumField.java
@@ -387,13 +387,12 @@ public class EnumField extends PrimitiveFieldType {
if (intValue == null || intValue.equals(DEFAULT_VALUE))
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown value for enum field: " + value.toString());
- String intAsString = intValue.toString();
final LegacyFieldType newType = new LegacyFieldType();
newType.setTokenized(field.isTokenized());
newType.setStored(field.stored());
newType.setOmitNorms(field.omitNorms());
- newType.setIndexOptions(field.indexed() ? getIndexOptions(field, intAsString) : IndexOptions.NONE);
+ newType.setIndexOptions(field.indexOptions());
newType.setStoreTermVectors(field.storeTermVector());
newType.setStoreTermVectorOffsets(field.storeTermOffsets());
newType.setStoreTermVectorPositions(field.storeTermPositions());
diff --git a/solr/core/src/java/org/apache/solr/schema/FieldType.java b/solr/core/src/java/org/apache/solr/schema/FieldType.java
index 32a91d98316..192e7b36454 100644
--- a/solr/core/src/java/org/apache/solr/schema/FieldType.java
+++ b/solr/core/src/java/org/apache/solr/schema/FieldType.java
@@ -271,17 +271,16 @@ public abstract class FieldType extends FieldProperties {
}
if (val==null) return null;
- org.apache.lucene.document.FieldType newType = new org.apache.lucene.document.FieldType();
- newType.setTokenized(field.isTokenized());
- newType.setStored(field.stored());
- newType.setOmitNorms(field.omitNorms());
- newType.setIndexOptions(field.indexed() ? getIndexOptions(field, val) : IndexOptions.NONE);
- newType.setStoreTermVectors(field.storeTermVector());
- newType.setStoreTermVectorOffsets(field.storeTermOffsets());
- newType.setStoreTermVectorPositions(field.storeTermPositions());
- newType.setStoreTermVectorPayloads(field.storeTermPayloads());
-
- return createField(field.getName(), val, newType, boost);
+ /*org.apache.lucene.document.FieldType newType = new org.apache.lucene.document.FieldType();
+ newType.setTokenized(field.isTokenized());
+ newType.setStored(field.stored());
+ newType.setOmitNorms(field.omitNorms());
+ newType.setIndexOptions(field.indexed() ? getIndexOptions(field, val) : IndexOptions.NONE);
+ newType.setStoreTermVectors(field.storeTermVector());
+ newType.setStoreTermVectorOffsets(field.storeTermOffsets());
+ newType.setStoreTermVectorPositions(field.storeTermPositions());
+ newType.setStoreTermVectorPayloads(field.storeTermPayloads());*/
+ return createField(field.getName(), val, field, boost);
}
/**
@@ -293,7 +292,7 @@ public abstract class FieldType extends FieldProperties {
* @param boost The boost value
* @return the {@link org.apache.lucene.index.IndexableField}.
*/
- protected IndexableField createField(String name, String val, org.apache.lucene.document.FieldType type, float boost){
+ protected IndexableField createField(String name, String val, org.apache.lucene.index.IndexableFieldType type, float boost){
Field f = new Field(name, val, type);
f.setBoost(boost);
return f;
@@ -320,20 +319,6 @@ public abstract class FieldType extends FieldProperties {
return f==null ? Collections.<IndexableField>emptyList() : Collections.singletonList(f);
}
- protected IndexOptions getIndexOptions(SchemaField field, String internalVal) {
-
- IndexOptions options = IndexOptions.DOCS_AND_FREQS_AND_POSITIONS;
- if (field.omitTermFreqAndPositions()) {
- options = IndexOptions.DOCS;
- } else if (field.omitPositions()) {
- options = IndexOptions.DOCS_AND_FREQS;
- } else if (field.storeOffsetsWithPositions()) {
- options = IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS;
- }
-
- return options;
- }
-
/**
* Convert an external value (from XML update command or from query string)
* into the internal format for both storing and indexing (which can be modified by any analyzers).
diff --git a/solr/core/src/java/org/apache/solr/schema/LatLonType.java b/solr/core/src/java/org/apache/solr/schema/LatLonType.java
index 53935b8d16c..db4d9dadeb2 100644
--- a/solr/core/src/java/org/apache/solr/schema/LatLonType.java
+++ b/solr/core/src/java/org/apache/solr/schema/LatLonType.java
@@ -21,6 +21,7 @@ import java.util.List;
import java.util.Map;
import org.apache.lucene.document.FieldType;
+import org.apache.lucene.document.StoredField;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.LeafReaderContext;
@@ -82,9 +83,7 @@ public class LatLonType extends AbstractSubTypeFieldType implements SpatialQuery
}
if (field.stored()) {
- FieldType customType = new FieldType();
- customType.setStored(true);
- f.add(createField(field.getName(), externalVal, customType, 1f));
+ f.add(createField(field.getName(), externalVal, StoredField.TYPE, 1f));
}
return f;
}
diff --git a/solr/core/src/java/org/apache/solr/schema/PointType.java b/solr/core/src/java/org/apache/solr/schema/PointType.java
index df5559a9af8..afbfeeced19 100644
--- a/solr/core/src/java/org/apache/solr/schema/PointType.java
+++ b/solr/core/src/java/org/apache/solr/schema/PointType.java
@@ -22,6 +22,7 @@ import java.util.List;
import java.util.Map;
import org.apache.lucene.document.FieldType;
+import org.apache.lucene.document.StoredField;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.queries.function.ValueSource;
import org.apache.lucene.queries.function.valuesource.VectorValueSource;
@@ -82,9 +83,7 @@ public class PointType extends CoordinateFieldType implements SpatialQueryable {
if (field.stored()) {
String storedVal = externalVal; // normalize or not?
- FieldType customType = new FieldType();
- customType.setStored(true);
- f.add(createField(field.getName(), storedVal, customType, 1f));
+ f.add(createField(field.getName(), storedVal, StoredField.TYPE, 1f));
}
return f;
diff --git a/solr/core/src/java/org/apache/solr/schema/PreAnalyzedField.java b/solr/core/src/java/org/apache/solr/schema/PreAnalyzedField.java
index 09d3590a21f..7bfed2b8c24 100644
--- a/solr/core/src/java/org/apache/solr/schema/PreAnalyzedField.java
+++ b/solr/core/src/java/org/apache/solr/schema/PreAnalyzedField.java
@@ -263,8 +263,8 @@ public class PreAnalyzedField extends TextField {
}
} else {
if (f != null) {
- f.fieldType().setIndexOptions(IndexOptions.NONE);
- f.fieldType().setTokenized(false);
+ type.setIndexOptions(IndexOptions.NONE);
+ type.setTokenized(false);
}
}
}
diff --git a/solr/core/src/java/org/apache/solr/schema/SchemaField.java b/solr/core/src/java/org/apache/solr/schema/SchemaField.java
index 5aadeda4ed4..bcd68c28285 100644
--- a/solr/core/src/java/org/apache/solr/schema/SchemaField.java
+++ b/solr/core/src/java/org/apache/solr/schema/SchemaField.java
@@ -22,7 +22,10 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import org.apache.lucene.index.DocValuesType;
+import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.IndexableField;
+import org.apache.lucene.index.IndexableFieldType;
import org.apache.lucene.search.SortField;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.util.SimpleOrderedMap;
@@ -33,7 +36,7 @@ import org.apache.solr.response.TextResponseWriter;
*
*
*/
-public final class SchemaField extends FieldProperties {
+public final class SchemaField extends FieldProperties implements IndexableFieldType {
private static final String FIELD_NAME = "name";
private static final String TYPE_NAME = "type";
private static final String DEFAULT_VALUE = "default";
@@ -353,4 +356,62 @@ public final class SchemaField extends FieldProperties {
}
return properties;
}
+
+ @Override
+ public boolean tokenized() {
+ return isTokenized();
+ }
+
+ @Override
+ public boolean storeTermVectors() {
+ return storeTermVector();
+ }
+
+ @Override
+ public boolean storeTermVectorOffsets() {
+ return storeTermOffsets();
+ }
+
+ @Override
+ public boolean storeTermVectorPositions() {
+ return storeTermPositions();
+ }
+
+ @Override
+ public boolean storeTermVectorPayloads() {
+ return storeTermPayloads();
+ }
+
+ @Override
+ public IndexOptions indexOptions() {
+ if (!indexed()) {
+ return IndexOptions.NONE;
+ }
+
+ IndexOptions options = IndexOptions.DOCS_AND_FREQS_AND_POSITIONS;
+ if (omitTermFreqAndPositions()) {
+ options = IndexOptions.DOCS;
+ } else if (omitPositions()) {
+ options = IndexOptions.DOCS_AND_FREQS;
+ } else if (storeOffsetsWithPositions()) {
+ options = IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS;
+ }
+
+ return options;
+ }
+
+ @Override
+ public DocValuesType docValuesType() {
+ return DocValuesType.NONE;
+ }
+
+ @Override
+ public int pointDimensionCount() {
+ return 0;
+ }
+
+ @Override
+ public int pointNumBytes() {
+ return 0;
+ }
}
diff --git a/solr/core/src/java/org/apache/solr/schema/TrieField.java b/solr/core/src/java/org/apache/solr/schema/TrieField.java
index b23a8b68730..49f93154340 100644
--- a/solr/core/src/java/org/apache/solr/schema/TrieField.java
+++ b/solr/core/src/java/org/apache/solr/schema/TrieField.java
@@ -670,7 +670,7 @@ public class TrieField extends PrimitiveFieldType {
ft.setStored(stored);
ft.setTokenized(true);
ft.setOmitNorms(field.omitNorms());
- ft.setIndexOptions(indexed ? getIndexOptions(field, value.toString()) : IndexOptions.NONE);
+ ft.setIndexOptions(field.indexOptions());
switch (type) {
case INTEGER: