pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / document / TestDocument.java
1 package org.apache.lucene.document;
2
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;
13
14 /**
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
21  *
22  *     http://www.apache.org/licenses/LICENSE-2.0
23  *
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.
29  */
30
31 /**
32  * Tests {@link Document} class.
33  */
34 public class TestDocument extends LuceneTestCase {
35   
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";
38   
39   public void testBinaryField() throws Exception {
40     Document doc = new Document();
41     Fieldable stringFld = new Field("string", binaryVal, Field.Store.YES,
42         Field.Index.NO);
43     Fieldable binaryFld = new Field("binary", binaryVal.getBytes());
44     Fieldable binaryFld2 = new Field("binary", binaryVal2.getBytes());
45     
46     doc.add(stringFld);
47     doc.add(binaryFld);
48     
49     assertEquals(2, doc.fields.size());
50     
51     assertTrue(binaryFld.isBinary());
52     assertTrue(binaryFld.isStored());
53     assertFalse(binaryFld.isIndexed());
54     assertFalse(binaryFld.isTokenized());
55     
56     String binaryTest = new String(doc.getBinaryValue("binary"));
57     assertTrue(binaryTest.equals(binaryVal));
58     
59     String stringTest = doc.get("string");
60     assertTrue(binaryTest.equals(stringTest));
61     
62     doc.add(binaryFld2);
63     
64     assertEquals(3, doc.fields.size());
65     
66     byte[][] binaryTests = doc.getBinaryValues("binary");
67     
68     assertEquals(2, binaryTests.length);
69     
70     binaryTest = new String(binaryTests[0]);
71     String binaryTest2 = new String(binaryTests[1]);
72     
73     assertFalse(binaryTest.equals(binaryTest2));
74     
75     assertTrue(binaryTest.equals(binaryVal));
76     assertTrue(binaryTest2.equals(binaryVal2));
77     
78     doc.removeField("string");
79     assertEquals(2, doc.fields.size());
80     
81     doc.removeFields("binary");
82     assertEquals(0, doc.fields.size());
83   }
84   
85   /**
86    * Tests {@link Document#removeField(String)} method for a brand new Document
87    * that has not been indexed yet.
88    * 
89    * @throws Exception on error
90    */
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
97                                        // siltenlty ignored
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
107                                       // siltenlty ignored
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
114                                        // siltenlty ignored
115     assertEquals(0, doc.fields.size());
116   }
117   
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
121     try {
122       new Field("name", "value", Field.Store.NO, Field.Index.NO);
123       fail();
124     } catch (IllegalArgumentException e) {
125       // expected exception
126     }
127     new Field("name", "value", Field.Store.YES, Field.Index.NO,
128         Field.TermVector.NO); // okay
129     try {
130       new Field("name", "value", Field.Store.YES, Field.Index.NO,
131           Field.TermVector.YES);
132       fail();
133     } catch (IllegalArgumentException e) {
134       // expected exception
135     }
136   }
137   
138   /**
139    * Tests {@link Document#getValues(String)} method for a brand new Document
140    * that has not been indexed yet.
141    * 
142    * @throws Exception on error
143    */
144   public void testGetValuesForNewDocument() throws Exception {
145     doAssert(makeDocumentWithFields(), false);
146   }
147   
148   /**
149    * Tests {@link Document#getValues(String)} method for a Document retrieved
150    * from an index.
151    * 
152    * @throws Exception on error
153    */
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();
159     
160     IndexSearcher searcher = newSearcher(reader);
161     
162     // search for something that does exists
163     Query query = new TermQuery(new Term("keyword", "test1"));
164     
165     // ensure that queries return expected results without DateFilter first
166     ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
167     assertEquals(1, hits.length);
168     
169     doAssert(searcher.doc(hits[0].doc), true);
170     writer.close();
171     searcher.close();
172     reader.close();
173     dir.close();
174   }
175   
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));
186     doc
187         .add(new Field("unstored", "test1", Field.Store.NO,
188             Field.Index.ANALYZED));
189     doc
190         .add(new Field("unstored", "test2", Field.Store.NO,
191             Field.Index.ANALYZED));
192     return doc;
193   }
194   
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");
200     
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
206     if (!fromIndex) {
207       assertTrue(unstoredFieldValues.length == 2);
208     }
209     
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
218     if (!fromIndex) {
219       assertTrue(unstoredFieldValues[0].equals("test1"));
220       assertTrue(unstoredFieldValues[1].equals("test2"));
221     }
222   }
223   
224   public void testFieldSetValue() throws Exception {
225     
226     Field field = new Field("id", "id1", Field.Store.YES,
227         Field.Index.NOT_ANALYZED);
228     Document doc = new Document();
229     doc.add(field);
230     doc.add(new Field("keyword", "test", Field.Store.YES,
231         Field.Index.NOT_ANALYZED));
232     
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);
240     
241     IndexReader reader = writer.getReader();
242     IndexSearcher searcher = newSearcher(reader);
243     
244     Query query = new TermQuery(new Term("keyword", "test"));
245     
246     // ensure that queries return expected results without DateFilter first
247     ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;
248     assertEquals(3, hits.length);
249     int result = 0;
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");
257     }
258     writer.close();
259     searcher.close();
260     reader.close();
261     dir.close();
262     assertEquals("did not see all IDs", 7, result);
263   }
264   
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);
269     try {
270       field1.setValue("abc");
271       fail("did not hit expected exception");
272     } catch (IllegalArgumentException iae) {
273       // expected
274     }
275     try {
276       field2.setValue(new byte[0]);
277       fail("did not hit expected exception");
278     } catch (IllegalArgumentException iae) {
279       // expected
280     }
281   }
282 }