Skip to content

Commit 4b23d99

Browse files
author
Luc Boutier
committed
Allow to register filters for abstract classes.
1 parent b773d8b commit 4b23d99

File tree

4 files changed

+76
-75
lines changed

4 files changed

+76
-75
lines changed

elasticsearch-annotations/src/main/java/org/elasticsearch/annotation/TypeName.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
* @author luc boutier
1212
*/
1313
@Retention(RetentionPolicy.RUNTIME)
14-
@Target({ ElementType.FIELD, ElementType.METHOD })
14+
@Target({ ElementType.TYPE })
1515
public @interface TypeName {
16-
/**
17-
* The name of the object type in elastic search.
18-
*
19-
* @return The name of the object type in elastic search.
20-
*/
21-
String typeName();
16+
/**
17+
* The name of the object type in elastic search.
18+
*
19+
* @return The name of the object type in elastic search.
20+
*/
21+
String typeName();
2222
}

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
import java.beans.IntrospectionException;
44
import java.io.IOException;
5-
import java.util.ArrayList;
6-
import java.util.HashMap;
7-
import java.util.List;
8-
import java.util.Map;
9-
import java.util.Set;
5+
import java.lang.reflect.Modifier;
6+
import java.util.*;
107

118
import org.elasticsearch.annotation.ESAll;
129
import org.elasticsearch.annotation.ESObject;
@@ -156,16 +153,18 @@ private void initialize(String packageName) throws IntrospectionException, JsonG
156153
}
157154
}
158155

