1 package org.apache.lucene.document;
3 import org.apache.lucene.index.IndexReader;
4 import org.apache.lucene.index.RandomIndexWriter;
5 import org.apache.lucene.index.Term;
6 import org.apache.lucene.search.IndexSearcher;
7 import org.apache.lucene.search.Query;
8 import org.apache.lucene.search.ScoreDoc;
9 import org.apache.lucene.search.Searcher;
10 import org.apache.lucene.search.TermQuery;
11 import org.apache.lucene.store.Directory;
12 import org.apache.lucene.util.LuceneTestCase;
15 * Licensed to the Apache Software Foundation (ASF) under one or more
16 * contributor license agreements. See the NOTICE file distributed with
17 * this work for additional information regarding copyright ownership.
18 * The ASF licenses this file to You under the Apache License, Version 2.0
19 * (the "License"); you may not use this file except in compliance with
20 * the License. You may obtain a copy of the License at
22 * http://www.apache.org/licenses/LICENSE-2.0
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS,
26 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
32 * Tests {@link Document} class.
34 public class TestDocument extends LuceneTestCase {
36 String binaryVal = "this text will be stored as a byte array in the index";
37 String binaryVal2 = "this text will be also stored as a byte array in the index";
39 public void testBinaryField() throws Exception {
40 Document doc = new Document();
41 Fieldable stringFld = new Field("string", binaryVal, Field.Store.YES,
43 Fieldable binaryFld = new Field("binary", binaryVal.getBytes());
44 Fieldable binaryFld2 = new Field("binary", binaryVal2.getBytes());
49 assertEquals(2, doc.fields.size());
51 assertTrue(binaryFld.isBinary());
52 assertTrue(binaryFld.isStored());
53 assertFalse(binaryFld.isIndexed());
54 assertFalse(binaryFld.isTokenized());
56 String binaryTest = new String(doc.getBinaryValue("binary"));
57 assertTrue(binaryTest.equals(binaryVal));
59 String stringTest = doc.get("string");
60 assertTrue(binaryTest.equals(stringTest));
64 assertEquals(3, doc.fields.size());
66 byte[][] binaryTests = doc.getBinaryValues("binary");
68 assertEquals(2, binaryTests.length);
70 binaryTest = new String(binaryTests[0]);
71 String binaryTest2 = new String(binaryTests[1]);
73 assertFalse(binaryTest.equals(binaryTest2));
75 assertTrue(binaryTest.equals(binaryVal));
76 assertTrue(binaryTest2.equals(binaryVal2));
78 doc.removeField("string");
79 assertEquals(2, doc.fields.size());
81 doc.removeFields("binary");
82 assertEquals(0, doc.fields.size());
86 * Tests {@link Document#removeField(String)} method for a brand new Document
87 * that has not been indexed yet.
89 * @throws Exception on error
91 public void testRemoveForNewDocument() throws Exception {
92 Document doc = makeDocumentWithFields();
93 assertEquals(8, doc.fields.size());
94 doc.removeFields("keyword");
95 assertEquals(6, doc.fields.size());
96 doc.removeFields("doesnotexists"); // removing non-existing fields is
98 doc.removeFields("keyword"); // removing a field more than once
99 assertEquals(6, doc.fields.size());
100 doc.removeField("text");
101 assertEquals(5, doc.fields.size());
102 doc.removeField("text");
103 assertEquals(4, doc.fields.size());
104 doc.removeField("text");
105 assertEquals(4, doc.fields.size());
106 doc.removeField("doesnotexists"); // removing non-existing fields is
108 assertEquals(4, doc.fields.size());
109 doc.removeFields("unindexed");
110 assertEquals(2, doc.fields.size());
111 doc.removeFields("unstored");
112 assertEquals(0, doc.fields.size());
113 doc.removeFields("doesnotexists"); // removing non-existing fields is
115 assertEquals(0, doc.fields.size());
118 public void testConstructorExceptions() {
119 new Field("name", "value", Field.Store.YES, Field.Index.NO); // okay
120 new Field("name", "value", Field.Store.NO, Field.Index.NOT_ANALYZED); // okay
122 new Field("name", "value", Field.Store.NO, Field.Index.NO);
124 } catch (IllegalArgumentException e) {
125 // expected exception
127 new Field("name", "value", Field.Store.YES, Field.Index.NO,
128 Field.TermVector.NO); // okay
130 new Field("name", "value", Field.Store.YES, Field.Index.NO,
131 Field.TermVector.YES);
133 } catch (IllegalArgumentException e) {
134 // expected exception
139 * Tests {@link Document#getValues(String)} method for a brand new Document
140 * that has not been indexed yet.
142 * @throws Exception on error
144 public void testGetValuesForNewDocument() throws Exception {
145 doAssert(makeDocumentWithFields(), false);
149 * Tests {@link Document#getValues(String)} method for a Document retrieved
152 * @throws Exception on error
154 public void testGetValuesForIndexedDocument() throws Exception {
155 Directory dir = newDirectory();
156 RandomIndexWriter writer = new RandomIndexWriter(random, dir);
157 writer.addDocument(makeDocumentWithFields());
158 IndexReader reader = writer.getReader();
160 IndexSearcher searcher = newSearcher(reader);
162 // search for something that does exists
163 Query query = new TermQuery(new Term("keyword", "test1"));
165 // ensure that queries return expected results without DateFilter first
166 ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
167 assertEquals(1, hits.length);
169 doAssert(searcher.doc(hits[0].doc), true);
176 private Document makeDocumentWithFields() {
177 Document doc = new Document();
178 doc.add(new Field("keyword", "test1", Field.Store.YES,
179 Field.Index.NOT_ANALYZED));
180 doc.add(new Field("keyword", "test2", Field.Store.YES,
181 Field.Index.NOT_ANALYZED));
182 doc.add(new Field("text", "test1", Field.Store.YES, Field.Index.ANALYZED));
183 doc.add(new Field("text", "test2", Field.Store.YES, Field.Index.ANALYZED));
184 doc.add(new Field("unindexed", "test1", Field.Store.YES, Field.Index.NO));
185 doc.add(new Field("unindexed", "test2", Field.Store.YES, Field.Index.NO));
187 .add(new Field("unstored", "test1", Field.Store.NO,
188 Field.Index.ANALYZED));
190 .add(new Field("unstored", "test2", Field.Store.NO,
191 Field.Index.ANALYZED));
195 private void doAssert(Document doc, boolean fromIndex) {
196 String[] keywordFieldValues = doc.getValues("keyword");
197 String[] textFieldValues = doc.getValues("text");
198 String[] unindexedFieldValues = doc.getValues("unindexed");
199 String[] unstoredFieldValues = doc.getValues("unstored");
201 assertTrue(keywordFieldValues.length == 2);
202 assertTrue(textFieldValues.length == 2);
203 assertTrue(unindexedFieldValues.length == 2);
204 // this test cannot work for documents retrieved from the index
205 // since unstored fields will obviously not be returned
207 assertTrue(unstoredFieldValues.length == 2);
210 assertTrue(keywordFieldValues[0].equals("test1"));
211 assertTrue(keywordFieldValues[1].equals("test2"));
212 assertTrue(textFieldValues[0].equals("test1"));
213 assertTrue(textFieldValues[1].equals("test2"));
214 assertTrue(unindexedFieldValues[0].equals("test1"));
215 assertTrue(unindexedFieldValues[1].equals("test2"));
216 // this test cannot work for documents retrieved from the index
217 // since unstored fields will obviously not be returned
219 assertTrue(unstoredFieldValues[0].equals("test1"));
220 assertTrue(unstoredFieldValues[1].equals("test2"));
224 public void testFieldSetValue() throws Exception {
226 Field field = new Field("id", "id1", Field.Store.YES,
227 Field.Index.NOT_ANALYZED);
228 Document doc = new Document();
230 doc.add(new Field("keyword", "test", Field.Store.YES,
231 Field.Index.NOT_ANALYZED));
233 Directory dir = newDirectory();
234 RandomIndexWriter writer = new RandomIndexWriter(random, dir);
235 writer.addDocument(doc);
236 field.setValue("id2");
237 writer.addDocument(doc);
238 field.setValue("id3");
239 writer.addDocument(doc);
241 IndexReader reader = writer.getReader();
242 IndexSearcher searcher = newSearcher(reader);
244 Query query = new TermQuery(new Term("keyword", "test"));
246 // ensure that queries return expected results without DateFilter first
247 ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
248 assertEquals(3, hits.length);
250 for (int i = 0; i < 3; i++) {
251 Document doc2 = searcher.doc(hits[i].doc);
252 Field f = doc2.getField("id");
253 if (f.stringValue().equals("id1")) result |= 1;
254 else if (f.stringValue().equals("id2")) result |= 2;
255 else if (f.stringValue().equals("id3")) result |= 4;
256 else fail("unexpected id field");
262 assertEquals("did not see all IDs", 7, result);
265 public void testFieldSetValueChangeBinary() {
266 Field field1 = new Field("field1", new byte[0]);
267 Field field2 = new Field("field2", "", Field.Store.YES,
268 Field.Index.ANALYZED);
270 field1.setValue("abc");
271 fail("did not hit expected exception");
272 } catch (IllegalArgumentException iae) {
276 field2.setValue(new byte[0]);
277 fail("did not hit expected exception");
278 } catch (IllegalArgumentException iae) {