7
7
8
8
import org .elasticsearch .annotation .ESAll ;
9
9
import org .elasticsearch .annotation .ESObject ;
10
+ import org .elasticsearch .annotation .IndexAnalyserDefinition ;
10
11
import org .elasticsearch .annotation .TypeName ;
12
+ import org .elasticsearch .common .collect .Maps ;
13
+ import org .elasticsearch .common .lang3 .ArrayUtils ;
11
14
import org .elasticsearch .util .AnnotationScanner ;
12
15
import org .elasticsearch .util .MapUtil ;
13
16
import org .springframework .context .annotation .Scope ;
14
17
import org .springframework .stereotype .Component ;
15
18
19
+ import com .fasterxml .jackson .annotation .JsonInclude ;
16
20
import com .fasterxml .jackson .core .JsonGenerationException ;
21
+ import com .fasterxml .jackson .core .JsonProcessingException ;
17
22
import com .fasterxml .jackson .databind .JsonMappingException ;
18
23
import com .fasterxml .jackson .databind .ObjectMapper ;
19
24
20
25
/**
21
26
* Helps to parse the ES annotations.
22
- *
27
+ *
23
28
* @author luc boutier
24
29
*/
25
30
@ Component
@@ -28,6 +33,7 @@ public class MappingBuilder {
28
33
private FieldsMappingBuilder fieldsMappingBuilder = new FieldsMappingBuilder ();
29
34
30
35
private Map <String , String > classesMappings = new HashMap <String , String >();
36
+ private Map <String , String > settingsByClassName = new HashMap <String , String >();
31
37
private Map <String , String > typeByClassName = new HashMap <String , String >();
32
38
33
39
private Map <String , List <IFilterBuilderHelper >> filtersByClassName = new HashMap <String , List <IFilterBuilderHelper >>();
@@ -37,7 +43,7 @@ public class MappingBuilder {
37
43
/**
38
44
* Helper to return a valid index type from a class. Currently uses clazz.getSimpleName().toLowerCase();
39
45
* Using the helper make sure this is done in a consistent way in different locations of the code.
40
- *
46
+ *
41
47
* @param clazz The class for which to get an index type.
42
48
* @return The index type.
43
49
*/
@@ -47,7 +53,7 @@ public static String indexTypeFromClass(Class<?> clazz) {
47
53
48
54
/**
49
55
* Build mapping for the given packages.
50
- *
56
+ *
51
57
* @param packages list of packages in which to look for {@link ESObject} annotated classes.
52
58
* @throws IntrospectionException Java reflection usage related exception.
53
59
* @throws IOException In case of an IO error while creating Json.
@@ -64,7 +70,7 @@ public void initialize(String... packages) throws IntrospectionException, JsonGe
64
70
65
71
/**
66
72
* Get the mapping json string for a given class.
67
- *
73
+ *
68
74
* @param clazz The class for which to get the mapping.
69
75
* @return The json mapping for this class.
70
76
* @throws JsonGenerationException In case the mapping failed to be serialized to json.
@@ -75,15 +81,24 @@ public void initialize(String... packages) throws IntrospectionException, JsonGe
75
81
public String getMapping (Class <?> clazz ) throws JsonGenerationException , JsonMappingException , IntrospectionException , IOException {
76
82
String classMapping = classesMappings .get (clazz .getName ());
77
83
if (classMapping == null ) {
78
- parseClassMapping (clazz , "" );
84
+ parseClassAnnotations (clazz , "" );
79
85
}
80
86
classMapping = classesMappings .get (clazz .getName ());
81
87
return classMapping ;
82
88
}
83
89
90
+ public String getIndexSettings (Class <?> clazz ) throws JsonGenerationException , JsonMappingException , IntrospectionException , IOException {
91
+ String settings = settingsByClassName .get (clazz .getName ());
92
+ if (settings == null ) {
93
+ parseClassAnnotations (clazz , "" );
94
+ }
95
+ settings = settingsByClassName .get (clazz .getName ());
96
+ return settings ;
97
+ }
98
+
84
99
/**
85
100
* Get the name of the type in elastic search for the given class.
86
- *
101
+ *
87
102
* @param clazz The class for which to get the type.
88
103
* @return The type name in elastic search.
89
104
*/
@@ -93,7 +108,7 @@ public String getTypeName(Class<?> clazz) {
93
108
94
109
/**
95
110
* Get the list of es fields that should be filtered in a filter search for the given class.
96
- *
111
+ *
97
112
* @param clazz The class for which to get the facets.
98
113
* @return The list of filters builders for this class.
99
114
*/
@@ -103,7 +118,7 @@ public List<IFilterBuilderHelper> getFilters(Class<?> clazz) {
103
118
104
119
/**
105
120
* Get the list of es fields that should be filtered in a filter search for the given class.
106
- *
121
+ *
107
122
* @param className The name for which to get the facets.
108
123
* @return The list of filters builders for this class.
109
124
*/
@@ -113,7 +128,7 @@ public List<IFilterBuilderHelper> getFilters(String className) {
113
128
114
129
/**
115
130
* Get the list of es fields that should be faceted in a facet search for the given class.
116
- *
131
+ *
117
132
* @param clazz The class for which to get the facets.
118
133
* @return The list of facet builders for this class.
119
134
*/
@@ -123,7 +138,7 @@ public List<IFacetBuilderHelper> getFacets(Class<?> clazz) {
123
138
124
139
/**
125
140
* Get the list of es fields that should be faceted in a facet search for the given class.
126
- *
141
+ *
127
142
* @param className The name for which to get the facets.
128
143
* @return The list of facet builders for this class.
129
144
*/
@@ -133,7 +148,7 @@ public List<IFacetBuilderHelper> getFacets(String className) {
133
148
134
149
/**
135
150
* Get the {@link SourceFetchContext} for a given fetch context.
136
- *
151
+ *
137
152
* @param className The class for which to get the fetch context.
138
153
* @param fetchContext The fetch context for which to get the field list.
139
154
* @return The requested {@link SourceFetchContext} or null if no context match the given class and fetch context key.
@@ -149,11 +164,12 @@ public SourceFetchContext getFetchSource(String className, String fetchContext)
149
164
private void initialize (String packageName ) throws IntrospectionException , JsonGenerationException , JsonMappingException , IOException {
150
165
Set <Class <?>> classSet = org .elasticsearch .util .AnnotationScanner .scan (packageName , ESObject .class );
151
166
for (Class <?> clazz : classSet ) {
152
- parseClassMapping (clazz , "" );
167
+ parseClassAnnotations (clazz , "" );
153
168
}
154
169
}
155
170
156
- public void parseClassMapping (Class <?> clazz , String pathPrefix ) throws IntrospectionException , JsonGenerationException , JsonMappingException , IOException {
171
+ public void parseClassAnnotations (Class <?> clazz , String pathPrefix )
172
+ throws IntrospectionException , JsonGenerationException , JsonMappingException , IOException {
157
173
ESObject esObject = AnnotationScanner .getAnnotation (ESObject .class , clazz );
158
174
ESAll esAll = AnnotationScanner .getAnnotation (ESAll .class , clazz );
159
175
@@ -196,5 +212,35 @@ public void parseClassMapping(Class<?> clazz, String pathPrefix) throws Introspe
196
212
this .facetByClassName .put (clazz .getName (), facetFields );
197
213
this .filtersByClassName .put (clazz .getName (), filteredFields );
198
214
this .fetchSourceContextByClass .put (clazz .getName (), fetchContexts );
215
+
216
+ this .settingsByClassName .put (clazz .getName (), buildSettings (mapper , esObject .analyzerDefinitions ()));
217
+ }
218
+
219
+ @ JsonInclude (JsonInclude .Include .NON_EMPTY )
220
+ private static class AnalyserFields {
221
+ public String tokenizer ;
222
+ public String type ;
223
+ public String [] char_filter ;
224
+ public String [] filter ;
225
+ public String [] stopwords ;
226
+ }
227
+
228
+ private String buildSettings (ObjectMapper mapper , IndexAnalyserDefinition [] customAnalyserDefinitions ) throws JsonProcessingException {
229
+ if (ArrayUtils .isEmpty (customAnalyserDefinitions )) {
230
+ return null ;
231
+ }
232
+
233
+ Map <Object , Object > analysers = Maps .newHashMap ();
234
+ for (IndexAnalyserDefinition analyserDefinition : customAnalyserDefinitions ) {
235
+ AnalyserFields analyserFields = new AnalyserFields ();
236
+ analyserFields .char_filter = analyserDefinition .char_filter ();
237
+ analyserFields .filter = analyserDefinition .filters ();
238
+ analyserFields .stopwords = analyserDefinition .stopwords ();
239
+ analyserFields .tokenizer = analyserDefinition .tokenizer ();
240
+ analyserFields .type = analyserDefinition .type ();
241
+ analysers .put (analyserDefinition .name (), analyserFields );
242
+ }
243
+
244
+ return "{\" analysis\" :{\" analyzer\" :" + mapper .writeValueAsString (analysers ) + "}}" ;
199
245
}
200
246
}
0 commit comments