KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > index > DocHelper


1 package org.apache.lucene.index;
2
3 /**
4  * Created by IntelliJ IDEA.
5  * User: Grant Ingersoll
6  * Date: Feb 2, 2004
7  * Time: 6:16:12 PM
8  * $Id: DocHelper.java,v 1.1 2004/02/20 20:14:55 cutting Exp $
9  * Copyright 2004. Center For Natural Language Processing
10  */

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 JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Enumeration JavaDoc;
23
24 /**
25  *
26  *
27  **/

28 class DocHelper {
29   public static final String JavaDoc FIELD_1_TEXT = "field one text";
30   public static final String JavaDoc 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 JavaDoc FIELD_2_TEXT = "field field field two text";
34   //Fields will be lexicographically sorted. So, the order is: field, text, two
35
public static final int [] FIELD_2_FREQS = {3, 1, 1};
36   public static final String JavaDoc 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 JavaDoc KEYWORD_TEXT = "Keyword";
40   public static final String JavaDoc KEYWORD_FIELD_KEY = "keyField";
41   public static Field keyField = Field.Keyword(KEYWORD_FIELD_KEY, KEYWORD_TEXT);
42   
43   public static final String JavaDoc UNINDEXED_FIELD_TEXT = "unindexed field text";
44   public static final String JavaDoc UNINDEXED_FIELD_KEY = "unIndField";
45   public static Field unIndField = Field.UnIndexed(UNINDEXED_FIELD_KEY, UNINDEXED_FIELD_TEXT);
46   
47   public static final String JavaDoc UNSTORED_1_FIELD_TEXT = "unstored field text";
48   public static final String JavaDoc 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 JavaDoc UNSTORED_2_FIELD_TEXT = "unstored field text";
52   public static final String JavaDoc 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 Set fieldNamesSet = null;
56
// public static Set fieldValuesSet = null;
57
public static Map JavaDoc nameValues = null;
58   
59   static
60   {
61     
62     nameValues = new HashMap JavaDoc();
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   /**
72    * Adds the fields above to a document
73    * @param doc The document to write
74    */

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   /**
84    * Writes the document to the directory using a segment named "test"
85    * @param dir
86    * @param doc
87    */

88   public static void writeDoc(Directory dir, Document doc)
89   {
90     
91     writeDoc(dir, "test", doc);
92   }
93   /**
94    * Writes the document to the directory in the given segment
95    * @param dir
96    * @param segment
97    * @param doc
98    */

99   public static void writeDoc(Directory dir, String JavaDoc segment, Document doc)
100   {
101     Analyzer analyzer = new WhitespaceAnalyzer();
102     Similarity similarity = Similarity.getDefault();
103     writeDoc(dir, analyzer, similarity, segment, doc);
104   }
105   /**
106    * Writes the document to the directory segment named "test" using the specified analyzer and similarity
107    * @param dir
108    * @param analyzer
109    * @param similarity
110    * @param doc
111    */

112   public static void writeDoc(Directory dir, Analyzer analyzer, Similarity similarity, Document doc)
113   {
114     writeDoc(dir, analyzer, similarity, "test", doc);
115   }
116   /**
117    * Writes the document to the directory segment using the analyzer and the similarity score
118    * @param dir
119    * @param analyzer
120    * @param similarity
121    * @param segment
122    * @param doc
123    */

124   public static void writeDoc(Directory dir, Analyzer analyzer, Similarity similarity, String JavaDoc segment, Document doc)
125   {
126     DocumentWriter writer = new DocumentWriter(dir, analyzer, similarity, 50);
127     try {
128       writer.addDocument(segment, doc);
129     } catch (IOException JavaDoc e) {
130       e.printStackTrace();
131     }
132   }
133
134   public static int numFields(Document doc) {
135     Enumeration JavaDoc fields = doc.fields();
136     int result = 0;
137     while (fields.hasMoreElements()) {
138       fields.nextElement();
139       result++;
140     }
141     return result;
142   }
143 }
144 /*
145     fieldNamesSet = new HashSet();
146     fieldNamesSet.add(TEXT_FIELD_1_KEY);
147     fieldNamesSet.add(TEXT_FIELD_2_KEY);
148     fieldNamesSet.add(KEYWORD_FIELD_KEY);
149     fieldNamesSet.add(UNINDEXED_FIELD_KEY);
150     fieldNamesSet.add(UNSTORED_FIELD_1_KEY);
151     fieldNamesSet.add(UNSTORED_FIELD_2_KEY);
152     fieldValuesSet = new HashSet();
153     fieldValuesSet.add(FIELD_1_TEXT);
154     fieldValuesSet.add(FIELD_2_TEXT);
155     fieldValuesSet.add(KEYWORD_TEXT);
156     fieldValuesSet.add(UNINDEXED_FIELD_TEXT);
157     fieldValuesSet.add(UNSTORED_1_FIELD_TEXT);
158     fieldValuesSet.add(UNSTORED_2_FIELD_TEXT);
159 */

160
Popular Tags