Search in sources :

Example 36 with IndexService

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

the class GenericStoreDynamicTemplateTests method testSimple.

public void testSimple() throws Exception {
    String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/dynamictemplate/genericstore/test-mapping.json");
    IndexService index = createIndex("test");
    client().admin().indices().preparePutMapping("test").setType("person").setSource(mapping, XContentType.JSON).get();
    DocumentMapper docMapper = index.mapperService().documentMapper("person");
    byte[] json = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/dynamictemplate/genericstore/test-data.json");
    ParsedDocument parsedDoc = docMapper.parse("test", "person", "1", new BytesArray(json));
    client().admin().indices().preparePutMapping("test").setType("person").setSource(parsedDoc.dynamicMappingsUpdate().toString(), XContentType.JSON).get();
    docMapper = index.mapperService().documentMapper("person");
    Document doc = parsedDoc.rootDoc();
    IndexableField f = doc.getField("name");
    assertThat(f.name(), equalTo("name"));
    assertThat(f.stringValue(), equalTo("some name"));
    assertThat(f.fieldType().stored(), equalTo(true));
    FieldMapper fieldMapper = docMapper.mappers().getMapper("name");
    assertThat(fieldMapper.fieldType().stored(), equalTo(true));
    boolean stored = false;
    for (IndexableField field : doc.getFields("age")) {
        stored |= field.fieldType().stored();
    }
    assertTrue(stored);
    fieldMapper = docMapper.mappers().getMapper("age");
    assertThat(fieldMapper.fieldType().stored(), equalTo(true));
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) BytesArray(org.elasticsearch.common.bytes.BytesArray) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) IndexService(org.elasticsearch.index.IndexService) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) Document(org.elasticsearch.index.mapper.ParseContext.Document) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) FieldMapper(org.elasticsearch.index.mapper.FieldMapper)

Example 37 with IndexService

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

the class DynamicMappingTests method testField.

public void testField() 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").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("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 38 with IndexService

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

the class DynamicMappingTests method testInnerDynamicMapping.

public void testInnerDynamicMapping() throws Exception {
    IndexService indexService = createIndex("test");
    DocumentMapperParser parser = indexService.mapperService().documentMapperParser();
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("foo").field("type", "object").endObject().endObject().endObject().endObject().string();
    DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
    assertEquals(mapping, serialize(mapper));
    Mapper update = parse(mapper, parser, XContentFactory.jsonBuilder().startObject().startObject("foo").startObject("bar").field("baz", "foo").endObject().endObject().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("foo").startObject("properties").startObject("bar").startObject("properties").startObject("baz").field("type", "text").startObject("fields").startObject("keyword").field("type", "keyword").field("ignore_above", 256).endObject().endObject().endObject().endObject().endObject().endObject().endObject().endObject().endObject().endObject().string(), serialize(update));
}
Also used : IndexService(org.elasticsearch.index.IndexService) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent)

Example 39 with IndexService

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

the class DynamicTemplatesTests method testSimple.

public void testSimple() throws Exception {
    String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/dynamictemplate/simple/test-mapping.json");
    IndexService index = createIndex("test");
    client().admin().indices().preparePutMapping("test").setType("person").setSource(mapping, XContentType.JSON).get();
    DocumentMapper docMapper = index.mapperService().documentMapper("person");
    byte[] json = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/dynamictemplate/simple/test-data.json");
    ParsedDocument parsedDoc = docMapper.parse("test", "person", "1", new BytesArray(json));
    client().admin().indices().preparePutMapping("test").setType("person").setSource(parsedDoc.dynamicMappingsUpdate().toString(), XContentType.JSON).get();
    docMapper = index.mapperService().documentMapper("person");
    Document doc = parsedDoc.rootDoc();
    IndexableField f = doc.getField("name");
    assertThat(f.name(), equalTo("name"));
    assertThat(f.binaryValue(), equalTo(new BytesRef("some name")));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    assertThat(f.fieldType().tokenized(), equalTo(false));
    FieldMapper fieldMapper = docMapper.mappers().getMapper("name");
    assertNotNull(fieldMapper);
    f = doc.getField("multi1");
    assertThat(f.name(), equalTo("multi1"));
    assertThat(f.stringValue(), equalTo("multi 1"));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    assertThat(f.fieldType().tokenized(), equalTo(true));
    fieldMapper = docMapper.mappers().getMapper("multi1");
    assertNotNull(fieldMapper);
    f = doc.getField("multi1.org");
    assertThat(f.name(), equalTo("multi1.org"));
    assertThat(f.binaryValue(), equalTo(new BytesRef("multi 1")));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    assertThat(f.fieldType().tokenized(), equalTo(false));
    fieldMapper = docMapper.mappers().getMapper("multi1.org");
    assertNotNull(fieldMapper);
    f = doc.getField("multi2");
    assertThat(f.name(), equalTo("multi2"));
    assertThat(f.stringValue(), equalTo("multi 2"));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    assertThat(f.fieldType().tokenized(), equalTo(true));
    fieldMapper = docMapper.mappers().getMapper("multi2");
    assertNotNull(fieldMapper);
    f = doc.getField("multi2.org");
    assertThat(f.name(), equalTo("multi2.org"));
    assertThat(f.binaryValue(), equalTo(new BytesRef("multi 2")));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    assertThat(f.fieldType().tokenized(), equalTo(false));
    fieldMapper = docMapper.mappers().getMapper("multi2.org");
    assertNotNull(fieldMapper);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) BytesArray(org.elasticsearch.common.bytes.BytesArray) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) IndexService(org.elasticsearch.index.IndexService) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) Document(org.elasticsearch.index.mapper.ParseContext.Document) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) FieldMapper(org.elasticsearch.index.mapper.FieldMapper) BytesRef(org.apache.lucene.util.BytesRef)

Example 40 with IndexService

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

the class DynamicTemplatesTests method testMatchTypeOnly.

public void testMatchTypeOnly() throws Exception {
    XContentBuilder builder = JsonXContent.contentBuilder();
    builder.startObject().startObject("person").startArray("dynamic_templates").startObject().startObject("test").field("match_mapping_type", "string").startObject("mapping").field("index", false).endObject().endObject().endObject().endArray().endObject().endObject();
    IndexService index = createIndex("test");
    client().admin().indices().preparePutMapping("test").setType("person").setSource(builder).get();
    DocumentMapper docMapper = index.mapperService().documentMapper("person");
    builder = JsonXContent.contentBuilder();
    builder.startObject().field("s", "hello").field("l", 1).endObject();
    ParsedDocument parsedDoc = docMapper.parse("test", "person", "1", builder.bytes());
    client().admin().indices().preparePutMapping("test").setType("person").setSource(parsedDoc.dynamicMappingsUpdate().toString(), XContentType.JSON).get();
    docMapper = index.mapperService().documentMapper("person");
    DocumentFieldMappers mappers = docMapper.mappers();
    assertThat(mappers.smartNameFieldMapper("s"), Matchers.notNullValue());
    assertEquals(IndexOptions.NONE, mappers.smartNameFieldMapper("s").fieldType().indexOptions());
    assertThat(mappers.smartNameFieldMapper("l"), Matchers.notNullValue());
    assertNotSame(IndexOptions.NONE, mappers.smartNameFieldMapper("l").fieldType().indexOptions());
}
Also used : ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) IndexService(org.elasticsearch.index.IndexService) DocumentFieldMappers(org.elasticsearch.index.mapper.DocumentFieldMappers) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

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