Skip to content

Commit b773d8b

Browse files
author
Luc Boutier
committed
Manage filters for multi string
Add back the es types based on classes on the query helper.
1 parent 81cc194 commit b773d8b

File tree

2 files changed

+77
-25
lines changed

2 files changed

+77
-25
lines changed

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

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -223,18 +223,10 @@ private void processFilterAnnotation(List<IFilterBuilderHelper> classFilters, St
223223
}
224224
for (String path : paths) {
225225
path = path.trim();
226-
boolean isAnalyzed = isAnalyzed(indexable);
227-
String nestedPath = indexable.getAnnotation(NestedObject.class) == null ? null : esFieldName;
228-
if (nestedPath == null) {
229-
int nestedIndicator = esFieldName.lastIndexOf(".");
230-
if (nestedIndicator > 0) {
231-
nestedPath = esFieldName.substring(0, nestedIndicator);
232-
esFieldName = esFieldName.substring(nestedIndicator + 1, esFieldName.length());
233-
}
226+
addFilter(classFilters, esFieldName, indexable, path, isAnalyzed(indexable, null));
227+
for (String alternateFieldName : alternateFieldNames(indexable)) {
228+
addFilter(classFilters, alternateFieldName, indexable, path, isAnalyzed(indexable, alternateFieldName));
234229
}
235-
String filterPath = getFilterPath(path, esFieldName);
236-
237-
classFilters.add(new TermsFilterBuilderHelper(isAnalyzed, nestedPath, filterPath));
238230
}
239231
return;
240232
}
@@ -245,6 +237,20 @@ private void processFilterAnnotation(List<IFilterBuilderHelper> classFilters, St
245237
}
246238
}
247239

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+
}
248+
}
249+
String filterPath = getFilterPath(path, esFieldName);
250+
251+
classFilters.add(new TermsFilterBuilderHelper(isAnalyzed, nestedPath, filterPath));
252+
}
253+
248254
private void processFacetAnnotation(List<IFacetBuilderHelper> classFacets, List<IFilterBuilderHelper> classFilters, String esFieldName,
249255
Indexable indexable) {
250256
TermsFacet termsFacet = indexable.getAnnotation(TermsFacet.class);
@@ -263,17 +269,11 @@ private void processFacetAnnotation(List<IFacetBuilderHelper> classFacets, List<
263269
}
264270
for (String path : paths) {
265271
path = path.trim();
266-
boolean isAnalyzed = isAnalyzed(indexable);
267-
String nestedPath = indexable.getAnnotation(NestedObject.class) == null ? null : esFieldName;
268-
String filterPath = getFilterPath(path, esFieldName);
269-
270-
IFacetBuilderHelper facetBuilderHelper = new TermsAggregationBuilderHelper(isAnalyzed, nestedPath, filterPath, termsFacet);
271-
classFacets.add(facetBuilderHelper);
272-
if (classFilters.contains(facetBuilderHelper)) {
273-
classFilters.remove(facetBuilderHelper);
274-
LOGGER.warn("Field <" + esFieldName + "> already had a filter that will be replaced by the defined facet. Only a single one is allowed.");
272+
273+
addAggregation(termsFacet, indexable, esFieldName, path, isAnalyzed(indexable, null), classFacets, classFilters);
274+
for (String alternateFieldName : alternateFieldNames(indexable)) {
275+
addAggregation(termsFacet, indexable, alternateFieldName, path, isAnalyzed(indexable, alternateFieldName), classFacets, classFilters);
275276
}
276-
classFilters.add(facetBuilderHelper);
277277
}
278278
return;
279279
}
@@ -293,15 +293,50 @@ private String getFilterPath(String path, String esFieldName) {
293293
return path.isEmpty() ? esFieldName : esFieldName + "." + path;
294294
}
295295

296-
private boolean isAnalyzed(Indexable indexable) {
296+
private boolean isAnalyzed(Indexable indexable, String name) {
297297
boolean isAnalysed = true;
298+
StringFieldMulti multiAnnotation = indexable.getAnnotation(StringFieldMulti.class);
299+
if (multiAnnotation != null) {
300+
if (name == null) {
301+
return IndexType.analyzed.equals(multiAnnotation.main().indexType());
302+
} else {
303+
for (int i = 0; i < multiAnnotation.multiNames().length; i++) {
304+
if (multiAnnotation.multiNames()[i].equals(name)) {
305+
return IndexType.analyzed.equals(multiAnnotation.multi()[i].indexType());
306+
}
307+
}
308+
}
309+
}
310+
298311
StringField stringFieldAnnotation = indexable.getAnnotation(StringField.class);
299-
if (stringFieldAnnotation != null && !IndexType.analyzed.equals(stringFieldAnnotation.indexType())) {
300-
isAnalysed = false;
312+
if (stringFieldAnnotation != null) {
313+
return IndexType.analyzed.equals(stringFieldAnnotation.indexType());
301314
}
302315
return isAnalysed;
303316
}
304317

318+
private void addAggregation(TermsFacet termsFacet, Indexable indexable, String esFieldName, String path, boolean isAnalyzed,
319+
List<IFacetBuilderHelper> classFacets, List<IFilterBuilderHelper> classFilters) {
320+
String nestedPath = indexable.getAnnotation(NestedObject.class) == null ? null : esFieldName;
321+
String filterPath = getFilterPath(path, esFieldName);
322+
323+
IFacetBuilderHelper facetBuilderHelper = new TermsAggregationBuilderHelper(isAnalyzed, nestedPath, filterPath, termsFacet);
324+
classFacets.add(facetBuilderHelper);
325+
if (classFilters.contains(facetBuilderHelper)) {
326+
classFilters.remove(facetBuilderHelper);
327+
LOGGER.warn("Field <" + esFieldName + "> already had a filter that will be replaced by the defined facet. Only a single one is allowed.");
328+
}
329+
classFilters.add(facetBuilderHelper);
330+
}
331+
332+
private String[] alternateFieldNames(Indexable indexable) {
333+
StringFieldMulti multi = indexable.getAnnotation(StringFieldMulti.class);
334+
if (multi == null) {
335+
return new String[0];
336+
}
337+
return multi.multiNames();
338+
}
339+
305340
private void processStringOrPrimitive(Class<?> clazz, Map<String, Object> propertiesDefinitionMap, String pathPrefix, Indexable indexable) {
306341
processFieldAnnotation(IndexName.class, new IndexNameAnnotationParser(), propertiesDefinitionMap, pathPrefix, indexable);
307342
processFieldAnnotation(NullValue.class, new NullValueAnnotationParser(), propertiesDefinitionMap, pathPrefix, indexable);

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ private FilterBuilder getAndFilter(List<FilterBuilder> filters) {
412412

413413
@Override
414414
public SearchResponse execute(int from, int size) {
415+
searchRequestBuilder.setTypes(getTypes());
415416
if (prefixField == null) {
416417
if (!fieldSort) {
417418
searchRequestBuilder.addSort(SortBuilders.scoreSort());
@@ -422,6 +423,22 @@ public SearchResponse execute(int from, int size) {
422423
return searchRequestBuilder.setFrom(from).setSize(size).execute().actionGet();
423424
}
424425

426+
protected String[] getTypes() {
427+
if (this.classes == null) {
428+
return null;
429+
}
430+
List<String> types = new ArrayList<String>(this.classes.length);
431+
for (Class<?> clazz : classes) {
432+
if (clazz != null) {
433+
types.add(MappingBuilder.indexTypeFromClass(clazz));
434+
}
435+
}
436+
if (types.isEmpty()) {
437+
return null;
438+
}
439+
return types.toArray(new String[types.size()]);
440+
}
441+
425442
@Override
426443
public SearchRequestBuilder getSearchRequestBuilder() {
427444
return searchRequestBuilder;
@@ -445,7 +462,7 @@ public QueryBuilderHelper fieldSort(String fieldName, boolean desc) {
445462
} else {
446463
sortBuilder.order(SortOrder.ASC);
447464
}
448-
// TODO: chenged to use sortBuilder.unmappedType
465+
// TODO: change to use sortBuilder.unmappedType
449466
sortBuilder.ignoreUnmapped(true);
450467
searchRequestBuilder.addSort(sortBuilder);
451468
return this;

0 commit comments

Comments
 (0)