Skip to content

Commit

Permalink
Make the index property a boolean.
Browse files Browse the repository at this point in the history
With the split of `string` into `text` and `keyword`, the `index` property can
only have two values and should be a boolean.
  • Loading branch information
jpountz committed Jan 27, 2016
1 parent 2aaa5e6 commit 2098608
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ protected void doXContentBody(XContentBuilder builder, boolean includeDefaults,
boolean defaultIndexed = defaultFieldType.indexOptions() != IndexOptions.NONE;
if (includeDefaults || indexed != defaultIndexed ||
fieldType().tokenized() != defaultFieldType.tokenized()) {
builder.field("index", indexTokenizeOptionToString(indexed, fieldType().tokenized()));
builder.field("index", indexTokenizeOption(indexed, fieldType().tokenized()));
}
if (includeDefaults || fieldType().stored() != defaultFieldType.stored()) {
builder.field("store", fieldType().stored());
Expand Down Expand Up @@ -495,14 +495,9 @@ public static String termVectorOptionsToString(FieldType fieldType) {
}
}

protected static String indexTokenizeOptionToString(boolean indexed, boolean tokenized) {
if (!indexed) {
return "no";
} else if (tokenized) {
return "analyzed";
} else {
return "not_analyzed";
}
/* Only protected so that string can override it */
protected Object indexTokenizeOption(boolean indexed, boolean tokenized) {
return indexed;
}

protected boolean hasCustomFieldDataSettings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,27 @@ public static class TypeParser implements Mapper.TypeParser {
@Override
public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext parserContext) throws MapperParsingException {
StringFieldMapper.Builder builder = stringField(name);
// hack for the fact that string can't just accept true/false for
// the index property and still accepts no/not_analyzed/analyzed
final Object index = node.remove("index");
if (index != null) {
final String normalizedIndex = Strings.toUnderscoreCase(index.toString());
switch (normalizedIndex) {
case "analyzed":
builder.tokenized(true);
node.put("index", true);
break;
case "not_analyzed":
builder.tokenized(false);
node.put("index", true);
break;
case "no":
node.put("index", false);
break;
default:
throw new IllegalArgumentException("Can't parse [index] value [" + index + "], expected [true], [false], [no], [not_analyzed] or [analyzed]");
}
}
parseTextField(builder, name, node, parserContext);
for (Iterator<Map.Entry<String, Object>> iterator = node.entrySet().iterator(); iterator.hasNext();) {
Map.Entry<String, Object> entry = iterator.next();
Expand Down Expand Up @@ -369,6 +390,17 @@ protected void doMerge(Mapper mergeWith, boolean updateAllTypes) {
this.ignoreAbove = ((StringFieldMapper) mergeWith).ignoreAbove;
}

@Override
protected String indexTokenizeOption(boolean indexed, boolean tokenized) {
if (!indexed) {
return "no";
} else if (tokenized) {
return "analyzed";
} else {
return "not_analyzed";
}
}

@Override
protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException {
super.doXContentBody(builder, includeDefaults, params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public static void parseField(FieldMapper.Builder builder, String name, Map<Stri
builder.store(parseStore(name, propNode.toString(), parserContext));
iterator.remove();
} else if (propName.equals("index")) {
parseIndex(name, propNode.toString(), builder);
builder.index(parseIndex(name, propNode.toString(), parserContext));
iterator.remove();
} else if (propName.equals(DOC_VALUES)) {
builder.docValues(nodeBooleanValue(propNode, parserContext));
Expand Down Expand Up @@ -328,18 +328,29 @@ public static void parseTermVector(String fieldName, String termVector, FieldMap
}
}

public static void parseIndex(String fieldName, String index, FieldMapper.Builder builder) throws MapperParsingException {
index = Strings.toUnderscoreCase(index);
if ("no".equals(index)) {
builder.index(false);
} else if ("not_analyzed".equals(index)) {
builder.index(true);
builder.tokenized(false);
} else if ("analyzed".equals(index)) {
builder.index(true);
builder.tokenized(true);
public static boolean parseIndex(String fieldName, String index, Mapper.TypeParser.ParserContext parserContext) throws MapperParsingException {
if (parserContext.indexVersionCreated().onOrAfter(Version.V_3_0_0)) {
switch (index) {
case "true":
return true;
case "false":
return false;
default:
throw new IllegalArgumentException("Can't parse [index] value [" + index + "], expected [true] or [false]");
}
} else {
throw new MapperParsingException("wrong value for index [" + index + "] for field [" + fieldName + "]");
final String normalizedIndex = Strings.toUnderscoreCase(index);
switch (normalizedIndex) {
case "true":
case "not_analyzed":
case "analyzed":
return true;
case "false":
case "no":
return false;
default:
throw new IllegalArgumentException("Can't parse [index] value [" + index + "], expected [true], [false], [no], [not_analyzed] or [analyzed]");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.DocValuesType;
import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.IndexableField;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.metadata.IndexMetaData;
Expand Down Expand Up @@ -354,6 +355,60 @@ public void testBwCompatDocValues() throws Exception {
assertEquals(DocValuesType.NONE, SimpleStringMappingTests.docValuesType(doc, "double2"));
}

public void testUnIndex() throws IOException {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties")
.startObject("int")
.field("type", "integer")
.field("index", false)
.endObject()
.startObject("double")
.field("type", "double")
.field("index", false)
.endObject()
.endObject()
.endObject().endObject().string();

DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));

assertEquals("{\"type\":{\"properties\":{\"double\":{\"type\":\"double\",\"index\":false},\"int\":{\"type\":\"integer\",\"index\":false}}}}",
defaultMapper.mapping().toString());

ParsedDocument parsedDoc = defaultMapper.parse("test", "type", "1", XContentFactory.jsonBuilder()
.startObject()
.field("int", "1234")
.field("double", "1234")
.endObject()
.bytes());
final Document doc = parsedDoc.rootDoc();
for (IndexableField field : doc.getFields("int")) {
assertEquals(IndexOptions.NONE, field.fieldType().indexOptions());
}
for (IndexableField field : doc.getFields("double")) {
assertEquals(IndexOptions.NONE, field.fieldType().indexOptions());
}
}

public void testBwCompatIndex() throws IOException {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties")
.startObject("int")
.field("type", "integer")
.field("index", "no")
.endObject()
.startObject("double")
.field("type", "double")
.field("index", "not_analyzed")
.endObject()
.endObject()
.endObject().endObject().string();

Settings oldSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_2_2_0).build();
DocumentMapper defaultMapper = createIndex("test", oldSettings).mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
assertEquals("{\"type\":{\"properties\":{\"double\":{\"type\":\"double\"},\"int\":{\"type\":\"integer\",\"index\":false}}}}",
defaultMapper.mapping().toString());
}

public void testDocValuesOnNested() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void testSimpleGetFieldMappingsWithDefaults() throws Exception {

GetFieldMappingsResponse response = client().admin().indices().prepareGetFieldMappings().setFields("num", "field1", "obj.subfield").includeDefaults(true).get();

assertThat((Map<String, Object>) response.fieldMappings("test", "type", "num").sourceAsMap().get("num"), hasEntry("index", (Object) "not_analyzed"));
assertThat((Map<String, Object>) response.fieldMappings("test", "type", "num").sourceAsMap().get("num"), hasEntry("index", Boolean.TRUE));
assertThat((Map<String, Object>) response.fieldMappings("test", "type", "num").sourceAsMap().get("num"), hasEntry("type", (Object) "long"));
assertThat((Map<String, Object>) response.fieldMappings("test", "type", "field1").sourceAsMap().get("field1"), hasEntry("index", (Object) "analyzed"));
assertThat((Map<String, Object>) response.fieldMappings("test", "type", "field1").sourceAsMap().get("field1"), hasEntry("type", (Object) "string"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public Settings indexSettings() {
public void setupSuiteScopeCluster() throws Exception {
assertAcked(prepareCreate("test").setSettings(SETTING_NUMBER_OF_SHARDS, 5, SETTING_NUMBER_OF_REPLICAS, 0).addMapping("fact",
"_routing", "required=true", "routing_id", "type=string,index=not_analyzed", "fact_category",
"type=integer,index=not_analyzed", "description", "type=string,index=analyzed"));
"type=integer,index=true", "description", "type=string,index=analyzed"));
createIndex("idx_unmapped");

ensureGreen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"test2": {
"type": "token_count",
"index": "not_analyzed",
"index": true,
"store": true,
"analyzer": "simple"
}
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/mapping/types/boolean.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ The following parameters are accepted by `boolean` fields:

<<mapping-index,`index`>>::

Should the field be searchable? Accepts `not_analyzed` (default) and `no`.
Should the field be searchable? Accepts `true` (default) and `false`.

<<null-value,`null_value`>>::

Expand Down
4 changes: 2 additions & 2 deletions docs/reference/mapping/types/date.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ The following parameters are accepted by `date` fields:

Whether or not the field value should be included in the
<<mapping-all-field,`_all`>> field? Accepts `true` or `false`. Defaults
to `false` if <<mapping-index,`index`>> is set to `no`, or if a parent
to `false` if <<mapping-index,`index`>> is set to `false`, or if a parent
<<object,`object`>> field sets `include_in_all` to `false`.
Otherwise defaults to `true`.

<<mapping-index,`index`>>::

Should the field be searchable? Accepts `not_analyzed` (default) and `no`.
Should the field be searchable? Accepts `true` (default) and `false`.

<<null-value,`null_value`>>::

Expand Down
4 changes: 2 additions & 2 deletions docs/reference/mapping/types/ip.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ The following parameters are accepted by `ip` fields:

Whether or not the field value should be included in the
<<mapping-all-field,`_all`>> field? Accepts `true` or `false`. Defaults
to `false` if <<mapping-index,`index`>> is set to `no`, or if a parent
to `false` if <<mapping-index,`index`>> is set to `false`, or if a parent
<<object,`object`>> field sets `include_in_all` to `false`.
Otherwise defaults to `true`.

<<mapping-index,`index`>>::

Should the field be searchable? Accepts `not_analyzed` (default) and `no`.
Should the field be searchable? Accepts `true` (default) and `false`.

<<null-value,`null_value`>>::

Expand Down
4 changes: 2 additions & 2 deletions docs/reference/mapping/types/numeric.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ The following parameters are accepted by numeric types:

Whether or not the field value should be included in the
<<mapping-all-field,`_all`>> field? Accepts `true` or `false`. Defaults
to `false` if <<mapping-index,`index`>> is set to `no`, or if a parent
to `false` if <<mapping-index,`index`>> is set to `false`, or if a parent
<<object,`object`>> field sets `include_in_all` to `false`.
Otherwise defaults to `true`.

<<mapping-index,`index`>>::

Should the field be searchable? Accepts `not_analyzed` (default) and `no`.
Should the field be searchable? Accepts `true` (default) and `false`.

<<null-value,`null_value`>>::

Expand Down
6 changes: 6 additions & 0 deletions docs/reference/migration/migrate_3_0.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ float by default instead of a double. The reasoning is that floats should be
more than enough for most cases but would decrease storage requirements
significantly.

==== `index` property

On all types but `string`, the `index` property now only accepts `true`/`false`
instead of `not_analyzed`/`no`. The `string` field still accepts
`analyzed`/`not_analyzed`/`no`.

==== `_source`'s `format` option

The `_source` mapping does not support the `format` option anymore. This option
Expand Down

0 comments on commit 2098608

Please sign in to comment.