Index: lucene/src/java/org/apache/lucene/document/StoredField.java
===================================================================
--- lucene/src/java/org/apache/lucene/document/StoredField.java (revision 0)
+++ lucene/src/java/org/apache/lucene/document/StoredField.java (revision 1231386)
@@ -0,0 +1,71 @@
+package org.apache.lucene.document;
+
+import org.apache.lucene.index.IndexReader; // javadocs
+import org.apache.lucene.search.IndexSearcher; // javadocs
+import org.apache.lucene.util.BytesRef;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/** A field whose value is stored so that {@link
+ * IndexSearcher#doc} and {@link IndexReader#document} will
+ * return the field and its value. */
+public final class StoredField extends Field {
+
+ public final static FieldType TYPE;
+ static {
+ TYPE = new FieldType();
+ TYPE.setStored(true);
+ TYPE.freeze();
+ }
+
+ public StoredField(String name, byte[] value) {
+ super(name, value, TYPE);
+ }
+
+ public StoredField(String name, byte[] value, int offset, int length) {
+ super(name, value, offset, length, TYPE);
+ }
+
+ public StoredField(String name, BytesRef value) {
+ super(name, value, TYPE);
+ }
+
+ public StoredField(String name, String value) {
+ super(name, value, TYPE);
+ }
+
+ public StoredField(String name, int value) {
+ super(name, TYPE);
+ fieldsData = value;
+ }
+
+ public StoredField(String name, float value) {
+ super(name, TYPE);
+ fieldsData = value;
+ }
+
+ public StoredField(String name, long value) {
+ super(name, TYPE);
+ fieldsData = value;
+ }
+
+ public StoredField(String name, double value) {
+ super(name, TYPE);
+ fieldsData = value;
+ }
+}
Property changes on: lucene/src/java/org/apache/lucene/document/StoredField.java
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
Index: lucene/MIGRATE.txt
===================================================================
--- lucene/MIGRATE.txt (revision 1230137)
+++ lucene/MIGRATE.txt (working copy)
@@ -422,13 +422,13 @@
-* LUCENE-2308: Separate IndexableFieldType from Field instances
+* LUCENE-2308,LUCENE-3453: Separate IndexableFieldType from Field instances
With this change, the indexing details (indexed, tokenized, norms,
indexOptions, stored, etc.) are moved into a separate FieldType
instance (rather than being stored directly on the Field).
-This means you can create the IndexableFieldType instance once, up front,
+This means you can create the FieldType instance once, up front,
for a given field, and then re-use that instance whenever you instantiate
the Field.
@@ -439,15 +439,21 @@
IDS (does not index term frequency nor positions). This field
does not store its value, but exposes TYPE_STORED as well.
- * BinaryField: a byte[] value that's only stored.
-
* TextField: indexes and tokenizes a String, Reader or TokenStream
value, without term vectors. This field does not store its value,
but exposes TYPE_STORED as well.
+ * StoredField: field that stores its value
+
+ * DocValuesField: indexes the value as a DocValues field
+
+ * NumericField: indexes the numeric value so that NumericRangeQuery
+ can be used at search-time.
+
If your usage fits one of those common cases you can simply
-instantiate the above class. To use the TYPE_STORED variant, do this
-instead:
+instantiate the above class. If you need to store the value, you can
+add a separate StoredField to the document, or you can use
+TYPE_STORED for the field:
Field f = new Field("field", "value", StringField.TYPE_STORED);
@@ -465,9 +471,14 @@
t.setStored(true);
t.setOmitNorms(true);
t.setIndexOptions(IndexOptions.DOCS_AND_FREQS);
+ t.freeze();
FieldType has a freeze() method to prevent further changes.
+There is also a deprecated transition API, providing the same Index,
+Store, TermVector enums from 3.x, and Field constructors taking these
+enums.
+
When migrating from the 3.x API, if you did this before:
new Field("field", "value", Field.Store.NO, Field.Indexed.NOT_ANALYZED_NO_NORMS)
@@ -528,7 +539,7 @@
you can now do this:
- new BinaryField("field", bytes)
+ new StoredField("field", bytes)
* LUCENE-3396: Analyzer.tokenStream() and .reusableTokenStream() have been made final.
It is now necessary to use Analyzer.TokenStreamComponents to define an analysis process.
Index: lucene/contrib/demo/src/java/org/apache/lucene/demo/IndexFiles.java
===================================================================
--- lucene/contrib/demo/src/java/org/apache/lucene/demo/IndexFiles.java (revision 1230137)
+++ lucene/contrib/demo/src/java/org/apache/lucene/demo/IndexFiles.java (working copy)
@@ -184,9 +184,7 @@
// year/month/day/hour/minutes/seconds, down the resolution you require.
// For example the long value 2011021714 would mean
// February 17, 2011, 2-3 PM.
- NumericField modifiedField = new NumericField("modified");
- modifiedField.setLongValue(file.lastModified());
- doc.add(modifiedField);
+ doc.add(new NumericField("modified", file.lastModified()));
// Add the contents of the file to a field named "contents". Specify a Reader,
// so that the text of the file is tokenized and indexed, but not stored.
Index: lucene/contrib/misc/src/java/org/apache/lucene/document/LazyDocument.java
===================================================================
--- lucene/contrib/misc/src/java/org/apache/lucene/document/LazyDocument.java (revision 1230137)
+++ lucene/contrib/misc/src/java/org/apache/lucene/document/LazyDocument.java (working copy)
@@ -23,13 +23,10 @@
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
-import org.apache.lucene.document.NumericField.DataType;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.IndexableFieldType;
-import org.apache.lucene.index.DocValue;
-import org.apache.lucene.index.DocValues;
import org.apache.lucene.util.BytesRef;
/** Defers actually loading a field's value until you ask
@@ -121,24 +118,6 @@
}
@Override
- public boolean numeric() {
- if (num == 0) {
- return getDocument().getField(name).numeric();
- } else {
- return getDocument().getFields(name)[num].numeric();
- }
- }
-
- @Override
- public DataType numericDataType() {
- if (num == 0) {
- return getDocument().getField(name).numericDataType();
- } else {
- return getDocument().getFields(name)[num].numericDataType();
- }
- }
-
- @Override
public Number numericValue() {
if (num == 0) {
return getDocument().getField(name).numericValue();
@@ -157,24 +136,6 @@
}
@Override
- public DocValue docValue() {
- if (num == 0) {
- return getDocument().getField(name).docValue();
- } else {
- return getDocument().getFields(name)[num].docValue();
- }
- }
-
- @Override
- public DocValues.Type docValueType() {
- if (num == 0) {
- return getDocument().getField(name).docValueType();
- } else {
- return getDocument().getFields(name)[num].docValueType();
- }
- }
-
- @Override
public TokenStream tokenStream(Analyzer analyzer) throws IOException {
if (num == 0) {
return getDocument().getField(name).tokenStream(analyzer);
Index: lucene/contrib/spatial/src/test/org/apache/lucene/spatial/tier/TestCartesian.java
===================================================================
--- lucene/contrib/spatial/src/test/org/apache/lucene/spatial/tier/TestCartesian.java (revision 1230137)
+++ lucene/contrib/spatial/src/test/org/apache/lucene/spatial/tier/TestCartesian.java (working copy)
@@ -23,9 +23,11 @@
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
+import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.NumericField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
+import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
@@ -91,6 +93,18 @@
}
}
+ private static final FieldType latLongType = new FieldType();
+ static {
+ latLongType.setIndexed(true);
+ latLongType.setStored(true);
+ latLongType.setTokenized(true);
+ latLongType.setOmitNorms(true);
+ latLongType.setIndexOptions(IndexOptions.DOCS_ONLY);
+ latLongType.setNumericType(NumericField.DataType.DOUBLE);
+ latLongType.setNumericPrecisionStep(Integer.MAX_VALUE);
+ latLongType.freeze();
+ }
+
private void addPoint(IndexWriter writer, String name, double lat, double lng) throws IOException{
Document doc = new Document();
@@ -98,8 +112,8 @@
doc.add(newField("name", name, TextField.TYPE_STORED));
// convert the lat / long to lucene fields
- doc.add(new NumericField(latField, Integer.MAX_VALUE, NumericField.TYPE_STORED).setDoubleValue(lat));
- doc.add(new NumericField(lngField, Integer.MAX_VALUE, NumericField.TYPE_STORED).setDoubleValue(lng));
+ doc.add(new NumericField(latField, lat, latLongType));
+ doc.add(new NumericField(lngField, lng, latLongType));
// add a default meta field to make searching all documents easy
doc.add(newField("metafile", "doc", TextField.TYPE_STORED));
@@ -107,7 +121,7 @@
int ctpsize = ctps.size();
for (int i =0; i < ctpsize; i++){
CartesianTierPlotter ctp = ctps.get(i);
- doc.add(new NumericField(ctp.getTierFieldName(), Integer.MAX_VALUE, TextField.TYPE_STORED).setDoubleValue(ctp.getTierBoxId(lat,lng)));
+ doc.add(new NumericField(ctp.getTierFieldName(), ctp.getTierBoxId(lat, lng), latLongType));
doc.add(newField(geoHashPrefix, GeoHashUtils.encode(lat,lng), StringField.TYPE_STORED));
}
@@ -248,8 +262,8 @@
Document d = searcher.doc(scoreDocs[i].doc);
String name = d.get("name");
- double rsLat = Double.parseDouble(d.get(latField));
- double rsLng = Double.parseDouble(d.get(lngField));
+ double rsLat = d.getField(latField).numericValue().doubleValue();
+ double rsLng = d.getField(lngField).numericValue().doubleValue();
Double geo_distance = distances.get(scoreDocs[i].doc);
double distance = DistanceUtils.getDistanceMi(lat, lng, rsLat, rsLng);
@@ -317,8 +331,8 @@
for(int i =0 ; i < results; i++){
Document d = searcher.doc(scoreDocs[i].doc);
String name = d.get("name");
- double rsLat = Double.parseDouble(d.get(latField));
- double rsLng = Double.parseDouble(d.get(lngField));
+ double rsLat = d.getField(latField).numericValue().doubleValue();
+ double rsLng = d.getField(lngField).numericValue().doubleValue();
Double geo_distance = distances.get(scoreDocs[i].doc);
double distance = DistanceUtils.getDistanceMi(lat, lng, rsLat, rsLng);
@@ -389,8 +403,8 @@
Document d = searcher.doc(scoreDocs[i].doc);
String name = d.get("name");
- double rsLat = Double.parseDouble(d.get(latField));
- double rsLng = Double.parseDouble(d.get(lngField));
+ double rsLat = d.getField(latField).numericValue().doubleValue();
+ double rsLng = d.getField(lngField).numericValue().doubleValue();
Double geo_distance = distances.get(scoreDocs[i].doc);
double distance = DistanceUtils.getDistanceMi(lat, lng, rsLat, rsLng);
@@ -461,8 +475,8 @@
Document d = searcher.doc(scoreDocs[i].doc);
String name = d.get("name");
- double rsLat = Double.parseDouble(d.get(latField));
- double rsLng = Double.parseDouble(d.get(lngField));
+ double rsLat = d.getField(latField).numericValue().doubleValue();
+ double rsLng = d.getField(lngField).numericValue().doubleValue();
Double geo_distance = distances.get(scoreDocs[i].doc);
double distance = DistanceUtils.getDistanceMi(lat, lng, rsLat, rsLng);
Index: lucene/contrib/spatial/src/test/org/apache/lucene/spatial/tier/TestDistance.java
===================================================================
--- lucene/contrib/spatial/src/test/org/apache/lucene/spatial/tier/TestDistance.java (revision 1230137)
+++ lucene/contrib/spatial/src/test/org/apache/lucene/spatial/tier/TestDistance.java (working copy)
@@ -20,17 +20,19 @@
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
+import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.NumericField;
import org.apache.lucene.document.TextField;
+import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.IndexReader.AtomicReaderContext;
+import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
-import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.QueryWrapperFilter;
-import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.ReaderUtil;
-import org.apache.lucene.store.Directory;
public class TestDistance extends LuceneTestCase {
@@ -58,6 +60,18 @@
directory.close();
super.tearDown();
}
+
+ private static final FieldType latLongType = new FieldType();
+ static {
+ latLongType.setIndexed(true);
+ latLongType.setStored(true);
+ latLongType.setTokenized(true);
+ latLongType.setOmitNorms(true);
+ latLongType.setIndexOptions(IndexOptions.DOCS_ONLY);
+ latLongType.setNumericType(NumericField.DataType.DOUBLE);
+ latLongType.setNumericPrecisionStep(Integer.MAX_VALUE);
+ latLongType.freeze();
+ }
private void addPoint(IndexWriter writer, String name, double lat, double lng) throws IOException{
@@ -66,8 +80,8 @@
doc.add(newField("name", name, TextField.TYPE_STORED));
// convert the lat / long to lucene fields
- doc.add(new NumericField(latField, Integer.MAX_VALUE, NumericField.TYPE_STORED).setDoubleValue(lat));
- doc.add(new NumericField(lngField, Integer.MAX_VALUE, NumericField.TYPE_STORED).setDoubleValue(lng));
+ doc.add(new NumericField(latField, lat, latLongType));
+ doc.add(new NumericField(lngField, lng, latLongType));
// add a default meta field to make searching all documents easy
doc.add(newField("metafile", "doc", TextField.TYPE_STORED));
Index: lucene/contrib/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java
===================================================================
--- lucene/contrib/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java (revision 1230137)
+++ lucene/contrib/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java (working copy)
@@ -386,7 +386,7 @@
Highlighter highlighter = new Highlighter(this, scorer);
for (int i = 0; i < hits.totalHits; i++) {
- String text = searcher.doc(hits.scoreDocs[i].doc).get(NUMERIC_FIELD_NAME);
+ String text = searcher.doc(hits.scoreDocs[i].doc).getField(NUMERIC_FIELD_NAME).numericValue().toString();
TokenStream tokenStream = analyzer.tokenStream(FIELD_NAME, new StringReader(text));
highlighter.setTextFragmenter(new SimpleFragmenter(40));
@@ -1738,25 +1738,21 @@
addDoc(writer, text);
}
Document doc = new Document();
- NumericField nfield = new NumericField(NUMERIC_FIELD_NAME, NumericField.TYPE_STORED);
- nfield.setIntValue(1);
- doc.add(nfield);
+ doc.add(new NumericField(NUMERIC_FIELD_NAME, 1, NumericField.getFieldType(NumericField.DataType.INT, true)));
writer.addDocument(doc, analyzer);
- nfield = new NumericField(NUMERIC_FIELD_NAME, NumericField.TYPE_STORED);
- nfield.setIntValue(3);
+
doc = new Document();
- doc.add(nfield);
+ doc.add(new NumericField(NUMERIC_FIELD_NAME, 3, NumericField.getFieldType(NumericField.DataType.INT, true)));
writer.addDocument(doc, analyzer);
- nfield = new NumericField(NUMERIC_FIELD_NAME, NumericField.TYPE_STORED);
- nfield.setIntValue(5);
+
doc = new Document();
- doc.add(nfield);
+ doc.add(new NumericField(NUMERIC_FIELD_NAME, 5, NumericField.getFieldType(NumericField.DataType.INT, true)));
writer.addDocument(doc, analyzer);
- nfield = new NumericField(NUMERIC_FIELD_NAME, NumericField.TYPE_STORED);
- nfield.setIntValue(7);
+
doc = new Document();
- doc.add(nfield);
+ doc.add(new NumericField(NUMERIC_FIELD_NAME, 7, NumericField.getFieldType(NumericField.DataType.INT, true)));
writer.addDocument(doc, analyzer);
+
writer.forceMerge(1);
writer.close();
reader = IndexReader.open(ramDir);
Index: lucene/src/test/org/apache/lucene/codecs/lucene40/TestDocValues.java
===================================================================
--- lucene/src/test/org/apache/lucene/codecs/lucene40/TestDocValues.java (revision 1230137)
+++ lucene/src/test/org/apache/lucene/codecs/lucene40/TestDocValues.java (working copy)
@@ -18,17 +18,21 @@
*/
import java.io.IOException;
+import java.io.Reader;
import java.util.Comparator;
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.codecs.DocValuesConsumer;
import org.apache.lucene.codecs.lucene40.values.Bytes;
import org.apache.lucene.codecs.lucene40.values.Floats;
import org.apache.lucene.codecs.lucene40.values.Ints;
-import org.apache.lucene.index.DocValue;
-import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.DocValues.SortedSource;
import org.apache.lucene.index.DocValues.Source;
import org.apache.lucene.index.DocValues.Type;
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.index.IndexableField;
+import org.apache.lucene.index.IndexableFieldType;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.Counter;
@@ -175,9 +179,9 @@
Directory dir = newDirectory();
final Counter trackBytes = Counter.newCounter();
DocValuesConsumer w = Ints.getWriter(dir, "test", trackBytes, Type.VAR_INTS, newIOContext(random));
- valueHolder.intValue = minMax[i][0];
+ valueHolder.numberValue = minMax[i][0];
w.add(0, valueHolder);
- valueHolder.intValue = minMax[i][1];
+ valueHolder.numberValue = minMax[i][1];
w.add(1, valueHolder);
w.finish(2);
assertEquals(0, trackBytes.get());
@@ -212,7 +216,7 @@
final Counter trackBytes = Counter.newCounter();
DocValuesConsumer w = Ints.getWriter(dir, "test", trackBytes, Type.FIXED_INTS_8, newIOContext(random));
for (int i = 0; i < sourceArray.length; i++) {
- valueHolder.intValue = (long) sourceArray[i];
+ valueHolder.numberValue = (long) sourceArray[i];
w.add(i, valueHolder);
}
w.finish(sourceArray.length);
@@ -235,7 +239,7 @@
final Counter trackBytes = Counter.newCounter();
DocValuesConsumer w = Ints.getWriter(dir, "test", trackBytes, Type.FIXED_INTS_16, newIOContext(random));
for (int i = 0; i < sourceArray.length; i++) {
- valueHolder.intValue = (long) sourceArray[i];
+ valueHolder.numberValue = (long) sourceArray[i];
w.add(i, valueHolder);
}
w.finish(sourceArray.length);
@@ -258,7 +262,7 @@
final Counter trackBytes = Counter.newCounter();
DocValuesConsumer w = Ints.getWriter(dir, "test", trackBytes, Type.FIXED_INTS_64, newIOContext(random));
for (int i = 0; i < sourceArray.length; i++) {
- valueHolder.intValue = sourceArray[i];
+ valueHolder.numberValue = sourceArray[i];
w.add(i, valueHolder);
}
w.finish(sourceArray.length);
@@ -281,7 +285,7 @@
final Counter trackBytes = Counter.newCounter();
DocValuesConsumer w = Ints.getWriter(dir, "test", trackBytes, Type.FIXED_INTS_32, newIOContext(random));
for (int i = 0; i < sourceArray.length; i++) {
- valueHolder.intValue = (long) sourceArray[i];
+ valueHolder.numberValue = (long) sourceArray[i];
w.add(i, valueHolder);
}
w.finish(sourceArray.length);
@@ -304,7 +308,7 @@
final Counter trackBytes = Counter.newCounter();
DocValuesConsumer w = Floats.getWriter(dir, "test", trackBytes, newIOContext(random), Type.FLOAT_32);
for (int i = 0; i < sourceArray.length; i++) {
- valueHolder.floatValue = sourceArray[i];
+ valueHolder.numberValue = sourceArray[i];
w.add(i, valueHolder);
}
w.finish(sourceArray.length);
@@ -327,7 +331,7 @@
final Counter trackBytes = Counter.newCounter();
DocValuesConsumer w = Floats.getWriter(dir, "test", trackBytes, newIOContext(random), Type.FLOAT_64);
for (int i = 0; i < sourceArray.length; i++) {
- valueHolder.floatValue = sourceArray[i];
+ valueHolder.numberValue = sourceArray[i];
w.add(i, valueHolder);
}
w.finish(sourceArray.length);
@@ -354,7 +358,7 @@
DocValuesConsumer w = Ints.getWriter(dir, "test", trackBytes, type, newIOContext(random));
for (int i = 0; i < NUM_VALUES; i++) {
final long v = random.nextLong() % (1 + maxV);
- valueHolder.intValue = values[i] = v;
+ valueHolder.numberValue = values[i] = v;
w.add(i, valueHolder);
}
final int additionalDocs = 1 + random.nextInt(9);
@@ -377,20 +381,20 @@
}
public void testFloats4() throws IOException {
- runTestFloats(Type.FLOAT_32, 0.00001);
+ runTestFloats(Type.FLOAT_32);
}
- private void runTestFloats(Type type, double delta) throws IOException {
+ private void runTestFloats(Type type) throws IOException {
DocValueHolder valueHolder = new DocValueHolder();
Directory dir = newDirectory();
final Counter trackBytes = Counter.newCounter();
DocValuesConsumer w = Floats.getWriter(dir, "test", trackBytes, newIOContext(random), type);
- final int NUM_VALUES = 777 + random.nextInt(777);;
+ final int NUM_VALUES = 777 + random.nextInt(777);
final double[] values = new double[NUM_VALUES];
for (int i = 0; i < NUM_VALUES; i++) {
final double v = type == Type.FLOAT_32 ? random.nextFloat() : random
.nextDouble();
- valueHolder.floatValue = values[i] = v;
+ valueHolder.numberValue = values[i] = v;
w.add(i, valueHolder);
}
final int additionalValues = 1 + random.nextInt(10);
@@ -409,7 +413,7 @@
}
public void testFloats8() throws IOException {
- runTestFloats(Type.FLOAT_64, 0.0);
+ runTestFloats(Type.FLOAT_64);
}
@@ -431,31 +435,49 @@
return getSource(values).asSortedSource();
}
- public static class DocValueHolder implements DocValue {
+ public static class DocValueHolder implements IndexableField {
BytesRef bytes;
- long intValue;
- double floatValue;
+ Number numberValue;
Comparator
* This method is used during merging to provide implementation agnostic
* default merge implementation.
@@ -177,67 +169,29 @@
* ID must always be greater than the previous ID or 0 if called the
* first time.
*/
- protected void mergeDoc(int docID, int sourceDoc)
+ protected void mergeDoc(Field scratchField, Source source, int docID, int sourceDoc)
throws IOException {
- switch(currentMergeSource.type()) {
+ switch(source.type()) {
case BYTES_FIXED_DEREF:
case BYTES_FIXED_SORTED:
case BYTES_FIXED_STRAIGHT:
case BYTES_VAR_DEREF:
case BYTES_VAR_SORTED:
case BYTES_VAR_STRAIGHT:
- add(docID, currentMergeSource.getBytes(sourceDoc, spare));
+ scratchField.setValue(source.getBytes(sourceDoc, spare));
break;
case FIXED_INTS_16:
case FIXED_INTS_32:
case FIXED_INTS_64:
case FIXED_INTS_8:
case VAR_INTS:
- add(docID, currentMergeSource.getInt(sourceDoc));
+ scratchField.setValue(source.getInt(sourceDoc));
break;
case FLOAT_32:
case FLOAT_64:
- add(docID, currentMergeSource.getFloat(sourceDoc));
+ scratchField.setValue(source.getFloat(sourceDoc));
break;
}
+ add(docID, scratchField);
}
-
- /**
- * Sets the next {@link Source} to consume values from on calls to
- * {@link #mergeDoc(int, int)}
- *
- * @param mergeSource
- * the next {@link Source}, this must not be null
- */
- protected final void setNextMergeSource(Source mergeSource) {
- currentMergeSource = mergeSource;
- }
-
- /**
- * Specialized auxiliary MergeState is necessary since we don't want to
- * exploit internals up to the codecs consumer. An instance of this class is
- * created for each merged low level {@link IndexReader} we are merging to
- * support low level bulk copies.
- */
- public static class SingleSubMergeState {
- /**
- * the source reader for this MergeState - merged values should be read from
- * this instance
- */
- public final DocValues reader;
- /** the absolute docBase for this MergeState within the resulting segment */
- public final int docBase;
- /** the number of documents in this MergeState */
- public final int docCount;
- /** the not deleted bits for this MergeState */
- public final Bits liveDocs;
-
- public SingleSubMergeState(DocValues reader, int docBase, int docCount, Bits liveDocs) {
- assert reader != null;
- this.reader = reader;
- this.docBase = docBase;
- this.docCount = docCount;
- this.liveDocs = liveDocs;
- }
- }
}
Index: lucene/src/java/org/apache/lucene/codecs/simpletext/SimpleTextStoredFieldsWriter.java
===================================================================
--- lucene/src/java/org/apache/lucene/codecs/simpletext/SimpleTextStoredFieldsWriter.java (revision 1230137)
+++ lucene/src/java/org/apache/lucene/codecs/simpletext/SimpleTextStoredFieldsWriter.java (working copy)
@@ -98,46 +98,39 @@
newLine();
write(TYPE);
- if (field.numeric()) {
- switch (field.numericDataType()) {
- case INT:
- write(TYPE_INT);
- newLine();
+ final Number n = field.numericValue();
+
+ if (n != null) {
+ if (n instanceof Byte || n instanceof Short || n instanceof Integer) {
+ write(TYPE_INT);
+ newLine();
- write(VALUE);
- write(Integer.toString(field.numericValue().intValue()));
- newLine();
+ write(VALUE);
+ write(Integer.toString(n.intValue()));
+ newLine();
+ } else if (n instanceof Long) {
+ write(TYPE_LONG);
+ newLine();
+
+ write(VALUE);
+ write(Long.toString(n.longValue()));
+ newLine();
+ } else if (n instanceof Float) {
+ write(TYPE_FLOAT);
+ newLine();
- break;
- case LONG:
- write(TYPE_LONG);
- newLine();
+ write(VALUE);
+ write(Float.toString(n.floatValue()));
+ newLine();
+ } else if (n instanceof Double) {
+ write(TYPE_DOUBLE);
+ newLine();
- write(VALUE);
- write(Long.toString(field.numericValue().longValue()));
- newLine();
-
- break;
- case FLOAT:
- write(TYPE_FLOAT);
- newLine();
-
- write(VALUE);
- write(Float.toString(field.numericValue().floatValue()));
- newLine();
-
- break;
- case DOUBLE:
- write(TYPE_DOUBLE);
- newLine();
-
- write(VALUE);
- write(Double.toString(field.numericValue().doubleValue()));
- newLine();
-
- break;
- default:
- assert false : "Should never get here";
+ write(VALUE);
+ write(Double.toString(n.doubleValue()));
+ newLine();
+ } else {
+ throw new IllegalArgumentException("cannot store numeric type " + n.getClass());
}
} else {
BytesRef bytes = field.binaryValue();
Index: lucene/src/java/org/apache/lucene/codecs/simpletext/SimpleTextNormsConsumer.java
===================================================================
--- lucene/src/java/org/apache/lucene/codecs/simpletext/SimpleTextNormsConsumer.java (revision 1230137)
+++ lucene/src/java/org/apache/lucene/codecs/simpletext/SimpleTextNormsConsumer.java (working copy)
@@ -23,13 +23,13 @@
import org.apache.lucene.codecs.DocValuesConsumer;
import org.apache.lucene.codecs.PerDocConsumer;
-import org.apache.lucene.index.DocValue;
import org.apache.lucene.index.DocValues.Type;
import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.FieldInfos;
import org.apache.lucene.index.IndexFileNames;
import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.SegmentInfo;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext;
@@ -130,8 +130,8 @@
}
@Override
- public void add(int docID, DocValue docValue) throws IOException {
- add(docID, docValue.getBytes());
+ public void add(int docID, IndexableField docValue) throws IOException {
+ add(docID, docValue.binaryValue());
}
protected void add(int docID, BytesRef value) throws IOException {
Index: lucene/src/java/org/apache/lucene/index/IndexableFieldType.java
===================================================================
--- lucene/src/java/org/apache/lucene/index/IndexableFieldType.java (revision 1230137)
+++ lucene/src/java/org/apache/lucene/index/IndexableFieldType.java (working copy)
@@ -46,4 +46,8 @@
/** {@link IndexOptions}, describing what should be
* recorded into the inverted index */
public IndexOptions indexOptions();
+
+ /** DocValues type; if non-null then the field's value
+ * will be indexed into docValues */
+ public DocValues.Type docValueType();
}
Index: lucene/src/java/org/apache/lucene/index/DocValue.java
===================================================================
--- lucene/src/java/org/apache/lucene/index/DocValue.java (revision 1230137)
+++ lucene/src/java/org/apache/lucene/index/DocValue.java (working copy)
@@ -1,53 +0,0 @@
-package org.apache.lucene.index;
-
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-import java.util.Comparator;
-
-import org.apache.lucene.codecs.DocValuesConsumer;
-import org.apache.lucene.document.DocValuesField;
-import org.apache.lucene.util.BytesRef;
-
-/**
- * Per document and field values consumed by {@link DocValuesConsumer}.
- * @see DocValuesField
- *
- * @lucene.experimental
- */
-public interface DocValue {
-
- /**
- * Returns the set {@link BytesRef} or docID. The methods
* implementation obtains the value for the sourceDoc id from the
- * current {@link Source} set to setNextMergeSource(Source).
+ * current {@link Source}.
* null if not set.
- */
- public BytesRef getBytes();
-
- /**
- * Returns the set {@link BytesRef} comparator or null if not set
- */
- public Comparator0.0d if not set.
- */
- public double getFloat();
-
- /**
- * Returns the set long value of 0 if not set.
- */
- public long getInt();
-
-}
Index: lucene/src/java/org/apache/lucene/index/DocFieldProcessor.java
===================================================================
--- lucene/src/java/org/apache/lucene/index/DocFieldProcessor.java (revision 1230137)
+++ lucene/src/java/org/apache/lucene/index/DocFieldProcessor.java (working copy)
@@ -26,11 +26,9 @@
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.codecs.DocValuesConsumer;
-import org.apache.lucene.codecs.DocValuesFormat;
import org.apache.lucene.codecs.FieldInfosWriter;
import org.apache.lucene.codecs.PerDocConsumer;
import org.apache.lucene.index.DocumentsWriterPerThread.DocState;
-import org.apache.lucene.index.DocValues;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.IOUtils;
@@ -82,17 +80,19 @@
fieldsWriter.flush(state);
consumer.flush(childFields, state);
+ for (DocValuesConsumerAndDocID consumer : docValues.values()) {
+ consumer.docValuesConsumer.finish(state.numDocs);
+ }
+
// Important to save after asking consumer to flush so
// consumer can alter the FieldInfo* if necessary. EG,
// FreqProxTermsWriter does this with
// FieldInfo.storePayload.
FieldInfosWriter infosWriter = codec.fieldInfosFormat().getFieldInfosWriter();
infosWriter.write(state.directory, state.segmentName, state.fieldInfos, IOContext.DEFAULT);
- for (DocValuesConsumerAndDocID consumers : docValues.values()) {
- consumers.docValuesConsumer.finish(state.numDocs);
- }
+
// close perDocConsumer during flush to ensure all files are flushed due to PerCodec CFS
- IOUtils.close(perDocConsumers.values());
+ IOUtils.close(perDocConsumer);
}
@Override
@@ -112,7 +112,7 @@
field = next;
}
}
- IOUtils.closeWhileHandlingException(perDocConsumers.values());
+ IOUtils.closeWhileHandlingException(perDocConsumer);
// TODO add abort to PerDocConsumer!
try {
@@ -132,7 +132,6 @@
}
try {
- PerDocConsumer perDocConsumer = perDocConsumers.get(0);
if (perDocConsumer != null) {
perDocConsumer.abort();
}
@@ -176,7 +175,7 @@
fieldHash = new DocFieldProcessorPerField[2];
hashMask = 1;
totalFieldCount = 0;
- perDocConsumers.clear();
+ perDocConsumer = null;
docValues.clear();
}
@@ -270,9 +269,9 @@
if (field.fieldType().stored()) {
fieldsWriter.addField(field, fp.fieldInfo);
}
- final DocValue docValue = field.docValue();
- if (docValue != null) {
- docValuesConsumer(field.docValueType(), docState, fp.fieldInfo).add(docState.docID, docValue);
+ final DocValues.Type dvType = field.fieldType().docValueType();
+ if (dvType != null) {
+ docValuesConsumer(dvType, docState, fp.fieldInfo).add(docState.docID, field);
}
}
@@ -310,6 +309,8 @@
}
private static class DocValuesConsumerAndDocID {
+ // Only used to enforce that same DV field name is never
+ // added more than once per doc:
public int docID;
final DocValuesConsumer docValuesConsumer;
@@ -319,7 +320,7 @@
}
final private Map
NOTE: the provided BytesRef is not copied so be sure + * not to change it until you're done with this field. + */ + public Field(String name, BytesRef bytes, FieldType type) { + if (name == null) { + throw new IllegalArgumentException("name cannot be null"); } - + if (type.indexed()) { + throw new IllegalArgumentException("Fields with BytesRef values cannot be indexed"); + } this.fieldsData = bytes; this.type = type; this.name = name; } - - public Field(String name, String value, IndexableFieldType type) { + + /** + * Create field with String value. + */ + public Field(String name, String value, FieldType type) { if (name == null) { throw new IllegalArgumentException("name cannot be null"); } @@ -122,7 +172,7 @@ throw new IllegalArgumentException("it doesn't make sense to have a field that " + "is neither indexed nor stored"); } - if (!type.indexed() && !type.tokenized() && (type.storeTermVectors())) { + if (!type.indexed() && (type.storeTermVectors())) { throw new IllegalArgumentException("cannot store term vector information " + "for a field that is not indexed"); } @@ -133,6 +183,54 @@ } /** + * Create field with an int value. + */ + public Field(String name, int value, FieldType type) { + if (name == null) { + throw new IllegalArgumentException("name cannot be null"); + } + this.type = type; + this.name = name; + this.fieldsData = Integer.valueOf(value); + } + + /** + * Create field with an long value. + */ + public Field(String name, long value, FieldType type) { + if (name == null) { + throw new IllegalArgumentException("name cannot be null"); + } + this.type = type; + this.name = name; + this.fieldsData = Long.valueOf(value); + } + + /** + * Create field with a float value. + */ + public Field(String name, float value, FieldType type) { + if (name == null) { + throw new IllegalArgumentException("name cannot be null"); + } + this.type = type; + this.name = name; + this.fieldsData = Float.valueOf(value); + } + + /** + * Create field with a double value. + */ + public Field(String name, double value, FieldType type) { + if (name == null) { + throw new IllegalArgumentException("name cannot be null"); + } + this.type = type; + this.name = name; + this.fieldsData = Double.valueOf(value); + } + + /** * The value of the field as a String, or null. If null, the Reader value or * binary value is used. Exactly one of stringValue(), readerValue(), and * getBinaryValue() must be set. @@ -175,9 +273,8 @@ *
*/ public void setValue(String value) { - if (isBinary()) { - throw new IllegalArgumentException( - "cannot set a String value on a binary field"); + if (!(fieldsData instanceof String)) { + throw new IllegalArgumentException("cannot change value type from " + fieldsData.getClass().getSimpleName() + " to String"); } fieldsData = value; } @@ -187,14 +284,9 @@ * href="#setValue(java.lang.String)">setValue(String). */ public void setValue(Reader value) { - if (isBinary()) { - throw new IllegalArgumentException( - "cannot set a Reader value on a binary field"); + if (!(fieldsData instanceof Reader)) { + throw new IllegalArgumentException("cannot change value type from " + fieldsData.getClass().getSimpleName() + " to Reader"); } - if (type.stored()) { - throw new IllegalArgumentException( - "cannot set a Reader value on a stored field"); - } fieldsData = value; } @@ -203,13 +295,88 @@ * href="#setValue(java.lang.String)">setValue(String). */ public void setValue(byte[] value) { - if (!isBinary()) { - throw new IllegalArgumentException( - "cannot set a byte[] value on a non-binary field"); + setValue(new BytesRef(value)); + } + + /** + * Expert: change the value of this field. See setValue(String). + * + *NOTE: the provided BytesRef is not copied so be sure
+ * not to change it until you're done with this field.
+ */
+ public void setValue(BytesRef value) {
+ if (!(fieldsData instanceof BytesRef)) {
+ throw new IllegalArgumentException("cannot change value type from " + fieldsData.getClass().getSimpleName() + " to BytesRef");
}
- fieldsData = new BytesRef(value);
+ if (type.indexed()) {
+ throw new IllegalArgumentException("cannot set a Reader value on an indexed field");
+ }
+ fieldsData = value;
}
-
+
+ /*
+ public void setValue(byte value) {
+ if (!(fieldsData instanceof Byte)) {
+ throw new IllegalArgumentException("cannot change value type from " + fieldsData.getClass().getSimpleName() + " to Byte");
+ }
+ if (numericTokenStream != null) {
+ numericTokenStream.setIntValue((int) value);
+ }
+ fieldsData = Byte.valueOf(value);
+ }
+
+ public void setValue(short value) {
+ if (!(fieldsData instanceof Short)) {
+ throw new IllegalArgumentException("cannot change value type from " + fieldsData.getClass().getSimpleName() + " to Short");
+ }
+ if (numericTokenStream != null) {
+ numericTokenStream.setIntValue((int) value);
+ }
+ fieldsData = Short.valueOf(value);
+ }
+ */
+
+ public void setValue(int value) {
+ if (!(fieldsData instanceof Integer)) {
+ throw new IllegalArgumentException("cannot change value type from " + fieldsData.getClass().getSimpleName() + " to Integer");
+ }
+ if (numericTokenStream != null) {
+ numericTokenStream.setIntValue(value);
+ }
+ fieldsData = Integer.valueOf(value);
+ }
+
+ public void setValue(long value) {
+ if (!(fieldsData instanceof Long)) {
+ throw new IllegalArgumentException("cannot change value type from " + fieldsData.getClass().getSimpleName() + " to Long");
+ }
+ if (numericTokenStream != null) {
+ numericTokenStream.setLongValue(value);
+ }
+ fieldsData = Long.valueOf(value);
+ }
+
+ public void setValue(float value) {
+ if (!(fieldsData instanceof Float)) {
+ throw new IllegalArgumentException("cannot change value type from " + fieldsData.getClass().getSimpleName() + " to Float");
+ }
+ if (numericTokenStream != null) {
+ numericTokenStream.setFloatValue(value);
+ }
+ fieldsData = Float.valueOf(value);
+ }
+
+ public void setValue(double value) {
+ if (!(fieldsData instanceof Double)) {
+ throw new IllegalArgumentException("cannot change value type from " + fieldsData.getClass().getSimpleName() + " to Double");
+ }
+ if (numericTokenStream != null) {
+ numericTokenStream.setDoubleValue(value);
+ }
+ fieldsData = Double.valueOf(value);
+ }
+
/**
* Expert: sets the token stream to be used for indexing and causes
* isIndexed() and isTokenized() to return true. May be combined with stored
@@ -217,9 +384,11 @@
*/
public void setTokenStream(TokenStream tokenStream) {
if (!type.indexed() || !type.tokenized()) {
- throw new IllegalArgumentException(
- "cannot set token stream on non indexed and tokenized field");
+ throw new IllegalArgumentException("TokenStream fields must be indexed and tokenized");
}
+ if (type.numericType() != null) {
+ throw new IllegalArgumentException("cannot set private TokenStream on numeric fields");
+ }
this.tokenStream = tokenStream;
}
@@ -248,33 +417,23 @@
public void setBoost(float boost) {
this.boost = boost;
}
-
- public boolean numeric() {
- return false;
- }
public Number numericValue() {
- return null;
+ if (fieldsData instanceof Number) {
+ return (Number) fieldsData;
+ } else {
+ return null;
+ }
}
- public NumericField.DataType numericDataType() {
- return null;
- }
-
public BytesRef binaryValue() {
- if (!isBinary()) {
+ if (fieldsData instanceof BytesRef) {
+ return (BytesRef) fieldsData;
+ } else {
return null;
- } else {
- return (BytesRef) fieldsData;
}
}
- /** methods from inner IndexableFieldType */
-
- public boolean isBinary() {
- return fieldsData instanceof BytesRef;
- }
-
/** Prints a Field for human consumption. */
@Override
public String toString() {
@@ -292,22 +451,8 @@
return result.toString();
}
- public void setDocValue(DocValue docValue) {
- this.docValue = docValue;
- }
-
- @Override
- public DocValue docValue() {
- return null;
- }
-
- @Override
- public DocValues.Type docValueType() {
- return null;
- }
-
- /** Returns FieldType for this field. */
- public IndexableFieldType fieldType() {
+ /** Returns the {@link FieldType} for this field. */
+ public FieldType fieldType() {
return type;
}
@@ -319,6 +464,38 @@
return null;
}
+ final NumericField.DataType numericType = fieldType().numericType();
+ if (numericType != null) {
+ if (numericTokenStream == null) {
+ // lazy init the TokenStream as it is heavy to instantiate
+ // (attributes,...) if not needed (stored field loading)
+ numericTokenStream = new NumericTokenStream(type.numericPrecisionStep());
+ // initialize value in TokenStream
+ final Number val = (Number) fieldsData;
+ switch (numericType) {
+ case INT:
+ numericTokenStream.setIntValue(val.intValue());
+ break;
+ case LONG:
+ numericTokenStream.setLongValue(val.longValue());
+ break;
+ case FLOAT:
+ numericTokenStream.setFloatValue(val.floatValue());
+ break;
+ case DOUBLE:
+ numericTokenStream.setDoubleValue(val.doubleValue());
+ break;
+ default:
+ assert false : "Should never get here";
+ }
+ } else {
+ // OK -- previously cached and we already updated if
+ // setters were called.
+ }
+
+ return numericTokenStream;
+ }
+
if (!fieldType().tokenized()) {
if (stringValue() == null) {
throw new IllegalArgumentException("Non-Tokenized Fields must have a String value");
@@ -355,6 +532,449 @@
return analyzer.tokenStream(name(), new StringReader(stringValue()));
}
- throw new IllegalArgumentException("Field must have either TokenStream, String or Reader value");
+ throw new IllegalArgumentException("Field must have either TokenStream, String, Reader or Number value");
}
+
+
+ //
+ // Deprecated transition API below:
+ //
+
+ /** Specifies whether and how a field should be stored.
+ *
+ * @deprecated This is here only to ease transition from
+ * the pre-4.0 APIs. */
+ @Deprecated
+ public static enum Store {
+
+ /** Store the original field value in the index. This is useful for short texts
+ * like a document's title which should be displayed with the results. The
+ * value is stored in its original form, i.e. no analyzer is used before it is
+ * stored.
+ */
+ YES {
+ @Override
+ public boolean isStored() { return true; }
+ },
+
+ /** Do not store the field value in the index. */
+ NO {
+ @Override
+ public boolean isStored() { return false; }
+ };
+
+ public abstract boolean isStored();
+ }
+
+ /** Specifies whether and how a field should be indexed.
+ *
+ * @deprecated This is here only to ease transition from
+ * the pre-4.0 APIs. */
+ @Deprecated
+ public static enum Index {
+
+ /** Do not index the field value. This field can thus not be searched,
+ * but one can still access its contents provided it is
+ * {@link Field.Store stored}. */
+ NO {
+ @Override
+ public boolean isIndexed() { return false; }
+ @Override
+ public boolean isAnalyzed() { return false; }
+ @Override
+ public boolean omitNorms() { return true; }
+ },
+
+ /** Index the tokens produced by running the field's
+ * value through an Analyzer. This is useful for
+ * common text. */
+ ANALYZED {
+ @Override
+ public boolean isIndexed() { return true; }
+ @Override
+ public boolean isAnalyzed() { return true; }
+ @Override
+ public boolean omitNorms() { return false; }
+ },
+
+ /** Index the field's value without using an Analyzer, so it can be searched.
+ * As no analyzer is used the value will be stored as a single term. This is
+ * useful for unique Ids like product numbers.
+ */
+ NOT_ANALYZED {
+ @Override
+ public boolean isIndexed() { return true; }
+ @Override
+ public boolean isAnalyzed() { return false; }
+ @Override
+ public boolean omitNorms() { return false; }
+ },
+
+ /** Expert: Index the field's value without an Analyzer,
+ * and also disable the indexing of norms. Note that you
+ * can also separately enable/disable norms by calling
+ * {@link FieldType#setOmitNorms}. No norms means that
+ * index-time field and document boosting and field
+ * length normalization are disabled. The benefit is
+ * less memory usage as norms take up one byte of RAM
+ * per indexed field for every document in the index,
+ * during searching. Note that once you index a given
+ * field with norms enabled, disabling norms will
+ * have no effect. In other words, for this to have the
+ * above described effect on a field, all instances of
+ * that field must be indexed with NOT_ANALYZED_NO_NORMS
+ * from the beginning. */
+ NOT_ANALYZED_NO_NORMS {
+ @Override
+ public boolean isIndexed() { return true; }
+ @Override
+ public boolean isAnalyzed() { return false; }
+ @Override
+ public boolean omitNorms() { return true; }
+ },
+
+ /** Expert: Index the tokens produced by running the
+ * field's value through an Analyzer, and also
+ * separately disable the storing of norms. See
+ * {@link #NOT_ANALYZED_NO_NORMS} for what norms are
+ * and why you may want to disable them. */
+ ANALYZED_NO_NORMS {
+ @Override
+ public boolean isIndexed() { return true; }
+ @Override
+ public boolean isAnalyzed() { return true; }
+ @Override
+ public boolean omitNorms() { return true; }
+ };
+
+ /** Get the best representation of the index given the flags. */
+ public static Index toIndex(boolean indexed, boolean analyzed) {
+ return toIndex(indexed, analyzed, false);
+ }
+
+ /** Expert: Get the best representation of the index given the flags. */
+ public static Index toIndex(boolean indexed, boolean analyzed, boolean omitNorms) {
+
+ // If it is not indexed nothing else matters
+ if (!indexed) {
+ return Index.NO;
+ }
+
+ // typical, non-expert
+ if (!omitNorms) {
+ if (analyzed) {
+ return Index.ANALYZED;
+ }
+ return Index.NOT_ANALYZED;
+ }
+
+ // Expert: Norms omitted
+ if (analyzed) {
+ return Index.ANALYZED_NO_NORMS;
+ }
+ return Index.NOT_ANALYZED_NO_NORMS;
+ }
+
+ public abstract boolean isIndexed();
+ public abstract boolean isAnalyzed();
+ public abstract boolean omitNorms();
+ }
+
+ /** Specifies whether and how a field should have term vectors.
+ *
+ * @deprecated This is here only to ease transition from
+ * the pre-4.0 APIs. */
+ @Deprecated
+ public static enum TermVector {
+
+ /** Do not store term vectors.
+ */
+ NO {
+ @Override
+ public boolean isStored() { return false; }
+ @Override
+ public boolean withPositions() { return false; }
+ @Override
+ public boolean withOffsets() { return false; }
+ },
+
+ /** Store the term vectors of each document. A term vector is a list
+ * of the document's terms and their number of occurrences in that document. */
+ YES {
+ @Override
+ public boolean isStored() { return true; }
+ @Override
+ public boolean withPositions() { return false; }
+ @Override
+ public boolean withOffsets() { return false; }
+ },
+
+ /**
+ * Store the term vector + token position information
+ *
+ * @see #YES
+ */
+ WITH_POSITIONS {
+ @Override
+ public boolean isStored() { return true; }
+ @Override
+ public boolean withPositions() { return true; }
+ @Override
+ public boolean withOffsets() { return false; }
+ },
+
+ /**
+ * Store the term vector + Token offset information
+ *
+ * @see #YES
+ */
+ WITH_OFFSETS {
+ @Override
+ public boolean isStored() { return true; }
+ @Override
+ public boolean withPositions() { return false; }
+ @Override
+ public boolean withOffsets() { return true; }
+ },
+
+ /**
+ * Store the term vector + Token position and offset information
+ *
+ * @see #YES
+ * @see #WITH_POSITIONS
+ * @see #WITH_OFFSETS
+ */
+ WITH_POSITIONS_OFFSETS {
+ @Override
+ public boolean isStored() { return true; }
+ @Override
+ public boolean withPositions() { return true; }
+ @Override
+ public boolean withOffsets() { return true; }
+ };
+
+ /** Get the best representation of a TermVector given the flags. */
+ public static TermVector toTermVector(boolean stored, boolean withOffsets, boolean withPositions) {
+
+ // If it is not stored, nothing else matters.
+ if (!stored) {
+ return TermVector.NO;
+ }
+
+ if (withOffsets) {
+ if (withPositions) {
+ return Field.TermVector.WITH_POSITIONS_OFFSETS;
+ }
+ return Field.TermVector.WITH_OFFSETS;
+ }
+
+ if (withPositions) {
+ return Field.TermVector.WITH_POSITIONS;
+ }
+ return Field.TermVector.YES;
+ }
+
+ public abstract boolean isStored();
+ public abstract boolean withPositions();
+ public abstract boolean withOffsets();
+ }
+
+ /** Translates the pre-4.0 enums for specifying how a
+ * field should be indexed into the 4.0 {@link FieldType}
+ * approach.
+ *
+ * @deprecated This is here only to ease transition from
+ * the pre-4.0 APIs.
+ */
+ @Deprecated
+ public static final FieldType translateFieldType(Store store, Index index, TermVector termVector) {
+ final FieldType ft = new FieldType();
+
+ ft.setStored(store == Store.YES);
+
+ switch(index) {
+ case ANALYZED:
+ ft.setIndexed(true);
+ ft.setTokenized(true);
+ break;
+ case ANALYZED_NO_NORMS:
+ ft.setIndexed(true);
+ ft.setTokenized(true);
+ ft.setOmitNorms(true);
+ break;
+ case NOT_ANALYZED:
+ ft.setIndexed(true);
+ break;
+ case NOT_ANALYZED_NO_NORMS:
+ ft.setIndexed(true);
+ ft.setOmitNorms(true);
+ break;
+ case NO:
+ break;
+ }
+
+ switch(termVector) {
+ case NO:
+ break;
+ case YES:
+ ft.setStoreTermVectors(true);
+ break;
+ case WITH_POSITIONS:
+ ft.setStoreTermVectors(true);
+ ft.setStoreTermVectorPositions(true);
+ break;
+ case WITH_OFFSETS:
+ ft.setStoreTermVectors(true);
+ ft.setStoreTermVectorOffsets(true);
+ break;
+ case WITH_POSITIONS_OFFSETS:
+ ft.setStoreTermVectors(true);
+ ft.setStoreTermVectorPositions(true);
+ ft.setStoreTermVectorOffsets(true);
+ break;
+ }
+ ft.freeze();
+ return ft;
+ }
+
+ /**
+ * Create a field by specifying its name, value and how it will
+ * be saved in the index. Term vectors will not be stored in the index.
+ *
+ * @param name The name of the field
+ * @param value The string to process
+ * @param store Whether value should be stored in the index
+ * @param index Whether the field should be indexed, and if so, if it should
+ * be tokenized before indexing
+ * @throws NullPointerException if name or value is null
+ * @throws IllegalArgumentException if the field is neither stored nor indexed
+ *
+ * @deprecated Use {@link StringField}, {@link TextField} instead. */
+ @Deprecated
+ public Field(String name, String value, Store store, Index index) {
+ this(name, value, translateFieldType(store, index, TermVector.NO));
+ }
+
+ /**
+ * Create a field by specifying its name, value and how it will
+ * be saved in the index.
+ *
+ * @param name The name of the field
+ * @param value The string to process
+ * @param store Whether value should be stored in the index
+ * @param index Whether the field should be indexed, and if so, if it should
+ * be tokenized before indexing
+ * @param termVector Whether term vector should be stored
+ * @throws NullPointerException if name or value is null
+ * @throws IllegalArgumentException in any of the following situations:
+ *
TermVector.YESnull
+ *
+ * @deprecated Use {@link TextField} instead.
+ */
+ @Deprecated
+ public Field(String name, Reader reader) {
+ this(name, reader, TermVector.NO);
+ }
+
+ /**
+ * Create a tokenized and indexed field that is not stored, optionally with
+ * storing term vectors. The Reader is read only when the Document is added to the index,
+ * i.e. you may not close the Reader until {@link IndexWriter#addDocument}
+ * has been called.
+ *
+ * @param name The name of the field
+ * @param reader The reader with the content
+ * @param termVector Whether term vector should be stored
+ * @throws NullPointerException if name or reader is null
+ *
+ * @deprecated Use {@link TextField} instead.
+ */
+ @Deprecated
+ public Field(String name, Reader reader, TermVector termVector) {
+ this(name, reader, translateFieldType(Store.NO, Index.ANALYZED, termVector));
+ }
+
+ /**
+ * Create a tokenized and indexed field that is not stored. Term vectors will
+ * not be stored. This is useful for pre-analyzed fields.
+ * The TokenStream is read only when the Document is added to the index,
+ * i.e. you may not close the TokenStream until {@link IndexWriter#addDocument}
+ * has been called.
+ *
+ * @param name The name of the field
+ * @param tokenStream The TokenStream with the content
+ * @throws NullPointerException if name or tokenStream is null
+ *
+ * @deprecated Use {@link TextField} instead
+ */
+ @Deprecated
+ public Field(String name, TokenStream tokenStream) {
+ this(name, tokenStream, TermVector.NO);
+ }
+
+ /**
+ * Create a tokenized and indexed field that is not stored, optionally with
+ * storing term vectors. This is useful for pre-analyzed fields.
+ * The TokenStream is read only when the Document is added to the index,
+ * i.e. you may not close the TokenStream until {@link IndexWriter#addDocument}
+ * has been called.
+ *
+ * @param name The name of the field
+ * @param tokenStream The TokenStream with the content
+ * @param termVector Whether term vector should be stored
+ * @throws NullPointerException if name or tokenStream is null
+ *
+ * @deprecated Use {@link TextField} instead
+ */
+ @Deprecated
+ public Field(String name, TokenStream tokenStream, TermVector termVector) {
+ this(name, tokenStream, translateFieldType(Store.NO, Index.ANALYZED, termVector));
+ }
+
+ /**
+ * Create a stored field with binary value. Optionally the value may be compressed.
+ *
+ * @param name The name of the field
+ * @param value The binary value
+ *
+ * @deprecated Use {@link StoredField} instead.
+ */
+ @Deprecated
+ public Field(String name, byte[] value) {
+ this(name, value, translateFieldType(Store.YES, Index.NO, TermVector.NO));
+ }
+
+ /**
+ * Create a stored field with binary value. Optionally the value may be compressed.
+ *
+ * @param name The name of the field
+ * @param value The binary value
+ * @param offset Starting offset in value where this Field's bytes are
+ * @param length Number of bytes to use for this Field, starting at offset
+ *
+ * @deprecated Use {@link StoredField} instead.
+ */
+ @Deprecated
+ public Field(String name, byte[] value, int offset, int length) {
+ this(name, value, offset, length, translateFieldType(Store.YES, Index.NO, TermVector.NO));
+ }
}
Index: lucene/src/java/org/apache/lucene/document/FieldType.java
===================================================================
--- lucene/src/java/org/apache/lucene/document/FieldType.java (revision 1230137)
+++ lucene/src/java/org/apache/lucene/document/FieldType.java (working copy)
@@ -17,8 +17,11 @@
* limitations under the License.
*/
+import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.apache.lucene.index.IndexableFieldType;
+import org.apache.lucene.search.NumericRangeQuery; // javadocs
+import org.apache.lucene.util.NumericUtils;
public class FieldType implements IndexableFieldType {
@@ -30,9 +33,12 @@
private boolean storeTermVectorPositions;
private boolean omitNorms;
private IndexOptions indexOptions = IndexOptions.DOCS_AND_FREQS_AND_POSITIONS;
+ private DocValues.Type docValueType;
+ private NumericField.DataType numericType;
private boolean frozen;
+ private int numericPrecisionStep = NumericUtils.PRECISION_STEP_DEFAULT;
- public FieldType(IndexableFieldType ref) {
+ public FieldType(FieldType ref) {
this.indexed = ref.indexed();
this.stored = ref.stored();
this.tokenized = ref.tokenized();
@@ -41,6 +47,8 @@
this.storeTermVectorPositions = ref.storeTermVectorPositions();
this.omitNorms = ref.omitNorms();
this.indexOptions = ref.indexOptions();
+ this.docValueType = ref.docValueType();
+ this.numericType = ref.numericType();
// Do not copy frozen!
}
@@ -49,7 +57,7 @@
private void checkIfFrozen() {
if (frozen) {
- throw new IllegalStateException();
+ throw new IllegalStateException("this FieldType is already frozen and cannot be changed");
}
}
@@ -134,6 +142,42 @@
this.indexOptions = value;
}
+ public void setDocValueType(DocValues.Type type) {
+ checkIfFrozen();
+ docValueType = type;
+ }
+
+ @Override
+ public DocValues.Type docValueType() {
+ return docValueType;
+ }
+
+ public void setNumericType(NumericField.DataType type) {
+ checkIfFrozen();
+ numericType = type;
+ }
+
+ /** Numeric {@link NumericField.DataType}; if
+ * non-null then the field's value will be indexed
+ * numerically so that {@link NumericRangeQuery} can be
+ * used at search time. */
+ public NumericField.DataType numericType() {
+ return numericType;
+ }
+
+ public void setNumericPrecisionStep(int precisionStep) {
+ checkIfFrozen();
+ if (precisionStep < 1) {
+ throw new IllegalArgumentException("precisionStep must be >= 1 (got " + precisionStep + ")");
+ }
+ this.numericPrecisionStep = precisionStep;
+ }
+
+ /** Precision step for numeric field. */
+ public int numericPrecisionStep() {
+ return numericPrecisionStep;
+ }
+
/** Prints a Field for human consumption. */
@Override
public final String toString() {
@@ -172,7 +216,17 @@
result.append(",indexOptions=");
result.append(indexOptions);
}
+ if (numericType != null) {
+ result.append(",numericType=");
+ result.append(numericType);
+ result.append(",numericPrecisionStep=");
+ result.append(numericPrecisionStep);
+ }
}
+ if (docValueType != null) {
+ result.append(",docValueType=");
+ result.append(docValueType);
+ }
return result.toString();
}
Index: lucene/src/java/org/apache/lucene/document/NumericField.java
===================================================================
--- lucene/src/java/org/apache/lucene/document/NumericField.java (revision 1230137)
+++ lucene/src/java/org/apache/lucene/document/NumericField.java (working copy)
@@ -17,17 +17,14 @@
* limitations under the License.
*/
-import java.io.Reader;
-import org.apache.lucene.analysis.Analyzer;
-import org.apache.lucene.analysis.TokenStream;
-import org.apache.lucene.analysis.NumericTokenStream;
+import org.apache.lucene.analysis.NumericTokenStream; // javadocs
+import org.apache.lucene.document.NumericField.DataType;
import org.apache.lucene.index.FieldInfo.IndexOptions;
-import org.apache.lucene.document.NumericField.DataType;
+import org.apache.lucene.search.FieldCache; // javadocs
+import org.apache.lucene.search.NumericRangeFilter; // javadocs
+import org.apache.lucene.search.NumericRangeQuery; // javadocs
import org.apache.lucene.util.NumericUtils;
-import org.apache.lucene.search.NumericRangeQuery; // javadocs
-import org.apache.lucene.search.NumericRangeFilter; // javadocs
-import org.apache.lucene.search.FieldCache; // javadocs
/**
* @@ -36,20 +33,20 @@ * int value: * *
- * document.add(new NumericField(name).setIntValue(value)); + * document.add(new NumericField(name, value)); ** * For optimal performance, re-use the
NumericField and
* {@link Document} instance for more than one document:
*
*
- * NumericField field = new NumericField(name);
+ * NumericField field = new NumericField(name, NumericField.DataType.INT);
* Document document = new Document();
* document.add(field);
*
* for(all documents) {
* ...
- * field.setIntValue(value)
+ * field.setValue(value)
* writer.addDocument(document);
* ...
* }
@@ -77,8 +74,8 @@
*
* By default, a NumericField's value is not stored but
* is indexed for range filtering and sorting. You can use
- * the {@link #NumericField(String, FieldType)}
- * constructor if you need to change these defaults.
+ * {@link Field#Field(String,Number,FieldType)}
+ * if you need to change these defaults.
*
* You may add the same field name as a NumericField to
* the same document more than once. Range querying and
@@ -104,8 +101,8 @@
* but may result in faster range search performance. The
* default value, 4, was selected for a reasonable tradeoff
* of disk space consumption versus performance. You can
- * use the expert constructor {@link
- * #NumericField(String,int, FieldType)} if you'd
+ * create a custom {@link FieldType} and invoke the {@link
+ * FieldType#setNumericPrecisionStep} method if you'd
* like to change the value. Note that you must also
* specify a congruent value when creating {@link
* NumericRangeQuery} or {@link NumericRangeFilter}.
@@ -137,244 +134,90 @@
/** Data type of the value in {@link NumericField}.
* @since 3.2
*/
- public static enum DataType { INT, LONG, FLOAT, DOUBLE }
+ public static enum DataType {INT, LONG, FLOAT, DOUBLE}
- public static final FieldType TYPE_UNSTORED = new FieldType();
- public static final FieldType TYPE_STORED = new FieldType();
- static {
- TYPE_UNSTORED.setIndexed(true);
- TYPE_UNSTORED.setTokenized(true);
- TYPE_UNSTORED.setOmitNorms(true);
- TYPE_UNSTORED.setIndexOptions(IndexOptions.DOCS_ONLY);
- TYPE_UNSTORED.freeze();
+ /** @lucene.experimental */
+ public static FieldType getFieldType(DataType type, boolean stored) {
+ final FieldType ft = new FieldType();
+ ft.setIndexed(true);
+ ft.setStored(stored);
+ ft.setTokenized(true);
+ ft.setOmitNorms(true);
+ ft.setIndexOptions(IndexOptions.DOCS_ONLY);
+ ft.setNumericType(type);
+ ft.freeze();
+ return ft;
+ }
- TYPE_STORED.setIndexed(true);
- TYPE_STORED.setStored(true);
- TYPE_STORED.setTokenized(true);
- TYPE_STORED.setOmitNorms(true);
- TYPE_STORED.setIndexOptions(IndexOptions.DOCS_ONLY);
- TYPE_STORED.freeze();
+ private static final FieldType INT_TYPE = getFieldType(DataType.INT, false);
+ private static final FieldType LONG_TYPE = getFieldType(DataType.LONG, false);
+ private static final FieldType FLOAT_TYPE = getFieldType(DataType.FLOAT, false);
+ private static final FieldType DOUBLE_TYPE = getFieldType(DataType.DOUBLE, false);
+
+ /** Creates an int NumericField with the provided value
+ * and default precisionStep {@link
+ * NumericUtils#PRECISION_STEP_DEFAULT} (4). */
+ public NumericField(String name, int value) {
+ super(name, INT_TYPE);
+ fieldsData = Integer.valueOf(value);
}
- //public static enum DataType { INT, LONG, FLOAT, DOUBLE }
-
- private DataType dataType;
- private transient NumericTokenStream numericTS;
- private final int precisionStep;
-
- /**
- * Creates a field for numeric values using the default
- * precisionStep {@link NumericUtils#PRECISION_STEP_DEFAULT} (4).
- * The instance is not yet initialized with a numeric value, before indexing a
- * document containing this field, set a value using the various set
- * ???Value() methods. This constructor creates an indexed, but not
- * stored field.
- *
- * @param name
- * the field name
- */
- public NumericField(String name) {
- this(name, NumericUtils.PRECISION_STEP_DEFAULT, NumericField.TYPE_UNSTORED);
+ /** Creates a long NumericField with the provided value.
+ * and default precisionStep {@link
+ * NumericUtils#PRECISION_STEP_DEFAULT} (4). */
+ public NumericField(String name, long value) {
+ super(name, LONG_TYPE);
+ fieldsData = Long.valueOf(value);
}
-
- /**
- * Creates a field for numeric values using the default
- * precisionStep {@link NumericUtils#PRECISION_STEP_DEFAULT} (4).
- * The instance is not yet initialized with a numeric value, before indexing a
- * document containing this field, set a value using the various set
- * ???Value() methods.
- *
- * @param name
- * the field name
- * @param type
- * if the defualt field should be altered, e.g. stored,
- * {@link Document#getField} then returns {@code NumericField}
- * instances on search results, or indexed using
- * {@link NumericTokenStream}
- */
- public NumericField(String name, FieldType type) {
- this(name, NumericUtils.PRECISION_STEP_DEFAULT, type);
+
+ /** Creates a float NumericField with the provided value.
+ * and default precisionStep {@link
+ * NumericUtils#PRECISION_STEP_DEFAULT} (4). */
+ public NumericField(String name, float value) {
+ super(name, FLOAT_TYPE);
+ fieldsData = Float.valueOf(value);
}
-
- /**
- * Creates a field for numeric values with the specified
- * precisionStep. The instance is not yet initialized with a
- * numeric value, before indexing a document containing this field, set a
- * value using the various set???Value() methods. This constructor
- * creates an indexed, but not stored field.
- *
- * @param name
- * the field name
- * @param precisionStep
- * the used precision step
- */
- public NumericField(String name, int precisionStep) {
- this(name, precisionStep, NumericField.TYPE_UNSTORED);
+
+ /** Creates a double NumericField with the provided value.
+ * and default precisionStep {@link
+ * NumericUtils#PRECISION_STEP_DEFAULT} (4). */
+ public NumericField(String name, double value) {
+ super(name, DOUBLE_TYPE);
+ fieldsData = Double.valueOf(value);
}
- /**
- * Creates a field for numeric values with the specified
- * precisionStep. The instance is not yet initialized with a
- * numeric value, before indexing a document containing this field, set a
- * value using the various set???Value() methods.
- *
- * @param name
- * the field name
- * @param precisionStep
- * the used precision step
- * @param type
- * if the defualt field should be altered, e.g. stored,
- * {@link Document#getField} then returns {@code NumericField}
- * instances on search results, or indexed using
- * {@link NumericTokenStream}
- */
- public NumericField(String name, int precisionStep, FieldType type) {
+ public NumericField(String name, Number value, FieldType type) {
super(name, type);
- if (precisionStep < 1)
- throw new IllegalArgumentException("precisionStep must be >=1");
- this.precisionStep = precisionStep;
- }
-
- /** Returns a {@link NumericTokenStream} for indexing the numeric value. */
- public TokenStream tokenStream(Analyzer analyzer) {
- if (!type.indexed()) return null;
- if (numericTS == null) {
- // lazy init the TokenStream as it is heavy to instantiate
- // (attributes,...),
- // if not needed (stored field loading)
- numericTS = new NumericTokenStream(precisionStep);
- // initialize value in TokenStream
- if (fieldsData != null) {
- assert dataType != null;
- final Number val = (Number) fieldsData;
- switch (dataType) {
- case INT:
- numericTS.setIntValue(val.intValue());
- break;
- case LONG:
- numericTS.setLongValue(val.longValue());
- break;
- case FLOAT:
- numericTS.setFloatValue(val.floatValue());
- break;
- case DOUBLE:
- numericTS.setDoubleValue(val.doubleValue());
- break;
- default:
- assert false : "Should never get here";
- }
+ final NumericField.DataType numericType = type.numericType();
+ if (numericType == null) {
+ throw new IllegalArgumentException("FieldType.numericType() cannot be null");
+ }
+
+ switch(numericType) {
+ case INT:
+ if (!(value instanceof Integer)) {
+ throw new IllegalArgumentException("value must be an Integer but got " + value);
}
+ break;
+ case LONG:
+ if (!(value instanceof Long)) {
+ throw new IllegalArgumentException("value must be a Long but got " + value);
+ }
+ break;
+ case FLOAT:
+ if (!(value instanceof Float)) {
+ throw new IllegalArgumentException("value must be a Float but got " + value);
+ }
+ break;
+ case DOUBLE:
+ if (!(value instanceof Double)) {
+ throw new IllegalArgumentException("value must be a Double but got " + value);
+ }
+ break;
+ default:
+ assert false : "Should never get here";
}
- return numericTS;
- }
-
- /** Returns always null for numeric fields */
- public Reader readerValue() {
- return null;
- }
-
- /**
- * Returns the numeric value as a string. It is recommended to
- * use {@link Document#getField} instead that returns {@code NumericField}
- * instances. You can then use {@link #numericValue} to return the stored
- * value.
- */
- @Override
- public String stringValue() {
- return (fieldsData == null) ? null : fieldsData.toString();
- }
-
- /**
- * Returns the current numeric value as a subclass of {@link Number},
- * null if not yet initialized.
- */
- @Override
- public Number numericValue() {
- return (Number) fieldsData;
- }
-
- /** Returns the precision step. */
- public int getPrecisionStep() {
- return precisionStep;
- }
-
- /**
- * Returns the data type of the current value, {@code null} if not yet set.
- *
- * @since 3.2
- */
- @Override
- public DataType numericDataType() {
- return dataType;
- }
- @Override
- public boolean numeric() {
- return true;
+ fieldsData = value;
}
-
- /**
- * Initializes the field with the supplied long value.
- *
- * @param value
- * the numeric value
- * @return this instance, because of this you can use it the following way:
- * document.add(new NumericField(name, precisionStep).setLongValue(value))
- */
- public NumericField setLongValue(final long value) {
- if (numericTS != null) numericTS.setLongValue(value);
- fieldsData = Long.valueOf(value);
- dataType = DataType.LONG;
- return this;
- }
-
- /**
- * Initializes the field with the supplied int value.
- *
- * @param value
- * the numeric value
- * @return this instance, because of this you can use it the following way:
- * document.add(new NumericField(name, precisionStep).setIntValue(value))
- */
- public NumericField setIntValue(final int value) {
- if (numericTS != null) numericTS.setIntValue(value);
- fieldsData = Integer.valueOf(value);
- dataType = DataType.INT;
- return this;
- }
-
- /**
- * Initializes the field with the supplied double value.
- *
- * @param value
- * the numeric value
- * @return this instance, because of this you can use it the following way:
- * document.add(new NumericField(name, precisionStep).setDoubleValue(value))
- */
- public NumericField setDoubleValue(final double value) {
- if (numericTS != null) numericTS.setDoubleValue(value);
- fieldsData = Double.valueOf(value);
- dataType = DataType.DOUBLE;
- return this;
- }
-
- /**
- * Initializes the field with the supplied float value.
- *
- * @param value
- * the numeric value
- * @return this instance, because of this you can use it the following way:
- * document.add(new NumericField(name, precisionStep).setFloatValue(value))
- */
- public NumericField setFloatValue(final float value) {
- if (numericTS != null) numericTS.setFloatValue(value);
- fieldsData = Float.valueOf(value);
- dataType = DataType.FLOAT;
- return this;
- }
-
}
Index: lucene/src/java/org/apache/lucene/document/BinaryField.java
===================================================================
--- lucene/src/java/org/apache/lucene/document/BinaryField.java (revision 1230137)
+++ lucene/src/java/org/apache/lucene/document/BinaryField.java (working copy)
@@ -1,46 +0,0 @@
-package org.apache.lucene.document;
-
-import org.apache.lucene.util.BytesRef;
-
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/** A field with byte[] value that is only stored. */
-
-public final class BinaryField extends Field {
-
- public static final FieldType TYPE_STORED = new FieldType();
- static {
- TYPE_STORED.setStored(true);
- TYPE_STORED.freeze();
- }
-
- /** Creates a new BinaryField */
- public BinaryField(String name, byte[] value) {
- super(name, value, BinaryField.TYPE_STORED);
- }
-
- /** Creates a new BinaryField */
- public BinaryField(String name, byte[] value, int offset, int length) {
- super(name, value, offset, length, BinaryField.TYPE_STORED);
- }
-
- /** Creates a new BinaryField */
- public BinaryField(String name, BytesRef bytes) {
- super(name, bytes, BinaryField.TYPE_STORED);
- }
-}
Index: lucene/src/java/org/apache/lucene/document/TextField.java
===================================================================
--- lucene/src/java/org/apache/lucene/document/TextField.java (revision 1230137)
+++ lucene/src/java/org/apache/lucene/document/TextField.java (working copy)
@@ -48,6 +48,8 @@
TYPE_STORED.freeze();
}
+ // TODO: add sugar for term vectors...?
+
/** Creates a new un-stored TextField */
public TextField(String name, Reader reader) {
super(name, reader, TextField.TYPE_UNSTORED);
Index: lucene/src/java/org/apache/lucene/document/DocValuesField.java
===================================================================
--- lucene/src/java/org/apache/lucene/document/DocValuesField.java (revision 1230137)
+++ lucene/src/java/org/apache/lucene/document/DocValuesField.java (working copy)
@@ -16,13 +16,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-import java.io.Reader;
+
import java.util.Comparator;
+import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.Map;
-import org.apache.lucene.index.IndexableFieldType;
-import org.apache.lucene.index.DocValue;
+import org.apache.lucene.index.DocValues.Type; // javadocs
import org.apache.lucene.index.DocValues;
-import org.apache.lucene.index.DocValues.Type; // javadocs
import org.apache.lucene.util.BytesRef;
/**
@@ -32,14 +33,16 @@
* example usage, adding an int value:
*
*
- * document.add(new DocValuesField(name).setInt(value));
+ * DocValuesField field = new DocValuesField(name, DocValues.Type.VAR_INTS);
+ * field.setInt(value);
+ * document.add(field);
*
*
* For optimal performance, re-use the DocValuesField and
* {@link Document} instance for more than one document:
*
*
- * DocValuesField field = new DocValuesField(name);
+ * DocValuesField field = new DocValuesField(name, DocValues.Type.VAR_INTS);
* Document document = new Document();
* document.add(field);
*
@@ -69,326 +72,97 @@
*
*
* */
-public class DocValuesField extends Field implements DocValue {
- protected BytesRef bytes;
- protected double doubleValue;
- protected long longValue;
- protected DocValues.Type type;
+public class DocValuesField extends Field {
+
protected Comparator bytesComparator;
- /**
- * Creates a new {@link DocValuesField} with the given name.
- */
- public DocValuesField(String name) {
- this(name, new FieldType());
- }
-
- public DocValuesField(String name, IndexableFieldType type) {
- this(name, type, null);
- }
-
- public DocValuesField(String name, IndexableFieldType type, String value) {
- super(name, type);
- fieldsData = value;
- }
-
- @Override
- public DocValue docValue() {
- return this;
- }
-
- /**
- * Sets the given long value and sets the field's {@link Type} to
- * {@link Type#VAR_INTS} unless already set. If you want to change the
- * default type use {@link #setDocValuesType(DocValues.Type)}.
- */
- public void setInt(long value) {
- setInt(value, false);
- }
-
- /**
- * Sets the given long value as a 64 bit signed integer.
- *
- * @param value
- * the value to set
- * @param fixed
- * if true {@link Type#FIXED_INTS_64} is used
- * otherwise {@link Type#VAR_INTS}
- */
- public void setInt(long value, boolean fixed) {
- if (type == null) {
- type = fixed ? DocValues.Type.FIXED_INTS_64 : DocValues.Type.VAR_INTS;
+ private static final Map types = new HashMap();
+ static {
+ for(DocValues.Type type : DocValues.Type.values()) {
+ final FieldType ft = new FieldType();
+ ft.setDocValueType(type);
+ ft.freeze();
+ types.put(type, ft);
}
- longValue = value;
}
- /**
- * Sets the given int value and sets the field's {@link Type} to
- * {@link Type#VAR_INTS} unless already set. If you want to change the
- * default type use {@link #setDocValuesType(DocValues.Type)}.
- */
- public void setInt(int value) {
- setInt(value, false);
- }
+ private static EnumSet BYTES = EnumSet.of(
+ Type.BYTES_FIXED_DEREF,
+ Type.BYTES_FIXED_STRAIGHT,
+ Type.BYTES_VAR_DEREF,
+ Type.BYTES_VAR_STRAIGHT,
+ Type.BYTES_FIXED_SORTED,
+ Type.BYTES_VAR_SORTED);
- /**
- * Sets the given int value as a 32 bit signed integer.
- *
- * @param value
- * the value to set
- * @param fixed
- * if true {@link Type#FIXED_INTS_32} is used
- * otherwise {@link Type#VAR_INTS}
- */
- public void setInt(int value, boolean fixed) {
- if (type == null) {
- type = fixed ? DocValues.Type.FIXED_INTS_32 : DocValues.Type.VAR_INTS;
- }
- longValue = value;
- }
+ private static EnumSet INTS = EnumSet.of(
+ Type.VAR_INTS,
+ Type.FIXED_INTS_8,
+ Type.FIXED_INTS_16,
+ Type.FIXED_INTS_32,
+ Type.FIXED_INTS_64);
- /**
- * Sets the given short value and sets the field's {@link Type} to
- * {@link Type#VAR_INTS} unless already set. If you want to change the
- * default type use {@link #setDocValuesType(DocValues.Type)}.
- */
- public void setInt(short value) {
- setInt(value, false);
+ public static FieldType getFieldType(DocValues.Type type) {
+ return types.get(type);
}
- /**
- * Sets the given short value as a 16 bit signed integer.
- *
- * @param value
- * the value to set
- * @param fixed
- * if true {@link Type#FIXED_INTS_16} is used
- * otherwise {@link Type#VAR_INTS}
- */
- public void setInt(short value, boolean fixed) {
- if (type == null) {
- type = fixed ? DocValues.Type.FIXED_INTS_16 : DocValues.Type.VAR_INTS;
+ public DocValuesField(String name, BytesRef bytes, DocValues.Type docValueType) {
+ super(name, getFieldType(docValueType));
+ if (!BYTES.contains(docValueType)) {
+ throw new IllegalArgumentException("docValueType must be one of: " + BYTES + "; got " + docValueType);
}
- longValue = value;
+ fieldsData = bytes;
}
- /**
- * Sets the given byte value and sets the field's {@link Type} to
- * {@link Type#VAR_INTS} unless already set. If you want to change the
- * default type use {@link #setDocValuesType(DocValues.Type)}.
- */
- public void setInt(byte value) {
- setInt(value, false);
- }
-
- /**
- * Sets the given byte value as a 8 bit signed integer.
- *
- * @param value
- * the value to set
- * @param fixed
- * if true {@link Type#FIXED_INTS_8} is used
- * otherwise {@link Type#VAR_INTS}
- */
- public void setInt(byte value, boolean fixed) {
- if (type == null) {
- type = fixed ? DocValues.Type.FIXED_INTS_8 : DocValues.Type.VAR_INTS;
+ /*
+ public DocValuesField(String name, byte value, DocValues.Type docValueType) {
+ super(name, getFieldType(docValueType));
+ if (!INTS.contains(docValueType)) {
+ throw new IllegalArgumentException("docValueType must be one of: " + INTS +"; got " + docValueType);
}
- longValue = value;
+ fieldsData = Byte.valueOf(value);
}
- /**
- * Sets the given float value and sets the field's {@link Type}
- * to {@link Type#FLOAT_32} unless already set. If you want to
- * change the type use {@link #setDocValuesType(DocValues.Type)}.
- */
- public void setFloat(float value) {
- if (type == null) {
- type = DocValues.Type.FLOAT_32;
+ public DocValuesField(String name, short value, DocValues.Type docValueType) {
+ super(name, getFieldType(docValueType));
+ if (!INTS.contains(docValueType)) {
+ throw new IllegalArgumentException("docValueType must be one of: " + INTS +"; got " + docValueType);
}
- doubleValue = value;
+ fieldsData = Short.valueOf(value);
}
+ */
- /**
- * Sets the given double value and sets the field's {@link Type}
- * to {@link Type#FLOAT_64} unless already set. If you want to
- * change the default type use {@link #setDocValuesType(DocValues.Type)}.
- */
- public void setFloat(double value) {
- if (type == null) {
- type = DocValues.Type.FLOAT_64;
+ public DocValuesField(String name, int value, DocValues.Type docValueType) {
+ super(name, getFieldType(docValueType));
+ if (!INTS.contains(docValueType)) {
+ throw new IllegalArgumentException("docValueType must be one of: " + INTS +"; got " + docValueType);
}
- doubleValue = value;
+ fieldsData = Integer.valueOf(value);
}
- /**
- * Sets the given {@link BytesRef} value and the field's {@link Type}. The
- * comparator for this field is set to null. If a
- * null comparator is set the default comparator for the given
- * {@link Type} is used.
- */
- public void setBytes(BytesRef value, DocValues.Type type) {
- setBytes(value, type, null);
- }
-
- /**
- * Sets the given {@link BytesRef} value, the field's {@link Type} and the
- * field's comparator. If the {@link Comparator} is set to null
- * the default for the given {@link Type} is used instead.
- *
- * @throws IllegalArgumentException
- * if the value or the type are null
- */
- public void setBytes(BytesRef value, DocValues.Type type, Comparator comp) {
- if (value == null) {
- throw new IllegalArgumentException("value must not be null");
+ public DocValuesField(String name, long value, DocValues.Type docValueType) {
+ super(name, getFieldType(docValueType));
+ if (!INTS.contains(docValueType)) {
+ throw new IllegalArgumentException("docValueType must be one of: " + INTS +"; got " + docValueType);
}
- setDocValuesType(type);
- if (bytes == null) {
- bytes = BytesRef.deepCopyOf(value);
- } else {
- bytes.copyBytes(value);
- }
- bytesComparator = comp;
+ fieldsData = Long.valueOf(value);
}
- /**
- * Returns the set {@link BytesRef} or null if not set.
- */
- public BytesRef getBytes() {
- return bytes;
- }
-
- /**
- * Returns the set {@link BytesRef} comparator or null if not set
- */
- public Comparator bytesComparator() {
- return bytesComparator;
- }
-
- /**
- * Returns the set floating point value or 0.0d if not set.
- */
- public double getFloat() {
- return doubleValue;
- }
-
- /**
- * Returns the set long value of 0 if not set.
- */
- public long getInt() {
- return longValue;
- }
-
- /**
- * Sets the {@link BytesRef} comparator for this field. If the field has a
- * numeric {@link Type} the comparator will be ignored.
- */
- public void setBytesComparator(Comparator comp) {
- this.bytesComparator = comp;
- }
-
- /**
- * Sets the {@link Type} for this field.
- */
- public void setDocValuesType(DocValues.Type type) {
- if (type == null) {
- throw new IllegalArgumentException("Type must not be null");
+ public DocValuesField(String name, float value, DocValues.Type docValueType) {
+ super(name, getFieldType(docValueType));
+ if (docValueType != DocValues.Type.FLOAT_32 &&
+ docValueType != DocValues.Type.FLOAT_64) {
+ throw new IllegalArgumentException("docValueType must be FLOAT_32/64; got " + docValueType);
}
- this.type = type;
+ fieldsData = Float.valueOf(value);
}
- /**
- * Returns always null
- */
- public Reader readerValue() {
- return null;
- }
-
- @Override
- public DocValues.Type docValueType() {
- return type;
- }
-
- @Override
- public String toString() {
- final String value;
- switch (type) {
- case BYTES_FIXED_DEREF:
- case BYTES_FIXED_STRAIGHT:
- case BYTES_VAR_DEREF:
- case BYTES_VAR_STRAIGHT:
- case BYTES_FIXED_SORTED:
- case BYTES_VAR_SORTED:
- // don't use to unicode string this is not necessarily unicode here
- value = "bytes: " + bytes.toString();
- break;
- case FIXED_INTS_16:
- value = "int16: " + longValue;
- break;
- case FIXED_INTS_32:
- value = "int32: " + longValue;
- break;
- case FIXED_INTS_64:
- value = "int64: " + longValue;
- break;
- case FIXED_INTS_8:
- value = "int8: " + longValue;
- break;
- case VAR_INTS:
- value = "vint: " + longValue;
- break;
- case FLOAT_32:
- value = "float32: " + doubleValue;
- break;
- case FLOAT_64:
- value = "float64: " + doubleValue;
- break;
- default:
- throw new IllegalArgumentException("unknown type: " + type);
+ public DocValuesField(String name, double value, DocValues.Type docValueType) {
+ super(name, getFieldType(docValueType));
+ if (docValueType != DocValues.Type.FLOAT_32 &&
+ docValueType != DocValues.Type.FLOAT_64) {
+ throw new IllegalArgumentException("docValueType must be FLOAT_32/64; got " + docValueType);
}
- return "<" + name() + ": DocValuesField " + value + ">";
+ fieldsData = Double.valueOf(value);
}
-
- /**
- * Returns an DocValuesField holding the value from
- * the provided string field, as the specified type. The
- * incoming field must have a string value. The name, {@link
- * FieldType} and string value are carried over from the
- * incoming Field.
- */
- public static DocValuesField build(Field field, DocValues.Type type) {
- if (field instanceof DocValuesField) {
- return (DocValuesField) field;
- }
- final DocValuesField valField = new DocValuesField(field.name(), field.fieldType(), field.stringValue());
- switch (type) {
- case BYTES_FIXED_DEREF:
- case BYTES_FIXED_STRAIGHT:
- case BYTES_VAR_DEREF:
- case BYTES_VAR_STRAIGHT:
- case BYTES_FIXED_SORTED:
- case BYTES_VAR_SORTED:
- BytesRef ref = field.isBinary() ? field.binaryValue() : new BytesRef(field.stringValue());
- valField.setBytes(ref, type);
- break;
- case FIXED_INTS_16:
- case FIXED_INTS_32:
- case FIXED_INTS_64:
- case FIXED_INTS_8:
- case VAR_INTS:
- valField.setInt(Long.parseLong(field.stringValue()));
- break;
- case FLOAT_32:
- valField.setFloat(Float.parseFloat(field.stringValue()));
- break;
- case FLOAT_64:
- valField.setFloat(Double.parseDouble(field.stringValue()));
- break;
- default:
- throw new IllegalArgumentException("unknown type: " + type);
- }
- return valField;
- }
}
Index: lucene/src/java/org/apache/lucene/document/StringField.java
===================================================================
--- lucene/src/java/org/apache/lucene/document/StringField.java (revision 1230137)
+++ lucene/src/java/org/apache/lucene/document/StringField.java (working copy)
@@ -51,12 +51,12 @@
TYPE_STORED.setIndexOptions(IndexOptions.DOCS_ONLY);
TYPE_STORED.freeze();
}
-
+
/** Creates a new un-stored StringField */
public StringField(String name, String value) {
super(name, value, TYPE_UNSTORED);
}
-
+
@Override
public String stringValue() {
return (fieldsData == null) ? null : fieldsData.toString();
Index: lucene/src/test-framework/java/org/apache/lucene/codecs/preflexrw/PreFlexNormsConsumer.java
===================================================================
--- lucene/src/test-framework/java/org/apache/lucene/codecs/preflexrw/PreFlexNormsConsumer.java (revision 1230137)
+++ lucene/src/test-framework/java/org/apache/lucene/codecs/preflexrw/PreFlexNormsConsumer.java (working copy)
@@ -22,12 +22,12 @@
import org.apache.lucene.codecs.DocValuesConsumer;
import org.apache.lucene.codecs.PerDocConsumer;
-import org.apache.lucene.index.DocValue;
-import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.DocValues.Source;
import org.apache.lucene.index.DocValues.Type;
+import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.IndexFileNames;
+import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.MergeState;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext;
@@ -133,8 +133,8 @@
}
@Override
- public void add(int docID, DocValue docValue) throws IOException {
- add(docID, docValue.getBytes());
+ public void add(int docID, IndexableField docValue) throws IOException {
+ add(docID, docValue.binaryValue());
}
protected void add(int docID, BytesRef value) throws IOException {
Index: lucene/src/test-framework/java/org/apache/lucene/index/DocHelper.java
===================================================================
--- lucene/src/test-framework/java/org/apache/lucene/index/DocHelper.java (revision 1230137)
+++ lucene/src/test-framework/java/org/apache/lucene/index/DocHelper.java (working copy)
@@ -26,13 +26,13 @@
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.analysis.MockTokenizer;
-import org.apache.lucene.document.BinaryField;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
+import org.apache.lucene.document.FieldType;
+import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.StringField;
+import org.apache.lucene.document.TextField;
import org.apache.lucene.index.FieldInfo.IndexOptions;
-import org.apache.lucene.document.FieldType;
-import org.apache.lucene.document.TextField;
import org.apache.lucene.search.similarities.SimilarityProvider;
import org.apache.lucene.store.Directory;
@@ -197,7 +197,7 @@
LAZY_FIELD_BINARY_BYTES = "These are some binary field bytes".getBytes("UTF8");
} catch (UnsupportedEncodingException e) {
}
- lazyFieldBinary = new BinaryField(LAZY_FIELD_BINARY_KEY, LAZY_FIELD_BINARY_BYTES);
+ lazyFieldBinary = new StoredField(LAZY_FIELD_BINARY_KEY, LAZY_FIELD_BINARY_BYTES);
fields[fields.length - 2] = lazyFieldBinary;
LARGE_LAZY_FIELD_TEXT = buffer.toString();
largeLazyField = new Field(LARGE_LAZY_FIELD_KEY, LARGE_LAZY_FIELD_TEXT, customType);
Index: lucene/src/test-framework/java/org/apache/lucene/index/RandomIndexWriter.java
===================================================================
--- lucene/src/test-framework/java/org/apache/lucene/index/RandomIndexWriter.java (revision 1230137)
+++ lucene/src/test-framework/java/org/apache/lucene/index/RandomIndexWriter.java (working copy)
@@ -25,10 +25,10 @@
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.codecs.Codec;
-import org.apache.lucene.document.DocValuesField;
import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.IndexWriter; // javadoc
-import org.apache.lucene.index.DocValues;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef;
@@ -172,7 +172,10 @@
String name = "random_" + type.name() + "" + docValuesFieldPrefix;
if ("Lucene3x".equals(codec.getName()) || doc.getField(name) != null)
return;
- DocValuesField docValuesField = new DocValuesField(name);
+ FieldType ft = new FieldType();
+ ft.setDocValueType(type);
+ ft.freeze();
+ final Field f;
switch (type) {
case BYTES_FIXED_DEREF:
case BYTES_FIXED_STRAIGHT:
@@ -186,40 +189,38 @@
fixedRef.grow(fixedBytesLength);
fixedRef.length = fixedBytesLength;
}
- docValuesField.setBytes(fixedRef, type);
+ f = new Field(name, fixedRef, ft);
break;
case BYTES_VAR_DEREF:
case BYTES_VAR_STRAIGHT:
case BYTES_VAR_SORTED:
- BytesRef ref = new BytesRef(_TestUtil.randomUnicodeString(random, 200));
- docValuesField.setBytes(ref, type);
+ f = new Field(name, new BytesRef(_TestUtil.randomUnicodeString(random, 200)), ft);
break;
case FLOAT_32:
- docValuesField.setFloat(random.nextFloat());
+ f = new Field(name, random.nextFloat(), ft);
break;
case FLOAT_64:
- docValuesField.setFloat(random.nextDouble());
+ f = new Field(name, random.nextDouble(), ft);
break;
case VAR_INTS:
- docValuesField.setInt(random.nextLong());
+ f = new Field(name, random.nextLong(), ft);
break;
case FIXED_INTS_16:
- docValuesField.setInt(random.nextInt(Short.MAX_VALUE));
+ f = new Field(name, random.nextInt(Short.MAX_VALUE), ft);
break;
case FIXED_INTS_32:
- docValuesField.setInt(random.nextInt());
+ f = new Field(name, random.nextInt(), ft);
break;
case FIXED_INTS_64:
- docValuesField.setInt(random.nextLong());
+ f = new Field(name, random.nextLong(), ft);
break;
case FIXED_INTS_8:
- docValuesField.setInt(random.nextInt(128));
+ f = new Field(name, random.nextInt(128), ft);
break;
default:
throw new IllegalArgumentException("no such type: " + type);
}
-
- doc.add(docValuesField);
+ doc.add(f);
}
private void maybeCommit() throws IOException {
Index: solr/core/src/test/org/apache/solr/schema/PolyFieldTest.java
===================================================================
--- solr/core/src/test/org/apache/solr/schema/PolyFieldTest.java (revision 1230137)
+++ solr/core/src/test/org/apache/solr/schema/PolyFieldTest.java (working copy)
@@ -88,7 +88,8 @@
//first two fields contain the values, third is just stored and contains the original
for (int i = 0; i < 3; i++) {
boolean hasValue = fields[i].binaryValue() != null
- || fields[i].stringValue() != null;
+ || fields[i].stringValue() != null
+ || fields[i].numericValue() != null;
assertTrue("Doesn't have a value: " + fields[i], hasValue);
}
/*assertTrue("first field " + fields[0].tokenStreamValue() + " is not 35.0", pt.getSubType().toExternal(fields[0]).equals(String.valueOf(xy[0])));
Index: solr/core/src/java/org/apache/solr/schema/TrieField.java
===================================================================
--- solr/core/src/java/org/apache/solr/schema/TrieField.java (revision 1230137)
+++ solr/core/src/java/org/apache/solr/schema/TrieField.java (working copy)
@@ -104,9 +104,8 @@
@Override
public Object toObject(IndexableField f) {
- if (f.numeric()) {
- final Number val = f.numericValue();
- if (val==null) return badFieldString(f);
+ final Number val = f.numericValue();
+ if (val != null) {
return (type == TrieTypes.DATE) ? new Date(val.longValue()) : val;
} else {
// the following code is "deprecated" and only to support pre-3.2 indexes using the old BinaryField encoding:
@@ -405,10 +404,8 @@
@Override
public String storedToIndexed(IndexableField f) {
final BytesRef bytes = new BytesRef(NumericUtils.BUF_SIZE_LONG);
- if (f instanceof org.apache.lucene.document.NumericField) {
- final Number val = ((org.apache.lucene.document.NumericField) f).numericValue();
- if (val==null)
- throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Invalid field contents: "+f.name());
+ final Number val = f.numericValue();
+ if (val != null) {
switch (type) {
case INTEGER:
NumericUtils.intToPrefixCoded(val.intValue(), 0, bytes);
@@ -481,38 +478,60 @@
ft.setIndexed(indexed);
ft.setOmitNorms(field.omitNorms());
ft.setIndexOptions(getIndexOptions(field, value.toString()));
-
- final org.apache.lucene.document.NumericField f = new org.apache.lucene.document.NumericField(field.getName(), precisionStep, ft);
+
switch (type) {
case INTEGER:
+ ft.setNumericType(NumericField.DataType.INT);
+ break;
+ case FLOAT:
+ ft.setNumericType(NumericField.DataType.FLOAT);
+ break;
+ case LONG:
+ ft.setNumericType(NumericField.DataType.LONG);
+ break;
+ case DOUBLE:
+ ft.setNumericType(NumericField.DataType.DOUBLE);
+ break;
+ case DATE:
+ ft.setNumericType(NumericField.DataType.LONG);
+ break;
+ default:
+ throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + type);
+ }
+ ft.setNumericPrecisionStep(precisionStep);
+
+ final org.apache.lucene.document.NumericField f;
+
+ switch (type) {
+ case INTEGER:
int i = (value instanceof Number)
? ((Number)value).intValue()
: Integer.parseInt(value.toString());
- f.setIntValue(i);
+ f = new org.apache.lucene.document.NumericField(field.getName(), i, ft);
break;
case FLOAT:
float fl = (value instanceof Number)
? ((Number)value).floatValue()
: Float.parseFloat(value.toString());
- f.setFloatValue(fl);
+ f = new org.apache.lucene.document.NumericField(field.getName(), fl, ft);
break;
case LONG:
long l = (value instanceof Number)
? ((Number)value).longValue()
: Long.parseLong(value.toString());
- f.setLongValue(l);
+ f = new org.apache.lucene.document.NumericField(field.getName(), l, ft);
break;
case DOUBLE:
double d = (value instanceof Number)
? ((Number)value).doubleValue()
: Double.parseDouble(value.toString());
- f.setDoubleValue(d);
+ f = new org.apache.lucene.document.NumericField(field.getName(), d, ft);
break;
case DATE:
Date date = (value instanceof Date)
? ((Date)value)
: dateField.parseMath(null, value.toString());
- f.setLongValue(date.getTime());
+ f = new org.apache.lucene.document.NumericField(field.getName(), date.getTime(), ft);
break;
default:
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + type);
Index: solr/core/src/java/org/apache/solr/schema/BinaryField.java
===================================================================
--- solr/core/src/java/org/apache/solr/schema/BinaryField.java (revision 1230137)
+++ solr/core/src/java/org/apache/solr/schema/BinaryField.java (working copy)
@@ -81,7 +81,7 @@
len = buf.length;
}
- Field f = new org.apache.lucene.document.BinaryField(field.getName(), buf, offset, len);
+ Field f = new org.apache.lucene.document.StoredField(field.getName(), buf, offset, len);
f.setBoost(boost);
return f;
}
Index: solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java
===================================================================
--- solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java (revision 1230137)
+++ solr/core/src/java/org/apache/solr/search/SolrIndexSearcher.java (working copy)
@@ -23,7 +23,7 @@
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
-import org.apache.lucene.document.BinaryField;
+import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
@@ -418,7 +418,7 @@
@Override
public void binaryField(FieldInfo fieldInfo, byte[] value, int offset, int length) throws IOException {
- doc.add(new BinaryField(fieldInfo.name, value));
+ doc.add(new StoredField(fieldInfo.name, value));
}
@Override
@@ -434,30 +434,30 @@
@Override
public void intField(FieldInfo fieldInfo, int value) {
- FieldType ft = new FieldType(NumericField.TYPE_STORED);
+ FieldType ft = new FieldType(NumericField.getFieldType(NumericField.DataType.INT, true));
ft.setIndexed(fieldInfo.isIndexed);
- doc.add(new NumericField(fieldInfo.name, ft).setIntValue(value));
+ doc.add(new NumericField(fieldInfo.name, value, ft));
}
@Override
public void longField(FieldInfo fieldInfo, long value) {
- FieldType ft = new FieldType(NumericField.TYPE_STORED);
+ FieldType ft = new FieldType(NumericField.getFieldType(NumericField.DataType.LONG, true));
ft.setIndexed(fieldInfo.isIndexed);
- doc.add(new NumericField(fieldInfo.name, ft).setLongValue(value));
+ doc.add(new NumericField(fieldInfo.name, value, ft));
}
@Override
public void floatField(FieldInfo fieldInfo, float value) {
- FieldType ft = new FieldType(NumericField.TYPE_STORED);
+ FieldType ft = new FieldType(NumericField.getFieldType(NumericField.DataType.FLOAT, true));
ft.setIndexed(fieldInfo.isIndexed);
- doc.add(new NumericField(fieldInfo.name, ft).setFloatValue(value));
+ doc.add(new NumericField(fieldInfo.name, value, ft));
}
@Override
public void doubleField(FieldInfo fieldInfo, double value) {
- FieldType ft = new FieldType(NumericField.TYPE_STORED);
+ FieldType ft = new FieldType(NumericField.getFieldType(NumericField.DataType.DOUBLE, true));
ft.setIndexed(fieldInfo.isIndexed);
- doc.add(new NumericField(fieldInfo.name, ft).setDoubleValue(value));
+ doc.add(new NumericField(fieldInfo.name, value, ft));
}
}
Index: solr/core/src/java/org/apache/solr/response/transform/BaseEditorialTransformer.java
===================================================================
--- solr/core/src/java/org/apache/solr/response/transform/BaseEditorialTransformer.java (revision 1230137)
+++ solr/core/src/java/org/apache/solr/response/transform/BaseEditorialTransformer.java (working copy)
@@ -19,9 +19,7 @@
*/
import org.apache.lucene.document.Field;
-import org.apache.lucene.document.NumericField;
import org.apache.solr.common.SolrDocument;
-import org.apache.solr.handler.component.QueryElevationComponent;
import org.apache.solr.schema.FieldType;
import java.util.Set;
@@ -66,8 +64,14 @@
protected String getKey(SolrDocument doc) {
String key;
Object field = doc.get(idFieldName);
- if (field instanceof NumericField){
- key = ((Field)field).stringValue();
+ final Number n;
+ if (field instanceof Field) {
+ n = ((Field) field).numericValue();
+ } else {
+ n = null;
+ }
+ if (n != null) {
+ key = n.toString();
key = ft.readableToIndexed(key);
} else if (field instanceof Field){
key = ((Field)field).stringValue();
Index: modules/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java
===================================================================
--- modules/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java (revision 1230137)
+++ modules/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java (working copy)
@@ -34,8 +34,6 @@
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.grouping.GroupDocs;
import org.apache.lucene.search.grouping.TopGroups;
-import org.apache.lucene.search.join.BlockJoinCollector;
-import org.apache.lucene.search.join.BlockJoinQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.LuceneTestCase;
@@ -57,7 +55,7 @@
private Document makeJob(String skill, int year) {
Document job = new Document();
job.add(newField("skill", skill, StringField.TYPE_STORED));
- job.add(new NumericField("year").setIntValue(year));
+ job.add(new NumericField("year", year));
return job;
}
@@ -65,7 +63,7 @@
private Document makeQualification(String qualification, int year) {
Document job = new Document();
job.add(newField("qualification", qualification, StringField.TYPE_STORED));
- job.add(new NumericField("year").setIntValue(year));
+ job.add(new NumericField("year", year));
return job;
}
Index: modules/queryparser/src/test/org/apache/lucene/queryparser/xml/TestParser.java
===================================================================
--- modules/queryparser/src/test/org/apache/lucene/queryparser/xml/TestParser.java (revision 1230137)
+++ modules/queryparser/src/test/org/apache/lucene/queryparser/xml/TestParser.java (working copy)
@@ -68,9 +68,7 @@
Document doc = new Document();
doc.add(newField("date", date, TextField.TYPE_STORED));
doc.add(newField("contents", content, TextField.TYPE_STORED));
- NumericField numericField = new NumericField("date2");
- numericField.setIntValue(Integer.valueOf(date));
- doc.add(numericField);
+ doc.add(new NumericField("date2", Integer.valueOf(date)));
writer.addDocument(doc);
line = d.readLine();
}
Index: modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestNumericQueryParser.java
===================================================================
--- modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestNumericQueryParser.java (revision 1230137)
+++ modules/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestNumericQueryParser.java (working copy)
@@ -33,6 +33,7 @@
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
+import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.NumericField;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.RandomIndexWriter;
@@ -192,17 +193,37 @@
for (NumericField.DataType type : NumericField.DataType.values()) {
numericConfigMap.put(type.name(), new NumericConfig(PRECISION_STEP,
NUMBER_FORMAT, type));
-
- NumericField field = new NumericField(type.name(), PRECISION_STEP, NumericField.TYPE_STORED);
-
+
+ FieldType ft = new FieldType(NumericField.getFieldType(type, true));
+ ft.setNumericPrecisionStep(PRECISION_STEP);
+ final NumericField field;
+
+ switch(type) {
+ case INT:
+ field = new NumericField(type.name(), 0, ft);
+ break;
+ case FLOAT:
+ field = new NumericField(type.name(), 0.0f, ft);
+ break;
+ case LONG:
+ field = new NumericField(type.name(), 0l, ft);
+ break;
+ case DOUBLE:
+ field = new NumericField(type.name(), 0.0, ft);
+ break;
+ default:
+ assert false;
+ field = null;
+ }
numericFieldMap.put(type.name(), field);
doc.add(field);
-
}
numericConfigMap.put(DATE_FIELD_NAME, new NumericConfig(PRECISION_STEP,
DATE_FORMAT, NumericField.DataType.LONG));
- NumericField dateField = new NumericField(DATE_FIELD_NAME, PRECISION_STEP, NumericField.TYPE_STORED);
+ FieldType ft = new FieldType(NumericField.getFieldType(NumericField.DataType.LONG, true));
+ ft.setNumericPrecisionStep(PRECISION_STEP);
+ NumericField dateField = new NumericField(DATE_FIELD_NAME, 0l, ft);
numericFieldMap.put(DATE_FIELD_NAME, dateField);
doc.add(dateField);
@@ -264,24 +285,23 @@
Number number = getNumberType(numberType, NumericField.DataType.DOUBLE
.name());
- numericFieldMap.get(NumericField.DataType.DOUBLE.name()).setDoubleValue(
+ numericFieldMap.get(NumericField.DataType.DOUBLE.name()).setValue(
number.doubleValue());
number = getNumberType(numberType, NumericField.DataType.INT.name());
- numericFieldMap.get(NumericField.DataType.INT.name()).setIntValue(
+ numericFieldMap.get(NumericField.DataType.INT.name()).setValue(
number.intValue());
number = getNumberType(numberType, NumericField.DataType.LONG.name());
- numericFieldMap.get(NumericField.DataType.LONG.name()).setLongValue(
+ numericFieldMap.get(NumericField.DataType.LONG.name()).setValue(
number.longValue());
number = getNumberType(numberType, NumericField.DataType.FLOAT.name());
- numericFieldMap.get(NumericField.DataType.FLOAT.name()).setFloatValue(
+ numericFieldMap.get(NumericField.DataType.FLOAT.name()).setValue(
number.floatValue());
number = getNumberType(numberType, DATE_FIELD_NAME);
- numericFieldMap.get(DATE_FIELD_NAME).setLongValue(number.longValue());
-
+ numericFieldMap.get(DATE_FIELD_NAME).setValue(number.longValue());
}
private static int randomDateStyle(Random random) {
Index: modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/feeds/DocMaker.java
===================================================================
--- modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/feeds/DocMaker.java (revision 1230137)
+++ modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/feeds/DocMaker.java (working copy)
@@ -109,8 +109,8 @@
fields.put(ID_FIELD, new Field(ID_FIELD, "", StringField.TYPE_STORED));
fields.put(NAME_FIELD, new Field(NAME_FIELD, "", ft));
- numericFields.put(DATE_MSEC_FIELD, new NumericField(DATE_MSEC_FIELD));
- numericFields.put(TIME_SEC_FIELD, new NumericField(TIME_SEC_FIELD));
+ numericFields.put(DATE_MSEC_FIELD, new NumericField(DATE_MSEC_FIELD, 0L));
+ numericFields.put(TIME_SEC_FIELD, new NumericField(TIME_SEC_FIELD, 0));
doc = new Document();
} else {
@@ -138,15 +138,34 @@
return f;
}
- NumericField getNumericField(String name) {
- if (!reuseFields) {
- return new NumericField(name);
+ NumericField getNumericField(String name, NumericField.DataType type) {
+ NumericField f;
+ if (reuseFields) {
+ f = numericFields.get(name);
+ } else {
+ f = null;
}
-
- NumericField f = numericFields.get(name);
+
if (f == null) {
- f = new NumericField(name);
- numericFields.put(name, f);
+ switch(type) {
+ case INT:
+ f = new NumericField(name, 0);
+ break;
+ case LONG:
+ f = new NumericField(name, 0L);
+ break;
+ case FLOAT:
+ f = new NumericField(name, 0.0f);
+ break;
+ case DOUBLE:
+ f = new NumericField(name, 0.0);
+ break;
+ default:
+ assert false;
+ }
+ if (reuseFields) {
+ numericFields.put(name, f);
+ }
}
return f;
}
@@ -249,15 +268,15 @@
date = new Date();
}
- NumericField dateField = ds.getNumericField(DATE_MSEC_FIELD);
- dateField.setLongValue(date.getTime());
+ NumericField dateField = ds.getNumericField(DATE_MSEC_FIELD, NumericField.DataType.LONG);
+ dateField.setValue(date.getTime());
doc.add(dateField);
util.cal.setTime(date);
final int sec = util.cal.get(Calendar.HOUR_OF_DAY)*3600 + util.cal.get(Calendar.MINUTE)*60 + util.cal.get(Calendar.SECOND);
- NumericField timeSecField = ds.getNumericField(TIME_SEC_FIELD);
- timeSecField.setIntValue(sec);
+ NumericField timeSecField = ds.getNumericField(TIME_SEC_FIELD, NumericField.DataType.INT);
+ timeSecField.setValue(sec);
doc.add(timeSecField);
// Set TITLE_FIELD
Index: modules/grouping/src/test/org/apache/lucene/search/grouping/AllGroupsCollectorTest.java
===================================================================
--- modules/grouping/src/test/org/apache/lucene/search/grouping/AllGroupsCollectorTest.java (revision 1230137)
+++ modules/grouping/src/test/org/apache/lucene/search/grouping/AllGroupsCollectorTest.java (working copy)
@@ -123,9 +123,7 @@
private void addGroupField(Document doc, String groupField, String value, boolean canUseIDV) {
doc.add(new Field(groupField, value, TextField.TYPE_STORED));
if (canUseIDV) {
- DocValuesField valuesField = new DocValuesField(groupField);
- valuesField.setBytes(new BytesRef(value), Type.BYTES_VAR_SORTED);
- doc.add(valuesField);
+ doc.add(new DocValuesField(groupField, new BytesRef(value), Type.BYTES_VAR_SORTED));
}
}
Index: modules/grouping/src/test/org/apache/lucene/search/grouping/AllGroupHeadsCollectorTest.java
===================================================================
--- modules/grouping/src/test/org/apache/lucene/search/grouping/AllGroupHeadsCollectorTest.java (revision 1230137)
+++ modules/grouping/src/test/org/apache/lucene/search/grouping/AllGroupHeadsCollectorTest.java (working copy)
@@ -211,7 +211,7 @@
doc.add(group);
DocValuesField valuesField = null;
if (canUseIDV) {
- valuesField = new DocValuesField("group");
+ valuesField = new DocValuesField("group", new BytesRef(), valueType);
doc.add(valuesField);
}
Field sort1 = newField("sort1", "", StringField.TYPE_UNSTORED);
@@ -226,7 +226,7 @@
Field content = newField("content", "", TextField.TYPE_UNSTORED);
doc.add(content);
docNoGroup.add(content);
- NumericField id = new NumericField("id");
+ NumericField id = new NumericField("id", 0);
doc.add(id);
docNoGroup.add(id);
final GroupDoc[] groupDocs = new GroupDoc[numDocs];
@@ -257,14 +257,14 @@
if (groupDoc.group != null) {
group.setValue(groupDoc.group.utf8ToString());
if (canUseIDV) {
- valuesField.setBytes(new BytesRef(groupDoc.group.utf8ToString()), valueType);
+ valuesField.setValue(new BytesRef(groupDoc.group.utf8ToString()));
}
}
sort1.setValue(groupDoc.sort1.utf8ToString());
sort2.setValue(groupDoc.sort2.utf8ToString());
sort3.setValue(groupDoc.sort3.utf8ToString());
content.setValue(groupDoc.content);
- id.setIntValue(groupDoc.id);
+ id.setValue(groupDoc.id);
if (groupDoc.group == null) {
w.addDocument(docNoGroup);
} else {
@@ -527,9 +527,7 @@
private void addGroupField(Document doc, String groupField, String value, boolean canUseIDV, Type valueType) {
doc.add(new Field(groupField, value, TextField.TYPE_STORED));
if (canUseIDV) {
- DocValuesField valuesField = new DocValuesField(groupField);
- valuesField.setBytes(new BytesRef(value), valueType);
- doc.add(valuesField);
+ doc.add(new DocValuesField(groupField, new BytesRef(value), valueType));
}
}
Index: modules/grouping/src/test/org/apache/lucene/search/grouping/TestGrouping.java
===================================================================
--- modules/grouping/src/test/org/apache/lucene/search/grouping/TestGrouping.java (revision 1230137)
+++ modules/grouping/src/test/org/apache/lucene/search/grouping/TestGrouping.java (working copy)
@@ -171,9 +171,7 @@
private void addGroupField(Document doc, String groupField, String value, boolean canUseIDV) {
doc.add(new Field(groupField, value, TextField.TYPE_STORED));
if (canUseIDV) {
- DocValuesField valuesField = new DocValuesField(groupField);
- valuesField.setBytes(new BytesRef(value), Type.BYTES_VAR_SORTED);
- doc.add(valuesField);
+ doc.add(new DocValuesField(groupField, new BytesRef(value), Type.BYTES_VAR_SORTED));
}
}
@@ -593,7 +591,7 @@
}
doc.add(newField("sort1", groupValue.sort1.utf8ToString(), StringField.TYPE_UNSTORED));
doc.add(newField("sort2", groupValue.sort2.utf8ToString(), StringField.TYPE_UNSTORED));
- doc.add(new NumericField("id").setIntValue(groupValue.id));
+ doc.add(new NumericField("id", groupValue.id));
doc.add(newField("content", groupValue.content, TextField.TYPE_UNSTORED));
//System.out.println("TEST: doc content=" + groupValue.content + " group=" + (groupValue.group == null ? "null" : groupValue.group.utf8ToString()) + " sort1=" + groupValue.sort1.utf8ToString() + " id=" + groupValue.id);
}
@@ -705,7 +703,7 @@
Document doc = new Document();
Document docNoGroup = new Document();
- DocValuesField idvGroupField = new DocValuesField("group");
+ DocValuesField idvGroupField = new DocValuesField("group", new BytesRef(), Type.BYTES_VAR_SORTED);
if (canUseIDV) {
doc.add(idvGroupField);
}
@@ -721,7 +719,7 @@
Field content = newField("content", "", TextField.TYPE_UNSTORED);
doc.add(content);
docNoGroup.add(content);
- NumericField id = new NumericField("id");
+ NumericField id = new NumericField("id", 0);
doc.add(id);
docNoGroup.add(id);
final GroupDoc[] groupDocs = new GroupDoc[numDocs];
@@ -747,13 +745,13 @@
if (groupDoc.group != null) {
group.setValue(groupDoc.group.utf8ToString());
if (canUseIDV) {
- idvGroupField.setBytes(BytesRef.deepCopyOf(groupDoc.group), Type.BYTES_VAR_SORTED);
+ idvGroupField.setValue(BytesRef.deepCopyOf(groupDoc.group));
}
}
sort1.setValue(groupDoc.sort1.utf8ToString());
sort2.setValue(groupDoc.sort2.utf8ToString());
content.setValue(groupDoc.content);
- id.setIntValue(groupDoc.id);
+ id.setValue(groupDoc.id);
if (groupDoc.group == null) {
w.addDocument(docNoGroup);
} else {