View Javadoc

1   /* Copyright (c) 2008 Sascha Kohlmann
2    *
3    * This program is free software: you can redistribute it and/or modify
4    * it under the terms of the GNU Affero General Public License as published by
5    * the Free Software Foundation, either version 3 of the License, or
6    * (at your option) any later version.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Affero General Public License for more details.
12   *
13   * You should have received a copy of the GNU Affero General Public License
14   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15   */
16  package net.sf.eos.lucene;
17  
18  import static org.apache.lucene.document.Field.Index.TOKENIZED;
19  import static org.apache.lucene.document.Field.Index.UN_TOKENIZED;
20  import static org.apache.lucene.document.Field.Store.YES;
21  import static org.apache.lucene.document.Field.Store.NO;
22  import net.sf.eos.EosException;
23  import net.sf.eos.document.EosDocument;
24  
25  import org.apache.lucene.document.Document;
26  import org.apache.lucene.document.Field;
27  
28  import java.util.List;
29  import java.util.Map;
30  import java.util.Map.Entry;
31  
32  /**
33   
34   * <table border='1' cellspacing='1' cellpadding='4'>
35   *   <tr><th>Name</th><th>Value of</th><th>Store</th><th>Tokenize</th><th>Comment</th></tr>
36   *   <tr><td><tt>CONTENT</tt></td><td>{@link EosDocument#getText() EosDocument.getText()}</td><td>Yes</td><td>TOKENIZED</td><td> </td></tr>
37   *   <tr><td><tt>CREATOR</tt></td><td>{@link EosDocument#CREATOR_META_KEY EosDocument.CREATOR_META_KEY}</td><td>Yes</td><td>TOKENIZED</td><td>All values where added to the field</td></tr>
38   *   <tr><td><tt>ID</tt></td><td>{@link EosDocument#ID_META_KEY EosDocument.ID_META_KEY}</td><td>Yes</td><td>UN_TOKENIZED</td><td>All values where added to the field</td></tr>
39   *   <tr><td><tt>YEAR</tt></td><td>{@link EosDocument#YEAR_META_KEY EosDocument.YEAR_META_KEY}</td><td>Yes</td><td>UN_TOKENIZED</td><td>All values where added to the field</td></tr>
40   * </table>
41   *
42   * @author Sascha Kohlmann
43   */
44  public class DefaultLuceneDocumentCreator extends LuceneDocumentCreator
45          implements CommonDocument {
46  
47      /** Creates a new document. */
48      @Override
49      public Document createLuceneForEosDocument(final EosDocument doc)
50              throws EosException {
51  
52          final Document lDoc = new Document();
53          final String text = doc.getText().toString();
54          final Field content = new Field(FieldName.CONTENT.name(),
55                                          text,
56                                          NO,
57                                          TOKENIZED);
58          lDoc.add(content);
59  
60          final Map<String, List<String>> meta = doc.getMeta();
61          if (meta != null) {
62              for (final Entry<String, List<String>> entry : meta.entrySet()) {
63                  final List<String> values = entry.getValue();
64                  if (values != null && values.size() != 0) {
65                      final String key = entry.getKey();
66                      for (final String value : values) {
67                          Field f = null;
68                          if (EosDocument.YEAR_META_KEY.equals(key)) {
69                              f = new Field(FieldName.YEAR.name(),
70                                            value,
71                                            YES,
72                                            UN_TOKENIZED);
73                          } else if (EosDocument.ID_META_KEY.equals(key)) {
74                              f = new Field(FieldName.ID.name(),
75                                            value,
76                                            YES,
77                                            UN_TOKENIZED);
78                          } else if (EosDocument.CREATOR_META_KEY.equals(key)) {
79                              f = new Field(FieldName.CREATOR.name(),
80                                            value,
81                                            YES,
82                                            TOKENIZED);
83                          }
84                          if (f != null) {
85                              lDoc.add(f);
86                          }
87                      }
88                  }
89              }
90          }
91  
92          return lDoc;
93      }
94  }