Skip to content

Commit 7e9ada4

Browse files
author
Luc Boutier
committed
Fixing filter generation for inner objects (non nested)
1 parent 80254b0 commit 7e9ada4

21 files changed

+81
-79
lines changed

elasticsearch-annotations/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>org.alien4cloud</groupId>
66
<artifactId>elasticsearch-mapping-parent</artifactId>
7-
<version>1.7.1-SNAPSHOT</version>
7+
<version>1.7.2-SNAPSHOT</version>
88
</parent>
99

1010
<artifactId>elasticsearch-annotations</artifactId>

elasticsearch-mapping/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>org.alien4cloud</groupId>
55
<artifactId>elasticsearch-mapping-parent</artifactId>
6-
<version>1.7.1-SNAPSHOT</version>
6+
<version>1.7.2-SNAPSHOT</version>
77
</parent>
88

99
<artifactId>elasticsearch-mapping</artifactId>

elasticsearch-mapping/src/main/java/org/elasticsearch/mapping/FieldsMappingBuilder.java

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ public class FieldsMappingBuilder {
3737
*/
3838
@SuppressWarnings("unchecked")
3939
public void parseFieldMappings(Class<?> clazz, Map<String, Object> classDefinitionMap, List<IFacetBuilderHelper> facetFields,
40-
List<IFilterBuilderHelper> filteredFields, Map<String, SourceFetchContext> fetchContexts, String pathPrefix) throws IntrospectionException {
40+
List<IFilterBuilderHelper> filteredFields, Map<String, SourceFetchContext> fetchContexts, String pathPrefix, String nestedPrefix)
41+
throws IntrospectionException {
4142
if (clazz.getSuperclass() != null && clazz.getSuperclass() != Object.class) {
42-
parseFieldMappings(clazz.getSuperclass(), classDefinitionMap, facetFields, filteredFields, fetchContexts, pathPrefix);
43+
parseFieldMappings(clazz.getSuperclass(), classDefinitionMap, facetFields, filteredFields, fetchContexts, pathPrefix, nestedPrefix);
4344
}
4445

4546
List<Indexable> indexables = getIndexables(clazz);
@@ -51,13 +52,14 @@ public void parseFieldMappings(Class<?> clazz, Map<String, Object> classDefiniti
5152
}
5253

5354
for (Indexable indexable : indexables) {
54-
parseFieldMappings(clazz, classDefinitionMap, facetFields, filteredFields, fetchContexts, propertiesDefinitionMap, pathPrefix, indexable);
55+
parseFieldMappings(clazz, classDefinitionMap, facetFields, filteredFields, fetchContexts, propertiesDefinitionMap, pathPrefix, nestedPrefix,
56+
indexable);
5557
}
5658
}
5759

5860
private void parseFieldMappings(Class<?> clazz, Map<String, Object> classDefinitionMap, List<IFacetBuilderHelper> facetFields,
5961
List<IFilterBuilderHelper> filteredFields, Map<String, SourceFetchContext> fetchContexts, Map<String, Object> propertiesDefinitionMap,
60-
String pathPrefix, Indexable indexable) {
62+
String pathPrefix, String nestedPrefix, Indexable indexable) {
6163
String esFieldName = pathPrefix + indexable.getName();
6264

6365
if (pathPrefix == null || pathPrefix.isEmpty()) {
@@ -70,51 +72,51 @@ private void parseFieldMappings(Class<?> clazz, Map<String, Object> classDefinit
7072
}
7173

7274
processFetchContextAnnotation(fetchContexts, esFieldName, indexable);
73-
processFilterAnnotation(filteredFields, esFieldName, indexable);
75+
processFilterAnnotation(filteredFields, nestedPrefix, esFieldName, indexable);
7476
processFacetAnnotation(facetFields, filteredFields, esFieldName, indexable);
7577

7678
// process the fields
7779
if (ClassUtils.isPrimitiveOrWrapper(indexable.getType()) || indexable.getType() == String.class || indexable.getType() == Date.class) {
78-
processStringOrPrimitive(clazz, propertiesDefinitionMap, pathPrefix, indexable);
80+
processStringOrPrimitive(clazz, propertiesDefinitionMap, pathPrefix, nestedPrefix, indexable);
7981
} else if (indexable.getType().isEnum()) {
8082
StringField annotation = indexable.getAnnotation(StringField.class);
8183
if (annotation != null) {
82-
processStringOrPrimitive(clazz, propertiesDefinitionMap, pathPrefix, indexable);
84+
processStringOrPrimitive(clazz, propertiesDefinitionMap, pathPrefix, nestedPrefix, indexable);
8385
}
8486
} else if (Map.class.isAssignableFrom(indexable.getType())) {
8587
MapKeyValue annotation = indexable.getAnnotation(MapKeyValue.class);
8688
if (annotation != null) {
8789
// Create an object mapping with key and value
8890
processFieldAnnotation(MapKeyValue.class, new MapKeyValueAnnotationParser(this, filteredFields, facetFields), propertiesDefinitionMap,
89-
pathPrefix, indexable);
91+
pathPrefix, nestedPrefix, indexable);
9092
} else {
91-
processComplexOrArray(clazz, facetFields, filteredFields, pathPrefix, propertiesDefinitionMap, indexable);
93+
processComplexOrArray(clazz, facetFields, filteredFields, pathPrefix, nestedPrefix, propertiesDefinitionMap, indexable);
9294
}
9395
} else {
94-
processComplexOrArray(clazz, facetFields, filteredFields, pathPrefix, propertiesDefinitionMap, indexable);
96+
processComplexOrArray(clazz, facetFields, filteredFields, pathPrefix, nestedPrefix, propertiesDefinitionMap, indexable);
9597
}
9698
}
9799

98100
private void processComplexOrArray(Class<?> clazz, List<IFacetBuilderHelper> facetFields, List<IFilterBuilderHelper> filteredFields, String pathPrefix,
99-
Map<String, Object> propertiesDefinitionMap, Indexable indexable) {
101+
String nestedPrefix, Map<String, Object> propertiesDefinitionMap, Indexable indexable) {
100102
// mapping of a complex field
101103
if (indexable.isArrayOrCollection()) {
102104
Class<?> arrayType = indexable.getComponentType();
103105
// process the array type.
104106
if (ClassUtils.isPrimitiveOrWrapper(arrayType) || arrayType == String.class || indexable.getType() == Date.class) {
105-
processStringOrPrimitive(clazz, propertiesDefinitionMap, pathPrefix, indexable);
107+
processStringOrPrimitive(clazz, propertiesDefinitionMap, pathPrefix, nestedPrefix, indexable);
106108
} else if (arrayType.isEnum()) {
107109
// if this is an enum and there is a String
108110
StringField annotation = indexable.getAnnotation(StringField.class);
109111
if (annotation != null) {
110-
processStringOrPrimitive(clazz, propertiesDefinitionMap, pathPrefix, indexable);
112+
processStringOrPrimitive(clazz, propertiesDefinitionMap, pathPrefix, nestedPrefix, indexable);
111113
}
112114
} else {
113-
processComplexType(clazz, propertiesDefinitionMap, pathPrefix, indexable, filteredFields, facetFields);
115+
processComplexType(clazz, propertiesDefinitionMap, pathPrefix, nestedPrefix, indexable, filteredFields, facetFields);
114116
}
115117
} else {
116118
// process the type
117-
processComplexType(clazz, propertiesDefinitionMap, pathPrefix, indexable, filteredFields, facetFields);
119+
processComplexType(clazz, propertiesDefinitionMap, pathPrefix, nestedPrefix, indexable, filteredFields, facetFields);
118120
}
119121
}
120122

@@ -206,7 +208,7 @@ private void processFetchContextAnnotation(Map<String, SourceFetchContext> fetch
206208
}
207209
}
208210

