Skip to content

Commit 81cc194

Browse files
author
Luc Boutier
committed
Add support for multi-string mapping.
1 parent 6312520 commit 81cc194

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,6 @@
128128
* @return
129129
*/
130130
int positionOffsetGap() default 0;
131+
132+
131133
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.elasticsearch.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* This annotation allow to specifies multiple StringField on a unique field to map it on multiple es fields.
10+
*/
11+
@Retention(RetentionPolicy.RUNTIME)
12+
@Target({ ElementType.FIELD, ElementType.METHOD })
13+
public @interface StringFieldMulti {
14+
/** The main string field to manage. */
15+
StringField main();
16+
17+
/** Name of the multi */
18+
String[] multiNames();
19+
20+
/** The multiple fields to add to the mapping. */
21+
StringField[] multi();
22+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ private void processStringOrPrimitive(Class<?> clazz, Map<String, Object> proper
308308

309309
// String field annotations.
310310
processFieldAnnotation(StringField.class, new StringFieldAnnotationParser(), propertiesDefinitionMap, pathPrefix, indexable);
311+
processFieldAnnotation(StringFieldMulti.class, new StringFieldMultiAnnotationParser(), propertiesDefinitionMap, pathPrefix, indexable);
311312
processFieldAnnotation(Analyser.class, new AnalyserAnnotationParser(), propertiesDefinitionMap, pathPrefix, indexable);
312313
processFieldAnnotation(IndexAnalyser.class, new IndexAnalyserAnnotationParser(), propertiesDefinitionMap, pathPrefix, indexable);
313314
processFieldAnnotation(SearchAnalyser.class, new SearchAnalyserAnnotationParser(), propertiesDefinitionMap, pathPrefix, indexable);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ public void parseAnnotation(StringField annotation, Map<String, Object> fieldDef
5959
fieldDefinition.put("ignore_above", annotation.ignoreAbove());
6060
}
6161

62-
annotation.positionOffsetGap();
62+
// FIXME annotation.positionOffsetGap();
6363
}
6464
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.elasticsearch.mapping.parser;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.elasticsearch.annotation.StringField;
7+
import org.elasticsearch.annotation.StringFieldMulti;
8+
import org.elasticsearch.common.collect.Maps;
9+
import org.elasticsearch.common.logging.ESLogger;
10+
import org.elasticsearch.common.logging.Loggers;
11+
import org.elasticsearch.mapping.*;
12+
13+
/**
14+
* Parse a {@link StringField} annotation.
15+
*
16+
* @author luc boutier
17+
*/
18+
public class StringFieldMultiAnnotationParser implements IPropertyAnnotationParser<StringFieldMulti> {
19+
private static final ESLogger LOGGER = Loggers.getLogger(MappingBuilder.class);
20+
private StringFieldAnnotationParser wrapped = new StringFieldAnnotationParser();
21+
22+
public void parseAnnotation(StringFieldMulti annotation, Map<String, Object> fieldDefinition, String pathPrefix, Indexable indexable) {
23+
if (fieldDefinition.get("type") != null) {
24+
LOGGER.info("Overriding mapping for field {} for class {} was defined as type {}", indexable.getName(), indexable.getDeclaringClassName(),
25+
fieldDefinition.get("type"));
26+
fieldDefinition.clear();
27+
}
28+
29+
StringField mainStringField = annotation.main();
30+
wrapped.parseAnnotation(mainStringField, fieldDefinition, pathPrefix, indexable);
31+
32+
Map<String, Object> multiFields = Maps.newHashMap();
33+
34+
for (int i = 0; i < annotation.multi().length; i++) {
35+
StringField multi = annotation.multi()[i];
36+
Map<String, Object> multiFieldDefinition = Maps.newHashMap();
37+
wrapped.parseAnnotation(multi, multiFieldDefinition, pathPrefix, indexable);
38+
multiFields.put(annotation.multiNames()[i], multiFieldDefinition);
39+
}
40+
fieldDefinition.put("fields", multiFields);
41+
}
42+
}

0 commit comments

Comments
 (0)