1 package org.apache.lucene.index; 2 3 11 12 import org.apache.lucene.analysis.Analyzer; 13 import org.apache.lucene.analysis.WhitespaceAnalyzer; 14 import org.apache.lucene.document.Document; 15 import org.apache.lucene.document.Field; 16 import org.apache.lucene.search.Similarity; 17 import org.apache.lucene.store.Directory; 18 19 import java.io.IOException ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 import java.util.Enumeration ; 23 24 28 class DocHelper { 29 public static final String FIELD_1_TEXT = "field one text"; 30 public static final String TEXT_FIELD_1_KEY = "textField1"; 31 public static Field textField1 = Field.Text(TEXT_FIELD_1_KEY, FIELD_1_TEXT, false); 32 33 public static final String FIELD_2_TEXT = "field field field two text"; 34 public static final int [] FIELD_2_FREQS = {3, 1, 1}; 36 public static final String TEXT_FIELD_2_KEY = "textField2"; 37 public static Field textField2 = Field.Text(TEXT_FIELD_2_KEY, FIELD_2_TEXT, true); 38 39 public static final String KEYWORD_TEXT = "Keyword"; 40 public static final String KEYWORD_FIELD_KEY = "keyField"; 41 public static Field keyField = Field.Keyword(KEYWORD_FIELD_KEY, KEYWORD_TEXT); 42 43 public static final String UNINDEXED_FIELD_TEXT = "unindexed field text"; 44 public static final String UNINDEXED_FIELD_KEY = "unIndField"; 45 public static Field unIndField = Field.UnIndexed(UNINDEXED_FIELD_KEY, UNINDEXED_FIELD_TEXT); 46 47 public static final String UNSTORED_1_FIELD_TEXT = "unstored field text"; 48 public static final String UNSTORED_FIELD_1_KEY = "unStoredField1"; 49 public static Field unStoredField1 = Field.UnStored(UNSTORED_FIELD_1_KEY, UNSTORED_1_FIELD_TEXT, false); 50 51 public static final String UNSTORED_2_FIELD_TEXT = "unstored field text"; 52 public static final String UNSTORED_FIELD_2_KEY = "unStoredField2"; 53 public static Field unStoredField2 = Field.UnStored(UNSTORED_FIELD_2_KEY, UNSTORED_2_FIELD_TEXT, true); 54 55 public static Map nameValues = null; 58 59 static 60 { 61 62 nameValues = new HashMap (); 63 nameValues.put(TEXT_FIELD_1_KEY, FIELD_1_TEXT); 64 nameValues.put(TEXT_FIELD_2_KEY, FIELD_2_TEXT); 65 nameValues.put(KEYWORD_FIELD_KEY, KEYWORD_TEXT); 66 nameValues.put(UNINDEXED_FIELD_KEY, UNINDEXED_FIELD_TEXT); 67 nameValues.put(UNSTORED_FIELD_1_KEY, UNSTORED_1_FIELD_TEXT); 68 nameValues.put(UNSTORED_FIELD_2_KEY, UNSTORED_2_FIELD_TEXT); 69 } 70 71 75 public static void setupDoc(Document doc) { 76 doc.add(textField1); 77 doc.add(textField2); 78 doc.add(keyField); 79 doc.add(unIndField); 80 doc.add(unStoredField1); 81 doc.add(unStoredField2); 82 } 83 88 public static void writeDoc(Directory dir, Document doc) 89 { 90 91 writeDoc(dir, "test", doc); 92 } 93 99 public static void writeDoc(Directory dir, String segment, Document doc) 100 { 101 Analyzer analyzer = new WhitespaceAnalyzer(); 102 Similarity similarity = Similarity.getDefault(); 103 writeDoc(dir, analyzer, similarity, segment, doc); 104 } 105 112 public static void writeDoc(Directory dir, Analyzer analyzer, Similarity similarity, Document doc) 113 { 114 writeDoc(dir, analyzer, similarity, "test", doc); 115 } 116 124 public static void writeDoc(Directory dir, Analyzer analyzer, Similarity similarity, String segment, Document doc) 125 { 126 DocumentWriter writer = new DocumentWriter(dir, analyzer, similarity, 50); 127 try { 128 writer.addDocument(segment, doc); 129 } catch (IOException e) { 130 e.printStackTrace(); 131 } 132 } 133 134 public static int numFields(Document doc) { 135 Enumeration fields = doc.fields(); 136 int result = 0; 137 while (fields.hasMoreElements()) { 138 fields.nextElement(); 139 result++; 140 } 141 return result; 142 } 143 } 144 160 | Popular Tags |