Index: solr/core/src/test/org/apache/solr/schema/PolyFieldTest.java
===================================================================
--- solr/core/src/test/org/apache/solr/schema/PolyFieldTest.java (revision 1159961)
+++ solr/core/src/test/org/apache/solr/schema/PolyFieldTest.java (working copy)
@@ -88,7 +88,7 @@
//first two fields contain the values, third is just stored and contains the original
for (int i = 0; i < 3; i++) {
boolean hasValue = fields[1].tokenStreamValue() != null
- || fields[1].binaryValue(null) != null
+ || fields[1].binaryValue() != null
|| fields[1].stringValue() != null;
assertTrue("Doesn't have a value: " + fields[1], hasValue);
}
Index: solr/core/src/test/org/apache/solr/search/TestSort.java
===================================================================
--- solr/core/src/test/org/apache/solr/search/TestSort.java (revision 1159961)
+++ solr/core/src/test/org/apache/solr/search/TestSort.java (working copy)
@@ -17,11 +17,13 @@
package org.apache.solr.search;
+import java.io.IOException;
+import java.util.*;
+
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
-import org.apache.lucene.document.FieldType;
-import org.apache.lucene.document.TextField;
+import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexReader.AtomicReaderContext;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
@@ -31,16 +33,10 @@
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.OpenBitSet;
import org.apache.lucene.util._TestUtil;
-
-import org.apache.solr.request.SolrQueryRequest;
-
import org.apache.solr.SolrTestCaseJ4;
-
+import org.apache.solr.request.SolrQueryRequest;
import org.junit.BeforeClass;
-import java.io.IOException;
-import java.util.*;
-
public class TestSort extends SolrTestCaseJ4 {
@BeforeClass
public static void beforeClass() throws Exception {
@@ -154,11 +150,8 @@
public void testSort() throws Exception {
Directory dir = new RAMDirectory();
- FieldType customType = new FieldType(TextField.TYPE_UNSTORED);
- customType.setTokenized(false);
- customType.setOmitNorms(false);
- Field f = new Field("f",customType,"0");
- Field f2 = new Field("f2",customType,"0");
+ Field f = new Field("f",StringField.TYPE_UNSTORED,"0");
+ Field f2 = new Field("f2",StringField.TYPE_UNSTORED,"0");
for (int iterCnt = 0; iterCnt listFields = new ArrayList();
for (IndexableField field : docFields) {
- if (field.binaryValue(null) == null)
+ if (field.binaryValue() == null)
listFields.add(field.stringValue());
}
- String[] altTexts;
- if (listFields.size() == 0) {
- altTexts = new String[0];
- }
- else {
- altTexts = listFields.toArray(new String[listFields.size()]);
- }
+ String[] altTexts = listFields.toArray(new String[listFields.size()]);
+
if (altTexts != null && altTexts.length > 0){
int alternateFieldLen = params.getFieldInt(fieldName, HighlightParams.ALTERNATE_FIELD_LENGTH,0);
if( alternateFieldLen <= 0 ){
Index: solr/core/src/java/org/apache/solr/handler/admin/LukeRequestHandler.java
===================================================================
--- solr/core/src/java/org/apache/solr/handler/admin/LukeRequestHandler.java (revision 1159996)
+++ solr/core/src/java/org/apache/solr/handler/admin/LukeRequestHandler.java (working copy)
@@ -178,7 +178,7 @@
flags.append( (f != null && f.storeTermVectorPositions()) ? FieldFlag.TERM_VECTOR_POSITION.getAbbreviation() : '-' );
flags.append( (f != null && f.omitNorms()) ? FieldFlag.OMIT_NORMS.getAbbreviation() : '-' );
flags.append( (f != null && f.getClass().getSimpleName().equals("LazyField")) ? FieldFlag.LAZY.getAbbreviation() : '-' );
- flags.append( (f != null && f.binaryValue(null)!=null) ? FieldFlag.BINARY.getAbbreviation() : '-' );
+ flags.append( (f != null && f.binaryValue()!=null) ? FieldFlag.BINARY.getAbbreviation() : '-' );
flags.append( (false) ? FieldFlag.SORT_MISSING_FIRST.getAbbreviation() : '-' ); // SchemaField Specific
flags.append( (false) ? FieldFlag.SORT_MISSING_LAST.getAbbreviation() : '-' ); // SchemaField Specific
return flags.toString();
@@ -257,7 +257,7 @@
// TODO: this really should be "stored"
f.add( "internal", field.stringValue() ); // may be a binary number
- BytesRef bytes = field.binaryValue(null);
+ BytesRef bytes = field.binaryValue();
if (bytes != null) {
f.add( "binary", Base64.byteArrayToBase64(bytes.bytes, bytes.offset, bytes.length));
}
Index: solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java
===================================================================
--- solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java (revision 1159961)
+++ solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java (working copy)
@@ -17,7 +17,12 @@
package org.apache.solr.handler.component;
+import java.io.IOException;
+import java.net.URL;
+import java.util.*;
+
import org.apache.lucene.document.Field;
+import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexReader.AtomicReaderContext;
import org.apache.lucene.index.IndexReader.ReaderContext;
import org.apache.lucene.index.Term;
@@ -47,10 +52,6 @@
import org.apache.solr.search.*;
import org.apache.solr.util.SolrPluginUtils;
-import java.io.IOException;
-import java.net.URL;
-import java.util.*;
-
/**
* TODO!
*
@@ -426,9 +427,7 @@
Sort sort = searcher.weightSort(rb.getSortSpec().getSort());
SortField[] sortFields = sort==null ? new SortField[]{SortField.FIELD_SCORE} : sort.getSort();
NamedList sortVals = new NamedList(); // order is important for the sort fields
- org.apache.lucene.document.FieldType docft = new org.apache.lucene.document.FieldType();
- docft.setStored(true);
- Field field = new Field("dummy", docft, ""); // a dummy Field
+ Field field = new StringField("dummy", ""); // a dummy Field
ReaderContext topReaderContext = searcher.getTopReaderContext();
AtomicReaderContext[] leaves = ReaderUtil.leaves(topReaderContext);
AtomicReaderContext currentLeaf = null;
Index: solr/core/src/java/org/apache/solr/response/BinaryResponseWriter.java
===================================================================
--- solr/core/src/java/org/apache/solr/response/BinaryResponseWriter.java (revision 1159961)
+++ solr/core/src/java/org/apache/solr/response/BinaryResponseWriter.java (working copy)
@@ -16,13 +16,17 @@
*/
package org.apache.solr.response;
+import java.io.*;
+import java.util.*;
+
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexableField;
+import org.apache.lucene.util.BytesRef;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.CommonParams;
+import org.apache.solr.common.util.JavaBinCodec;
import org.apache.solr.common.util.NamedList;
-import org.apache.solr.common.util.JavaBinCodec;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.transform.DocTransformer;
import org.apache.solr.response.transform.TransformContext;
@@ -33,10 +37,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.io.*;
-import java.util.*;
-
public class BinaryResponseWriter implements BinaryQueryResponseWriter {
private static final Logger LOG = LoggerFactory.getLogger(BinaryResponseWriter.class);
public static final Set KNOWN_TYPES = new HashSet();
@@ -168,8 +169,16 @@
if(sf != null) ft =sf.getType();
Object val;
if (ft == null) { // handle fields not in the schema
- if (f.binaryValue(null)!=null) val = f.binaryValue(null).bytes;
- else val = f.stringValue();
+ BytesRef bytesRef = f.binaryValue();
+ if (bytesRef != null) {
+ if (bytesRef.offset == 0 && bytesRef.length == bytesRef.bytes.length) {
+ val = bytesRef.bytes;
+ } else {
+ final byte[] bytes = new byte[bytesRef.length];
+ val = bytes;
+ System.arraycopy(bytesRef.bytes, bytesRef.offset, bytes, 0, bytesRef.length);
+ }
+ } else val = f.stringValue();
} else {
try {
if (useFieldObjects && KNOWN_TYPES.contains(ft.getClass())) {
Index: modules/queries/src/java/org/apache/lucene/queries/mlt/MoreLikeThis.java
===================================================================
--- modules/queries/src/java/org/apache/lucene/queries/mlt/MoreLikeThis.java (revision 1159961)
+++ modules/queries/src/java/org/apache/lucene/queries/mlt/MoreLikeThis.java (working copy)
@@ -824,22 +824,8 @@
*
* This is a somewhat "advanced" routine, and in general only the 1st entry in the array is of interest.
* This method is exposed so that you can identify the "interesting words" in a document.
-<<<<<<<
* For an easier method to call see {@link #retrieveInterestingTerms retrieveInterestingTerms()}.
*
-=======
- // field does not store term vector info
- if (vector == null) {
- Document d=ir.document(docNum);
- IndexableField text[]=d.getFields(fieldName);
- if(text!=null)
- {
- for (int j = 0; j < text.length; j++) {
- addTermFrequencies(new StringReader(text[j].stringValue()), termFreqMap, fieldName);
- }
- }
- }
->>>>>>>
* @param r the reader that has the content of the document
* @param fieldName field passed to the analyzer to use when analyzing the content
* @return the most interesting words in the document ordered by score, with the highest scoring, or best entry, first
Index: lucene/contrib/CHANGES.txt
===================================================================
--- lucene/contrib/CHANGES.txt (revision 1159961)
+++ lucene/contrib/CHANGES.txt (working copy)
@@ -4,8 +4,6 @@
http://s.apache.org/luceneversions
======================= Trunk (not yet released) =======================
-<<<<<<<
-=======
Changes in Runtime Behavior
@@ -14,7 +12,6 @@
ANALYZED fields). To ensure your offsets are correct you should
provide an analyzer that returns 1 from the offsetGap method.
(Mike McCandless)
->>>>>>>
Build
Index: lucene/contrib/instantiated/src/java/org/apache/lucene/store/instantiated/InstantiatedIndexWriter.java
===================================================================
--- lucene/contrib/instantiated/src/java/org/apache/lucene/store/instantiated/InstantiatedIndexWriter.java (revision 1159961)
+++ lucene/contrib/instantiated/src/java/org/apache/lucene/store/instantiated/InstantiatedIndexWriter.java (working copy)
@@ -496,7 +496,7 @@
if (field.stored()) {
fieldSetting.stored = true;
}
- if (field.binaryValue(null) != null) {
+ if (field.binaryValue() != null) {
fieldSetting.isBinary = true;
}
if (field.storeTermVectors()) {
Index: lucene/contrib/misc/src/test/org/apache/lucene/index/TestContribFieldsReader.java
===================================================================
--- lucene/contrib/misc/src/test/org/apache/lucene/index/TestContribFieldsReader.java (revision 1159996)
+++ lucene/contrib/misc/src/test/org/apache/lucene/index/TestContribFieldsReader.java (working copy)
@@ -111,10 +111,10 @@
assertTrue("field is null and it shouldn't be", field != null);
assertTrue("stringValue isn't null for lazy binary field", field.stringValue() == null);
- byte [] bytes = field.binaryValue(null).bytes;
+ byte [] bytes = field.binaryValue().bytes;
assertTrue("bytes is null and it shouldn't be", bytes != null);
assertTrue("", DocHelper.LAZY_FIELD_BINARY_BYTES.length == bytes.length);
- assertTrue("calling binaryValue() twice should give same reference", field.binaryValue(null).bytes == field.binaryValue(null).bytes);
+ assertTrue("calling binaryValue() twice should give same reference", field.binaryValue().bytes == field.binaryValue().bytes);
for (int i = 0; i < bytes.length; i++) {
assertTrue("byte[" + i + "] is mismatched", bytes[i] == DocHelper.LAZY_FIELD_BINARY_BYTES[i]);
@@ -179,9 +179,9 @@
field = doc.getField(DocHelper.LAZY_FIELD_BINARY_KEY);
assertTrue("field is null and it shouldn't be", field != null);
assertTrue("stringValue isn't null for lazy binary field", field.stringValue() == null);
- assertTrue("calling binaryValue() twice should give different references", field.binaryValue(null).bytes != field.binaryValue(null).bytes);
+ assertTrue("calling binaryValue() twice should give different references", field.binaryValue().bytes != field.binaryValue().bytes);
- byte [] bytes = field.binaryValue(null).bytes;
+ byte [] bytes = field.binaryValue().bytes;
assertTrue("bytes is null and it shouldn't be", bytes != null);
assertTrue("", DocHelper.LAZY_FIELD_BINARY_BYTES.length == bytes.length);
for (int i = 0; i < bytes.length; i++) {
@@ -300,12 +300,12 @@
IndexableField f1 = doc.getField(DocHelper.TEXT_FIELD_1_KEY);
IndexableField f3 = doc.getField(DocHelper.TEXT_FIELD_3_KEY);
IndexableField fb = doc.getField(DocHelper.LAZY_FIELD_BINARY_KEY);
- assertTrue(f1.binaryValue(null)!=null);
- assertTrue(f3.binaryValue(null)==null);
- assertTrue(fb.binaryValue(null)!=null);
- assertSizeEquals(2*DocHelper.FIELD_1_TEXT.length(), f1.binaryValue(null).bytes);
+ assertTrue(f1.binaryValue()!=null);
+ assertTrue(f3.binaryValue()==null);
+ assertTrue(fb.binaryValue()!=null);
+ assertSizeEquals(2*DocHelper.FIELD_1_TEXT.length(), f1.binaryValue().bytes);
assertEquals(DocHelper.FIELD_3_TEXT, f3.stringValue());
- assertSizeEquals(DocHelper.LAZY_FIELD_BINARY_BYTES.length, fb.binaryValue(null).bytes);
+ assertSizeEquals(DocHelper.LAZY_FIELD_BINARY_BYTES.length, fb.binaryValue().bytes);
reader.close();
}
Index: lucene/contrib/misc/src/test/org/apache/lucene/index/TestContribIndexReader.java
===================================================================
--- lucene/contrib/misc/src/test/org/apache/lucene/index/TestContribIndexReader.java (revision 1159961)
+++ lucene/contrib/misc/src/test/org/apache/lucene/index/TestContribIndexReader.java (working copy)
@@ -143,7 +143,7 @@
assertEquals(1, fields.length);
Field b1 = (Field) fields[0];
assertTrue(b1.isBinary());
- BytesRef bytesRef = b1.binaryValue(null);
+ BytesRef bytesRef = b1.binaryValue();
assertEquals(bin.length, bytesRef.length);
for (int i = 0; i < bin.length; i++) {
assertEquals(bin[i], bytesRef.bytes[i + bytesRef.offset]);
@@ -156,8 +156,8 @@
assertNotNull(fields);
assertEquals(1, fields.length);
IndexableField fb1 = fields[0];
- assertTrue(fb1.binaryValue(null)!=null);
- bytesRef = fb1.binaryValue(null);
+ assertTrue(fb1.binaryValue()!=null);
+ bytesRef = fb1.binaryValue();
assertEquals(bin.length, bytesRef.bytes.length);
assertEquals(bin.length, bytesRef.length);
for (int i = 0; i < bin.length; i++) {
@@ -177,7 +177,7 @@
assertEquals(1, fields.length);
b1 = (Field) fields[0];
assertTrue(b1.isBinary());
- bytesRef = b1.binaryValue(null);
+ bytesRef = b1.binaryValue();
assertEquals(bin.length, bytesRef.length);
for (int i = 0; i < bin.length; i++) {
assertEquals(bin[i], bytesRef.bytes[i + bytesRef.offset]);
Index: lucene/contrib/misc/src/java/org/apache/lucene/document/SetBasedFieldSelector.java
===================================================================
--- lucene/contrib/misc/src/java/org/apache/lucene/document/SetBasedFieldSelector.java (revision 1159961)
+++ lucene/contrib/misc/src/java/org/apache/lucene/document/SetBasedFieldSelector.java (working copy)
@@ -59,4 +59,4 @@
}
return result;
}
-}
\ No newline at end of file
+}
Index: lucene/contrib/misc/src/java/org/apache/lucene/document/FieldSelectorVisitor.java
===================================================================
--- lucene/contrib/misc/src/java/org/apache/lucene/document/FieldSelectorVisitor.java (revision 1159961)
+++ lucene/contrib/misc/src/java/org/apache/lucene/document/FieldSelectorVisitor.java (working copy)
@@ -215,6 +215,7 @@
private long pointer;
private final boolean cacheResult;
private final IndexInput in;
+ private boolean isBinary;
public LazyField(IndexInput in, String name, FieldType ft, int toRead, long pointer, boolean isBinary, boolean cacheResult) {
super(name, ft);
@@ -223,8 +224,6 @@
this.pointer = pointer;
this.isBinary = isBinary;
this.cacheResult = cacheResult;
- if (isBinary)
- binaryLength = toRead;
}
@Override
@@ -249,6 +248,7 @@
/** The value of the field as a Reader, or null. If null, the String value,
* binary value, or TokenStream value is used. Exactly one of stringValue(),
* readerValue(), getBinaryValue(), and tokenStreamValue() must be set. */
+ @Override
public Reader readerValue() {
return null;
}
@@ -256,6 +256,7 @@
/** The value of the field as a TokenStream, or null. If null, the Reader value,
* String value, or binary value is used. Exactly one of stringValue(),
* readerValue(), getBinaryValue(), and tokenStreamValue() must be set. */
+ @Override
public TokenStream tokenStreamValue() {
return null;
}
@@ -263,10 +264,11 @@
/** The value of the field as a String, or null. If null, the Reader value,
* binary value, or TokenStream value is used. Exactly one of stringValue(),
* readerValue(), getBinaryValue(), and tokenStreamValue() must be set. */
+ @Override
synchronized public String stringValue() {
- if (isBinary)
+ if (isBinary) {
return null;
- else {
+ } else {
if (fieldsData == null) {
String result = null;
IndexInput localFieldsStream = getFieldStream();
@@ -288,15 +290,12 @@
}
}
- synchronized private byte[] getBinaryValue(byte[] result) {
+ @Override
+ synchronized public BytesRef binaryValue() {
if (isBinary) {
if (fieldsData == null) {
// Allocate new buffer if result is null or too small
- final byte[] b;
- if (result == null || result.length < toRead)
- b = new byte[toRead];
- else
- b = result;
+ final byte[] b = new byte[toRead];
IndexInput localFieldsStream = getFieldStream();
@@ -309,24 +308,15 @@
throw new FieldReaderException(e);
}
- binaryOffset = 0;
- binaryLength = toRead;
+ final BytesRef result = new BytesRef(b);
+ result.length = toRead;
if (cacheResult == true){
- fieldsData = b;
+ fieldsData = result;
}
- return b;
+ return result;
} else {
- return (byte[]) fieldsData;
+ return (BytesRef) fieldsData;
}
- } else
- return null;
- }
-
- @Override
- public BytesRef binaryValue(BytesRef reuse) {
- final byte[] bytes = getBinaryValue(reuse != null ? reuse.bytes : null);
- if (bytes != null) {
- return new BytesRef(bytes, 0, bytes.length);
} else {
return null;
}
Index: lucene/contrib/misc/src/java/org/apache/lucene/document/LoadFirstFieldSelector.java
===================================================================
--- lucene/contrib/misc/src/java/org/apache/lucene/document/LoadFirstFieldSelector.java (revision 1159961)
+++ lucene/contrib/misc/src/java/org/apache/lucene/document/LoadFirstFieldSelector.java (working copy)
@@ -26,4 +26,4 @@
public FieldSelectorResult accept(String fieldName) {
return FieldSelectorResult.LOAD_AND_BREAK;
}
-}
\ No newline at end of file
+}
Index: lucene/src/test/org/apache/lucene/search/TestSort.java
===================================================================
--- lucene/src/test/org/apache/lucene/search/TestSort.java (revision 1159961)
+++ lucene/src/test/org/apache/lucene/search/TestSort.java (working copy)
@@ -135,15 +135,17 @@
doc.add (new Field ("tracer", ft1, data[i][0]));
doc.add (new TextField ("contents", data[i][1]));
if (data[i][2] != null) {
- Field f = supportsDocValues ?
- IndexDocValuesField.set(new StringField ("int", data[i][2]), ValueType.VAR_INTS)
- : new StringField ("int", data[i][2]);
+ Field f = new StringField ("int", data[i][2]);
+ if (supportsDocValues) {
+ f = IndexDocValuesField.build(f, ValueType.VAR_INTS);
+ };
doc.add(f);
}
if (data[i][3] != null) {
- Field f = supportsDocValues ?
- IndexDocValuesField.set(new StringField ("float", data[i][3]), ValueType.FLOAT_32)
- : new StringField ("float", data[i][3]);
+ Field f = new StringField ("float", data[i][3]);
+ if (supportsDocValues) {
+ f = IndexDocValuesField.build(f, ValueType.FLOAT_32);
+ }
doc.add(f);
}
if (data[i][4] != null) doc.add (new StringField ("string", data[i][4]));
@@ -151,16 +153,20 @@
if (data[i][6] != null) doc.add (new StringField ("i18n", data[i][6]));
if (data[i][7] != null) doc.add (new StringField ("long", data[i][7]));
if (data[i][8] != null) {
- Field f = supportsDocValues ?
- IndexDocValuesField.set(new StringField ("double", data[i][8]), ValueType.FLOAT_64)
- : new StringField ("double", data[i][8]);
+ Field f = new StringField ("double", data[i][8]);
+ if (supportsDocValues) {
+ f = IndexDocValuesField.build(f, ValueType.FLOAT_64);
+ }
doc.add(f);
}
if (data[i][9] != null) doc.add (new StringField ("short", data[i][9]));
if (data[i][10] != null) doc.add (new StringField ("byte", data[i][10]));
if (data[i][11] != null) doc.add (new StringField ("parser", data[i][11]));
- //doc.setBoost(2); // produce some scores above 1.0
+ for(IndexableField f : doc.getFields()) {
+ ((Field) f).setBoost(2.0f);
+ }
+
writer.addDocument (doc);
}
}
@@ -196,9 +202,10 @@
String num2 = getRandomCharString(getRandomNumber(1, 4), 48, 50);
doc.add (new StringField ("string2", num2));
doc.add (new Field ("tracer2", customType, num2));
- // doc.setBoost(2); // produce some scores above 1.0
+ for(IndexableField f : doc.getFields()) {
+ ((Field) f).setBoost(2.0f);
+ }
writer.addDocument (doc);
-
}
//writer.optimize ();
//System.out.println(writer.getSegmentCount());
Index: lucene/src/test/org/apache/lucene/search/TestDocBoost.java
===================================================================
--- lucene/src/test/org/apache/lucene/search/TestDocBoost.java (revision 1159961)
+++ lucene/src/test/org/apache/lucene/search/TestDocBoost.java (working copy)
@@ -23,7 +23,6 @@
import org.apache.lucene.document.*;
import org.apache.lucene.index.IndexReader.AtomicReaderContext;
import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
@@ -45,20 +44,12 @@
Document d1 = new Document();
Document d2 = new Document();
- Document d3 = new Document();
- Document d4 = new Document();
- //d3.setBoost(3.0f);
- //d4.setBoost(2.0f);
d1.add(f1); // boost = 1
d2.add(f2); // boost = 2
- //d3.add(f1); // boost = 3
- //d4.add(f2); // boost = 4
writer.addDocument(d1);
writer.addDocument(d2);
- //writer.addDocument(d3);
- //writer.addDocument(d4);
IndexReader reader = writer.getReader();
writer.close();
Index: lucene/src/test/org/apache/lucene/index/TestIndexableField.java
===================================================================
--- lucene/src/test/org/apache/lucene/index/TestIndexableField.java (revision 1159996)
+++ lucene/src/test/org/apache/lucene/index/TestIndexableField.java (working copy)
@@ -17,70 +17,26 @@
* limitations under the License.
*/
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.PrintStream;
import java.io.Reader;
import java.io.StringReader;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Random;
-import java.util.Set;
-import java.util.concurrent.atomic.AtomicBoolean;
-import org.apache.lucene.analysis.Analyzer;
-import org.apache.lucene.analysis.MockAnalyzer;
-import org.apache.lucene.analysis.MockFixedLengthPayloadFilter;
-import org.apache.lucene.analysis.MockTokenizer;
import org.apache.lucene.analysis.TokenStream;
-import org.apache.lucene.analysis.Tokenizer;
-import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
-import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
-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.NumericField.DataType;
import org.apache.lucene.document.NumericField;
import org.apache.lucene.document.StringField;
-import org.apache.lucene.document.TextField;
-import org.apache.lucene.index.IndexWriterConfig.OpenMode;
-import org.apache.lucene.index.codecs.preflexrw.PreFlexRWCodec;
import org.apache.lucene.index.values.PerDocFieldValues;
import org.apache.lucene.index.values.ValueType;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
-import org.apache.lucene.search.DocIdSetIterator;
-import org.apache.lucene.search.FieldCache;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.NumericRangeQuery;
-import org.apache.lucene.search.PhraseQuery;
-import org.apache.lucene.search.Query;
-import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
-import org.apache.lucene.search.spans.SpanTermQuery;
-import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.store.Directory;
-import org.apache.lucene.store.IOContext;
-import org.apache.lucene.store.IndexOutput;
-import org.apache.lucene.store.Lock;
-import org.apache.lucene.store.LockFactory;
-import org.apache.lucene.store.MockDirectoryWrapper;
-import org.apache.lucene.store.NoLockFactory;
-import org.apache.lucene.store.RAMDirectory;
-import org.apache.lucene.store.SingleInstanceLockFactory;
import org.apache.lucene.util.BytesRef;
-import org.apache.lucene.util.CharsRef;
import org.apache.lucene.util.LuceneTestCase;
-import org.apache.lucene.util.ThreadInterruptedException;
-import org.apache.lucene.util.UnicodeUtil;
import org.apache.lucene.util._TestUtil;
public class TestIndexableField extends LuceneTestCase {
@@ -109,7 +65,7 @@
}
@Override
- public BytesRef binaryValue(BytesRef reuse) {
+ public BytesRef binaryValue() {
if ((counter%10) == 3) {
final byte[] bytes = new byte[10];
for(int idx=0;idx as a document.
*
@@ -37,38 +39,66 @@
// TODO: add attrs to this API?
+ /* Field name */
public String name();
// NOTE: if doc/field impl has the notion of "doc level boost"
// it must be multiplied in w/ this field's boost
+
+ /** Field boost (you must pre-multiply in any doc boost). */
public float boost();
+ /* True if the field's value should be stored */
public boolean stored();
- public BytesRef binaryValue(BytesRef reuse);
+ /* Non-null if this field has a binary value */
+ public BytesRef binaryValue();
+
+ /* Non-null if this field has a string value */
public String stringValue();
+
+ /* Non-null if this field has a Reader value */
public Reader readerValue();
+ /* Non-null if this field has a pre-tokenized ({@link TokenStream}) value */
public TokenStream tokenStreamValue();
// Numeric field:
+ /* True if this field is numeric */
public boolean numeric();
+
+ /* Numeric {@link NumericField.DataType}; only used if
+ * the field is numeric */
public NumericField.DataType numericDataType();
+
+ /* Numeric value; only used if the field is numeric */
public Number numericValue();
- // If this returns true then we index this field:
+ /* True if this field should be indexed (inverted) */
public boolean indexed();
+ /* True if this field's value should be analyzed */
public boolean tokenized();
+
+ /* True if norms should not be indexed */
public boolean omitNorms();
+
+ /* {@link IndexOptions}, describing what should be
+ * recorded into the inverted index */
public IndexOptions indexOptions();
+ /* True if term vectors should be indexed */
public boolean storeTermVectors();
+
+ /* True if term vector offsets should be indexed */
public boolean storeTermVectorOffsets();
+
+ /* True if term vector positions should be indexed */
public boolean storeTermVectorPositions();
- // doc values
- public boolean hasDocValues();
+ /* Non-null if doc values should be indexed */
public PerDocFieldValues docValues();
+
+ /* DocValues type; only used if docValues is non-null */
public ValueType docValuesType();
}
Index: lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java
===================================================================
--- lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java (revision 1159961)
+++ lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java (working copy)
@@ -69,8 +69,7 @@
// index is allowed to have exactly one document or 0.
if (numDocs == 1) {
Document doc = r.document(r.maxDoc() - 1);
- Field sid = (Field) doc.getField(SNAPSHOTS_ID);
- if (sid == null) {
+ if (doc.getField(SNAPSHOTS_ID) == null) {
throw new IllegalStateException("directory is not a valid snapshots store!");
}
doc.removeField(SNAPSHOTS_ID);
Index: lucene/src/java/org/apache/lucene/index/DocFieldProcessor.java
===================================================================
--- lucene/src/java/org/apache/lucene/index/DocFieldProcessor.java (revision 1159996)
+++ lucene/src/java/org/apache/lucene/index/DocFieldProcessor.java (working copy)
@@ -24,12 +24,12 @@
import java.util.HashSet;
import java.util.Map;
-import org.apache.lucene.document.Document;
-import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.index.DocumentsWriterPerThread.DocState;
import org.apache.lucene.index.codecs.Codec;
-import org.apache.lucene.index.codecs.PerDocConsumer;
import org.apache.lucene.index.codecs.DocValuesConsumer;
+import org.apache.lucene.index.codecs.PerDocConsumer;
+import org.apache.lucene.index.values.PerDocFieldValues;
+import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.IOUtils;
@@ -262,9 +262,9 @@
if (field.stored()) {
fieldsWriter.addField(field, fp.fieldInfo);
}
- if (field.hasDocValues()) {
- final DocValuesConsumer docValuesConsumer = docValuesConsumer(docState, fp.fieldInfo);
- docValuesConsumer.add(docState.docID, field.docValues());
+ final PerDocFieldValues docValues = field.docValues();
+ if (docValues != null) {
+ docValuesConsumer(docState, fp.fieldInfo).add(docState.docID, docValues);
}
}
@@ -332,5 +332,4 @@
docValues.put(fieldInfo.name, docValuesConsumer);
return docValuesConsumer;
}
-
}
Index: lucene/src/java/org/apache/lucene/index/DocumentStoredFieldVisitor.java
===================================================================
--- lucene/src/java/org/apache/lucene/index/DocumentStoredFieldVisitor.java (revision 1159961)
+++ lucene/src/java/org/apache/lucene/index/DocumentStoredFieldVisitor.java (working copy)
@@ -84,7 +84,6 @@
ft.setOmitNorms(fieldInfo.omitNorms);
ft.setIndexOptions(fieldInfo.indexOptions);
doc.add(new Field(fieldInfo.name,
- false,
ft,
new String(b, "UTF-8")));
} else {
@@ -140,4 +139,4 @@
public Document getDocument() {
return doc;
}
-}
\ No newline at end of file
+}
Index: lucene/src/java/org/apache/lucene/document/IndexDocValuesField.java
===================================================================
--- lucene/src/java/org/apache/lucene/document/IndexDocValuesField.java (revision 1159961)
+++ lucene/src/java/org/apache/lucene/document/IndexDocValuesField.java (working copy)
@@ -70,6 +70,7 @@
*
*
* */
+// TODO: maybe rename to DocValuesField?
public class IndexDocValuesField extends Field implements PerDocFieldValues {
protected BytesRef bytes;
@@ -82,21 +83,27 @@
* Creates a new {@link IndexDocValuesField} with the given name.
*/
public IndexDocValuesField(String name) {
- super(name, new FieldType());
- setDocValues(this);
+ this(name, new FieldType());
}
- /**
- * Creates a {@link IndexDocValuesField} prototype
- */
- IndexDocValuesField() {
- this("");
+ public IndexDocValuesField(String name, FieldType type) {
+ this(name, type, null);
}
+ public IndexDocValuesField(String name, FieldType type, String value) {
+ super(name, type);
+ fieldsData = value;
+ }
+
+ @Override
+ public PerDocFieldValues docValues() {
+ return this;
+ }
+
/**
* Sets the given long value and sets the field's {@link ValueType} to
* {@link ValueType#VAR_INTS} unless already set. If you want to change the
- * default type use {@link #setType(ValueType)}.
+ * default type use {@link #setDocValuesType(ValueType)}.
*/
public void setInt(long value) {
setInt(value, false);
@@ -121,7 +128,7 @@
/**
* Sets the given int value and sets the field's {@link ValueType} to
* {@link ValueType#VAR_INTS} unless already set. If you want to change the
- * default type use {@link #setType(ValueType)}.
+ * default type use {@link #setDocValuesType(ValueType)}.
*/
public void setInt(int value) {
setInt(value, false);
@@ -146,7 +153,7 @@
/**
* Sets the given short value and sets the field's {@link ValueType} to
* {@link ValueType#VAR_INTS} unless already set. If you want to change the
- * default type use {@link #setType(ValueType)}.
+ * default type use {@link #setDocValuesType(ValueType)}.
*/
public void setInt(short value) {
setInt(value, false);
@@ -171,11 +178,12 @@
/**
* Sets the given byte value and sets the field's {@link ValueType} to
* {@link ValueType#VAR_INTS} unless already set. If you want to change the
- * default type use {@link #setType(ValueType)}.
+ * default type use {@link #setDocValuesType(ValueType)}.
*/
public void setInt(byte value) {
setInt(value, false);
}
+
/**
* Sets the given byte value as a 8 bit signed integer.
*
@@ -195,7 +203,7 @@
/**
* Sets the given float value and sets the field's {@link ValueType}
* to {@link ValueType#FLOAT_32} unless already set. If you want to
- * change the type use {@link #setType(ValueType)}.
+ * change the type use {@link #setDocValuesType(ValueType)}.
*/
public void setFloat(float value) {
if (type == null) {
@@ -207,7 +215,7 @@
/**
* Sets the given double value and sets the field's {@link ValueType}
* to {@link ValueType#FLOAT_64} unless already set. If you want to
- * change the default type use {@link #setType(ValueType)}.
+ * change the default type use {@link #setDocValuesType(ValueType)}.
*/
public void setFloat(double value) {
if (type == null) {
@@ -238,7 +246,7 @@
if (value == null) {
throw new IllegalArgumentException("value must not be null");
}
- setType(type);
+ setDocValuesType(type);
if (bytes == null) {
bytes = new BytesRef(value);
} else {
@@ -286,7 +294,7 @@
/**
* Sets the {@link ValueType} for this field.
*/
- public void setType(ValueType type) {
+ public void setDocValuesType(ValueType type) {
if (type == null) {
throw new IllegalArgumentException("Type must not be null");
}
@@ -294,13 +302,6 @@
}
/**
- * Returns the field's {@link ValueType}
- */
- public ValueType type() {
- return type;
- }
-
- /**
* Returns always null
*/
public Reader readerValue() {
@@ -310,36 +311,54 @@
/**
* Returns always null
*/
- public String stringValue() {
+ public TokenStream tokenStreamValue() {
return null;
}
- /**
- * Returns always null
- */
- public TokenStream tokenStreamValue() {
- return null;
+ @Override
+ public ValueType docValuesType() {
+ return type;
}
- /**
- * Sets this {@link IndexDocValuesField} to the given {@link Field} and
- * returns the given field. Any modifications to this instance will be visible
- * to the given field.
- */
- public T set(T field) {
- field.setDocValues(this);
- return field;
+ @Override
+ public String toString() {
+ final String value;
+ switch (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:
+ value = "bytes:bytes.utf8ToString();";
+ break;
+ case VAR_INTS:
+ value = "int:" + longValue;
+ break;
+ case FLOAT_32:
+ value = "float32:" + doubleValue;
+ break;
+ case FLOAT_64:
+ value = "float64:" + doubleValue;
+ break;
+ default:
+ throw new IllegalArgumentException("unknown type: " + type);
+ }
+ return "<" + name() + ": IndexDocValuesField " + value + ">";
}
/**
- * Sets a new {@link PerDocFieldValues} instance on the given field with the
- * given type and returns it.
- *
+ * Returns an IndexDocValuesField 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 T set(T field, ValueType type) {
- if (field instanceof IndexDocValuesField)
- return field;
- final IndexDocValuesField valField = new IndexDocValuesField();
+ public static IndexDocValuesField build(Field field, ValueType type) {
+ if (field instanceof IndexDocValuesField) {
+ return (IndexDocValuesField) field;
+ }
+ final IndexDocValuesField valField = new IndexDocValuesField(field.name(), field.getFieldType(), field.stringValue());
switch (type) {
case BYTES_FIXED_DEREF:
case BYTES_FIXED_SORTED:
@@ -347,9 +366,7 @@
case BYTES_VAR_DEREF:
case BYTES_VAR_SORTED:
case BYTES_VAR_STRAIGHT:
- BytesRef ref = field.isBinary() ? new BytesRef(field.getBinaryValue(),
- field.getBinaryOffset(), field.getBinaryLength()) : new BytesRef(
- field.stringValue());
+ BytesRef ref = field.isBinary() ? field.binaryValue() : new BytesRef(field.stringValue());
valField.setBytes(ref, type);
break;
case VAR_INTS:
@@ -364,7 +381,6 @@
default:
throw new IllegalArgumentException("unknown type: " + type);
}
- return valField.set(field);
+ return valField;
}
-
}
Index: lucene/src/java/org/apache/lucene/document/CompressionTools.java
===================================================================
--- lucene/src/java/org/apache/lucene/document/CompressionTools.java (revision 1159961)
+++ lucene/src/java/org/apache/lucene/document/CompressionTools.java (working copy)
@@ -92,16 +92,24 @@
return compress(result.bytes, 0, result.length, compressionLevel);
}
+ public static byte[] decompress(BytesRef bytes) throws DataFormatException {
+ return decompress(bytes.bytes, bytes.offset, bytes.length);
+ }
+
+ public static byte[] decompress(byte[] value) throws DataFormatException {
+ return decompress(value, 0, value.length);
+ }
+
/** Decompress the byte array previously returned by
* compress */
- public static byte[] decompress(byte[] value) throws DataFormatException {
+ public static byte[] decompress(byte[] value, int offset, int length) throws DataFormatException {
// Create an expandable byte array to hold the decompressed data
- ByteArrayOutputStream bos = new ByteArrayOutputStream(value.length);
+ ByteArrayOutputStream bos = new ByteArrayOutputStream(length);
Inflater decompressor = new Inflater();
try {
- decompressor.setInput(value);
+ decompressor.setInput(value, offset, length);
// Decompress the data
final byte[] buf = new byte[1024];
@@ -119,9 +127,17 @@
/** Decompress the byte array previously returned by
* compressString back into a String */
public static String decompressString(byte[] value) throws DataFormatException {
- final byte[] bytes = decompress(value);
+ return decompressString(value, 0, value.length);
+ }
+
+ public static String decompressString(byte[] value, int offset, int length) throws DataFormatException {
+ final byte[] bytes = decompress(value, offset, length);
CharsRef result = new CharsRef(bytes.length);
UnicodeUtil.UTF8toUTF16(bytes, 0, bytes.length, result);
return new String(result.chars, 0, result.length);
}
+
+ public static String decompressString(BytesRef bytes) throws DataFormatException {
+ return decompressString(bytes.bytes, bytes.offset, bytes.length);
+ }
}
Index: lucene/src/java/org/apache/lucene/document/Field.java
===================================================================
--- lucene/src/java/org/apache/lucene/document/Field.java (revision 1159996)
+++ lucene/src/java/org/apache/lucene/document/Field.java (working copy)
@@ -21,13 +21,10 @@
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.index.FieldInfo.IndexOptions;
-import org.apache.lucene.index.IndexWriter;
-import org.apache.lucene.document.NumericField;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.values.PerDocFieldValues;
import org.apache.lucene.index.values.ValueType;
import org.apache.lucene.util.BytesRef;
-import org.apache.lucene.util.StringHelper;
/**
* A field is a section of a Document. Each field has two parts, a name and a
@@ -42,13 +39,10 @@
protected FieldType type;
protected String name = "body";
// the data object for all different kind of field values
- protected Object fieldsData = null;
+ protected Object fieldsData;
// pre-analyzed tokenStream for indexed fields
protected TokenStream tokenStream;
- protected boolean isBinary = false;
// length/offset for all primitive types
- protected int binaryLength;
- protected int binaryOffset;
protected PerDocFieldValues docValues;
protected float boost = 1.0f;
@@ -59,23 +53,27 @@
}
public Field(String name, FieldType type, Reader reader) {
- if (name == null)
+ if (name == null) {
throw new NullPointerException("name cannot be null");
- if (reader == null)
+ }
+ if (reader == null) {
throw new NullPointerException("reader cannot be null");
+ }
- this.name = name; // field names are interned
+ this.name = name;
this.fieldsData = reader;
this.type = type;
}
public Field(String name, FieldType type, TokenStream tokenStream) {
- if (name == null)
+ if (name == null) {
throw new NullPointerException("name cannot be null");
- if (tokenStream == null)
+ }
+ if (tokenStream == null) {
throw new NullPointerException("tokenStream cannot be null");
+ }
- this.name = name; // field names are interned
+ this.name = name;
this.fieldsData = null;
this.tokenStream = tokenStream;
this.type = type;
@@ -84,21 +82,20 @@
public Field(String name, FieldType type, byte[] value) {
this(name, type, value, 0, value.length);
}
-
+
public Field(String name, FieldType type, byte[] value, int offset, int length) {
- this.isBinary = true;
- this.fieldsData = value;
+ this.fieldsData = new BytesRef(value, offset, length);
this.type = type;
- this.binaryOffset = offset;
- this.binaryLength = length;
this.name = name;
}
-
- public Field(String name, FieldType type, String value) {
- this(name, true, type, value);
+
+ public Field(String name, FieldType type, BytesRef bytes) {
+ this.fieldsData = bytes;
+ this.type = type;
+ this.name = name;
}
- public Field(String name, boolean internName, FieldType type, String value) {
+ public Field(String name, FieldType type, String value) {
if (name == null) {
throw new IllegalArgumentException("name cannot be null");
}
@@ -162,7 +159,7 @@
*
*/
public void setValue(String value) {
- if (isBinary) {
+ if (isBinary()) {
throw new IllegalArgumentException(
"cannot set a String value on a binary field");
}
@@ -174,7 +171,7 @@
* href="#setValue(java.lang.String)">setValue(String).
*/
public void setValue(Reader value) {
- if (isBinary) {
+ if (isBinary()) {
throw new IllegalArgumentException(
"cannot set a Reader value on a binary field");
}
@@ -190,19 +187,18 @@
* href="#setValue(java.lang.String)">setValue(String).
*/
public void setValue(byte[] value) {
- if (!isBinary) {
+ if (!isBinary()) {
throw new IllegalArgumentException(
"cannot set a byte[] value on a non-binary field");
}
- fieldsData = value;
- binaryLength = value.length;
- binaryOffset = 0;
+ fieldsData = new BytesRef(value);
}
/**
* Expert: change the value of this field. See setValue(String).
*/
+ /*
public void setValue(byte[] value, int offset, int length) {
if (!isBinary) {
throw new IllegalArgumentException(
@@ -212,6 +208,7 @@
binaryLength = length;
binaryOffset = offset;
}
+ */
/**
* Expert: sets the token stream to be used for indexing and causes
@@ -264,61 +261,20 @@
return null;
}
- private byte[] getBinaryValue(byte[] result /* unused */) {
- if (isBinary || fieldsData instanceof byte[]) return (byte[]) fieldsData;
- else return null;
- }
-
- protected byte[] getBinaryValue() {
- return getBinaryValue(null);
- }
-
- public BytesRef binaryValue(BytesRef reuse) {
- final byte[] bytes = getBinaryValue();
- if (bytes != null) {
- if (reuse == null) {
- return new BytesRef(bytes, getBinaryOffset(), getBinaryLength());
- } else {
- reuse.bytes = bytes;
- reuse.offset = getBinaryOffset();
- reuse.length = getBinaryLength();
- return reuse;
- }
- } else {
+ public BytesRef binaryValue() {
+ if (!isBinary()) {
return null;
+ } else {
+ return (BytesRef) fieldsData;
}
}
- /**
- * Returns length of byte[] segment that is used as value, if Field is not
- * binary returned value is undefined
- *
- * @return length of byte[] segment that represents this Field value
- */
- protected int getBinaryLength() {
- if (isBinary) {
- return binaryLength;
- } else if (fieldsData instanceof byte[]) return ((byte[]) fieldsData).length;
- else return 0;
- }
+ /** methods from inner FieldType */
- /**
- * Returns offset into byte[] segment that is used as value, if Field is not
- * binary returned value is undefined
- *
- * @return index of the first character in byte[] segment that represents this
- * Field value
- */
- public int getBinaryOffset() {
- return binaryOffset;
- }
-
public boolean isBinary() {
- return isBinary;
+ return fieldsData instanceof BytesRef;
}
- /** methods from inner FieldType */
-
public boolean stored() {
return type.stored();
}
@@ -353,7 +309,7 @@
/** Prints a Field for human consumption. */
@Override
- public final String toString() {
+ public String toString() {
StringBuilder result = new StringBuilder();
result.append(type.toString());
result.append('<');
@@ -368,24 +324,22 @@
return result.toString();
}
- public PerDocFieldValues docValues() {
- return docValues;
- }
-
public void setDocValues(PerDocFieldValues docValues) {
this.docValues = docValues;
}
-
- public boolean hasDocValues() {
- return docValues != null && docValues.type() != null;
+
+ @Override
+ public PerDocFieldValues docValues() {
+ return null;
}
+ @Override
public ValueType docValuesType() {
- return docValues == null? null : docValues.type();
+ return null;
}
+ /** Returns FieldType for this field. */
public FieldType getFieldType() {
- // get a copy
- return new FieldType(type);
+ return type;
}
}
Index: lucene/src/java/org/apache/lucene/document/BinaryField.java
===================================================================
--- lucene/src/java/org/apache/lucene/document/BinaryField.java (revision 1159961)
+++ lucene/src/java/org/apache/lucene/document/BinaryField.java (working copy)
@@ -1,5 +1,7 @@
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
@@ -27,17 +29,18 @@
public BinaryField(String name, byte[] value) {
super(name, BinaryField.TYPE_STORED, value);
- this.isBinary = true;
}
public BinaryField(String name, byte[] value, int offset, int length) {
super(name, BinaryField.TYPE_STORED, value, offset, length);
- this.isBinary = true;
}
+
+ public BinaryField(String name, BytesRef bytes) {
+ super(name, BinaryField.TYPE_STORED, bytes.bytes, bytes.offset, bytes.length);
+ }
public BinaryField(String name, FieldType custom, byte[] value) {
- super(name, custom, value);
- this.isBinary = true;
+ super(name, custom, value);
}
public boolean isNumeric() {
Index: lucene/src/java/org/apache/lucene/document/StringField.java
===================================================================
--- lucene/src/java/org/apache/lucene/document/StringField.java (revision 1159961)
+++ lucene/src/java/org/apache/lucene/document/StringField.java (working copy)
@@ -36,12 +36,8 @@
TYPE_STORED.freeze();
}
- public StringField(String name, boolean internName, String value) {
- super(name, StringField.TYPE_UNSTORED, value);
- }
-
public StringField(String name, String value) {
- this(name, true, value);
+ super(name, TYPE_UNSTORED, value);
}
@Override
Index: lucene/src/java/org/apache/lucene/document/Document.java
===================================================================
--- lucene/src/java/org/apache/lucene/document/Document.java (revision 1159961)
+++ lucene/src/java/org/apache/lucene/document/Document.java (working copy)
@@ -23,6 +23,7 @@
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.search.IndexSearcher; // for javadoc
import org.apache.lucene.search.ScoreDoc; // for javadoc
+import org.apache.lucene.util.BytesRef;
/** Documents are the unit of indexing and search.
*
@@ -39,25 +40,28 @@
public final class Document implements Iterable {
- List fields = new ArrayList();
+ private final List fields = new ArrayList();
/** Constructs a new document with no fields. */
public Document() {}
- // @Override not until Java 1.6
+ @Override
public Iterator iterator() {
return new Iterator() {
private int fieldUpto = 0;
+ @Override
public boolean hasNext() {
return fieldUpto < fields.size();
}
+ @Override
public void remove() {
throw new UnsupportedOperationException();
}
+ @Override
public IndexableField next() {
return fields.get(fieldUpto++);
}
@@ -118,7 +122,6 @@
}
}
- private final static byte[][] NO_BYTES = new byte[0][];
/**
* Returns an array of byte arrays for of the fields that have the name specified
@@ -129,17 +132,18 @@
* @param name the name of the field
* @return a byte[][] of binary field values
*/
- public final byte[][] getBinaryValues(String name) {
- List result = new ArrayList();
+ public final BytesRef[] getBinaryValues(String name) {
+ final List result = new ArrayList();
for (IndexableField field : fields) {
- if (field.name().equals(name) && ((Field) field).isBinary())
- result.add(field.binaryValue(null).bytes);
+ if (field.name().equals(name)) {
+ final BytesRef bytes = field.binaryValue();
+ if (bytes != null) {
+ result.add(bytes);
+ }
+ }
}
- if (result.size() == 0)
- return NO_BYTES;
-
- return result.toArray(new byte[result.size()][]);
+ return result.toArray(new BytesRef[result.size()]);
}
/**
@@ -151,24 +155,39 @@
* @param name the name of the field.
* @return a byte[] containing the binary field value or null
*/
- public final byte[] getBinaryValue(String name) {
+ public final BytesRef getBinaryValue(String name) {
for (IndexableField field : fields) {
- if (field.name().equals(name) && ((Field) field).isBinary())
- return field.binaryValue(null).bytes;
+ if (field.name().equals(name)) {
+ final BytesRef bytes = field.binaryValue();
+ if (bytes != null) {
+ return bytes;
+ }
+ }
}
return null;
}
+ /** Returns a field with the given name if any exist in this document, or
+ * null. If multiple fields exists with this name, this method returns the
+ * first value added.
+ */
public final IndexableField getField(String name) {
for (IndexableField field : fields) {
- if (field.name().equals(name))
+ if (field.name().equals(name)) {
return field;
+ }
}
return null;
}
- private final static IndexableField[] NO_FIELDS = new IndexableField[0];
-
+ /**
+ * Returns an array of {@link IndexablField}s with the given name.
+ * This method returns an empty array when there are no
+ * matching fields. It never returns null.
+ *
+ * @param name the name of the field
+ * @return a Fieldable[] array
+ */
public IndexableField[] getFields(String name) {
List result = new ArrayList();
for (IndexableField field : fields) {
@@ -177,24 +196,31 @@
}
}
- if (result.size() == 0)
- return NO_FIELDS;
-
return result.toArray(new IndexableField[result.size()]);
}
- public Integer size() {
- return fields.size();
- }
-
+ /** Returns a List of all the fields in a document.
+ * Note that fields which are not stored are
+ * not available in documents retrieved from the
+ * index, e.g. {@link IndexSearcher#doc(int)} or {@link
+ * IndexReader#document(int)}.
+ */
public final List getFields() {
return fields;
}
+ /** Returns the string value of the field with the given name if any exist in
+ * this document, or null. If multiple fields exist with this name, this
+ * method returns the first value added. If only binary fields with this name
+ * exist, returns null.
+ * For {@link NumericField} it returns the string value of the number. If you want
+ * the actual {@code NumericField} instance back, use {@link #getFieldable}.
+ */
public final String get(String name) {
- for (IndexableField field : fields) {
- if (field.name().equals(name) && (field.binaryValue(null) == null))
+ for (IndexableField field : fields) {
+ if (field.name().equals(name) && field.stringValue() != null) {
return field.stringValue();
+ }
}
return null;
}
Index: lucene/src/test-framework/org/apache/lucene/index/DocHelper.java
===================================================================
--- lucene/src/test-framework/org/apache/lucene/index/DocHelper.java (revision 1159996)
+++ lucene/src/test-framework/org/apache/lucene/index/DocHelper.java (working copy)
@@ -294,6 +294,6 @@
}
public static int numFields(Document doc) {
- return doc.size();
+ return doc.getFields().size();
}
}
Index: lucene/src/test-framework/org/apache/lucene/index/RandomIndexWriter.java
===================================================================
--- lucene/src/test-framework/org/apache/lucene/index/RandomIndexWriter.java (revision 1159996)
+++ lucene/src/test-framework/org/apache/lucene/index/RandomIndexWriter.java (working copy)
@@ -133,7 +133,7 @@
// addDocuments? Would be better testing.
w.addDocuments(new Iterable>() {
- // @Override -- not until Java 1.6
+ @Override
public Iterator> iterator() {
return new Iterator>() {
boolean done;
@@ -148,7 +148,7 @@
throw new UnsupportedOperationException();
}
- // @Override -- not until Java 1.6
+ @Override
public Iterable next() {
if (done) {
throw new IllegalStateException();
@@ -258,7 +258,7 @@
if (r.nextInt(5) == 3) {
w.updateDocuments(t, new Iterable>() {
- // @Override -- not until Java 1.6
+ @Override
public Iterator> iterator() {
return new Iterator>() {
boolean done;
@@ -273,7 +273,7 @@
throw new UnsupportedOperationException();
}
- // @Override -- not until Java 1.6
+ @Override
public Iterable next() {
if (done) {
throw new IllegalStateException();