1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.opencastproject.elasticsearch.impl;
24
25 import static org.opencastproject.elasticsearch.impl.IndexSchema.FUZZY_FIELDNAME_EXTENSION;
26
27 import org.opencastproject.elasticsearch.api.Language;
28 import org.opencastproject.elasticsearch.api.SearchMetadata;
29
30 import java.text.MessageFormat;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.HashMap;
34 import java.util.List;
35
36
37
38
39 public final class ElasticsearchDocument extends HashMap<String, Object> {
40
41
42 private static final long serialVersionUID = 2687550418831284487L;
43
44
45 private String id = null;
46
47
48 private String type = null;
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public ElasticsearchDocument(String id, String type, List<SearchMetadata<?>> resource) {
63 this.id = id;
64 this.type = type;
65 for (SearchMetadata<?> entry : resource) {
66 String metadataKey = entry.getName();
67 put(metadataKey, entry.getValues());
68
69
70 if (entry.addToText()) {
71 addToFulltext(entry, IndexSchema.TEXT, IndexSchema.TEXT);
72 }
73 }
74 }
75
76
77
78
79
80
81 public String getType() {
82 return type;
83 }
84
85
86
87
88
89
90
91
92
93
94
95 @SuppressWarnings("unchecked")
96 private void addToFulltext(SearchMetadata<?> item, String fulltextFieldName, String localizedFulltextFieldName) {
97
98
99 Collection<String> fulltext = (Collection<String>) get(fulltextFieldName);
100 if (fulltext == null) {
101 fulltext = new ArrayList<String>();
102 put(fulltextFieldName, fulltext);
103 put(fulltextFieldName + FUZZY_FIELDNAME_EXTENSION, fulltext);
104 }
105
106
107 for (Object value : item.getValues()) {
108 if (value.getClass().isArray()) {
109 Object[] fieldValues = (Object[]) value;
110 for (Object v : fieldValues) {
111 fulltext.add(v.toString());
112 }
113 } else {
114 fulltext.add(value.toString());
115 }
116 }
117
118
119 for (Language language : item.getLocalizedValues().keySet()) {
120
121 String localizedFieldName = MessageFormat.format(localizedFulltextFieldName, language.getIdentifier());
122 Collection<String> localizedFulltext = (Collection<String>) get(localizedFieldName);
123 if (fulltext == null) {
124 fulltext = new ArrayList<String>();
125 put(localizedFieldName, fulltext);
126 }
127 Collection<?> values = item.getLocalizedValues().get(language);
128 for (Object value : values) {
129 if (value.getClass().isArray()) {
130 Object[] fieldValues = (Object[]) value;
131 for (Object v : fieldValues) {
132 localizedFulltext.add(v.toString());
133 }
134 } else {
135 localizedFulltext.add(value.toString());
136 }
137 }
138 }
139
140 }
141
142
143
144
145
146
147 public String getUID() {
148 return id;
149 }
150
151 }