209-
private void processFilterAnnotation(List<IFilterBuilderHelper> classFilters, String esFieldName, Indexable indexable) {
211+
private void processFilterAnnotation(List<IFilterBuilderHelper> classFilters, String nestedPrefix, String esFieldName, Indexable indexable) {
210212
TermFilter termFilter = indexable.getAnnotation(TermFilter.class);
211213
if (termFilter != null) {
212214
String[] paths = termFilter.paths();
@@ -223,9 +225,9 @@ private void processFilterAnnotation(List<IFilterBuilderHelper> classFilters, St
223225
}
224226
for (String path : paths) {
225227
path = path.trim();
226-
addFilter(classFilters, esFieldName, indexable, path, isAnalyzed(indexable, null));
228+
addFilter(classFilters, nestedPrefix, esFieldName, indexable, path, isAnalyzed(indexable, null));
227229
for (String alternateFieldName : alternateFieldNames(indexable)) {
228-
addFilter(classFilters, alternateFieldName, indexable, path, isAnalyzed(indexable, alternateFieldName));
230+
addFilter(classFilters, nestedPrefix, alternateFieldName, indexable, path, isAnalyzed(indexable, alternateFieldName));
229231
}
230232
}
231233
return;
@@ -237,18 +239,14 @@ private void processFilterAnnotation(List<IFilterBuilderHelper> classFilters, St
237239
}
238240
}
239241

240-
private void addFilter(List<IFilterBuilderHelper> classFilters, String esFieldName, Indexable indexable, String path, boolean isAnalyzed) {
241-
String nestedPath = indexable.getAnnotation(NestedObject.class) == null ? null : esFieldName;
242-
if (nestedPath == null) {
243-
int nestedIndicator = esFieldName.lastIndexOf(".");
244-
if (nestedIndicator > 0) {
245-
nestedPath = esFieldName.substring(0, nestedIndicator);
246-
esFieldName = esFieldName.substring(nestedIndicator + 1, esFieldName.length());
247-
}
242+
private void addFilter(List<IFilterBuilderHelper> classFilters, String nestedPrefix, String esFieldName, Indexable indexable, String path,
243+
boolean isAnalyzed) {
244+
if (nestedPrefix != null) {
245+
esFieldName = esFieldName.substring(nestedPrefix.length() + 1);
248246
}
249247
String filterPath = getFilterPath(path, esFieldName);
250248

251-
classFilters.add(new TermsFilterBuilderHelper(isAnalyzed, nestedPath, filterPath));
249+
classFilters.add(new TermsFilterBuilderHelper(isAnalyzed, nestedPrefix, filterPath));
252250
}
253251

254252
private void processFacetAnnotation(List<IFacetBuilderHelper> classFacets, List<IFilterBuilderHelper> classFilters, String esFieldName,
@@ -337,37 +335,38 @@ private String[] alternateFieldNames(Indexable indexable) {
337335
return multi.multiNames();
338336
}
339337

340-
private void processStringOrPrimitive(Class<?> clazz, Map<String, Object> propertiesDefinitionMap, String pathPrefix, Indexable indexable) {
341-
processFieldAnnotation(IndexName.class, new IndexNameAnnotationParser(), propertiesDefinitionMap, pathPrefix, indexable);
342-
processFieldAnnotation(NullValue.class, new NullValueAnnotationParser(), propertiesDefinitionMap, pathPrefix, indexable);
338+
private void processStringOrPrimitive(Class<?> clazz, Map<String, Object> propertiesDefinitionMap, String pathPrefix, String nestedPrefix,
339+
Indexable indexable) {
340+
processFieldAnnotation(IndexName.class, new IndexNameAnnotationParser(), propertiesDefinitionMap, pathPrefix, nestedPrefix, indexable);
341+
processFieldAnnotation(NullValue.class, new NullValueAnnotationParser(), propertiesDefinitionMap, pathPrefix, nestedPrefix, indexable);
343342

344343
// String field annotations.
345-
processFieldAnnotation(StringField.class, new StringFieldAnnotationParser(), propertiesDefinitionMap, pathPrefix, indexable);
346-
processFieldAnnotation(StringFieldMulti.class, new StringFieldMultiAnnotationParser(), propertiesDefinitionMap, pathPrefix, indexable);
347-
processFieldAnnotation(Analyser.class, new AnalyserAnnotationParser(), propertiesDefinitionMap, pathPrefix, indexable);
348-
processFieldAnnotation(IndexAnalyser.class, new IndexAnalyserAnnotationParser(), propertiesDefinitionMap, pathPrefix, indexable);
349-
processFieldAnnotation(SearchAnalyser.class, new SearchAnalyserAnnotationParser(), propertiesDefinitionMap, pathPrefix, indexable);
344+
processFieldAnnotation(StringField.class, new StringFieldAnnotationParser(), propertiesDefinitionMap, pathPrefix, nestedPrefix, indexable);
345+
processFieldAnnotation(StringFieldMulti.class, new StringFieldMultiAnnotationParser(), propertiesDefinitionMap, pathPrefix, nestedPrefix, indexable);
346+
processFieldAnnotation(Analyser.class, new AnalyserAnnotationParser(), propertiesDefinitionMap, pathPrefix, nestedPrefix, indexable);
347+
processFieldAnnotation(IndexAnalyser.class, new IndexAnalyserAnnotationParser(), propertiesDefinitionMap, pathPrefix, nestedPrefix, indexable);
348+
processFieldAnnotation(SearchAnalyser.class, new SearchAnalyserAnnotationParser(), propertiesDefinitionMap, pathPrefix, nestedPrefix, indexable);
350349

351350
// Numeric field annotation
352-
processFieldAnnotation(NumberField.class, new NumberFieldAnnotationParser(), propertiesDefinitionMap, pathPrefix, indexable);
351+
processFieldAnnotation(NumberField.class, new NumberFieldAnnotationParser(), propertiesDefinitionMap, pathPrefix, nestedPrefix, indexable);
353352

354353
// Date field annotation
355-
processFieldAnnotation(DateField.class, new DateFieldAnnotationParser(), propertiesDefinitionMap, pathPrefix, indexable);
356-
processFieldAnnotation(DateFormat.class, new DateFormatAnnotationParser(), propertiesDefinitionMap, pathPrefix, indexable);
354+
processFieldAnnotation(DateField.class, new DateFieldAnnotationParser(), propertiesDefinitionMap, pathPrefix, nestedPrefix, indexable);
355+
processFieldAnnotation(DateFormat.class, new DateFormatAnnotationParser(), propertiesDefinitionMap, pathPrefix, nestedPrefix, indexable);
357356

358357
// Boolean field annotation
359-
processFieldAnnotation(BooleanField.class, new BooleanFieldAnnotationParser(), propertiesDefinitionMap, pathPrefix, indexable);
358+
processFieldAnnotation(BooleanField.class, new BooleanFieldAnnotationParser(), propertiesDefinitionMap, pathPrefix, nestedPrefix, indexable);
360359
// TODO binary type mapping
361360
}
362361

363-
private void processComplexType(Class<?> clazz, Map<String, Object> propertiesDefinitionMap, String pathPrefix, Indexable indexable,
362+
private void processComplexType(Class<?> clazz, Map<String, Object> propertiesDefinitionMap, String pathPrefix, String nestedPrefix, Indexable indexable,
364363
List<IFilterBuilderHelper> filters, List<IFacetBuilderHelper> facets) {
365364
NestedObjectFieldAnnotationParser nested = new NestedObjectFieldAnnotationParser(this, filters, facets);
366-
processFieldAnnotation(NestedObject.class, nested, propertiesDefinitionMap, pathPrefix, indexable);
365+
processFieldAnnotation(NestedObject.class, nested, propertiesDefinitionMap, pathPrefix, nestedPrefix, indexable);
367366

368367
if (propertiesDefinitionMap.get(indexable.getName()) == null) {
369368
ObjectFieldAnnotationParser objectFieldAnnotationParser = new ObjectFieldAnnotationParser(this, filters, facets);
370-
processFieldAnnotation(ObjectField.class, objectFieldAnnotationParser, propertiesDefinitionMap, pathPrefix, indexable);
369+
processFieldAnnotation(ObjectField.class, objectFieldAnnotationParser, propertiesDefinitionMap, pathPrefix, nestedPrefix, indexable);
371370
// by default we consider the complex object as an object mapping and process recursive mapping of every field just as ES would process based on
372371
// dynamic
373372
// mapping
@@ -378,22 +377,22 @@ private void processComplexType(Class<?> clazz, Map<String, Object> propertiesDe
378377
fieldDefinition = new HashMap<String, Object>();
379378
propertiesDefinitionMap.put(indexable.getName(), fieldDefinition);
380379
}
381-
objectFieldAnnotationParser.parseAnnotation(null, fieldDefinition, pathPrefix, indexable);
380+
objectFieldAnnotationParser.parseAnnotation(null, fieldDefinition, pathPrefix, nestedPrefix, indexable);
382381
}
383382
}
384383
}
385384

386385
@SuppressWarnings("unchecked")
387386
private <T extends Annotation> void processFieldAnnotation(Class<T> annotationClass, IPropertyAnnotationParser<T> propertyAnnotationParser,
388-
Map<String, Object> propertiesDefinitionMap, String pathPrefix, Indexable indexable) {
387+
Map<String, Object> propertiesDefinitionMap, String pathPrefix, String nestedPrefix, Indexable indexable) {
389388
T annotation = indexable.getAnnotation(annotationClass);
390389
if (annotation != null) {
391390
Map<String, Object> fieldDefinition = (Map<String, Object>) propertiesDefinitionMap.get(indexable.getName());
392391
if (fieldDefinition == null) {
393392
fieldDefinition = new HashMap<String, Object>();
394393
propertiesDefinitionMap.put(indexable.getName(), fieldDefinition);
395394
}
396-
propertyAnnotationParser.parseAnnotation(annotation, fieldDefinition, pathPrefix, indexable);
395+
propertyAnnotationParser.parseAnnotation(annotation, fieldDefinition, pathPrefix, nestedPrefix, indexable);
397396
}
398397
}
399398

elasticsearch-mapping/src/main/java/org/elasticsearch/mapping/MappingBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public void parseClassMapping(Class<?> clazz, String pathPrefix) throws Introspe
184184
classDefinitionMap.put("_source", MapUtil.getMap("enabled", esObject.source()));
185185
classDefinitionMap.put("_type", MapUtil.getMap(new String[] { "store", "index" }, new Object[] { esObject.store(), esObject.index() }));
186186

187-
this.fieldsMappingBuilder.parseFieldMappings(clazz, classDefinitionMap, facetFields, filteredFields, fetchContexts, pathPrefix);
187+
this.fieldsMappingBuilder.parseFieldMappings(clazz, classDefinitionMap, facetFields, filteredFields, fetchContexts, pathPrefix, null);
188188

189189
ObjectMapper mapper = new ObjectMapper();
190190
if (typeNameStr != null) {

elasticsearch-mapping/src/main/java/org/elasticsearch/mapping/QueryHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ public CountResponse count(String[] indices, String... types) {
286286
countRequestBuilder.setTypes(types);
287287
}
288288
countRequestBuilder.setQuery(this.queryBuilder);
289+
String query = countRequestBuilder.toString();
289290
return countRequestBuilder.execute().actionGet();
290291
}
291292

elasticsearch-mapping/src/main/java/org/elasticsearch/mapping/parser/AnalyserAnnotationParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @author luc boutier
1212
*/
1313
public class AnalyserAnnotationParser implements IPropertyAnnotationParser<Analyser> {
14-
public void parseAnnotation(Analyser annotation, Map<String, Object> fieldDefinition, String pathPrefix, Indexable indexable) {
14+
public void parseAnnotation(Analyser annotation, Map<String, Object> fieldDefinition, String pathPrefix, String nestedPrefix, Indexable indexable) {
1515
fieldDefinition.put("analyser", annotation.analyzer());
1616
}
1717
}

elasticsearch-mapping/src/main/java/org/elasticsearch/mapping/parser/BooleanFieldAnnotationParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public class BooleanFieldAnnotationParser implements IPropertyAnnotationParser<BooleanField> {
1717
private static final ESLogger LOGGER = Loggers.getLogger(MappingBuilder.class);
1818

19-
public void parseAnnotation(BooleanField annotation, Map<String, Object> fieldDefinition, String pathPrefix,
19+
public void parseAnnotation(BooleanField annotation, Map<String, Object> fieldDefinition, String pathPrefix, String nestedPrefix,
2020
Indexable indexable) {
2121
if (fieldDefinition.get("type") != null) {
2222
LOGGER.info("Overriding mapping for field {} for class {} was defined as type {}", indexable.getName(), indexable.getDeclaringClassName(),

elasticsearch-mapping/src/main/java/org/elasticsearch/mapping/parser/DateFieldAnnotationParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
public class DateFieldAnnotationParser implements IPropertyAnnotationParser<DateField> {
1818
private static final ESLogger LOGGER = Loggers.getLogger(MappingBuilder.class);
1919

20-
public void parseAnnotation(DateField annotation, Map<String, Object> fieldDefinition, String pathPrefix, Indexable indexable) {
20+
public void parseAnnotation(DateField annotation, Map<String, Object> fieldDefinition, String pathPrefix, String nestedPrefix, Indexable indexable) {
2121
if (fieldDefinition.get("type") != null) {
2222
LOGGER.info("Overriding mapping for field {} for class {} was defined as type {}", indexable.getName(), indexable.getDeclaringClassName(),
2323
fieldDefinition.get("type"));

elasticsearch-mapping/src/main/java/org/elasticsearch/mapping/parser/DateFormatAnnotationParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
public class DateFormatAnnotationParser implements IPropertyAnnotationParser<DateFormat> {
1818
private static final ESLogger LOGGER = Loggers.getLogger(MappingBuilder.class);
1919

20-
public void parseAnnotation(DateFormat annotation, Map<String, Object> fieldDefinition, String pathPrefix,
20+
public void parseAnnotation(DateFormat annotation, Map<String, Object> fieldDefinition, String pathPrefix, String nestedPrefix,
2121
Indexable indexable) {
2222
if (fieldDefinition.get("type") == null) {
2323
fieldDefinition.put("type", "date");

0 commit comments

Comments
 (0)