Required to accept long string values on non-searchable fields, for example sourceLines/sourceLine/highlighting
public static class StringFieldBuilder {
private final NewIndexType indexType;
private final String fieldName;
- private boolean sortable = false, wordSearch = false, gramSearch = false, docValues = false;
+ private boolean sortable = false, wordSearch = false, gramSearch = false, docValues = false, disableSearch = false;
private StringFieldBuilder(NewIndexType indexType, String fieldName) {
this.indexType = indexType;
return this;
}
+ /**
+ * "index: no" -> Don’t index this field at all. This field will not be searchable.
+ * By default field is "not_analyzed": it is searchable, but index the value exactly
+ * as specified.
+ */
+ public StringFieldBuilder disableSearch() {
+ this.disableSearch = true;
+ return this;
+ }
+
public void build() {
validate();
- Map<String, Object> hash = new TreeMap<String, Object>();
+ Map<String, Object> hash = new TreeMap<>();
if (wordSearch || sortable || gramSearch) {
hash.put("type", "multi_field");
- Map<String, Object> multiFields = new TreeMap<String, Object>();
+ Map<String, Object> multiFields = new TreeMap<>();
if (sortable) {
multiFields.put(IndexField.SORT_SUFFIX, ImmutableSortedMap.of(
} else {
hash.putAll(ImmutableMap.of(
"type", "string",
- "index", "not_analyzed",
+ "index", disableSearch ? "no" : "not_analyzed",
"omit_norms", "true",
"doc_values", docValues));
}
private void validate() {
if (docValues && (gramSearch || wordSearch || sortable)) {
- throw new IllegalStateException("Doc values are not supported on analyzed strings");
+ throw new IllegalStateException("Doc values are not supported on analyzed strings of field: " + fieldName);
+ }
+ if (disableSearch && (gramSearch || wordSearch || sortable)) {
+ throw new IllegalStateException("Can't mix searchable and non-searchable arguments on field: " + fieldName);
}
}
}
issueMapping.setAttribute("_routing", ImmutableMap.of("required", true, "path", FIELD_ISSUE_PROJECT_UUID));
issueMapping.stringFieldBuilder(FIELD_ISSUE_ACTION_PLAN).build();
issueMapping.stringFieldBuilder(FIELD_ISSUE_ASSIGNEE).enableSorting().build();
- issueMapping.stringFieldBuilder(FIELD_ISSUE_ATTRIBUTES).build();
+ issueMapping.stringFieldBuilder(FIELD_ISSUE_ATTRIBUTES).disableSearch().build();
issueMapping.stringFieldBuilder(FIELD_ISSUE_AUTHOR_LOGIN).build();
issueMapping.stringFieldBuilder(FIELD_ISSUE_COMPONENT_UUID).build();
issueMapping.createLongField(FIELD_ISSUE_DEBT);
sourceLineMapping.stringFieldBuilder(FIELD_SCM_REVISION).build();
sourceLineMapping.stringFieldBuilder(FIELD_SCM_AUTHOR).build();
sourceLineMapping.createDateTimeField(FIELD_SCM_DATE);
- sourceLineMapping.stringFieldBuilder(FIELD_HIGHLIGHTING).build();
- sourceLineMapping.stringFieldBuilder(FIELD_SOURCE).build();
+ sourceLineMapping.stringFieldBuilder(FIELD_HIGHLIGHTING).disableSearch().build();
+ sourceLineMapping.stringFieldBuilder(FIELD_SOURCE).disableSearch().build();
sourceLineMapping.createIntegerField(FIELD_UT_LINE_HITS);
sourceLineMapping.createIntegerField(FIELD_UT_CONDITIONS);
sourceLineMapping.createIntegerField(FIELD_UT_COVERED_CONDITIONS);
sourceLineMapping.createIntegerField(FIELD_OVERALL_LINE_HITS);
sourceLineMapping.createIntegerField(FIELD_OVERALL_CONDITIONS);
sourceLineMapping.createIntegerField(FIELD_OVERALL_COVERED_CONDITIONS);
- sourceLineMapping.stringFieldBuilder(FIELD_SYMBOLS).build();
+ sourceLineMapping.stringFieldBuilder(FIELD_SYMBOLS).disableSearch().build();
sourceLineMapping.createShortField(FIELD_DUPLICATIONS);
sourceLineMapping.createDateTimeField(BaseNormalizer.UPDATED_AT_FIELD);
}
NewIndex index = new NewIndex("issues");
NewIndex.NewIndexType mapping = index.createType("issue");
mapping.stringFieldBuilder("basic_field").build();
+ mapping.stringFieldBuilder("not_searchable_field").disableSearch().build();
mapping.stringFieldBuilder("all_capabilities_field")
.enableGramSearch()
.enableWordSearch()
assertThat(props.get("index")).isEqualTo("not_analyzed");
assertThat(props.get("fields")).isNull();
+ props = (Map) mapping.getProperty("not_searchable_field");
+ assertThat(props.get("type")).isEqualTo("string");
+ assertThat(props.get("index")).isEqualTo("no");
+ assertThat(props.get("fields")).isNull();
+
props = (Map) mapping.getProperty("all_capabilities_field");
assertThat(props.get("type")).isEqualTo("multi_field");
// no need to test values, it's not the scope of this test
mapping.stringFieldBuilder("the_doc_value").enableGramSearch().docValues().build();
fail();
} catch (IllegalStateException e) {
- assertThat(e).hasMessage("Doc values are not supported on analyzed strings");
+ assertThat(e).hasMessage("Doc values are not supported on analyzed strings of field: the_doc_value");
+ }
+ }
+
+ @Test
+ public void do_not_disable_search_on_searchable_fields() throws Exception {
+ NewIndex index = new NewIndex("issues");
+ NewIndex.NewIndexType mapping = index.createType("issue");
+ try {
+ mapping.stringFieldBuilder("my_field").enableGramSearch().disableSearch().build();
+ fail();
+ } catch (IllegalStateException e) {
+ assertThat(e).hasMessage("Can't mix searchable and non-searchable arguments on field: my_field");
}
}
}