Search in sources :

Example 46 with IndexService

use of org.elasticsearch.index.IndexService in project elasticsearch by elastic.

the class DynamicMappingTests method testIntroduceTwoFields.

public void testIntroduceTwoFields() throws Exception {
    IndexService indexService = createIndex("test");
    DocumentMapperParser parser = indexService.mapperService().documentMapperParser();
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string();
    DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
    assertEquals(mapping, serialize(mapper));
    Mapper update = parse(mapper, parser, XContentFactory.jsonBuilder().startObject().field("foo", "bar").field("bar", "baz").endObject());
    assertNotNull(update);
    // original mapping not modified
    assertEquals(mapping, serialize(mapper));
    // but we have an update
    assertEquals(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("bar").field("type", "text").startObject("fields").startObject("keyword").field("type", "keyword").field("ignore_above", 256).endObject().endObject().endObject().startObject("foo").field("type", "text").startObject("fields").startObject("keyword").field("type", "keyword").field("ignore_above", 256).endObject().endObject().endObject().endObject().endObject().endObject().string(), serialize(update));
}
Also used : IndexService(org.elasticsearch.index.IndexService) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent)

Example 47 with IndexService

use of org.elasticsearch.index.IndexService in project elasticsearch by elastic.

the class DynamicMappingTests method testDateDetectionInheritsFormat.

public void testDateDetectionInheritsFormat() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startArray("dynamic_date_formats").value("yyyy-MM-dd").endArray().startArray("dynamic_templates").startObject().startObject("dates").field("match_mapping_type", "date").field("match", "*2").startObject("mapping").endObject().endObject().endObject().startObject().startObject("dates").field("match_mapping_type", "date").field("match", "*3").startObject("mapping").field("format", "yyyy-MM-dd||epoch_millis").endObject().endObject().endObject().endArray().endObject().endObject().string();
    IndexService index = createIndex("test");
    client().admin().indices().preparePutMapping("test").setType("type").setSource(mapping, XContentType.JSON).get();
    DocumentMapper defaultMapper = index.mapperService().documentMapper("type");
    ParsedDocument doc = defaultMapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("date1", "2016-11-20").field("date2", "2016-11-20").field("date3", "2016-11-20").endObject().bytes());
    assertNotNull(doc.dynamicMappingsUpdate());
    assertAcked(client().admin().indices().preparePutMapping("test").setType("type").setSource(doc.dynamicMappingsUpdate().toString(), XContentType.JSON).get());
    defaultMapper = index.mapperService().documentMapper("type");
    DateFieldMapper dateMapper1 = (DateFieldMapper) defaultMapper.mappers().smartNameFieldMapper("date1");
    DateFieldMapper dateMapper2 = (DateFieldMapper) defaultMapper.mappers().smartNameFieldMapper("date2");
    DateFieldMapper dateMapper3 = (DateFieldMapper) defaultMapper.mappers().smartNameFieldMapper("date3");
    // inherited from dynamic date format
    assertEquals("yyyy-MM-dd", dateMapper1.fieldType().dateTimeFormatter().format());
    // inherited from dynamic date format since the mapping in the template did not specify a format
    assertEquals("yyyy-MM-dd", dateMapper2.fieldType().dateTimeFormatter().format());
    // not inherited from the dynamic date format since the template defined an explicit format
    assertEquals("yyyy-MM-dd||epoch_millis", dateMapper3.fieldType().dateTimeFormatter().format());
}
Also used : IndexService(org.elasticsearch.index.IndexService)

Example 48 with IndexService

use of org.elasticsearch.index.IndexService in project elasticsearch by elastic.

the class DynamicMappingTests method testNumericDetectionDefault.

public void testNumericDetectionDefault() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string();
    IndexService index = createIndex("test");
    client().admin().indices().preparePutMapping("test").setType("type").setSource(mapping, XContentType.JSON).get();
    DocumentMapper defaultMapper = index.mapperService().documentMapper("type");
    ParsedDocument doc = defaultMapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("s_long", "100").field("s_double", "100.0").endObject().bytes());
    assertNotNull(doc.dynamicMappingsUpdate());
    assertAcked(client().admin().indices().preparePutMapping("test").setType("type").setSource(doc.dynamicMappingsUpdate().toString(), XContentType.JSON).get());
    defaultMapper = index.mapperService().documentMapper("type");
    FieldMapper mapper = defaultMapper.mappers().smartNameFieldMapper("s_long");
    assertThat(mapper, instanceOf(TextFieldMapper.class));
    mapper = defaultMapper.mappers().smartNameFieldMapper("s_double");
    assertThat(mapper, instanceOf(TextFieldMapper.class));
}
Also used : IndexService(org.elasticsearch.index.IndexService)

Example 49 with IndexService

use of org.elasticsearch.index.IndexService in project elasticsearch by elastic.

the class TokenCountFieldMapperTests method testEmptyName.

public void testEmptyName() throws IOException {
    IndexService indexService = createIndex("test");
    DocumentMapperParser parser = indexService.mapperService().documentMapperParser();
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("").field("type", "token_count").field("analyzer", "standard").endObject().endObject().endObject().endObject().string();
    // Empty name not allowed in index created after 5.0
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> parser.parse("type", new CompressedXContent(mapping)));
    assertThat(e.getMessage(), containsString("name cannot be empty string"));
}
Also used : IndexService(org.elasticsearch.index.IndexService) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 50 with IndexService

use of org.elasticsearch.index.IndexService in project elasticsearch by elastic.

the class UpdateMappingTests method testNoConflictWhileMergingAndMappingChanged.

private void testNoConflictWhileMergingAndMappingChanged(XContentBuilder mapping, XContentBuilder mappingUpdate, XContentBuilder expectedMapping) throws IOException {
    IndexService indexService = createIndex("test", Settings.builder().build(), "type", mapping);
    // simulate like in MetaDataMappingService#putMapping
    indexService.mapperService().merge("type", new CompressedXContent(mappingUpdate.bytes()), MapperService.MergeReason.MAPPING_UPDATE, false);
    // make sure mappings applied
    CompressedXContent mappingAfterUpdate = indexService.mapperService().documentMapper("type").mappingSource();
    assertThat(mappingAfterUpdate.toString(), equalTo(expectedMapping.string()));
}
Also used : IndexService(org.elasticsearch.index.IndexService) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent)

Aggregations

IndexService (org.elasticsearch.index.IndexService)173 IndexShard (org.elasticsearch.index.shard.IndexShard)53 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)49 IndicesService (org.elasticsearch.indices.IndicesService)38 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)30 Settings (org.elasticsearch.common.settings.Settings)25 ShardId (org.elasticsearch.index.shard.ShardId)24 Index (org.elasticsearch.index.Index)20 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)17 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)16 IOException (java.io.IOException)15 HashMap (java.util.HashMap)15 ArrayList (java.util.ArrayList)14 ClusterState (org.elasticsearch.cluster.ClusterState)13 Map (java.util.Map)12 ClusterService (org.elasticsearch.cluster.service.ClusterService)12 ElasticsearchException (org.elasticsearch.ElasticsearchException)11 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)11 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)11 List (java.util.List)10