Skip to content

Commit 3591500

Browse files
author
Luc Boutier
committed
Add advanced generators for facet sub-paths generation
1 parent 2a00425 commit 3591500

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.elasticsearch.annotation.query;
2+
3+
/**
4+
* Created by lucboutier on 17/03/2016.
5+
*/
6+
public interface IPathGenerator {
7+
/**
8+
* Generate paths to be used from the one set on the annotation.
9+
*
10+
* @param annotationPaths The paths configured on the annotation.
11+
* @return A String array to override the annotationPaths
12+
*/
13+
String[] getPaths(String[] annotationPaths);
14+
15+
class DEFAULT implements IPathGenerator {
16+
@Override
17+
public String[] getPaths(String[] annotationPaths) {
18+
return annotationPaths;
19+
}
20+
}
21+
}

elasticsearch-annotations/src/main/java/org/elasticsearch/annotation/query/TermsFacet.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
*/
2828
String[] paths() default "";
2929

30+
/**
31+
* Optional paths generator to override the paths property of the annotation for complex use-case or reflection etc.
32+
*
33+
* @return the path generator.
34+
*/
35+
Class<? extends IPathGenerator> pathGenerator() default IPathGenerator.DEFAULT.class;
36+
3037
/**
3138
* The number of terms to return
3239
*

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,22 @@ private void processFilterAnnotation(List<IFilterBuilderHelper> classFilters, St
215215
}
216216
}
217217

218-
private void processFacetAnnotation(List<IFacetBuilderHelper> classFacets, List<IFilterBuilderHelper> classFilters, String esFieldName, Indexable indexable) {
218+
private void processFacetAnnotation(List<IFacetBuilderHelper> classFacets, List<IFilterBuilderHelper> classFilters, String esFieldName,
219+
Indexable indexable) {
219220
TermsFacet termsFacet = indexable.getAnnotation(TermsFacet.class);
220221
if (termsFacet != null) {
221222
String[] paths = termsFacet.paths();
223+
if (termsFacet.pathGenerator() != null) {
224+
// create an instance of the generator
225+
try {
226+
IPathGenerator generator = termsFacet.pathGenerator().newInstance();
227+
paths = generator.getPaths(paths);
228+
} catch (InstantiationException e) {
229+
e.printStackTrace(); // TODO better exception handling
230+
} catch (IllegalAccessException e) {
231+
e.printStackTrace(); // TODO better exception handling
232+
}
233+
}
222234
for (String path : paths) {
223235
path = path.trim();
224236
boolean isAnalyzed = isAnalyzed(indexable);

0 commit comments

Comments
 (0)