Search in sources :

Example 31 with IndexService

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

the class DocumentParserTests method testNoDocumentSent.

public void testNoDocumentSent() throws Exception {
    IndexService indexService = createIndex("test");
    DocumentMapper docMapper = new DocumentMapper.Builder(new RootObjectMapper.Builder("person").add(new ObjectMapper.Builder("name").add(new TextFieldMapper.Builder("first").store(true).index(false))), indexService.mapperService()).build(indexService.mapperService());
    BytesReference json = new BytesArray("".getBytes(StandardCharsets.UTF_8));
    try {
        docMapper.parse("test", "person", "1", json).rootDoc();
        fail("this point is never reached");
    } catch (MapperParsingException e) {
        assertThat(e.getMessage(), equalTo("failed to parse, document is empty"));
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) IndexService(org.elasticsearch.index.IndexService) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder)

Example 32 with IndexService

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

the class MapperServiceTests method testAllEnabled.

public void testAllEnabled() throws Exception {
    IndexService indexService = createIndex("test");
    assertFalse(indexService.mapperService().allEnabled());
    CompressedXContent enabledAll = new CompressedXContent(XContentFactory.jsonBuilder().startObject().startObject("_all").field("enabled", true).endObject().endObject().bytes());
    CompressedXContent disabledAll = new CompressedXContent(XContentFactory.jsonBuilder().startObject().startObject("_all").field("enabled", false).endObject().endObject().bytes());
    Exception e = expectThrows(MapperParsingException.class, () -> indexService.mapperService().merge(MapperService.DEFAULT_MAPPING, enabledAll, MergeReason.MAPPING_UPDATE, random().nextBoolean()));
    assertThat(e.getMessage(), containsString("[_all] is disabled in 6.0"));
}
Also used : IndexService(org.elasticsearch.index.IndexService) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 33 with IndexService

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

the class MapperServiceTests method testOtherDocumentMappersOnlyUpdatedWhenChangingFieldType.

public void testOtherDocumentMappersOnlyUpdatedWhenChangingFieldType() throws IOException {
    IndexService indexService = createIndex("test");
    CompressedXContent simpleMapping = new CompressedXContent(XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("field").field("type", "text").endObject().endObject().endObject().bytes());
    indexService.mapperService().merge("type1", simpleMapping, MergeReason.MAPPING_UPDATE, true);
    DocumentMapper documentMapper = indexService.mapperService().documentMapper("type1");
    indexService.mapperService().merge("type2", simpleMapping, MergeReason.MAPPING_UPDATE, true);
    assertSame(indexService.mapperService().documentMapper("type1"), documentMapper);
    CompressedXContent normsDisabledMapping = new CompressedXContent(XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("field").field("type", "text").field("norms", false).endObject().endObject().endObject().bytes());
    indexService.mapperService().merge("type3", normsDisabledMapping, MergeReason.MAPPING_UPDATE, true);
    assertNotSame(indexService.mapperService().documentMapper("type1"), documentMapper);
}
Also used : IndexService(org.elasticsearch.index.IndexService) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent)

Example 34 with IndexService

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

the class MapperServiceTests method testMergeWithMap.

public void testMergeWithMap() throws Throwable {
    IndexService indexService1 = createIndex("index1");
    MapperService mapperService = indexService1.mapperService();
    Map<String, Map<String, Object>> mappings = new HashMap<>();
    mappings.put(MapperService.DEFAULT_MAPPING, MapperService.parseMapping(xContentRegistry(), "{}"));
    MapperException e = expectThrows(MapperParsingException.class, () -> mapperService.merge(mappings, MergeReason.MAPPING_UPDATE, false));
    assertThat(e.getMessage(), startsWith("Failed to parse mapping [" + MapperService.DEFAULT_MAPPING + "]: "));
    mappings.clear();
    mappings.put("type1", MapperService.parseMapping(xContentRegistry(), "{}"));
    e = expectThrows(MapperParsingException.class, () -> mapperService.merge(mappings, MergeReason.MAPPING_UPDATE, false));
    assertThat(e.getMessage(), startsWith("Failed to parse mapping [type1]: "));
}
Also used : IndexService(org.elasticsearch.index.IndexService) HashMap(java.util.HashMap) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HashMap(java.util.HashMap) Map(java.util.Map)

Example 35 with IndexService

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

the class MapperServiceTests method testIndexIntoDefaultMapping.

public void testIndexIntoDefaultMapping() throws Throwable {
    // 1. test implicit index creation
    ExecutionException e = expectThrows(ExecutionException.class, () -> {
        client().prepareIndex("index1", MapperService.DEFAULT_MAPPING, "1").setSource("{}", XContentType.JSON).execute().get();
    });
    Throwable throwable = ExceptionsHelper.unwrapCause(e.getCause());
    if (throwable instanceof IllegalArgumentException) {
        assertEquals("It is forbidden to index into the default mapping [_default_]", throwable.getMessage());
    } else {
        throw e;
    }
    // 2. already existing index
    IndexService indexService = createIndex("index2");
    e = expectThrows(ExecutionException.class, () -> {
        client().prepareIndex("index1", MapperService.DEFAULT_MAPPING, "2").setSource().execute().get();
    });
    throwable = ExceptionsHelper.unwrapCause(e.getCause());
    if (throwable instanceof IllegalArgumentException) {
        assertEquals("It is forbidden to index into the default mapping [_default_]", throwable.getMessage());
    } else {
        throw e;
    }
    assertFalse(indexService.mapperService().hasMapping(MapperService.DEFAULT_MAPPING));
}
Also used : IndexService(org.elasticsearch.index.IndexService) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

IndexService (org.elasticsearch.index.IndexService)212 IndexShard (org.elasticsearch.index.shard.IndexShard)77 IndicesService (org.elasticsearch.indices.IndicesService)56 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)50 ShardId (org.elasticsearch.index.shard.ShardId)38 Index (org.elasticsearch.index.Index)36 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)33 Settings (org.elasticsearch.common.settings.Settings)31 IOException (java.io.IOException)22 ArrayList (java.util.ArrayList)22 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)22 HashMap (java.util.HashMap)21 Map (java.util.Map)19 ClusterService (org.elasticsearch.cluster.service.ClusterService)19 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)18 ElasticsearchException (org.elasticsearch.ElasticsearchException)17 ClusterState (org.elasticsearch.cluster.ClusterState)16 List (java.util.List)14 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)13 IndexSettings (org.elasticsearch.index.IndexSettings)13