Skip to content

Commit 3a322a6

Browse files
committed
a test for lucene FieldCache
1 parent 5b11de8 commit 3a322a6

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Licensed to Elastic Search and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. Elastic Search licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.deps.lucene;
21+
22+
import org.apache.lucene.document.Document;
23+
import org.apache.lucene.document.NumericField;
24+
import org.apache.lucene.index.IndexReader;
25+
import org.apache.lucene.index.IndexWriter;
26+
import org.apache.lucene.search.FieldCache;
27+
import org.apache.lucene.store.Directory;
28+
import org.apache.lucene.store.RAMDirectory;
29+
import org.elasticsearch.util.lucene.Lucene;
30+
import org.testng.annotations.Test;
31+
32+
import static org.hamcrest.MatcherAssert.*;
33+
import static org.hamcrest.Matchers.*;
34+
35+
/**
36+
* @author kimchy (shay.banon)
37+
*/
38+
@Test
39+
public class NumericFieldCacheTests {
40+
41+
/**
42+
* A test that verifies that when using FieldCache for a field that has been added twice (under the same name)
43+
* to the document, returns the last one.
44+
*/
45+
@Test public void testTwoFieldSameNameNumericFieldCache() throws Exception {
46+
Directory dir = new RAMDirectory();
47+
IndexWriter indexWriter = new IndexWriter(dir, Lucene.STANDARD_ANALYZER, true, IndexWriter.MaxFieldLength.UNLIMITED);
48+
49+
Document doc = new Document();
50+
NumericField field = new NumericField("int1").setIntValue(1);
51+
doc.add(field);
52+
53+
field = new NumericField("int1").setIntValue(2);
54+
doc.add(field);
55+
56+
indexWriter.addDocument(doc);
57+
58+
IndexReader reader = indexWriter.getReader();
59+
int[] ints = FieldCache.DEFAULT.getInts(reader, "int1");
60+
assertThat(ints.length, equalTo(1));
61+
assertThat(ints[0], equalTo(2));
62+
}
63+
}

0 commit comments

Comments
 (0)