159-
private void parseClassMapping(Class<?> clazz, String pathPrefix) throws IntrospectionException, JsonGenerationException, JsonMappingException, IOException {
156+
public void parseClassMapping(Class<?> clazz, String pathPrefix) throws IntrospectionException, JsonGenerationException, JsonMappingException, IOException {
160157
ESObject esObject = AnnotationScanner.getAnnotation(ESObject.class, clazz);
161158
ESAll esAll = AnnotationScanner.getAnnotation(ESAll.class, clazz);
162159

163-
TypeName typeName = clazz.getAnnotation(TypeName.class);
164-
String typeNameStr;
165-
if (typeName == null) {
166-
typeNameStr = MappingBuilder.indexTypeFromClass(clazz);
167-
} else {
168-
typeNameStr = typeName.typeName();
160+
String typeNameStr = null;
161+
if (!Modifier.isAbstract(clazz.getModifiers())) {
162+
TypeName typeName = clazz.getAnnotation(TypeName.class);
163+
if (typeName == null) {
164+
typeNameStr = MappingBuilder.indexTypeFromClass(clazz);
165+
} else {
166+
typeNameStr = typeName.typeName();
167+
}
169168
}
170169

171170
Map<String, Object> typeDefinitionMap = new HashMap<String, Object>();
@@ -176,8 +175,9 @@ private void parseClassMapping(Class<?> clazz, String pathPrefix) throws Introsp
176175

177176
typeDefinitionMap.put(typeNameStr, classDefinitionMap);
178177

179-
if(esAll!=null) {
180-
classDefinitionMap.put("_all", MapUtil.getMap(new String[]{"enabled", "analyzer", "store"}, new Object[]{true, esAll.analyser(), esAll.store()}));
178+
if (esAll != null) {
179+
classDefinitionMap.put("_all",
180+
MapUtil.getMap(new String[] { "enabled", "analyzer", "store" }, new Object[] { true, esAll.analyser(), esAll.store() }));
181181
} else {
182182
classDefinitionMap.put("_all", MapUtil.getMap("enabled", esObject.all()));
183183
}
@@ -187,8 +187,11 @@ private void parseClassMapping(Class<?> clazz, String pathPrefix) throws Introsp
187187
this.fieldsMappingBuilder.parseFieldMappings(clazz, classDefinitionMap, facetFields, filteredFields, fetchContexts, pathPrefix);
188188

189189
ObjectMapper mapper = new ObjectMapper();
190-
String jsonMapping = mapper.writeValueAsString(typeDefinitionMap);
191-
this.classesMappings.put(clazz.getName(), jsonMapping);
190+
if (typeNameStr != null) {
191+
String jsonMapping = mapper.writeValueAsString(typeDefinitionMap);
192+
this.classesMappings.put(clazz.getName(), jsonMapping);
193+
}
194+
// abstract types are not registered but can be use for global queries over indexes.
192195
this.typeByClassName.put(clazz.getName(), typeNameStr);
193196
this.facetByClassName.put(clazz.getName(), facetFields);
194197
this.filtersByClassName.put(clazz.getName(), filteredFields);

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,7 @@ public QueryBuilderHelper filters(Map<String, String[]> filters, FilterBuilder..
310310
public QueryBuilderHelper filters(Map<String, String[]> filters, Map<String, FilterValuesStrategy> filterStrategies, FilterBuilder... customFilters) {
311311
this.filters = filters;
312312
if (classes != null && classes.length > 0) {
313-
QueryBuilder filteredQueryBuilder = null;
314-
for (Class<?> clazz : classes) {
315-
filteredQueryBuilder = addFilters(this.queryBuilder, clazz, filters, filterStrategies, customFilters);
316-
}
313+
QueryBuilder filteredQueryBuilder = addFilters(this.queryBuilder, classes[0], filters, filterStrategies, customFilters);
317314
if (filteredQueryBuilder != null) {
318315
this.queryBuilder = filteredQueryBuilder;
319316
}

elasticsearch-mapping/src/main/java/org/elasticsearch/util/AnnotationScanner.java

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,59 +17,60 @@
1717
* @author Luc Boutier
1818
*/
1919
public final class AnnotationScanner {
20-
private static final ESLogger LOGGER = Loggers.getLogger(MappingBuilder.class);
20+
private static final ESLogger LOGGER = Loggers.getLogger(MappingBuilder.class);
2121

22-
/** Utility classes should have private constructor. */
23-
private AnnotationScanner() {}
22+
/** Utility classes should have private constructor. */
23+
private AnnotationScanner() {
24+
}
2425

25-
/**
26-
* Scan a package to find classes that have the given annotation.
27-
*
28-
* @param packageRoot The package to scan.
29-
* @param anno Annotation that should be on the class that we are interested in.
30-
* @return A set of classes that have the annotation.
31-
*/
32-
public static Set<Class<?>> scan(String packageRoot, Class<? extends Annotation> anno) {
33-
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
26+
/**
27+
* Scan a package to find classes that have the given annotation.
28+
*
29+
* @param packageRoot The package to scan.
30+
* @param anno Annotation that should be on the class that we are interested in.
31+
* @return A set of classes that have the annotation.
32+
*/
33+
public static Set<Class<?>> scan(String packageRoot, Class<? extends Annotation> anno) {
34+
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
3435

35-
AnnotationTypeFilter filter = new AnnotationTypeFilter(anno);
36-
scanner.addIncludeFilter(filter);
37-
Set<BeanDefinition> beanSet = scanner.findCandidateComponents(packageRoot);
36+
AnnotationTypeFilter filter = new AnnotationTypeFilter(anno);
37+
scanner.addIncludeFilter(filter);
38+
Set<BeanDefinition> beanSet = scanner.findCandidateComponents(packageRoot);
3839

39-
Set<Class<?>> classSet = new HashSet<Class<?>>();
40-
for (BeanDefinition beanDef : beanSet) {
41-
LOGGER.debug("found candidate bean = " + beanDef.getBeanClassName());
40+
Set<Class<?>> classSet = new HashSet<Class<?>>();
41+
for (BeanDefinition beanDef : beanSet) {
42+
LOGGER.debug("found candidate bean = " + beanDef.getBeanClassName());
4243

43-
Class<?> clazz;
44-
try {
45-
clazz = Class.forName(beanDef.getBeanClassName(), true, Thread.currentThread().getContextClassLoader());
46-
if (clazz.isAnnotationPresent(anno)) {
47-
LOGGER.debug("found annotated class, " + clazz.getName());
48-
classSet.add(clazz);
49-
}
50-
} catch (ClassNotFoundException e) {
51-
LOGGER.error("exception while scanning classpath for annotated classes", e);
52-
}
53-
}
44+
Class<?> clazz;
45+
try {
46+
clazz = Class.forName(beanDef.getBeanClassName(), true, Thread.currentThread().getContextClassLoader());
47+
if (clazz.isAnnotationPresent(anno)) {
48+
LOGGER.debug("found annotated class, " + clazz.getName());
49+
classSet.add(clazz);
50+
}
51+
} catch (ClassNotFoundException e) {
52+
LOGGER.error("exception while scanning classpath for annotated classes", e);
53+
}
54+
}
5455

55-
return classSet;
56-
}
56+
return classSet;
57+
}
5758

58-
/**
59-
* Get an annotation on the class or one of the super classes.
60-
*
61-
* @param annotationClass The annotation to get.
62-
* @param clazz The class on which to search for the annotation.
63-
* @return The annotation for this class or null if not found neither on the class or one of it's super class.
64-
*/
65-
public static <T extends Annotation> T getAnnotation(Class<T> annotationClass, Class<?> clazz) {
66-
if (clazz == Object.class) {
67-
return null;
68-
}
69-
T annotationInstance = clazz.getAnnotation(annotationClass);
70-
if (annotationInstance == null) {
71-
return getAnnotation(annotationClass, clazz.getSuperclass());
72-
}
73-
return annotationInstance;
74-
}
59+
/**
60+
* Get an annotation on the class or one of the super classes.
61+
*
62+
* @param annotationClass The annotation to get.
63+
* @param clazz The class on which to search for the annotation.
64+
* @return The annotation for this class or null if not found neither on the class or one of it's super class.
65+
*/
66+
public static <T extends Annotation> T getAnnotation(Class<T> annotationClass, Class<?> clazz) {
67+
if (clazz == Object.class) {
68+
return null;
69+
}
70+
T annotationInstance = clazz.getAnnotation(annotationClass);
71+
if (annotationInstance == null) {
72+
return getAnnotation(annotationClass, clazz.getSuperclass());
73+
}
74+
return annotationInstance;
75+
}
7576
}

0 commit comments

Comments
 (0)