public static class StringFieldBuilder {
private final NewIndexType indexType;
private final String fieldName;
- private boolean sortable = false, wordSearch = false, gramSearch = false, docValues = false, disableSearch = false;
+ private boolean multiField = false, sortable = false, wordSearch = false, gramSearch = false, docValues = false, disableSearch = false;
private StringFieldBuilder(NewIndexType indexType, String fieldName) {
this.indexType = indexType;
return this;
}
+ /**
+ * Prepare the structure to add sub-fields
+ */
+ public StringFieldBuilder enableMultiField() {
+ this.multiField = true;
+ return this;
+ }
+
/**
* Create an inner-field named "sort" with analyzer "sortable"
*/
public StringFieldBuilder enableSorting() {
+ this.enableMultiField();
this.sortable = true;
return this;
}
* Create an inner-field named "words" with analyzer "words"
*/
public StringFieldBuilder enableWordSearch() {
+ this.enableMultiField();
this.wordSearch = true;
return this;
}
* Create a inner-field named "grams" with analyzer "grams"
*/
public StringFieldBuilder enableGramSearch() {
+ this.enableMultiField();
this.gramSearch = true;
return this;
}
public void build() {
validate();
Map<String, Object> hash = new TreeMap<>();
- if (wordSearch || sortable || gramSearch) {
+ if (multiField) {
hash.put("type", "multi_field");
Map<String, Object> multiFields = new TreeMap<>();
NewIndex.NewIndexType mapping = index.createType(TYPE_USER);
mapping.setAttribute("_id", ImmutableMap.of("path", FIELD_LOGIN));
- mapping.stringFieldBuilder(FIELD_LOGIN).enableGramSearch().build();
+ mapping.stringFieldBuilder(FIELD_LOGIN).enableMultiField().build();
addSubSearchField(mapping, FIELD_LOGIN);
- mapping.stringFieldBuilder(FIELD_NAME).enableGramSearch().build();
+ mapping.stringFieldBuilder(FIELD_NAME).enableMultiField().build();
addSubSearchField(mapping, FIELD_NAME);
mapping.stringFieldBuilder(FIELD_EMAIL).enableSorting().build();
mapping.createDateTimeField(FIELD_CREATED_AT);
.enableWordSearch()
.enableSorting()
.build();
+ mapping.stringFieldBuilder("just_multi_field").enableMultiField().build();
Map<String, Object> props = (Map) mapping.getProperty("basic_field");
assertThat(props.get("type")).isEqualTo("string");
assertThat(props.get("type")).isEqualTo("multi_field");
// no need to test values, it's not the scope of this test
assertThat((Map) props.get("fields")).isNotEmpty();
+
+ props = (Map) mapping.getProperty("just_multi_field");
+ assertThat(props.get("type")).isEqualTo("multi_field");
+ assertThat((Map) props.get("fields")).isNotEmpty();
}
@Test