1 package org.apache.lucene.document; 2 3 import junit.framework.TestCase; 4 5 import org.apache.lucene.store.RAMDirectory; 6 import org.apache.lucene.document.Document; 7 import org.apache.lucene.document.Field; 8 import org.apache.lucene.analysis.standard.StandardAnalyzer; 9 import org.apache.lucene.index.IndexWriter; 10 import org.apache.lucene.index.Term; 11 import org.apache.lucene.search.Query; 12 import org.apache.lucene.search.TermQuery; 13 import org.apache.lucene.search.IndexSearcher; 14 import org.apache.lucene.search.Searcher; 15 import org.apache.lucene.search.Hits; 16 17 import java.io.IOException ; 18 19 34 35 41 public class TestDocument extends TestCase 42 { 43 44 50 public void testRemoveForNewDocument() throws Exception 51 { 52 Document doc = makeDocumentWithFields(); 53 assertEquals(8, doc.fields.size()); 54 doc.removeFields("keyword"); 55 assertEquals(6, doc.fields.size()); 56 doc.removeFields("doesnotexists"); doc.removeFields("keyword"); assertEquals(6, doc.fields.size()); 59 doc.removeField("text"); 60 assertEquals(5, doc.fields.size()); 61 doc.removeField("text"); 62 assertEquals(4, doc.fields.size()); 63 doc.removeField("text"); 64 assertEquals(4, doc.fields.size()); 65 doc.removeField("doesnotexists"); assertEquals(4, doc.fields.size()); 67 doc.removeFields("unindexed"); 68 assertEquals(2, doc.fields.size()); 69 doc.removeFields("unstored"); 70 assertEquals(0, doc.fields.size()); 71 doc.removeFields("doesnotexists"); assertEquals(0, doc.fields.size()); 73 } 74 75 81 public void testGetValuesForNewDocument() throws Exception 82 { 83 doAssert(makeDocumentWithFields(), false); 84 } 85 86 92 public void testGetValuesForIndexedDocument() throws Exception 93 { 94 RAMDirectory dir = new RAMDirectory(); 95 IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true); 96 writer.addDocument(makeDocumentWithFields()); 97 writer.close(); 98 99 Searcher searcher = new IndexSearcher(dir); 100 101 Query query = new TermQuery(new Term("keyword", "test1")); 103 104 Hits hits = searcher.search(query); 106 assertEquals(1, hits.length()); 107 108 try 109 { 110 doAssert(hits.doc(0), true); 111 } 112 catch (Exception e) 113 { 114 e.printStackTrace(System.err); 115 System.err.print("\n"); 116 } 117 finally 118 { 119 searcher.close(); 120 } 121 } 122 123 private Document makeDocumentWithFields() throws IOException 124 { 125 Document doc = new Document(); 126 doc.add(Field.Keyword( "keyword", "test1")); 127 doc.add(Field.Keyword( "keyword", "test2")); 128 doc.add(Field.Text( "text", "test1")); 129 doc.add(Field.Text( "text", "test2")); 130 doc.add(Field.UnIndexed("unindexed", "test1")); 131 doc.add(Field.UnIndexed("unindexed", "test2")); 132 doc.add(Field.UnStored( "unstored", "test1")); 133 doc.add(Field.UnStored( "unstored", "test2")); 134 return doc; 135 } 136 137 private void doAssert(Document doc, boolean fromIndex) 138 { 139 String [] keywordFieldValues = doc.getValues("keyword"); 140 String [] textFieldValues = doc.getValues("text"); 141 String [] unindexedFieldValues = doc.getValues("unindexed"); 142 String [] unstoredFieldValues = doc.getValues("unstored"); 143 144 assertTrue(keywordFieldValues.length == 2); 145 assertTrue(textFieldValues.length == 2); 146 assertTrue(unindexedFieldValues.length == 2); 147 if (! fromIndex) 150 { 151 assertTrue(unstoredFieldValues.length == 2); 152 } 153 154 assertTrue(keywordFieldValues[0].equals("test1")); 155 assertTrue(keywordFieldValues[1].equals("test2")); 156 assertTrue(textFieldValues[0].equals("test1")); 157 assertTrue(textFieldValues[1].equals("test2")); 158 assertTrue(unindexedFieldValues[0].equals("test1")); 159 assertTrue(unindexedFieldValues[1].equals("test2")); 160 if (! fromIndex) 163 { 164 assertTrue(unstoredFieldValues[0].equals("test1")); 165 assertTrue(unstoredFieldValues[1].equals("test2")); 166 } 167 } 168 } 169 | Popular Tags |