Index: src/java/org/apache/lucene/search/FieldCacheImpl.java =================================================================== --- src/java/org/apache/lucene/search/FieldCacheImpl.java (revision 690675) +++ src/java/org/apache/lucene/search/FieldCacheImpl.java (working copy) @@ -335,7 +335,9 @@ String termval = term.text(); termDocs.seek (termEnum); while (termDocs.next()) { - retArray[termDocs.doc()] = termval; + if (retArray[termDocs.doc()] == null) { + retArray[termDocs.doc()] = termval; + } } } while (termEnum.next()); } finally { @@ -383,7 +385,9 @@ termDocs.seek (termEnum); while (termDocs.next()) { - retArray[termDocs.doc()] = t; + if (retArray[termDocs.doc()] == 0) { + retArray[termDocs.doc()] = t; + } } t++; Index: src/test/org/apache/lucene/search/TestFieldCache.java =================================================================== --- src/test/org/apache/lucene/search/TestFieldCache.java (revision 0) +++ src/test/org/apache/lucene/search/TestFieldCache.java (revision 0) @@ -0,0 +1,70 @@ +package org.apache.lucene.search; + +/** + * Copyright 2004 The Apache Software Foundation + * + * Licensed 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.io.IOException; +import java.util.Arrays; + +import org.apache.lucene.analysis.WhitespaceAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.search.FieldCache.StringIndex; +import org.apache.lucene.store.RAMDirectory; +import org.apache.lucene.util.LuceneTestCase; + +public class TestFieldCache extends LuceneTestCase { + protected IndexReader reader; + private static final String MULTI_VALUE_FIELD = "multi"; + private static final String[][] MULTI_VALUES = new String[][] { + new String[] {"apple"}, new String[] {"banana"}, + new String[] {"apple", "banana"}, new String[] {"apple", "zebra"}}; + + public TestFieldCache(String s) { + super(s); + } + + protected void setUp() throws Exception { + super.setUp(); + RAMDirectory directory = new RAMDirectory(); + IndexWriter writer= new IndexWriter(directory, new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED); + for (int i = 0; i < MULTI_VALUES.length; i++){ + Document doc = new Document(); + for (int j = 0; j < MULTI_VALUES[i].length; j++) { + doc.add(new Field(MULTI_VALUE_FIELD, MULTI_VALUES[i][j], Field.Store.NO, Field.Index.TOKENIZED)); + } + writer.addDocument(doc); + } + writer.close(); + reader = IndexReader.open(directory); + } + + + public void testGetStringsMultiValues() throws IOException { + FieldCacheImpl cache = new FieldCacheImpl(); + String[] strings = cache.getStrings(reader, MULTI_VALUE_FIELD); + assertEquals("First term lexicographically should be used to sort", "apple", strings[3]); + } + + public void testGetStringIndexesMultiValues() throws IOException { + FieldCacheImpl cache = new FieldCacheImpl(); + StringIndex indexes = cache.getStringIndex(reader, MULTI_VALUE_FIELD); + int appleIndex = Arrays.binarySearch(indexes.lookup, "apple"); + assertEquals("First term lexicographically should be used to sort", appleIndex, indexes.order[3]); + } +}\ No newline at end of file