Search in sources :

Example 21 with IndexService

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

the class Murmur3FieldMapperTests method testEmptyName.

public void testEmptyName() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("").field("type", "murmur3").endObject().endObject().endObject().endObject().string();
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> parser.parse("type", new CompressedXContent(mapping)));
    assertThat(e.getMessage(), containsString("name cannot be empty string"));
    // before 5.x
    Version oldVersion = VersionUtils.randomVersionBetween(getRandom(), Version.V_2_0_0, Version.V_2_3_5);
    Settings oldIndexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, oldVersion).build();
    IndexService indexService2x = createIndex("test_old", oldIndexSettings);
    Supplier<QueryShardContext> queryShardContext = () -> {
        return indexService2x.newQueryShardContext(0, null, () -> {
            throw new UnsupportedOperationException();
        });
    };
    DocumentMapperParser parser = new DocumentMapperParser(indexService2x.getIndexSettings(), indexService2x.mapperService(), indexService2x.getIndexAnalyzers(), indexService2x.xContentRegistry(), indexService2x.similarityService(), mapperRegistry, queryShardContext);
    DocumentMapper defaultMapper = parser.parse("type", new CompressedXContent(mapping));
    assertEquals(mapping, defaultMapper.mappingSource().string());
}
Also used : Version(org.elasticsearch.Version) IndexService(org.elasticsearch.index.IndexService) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) Matchers.containsString(org.hamcrest.Matchers.containsString) DocumentMapperParser(org.elasticsearch.index.mapper.DocumentMapperParser) Settings(org.elasticsearch.common.settings.Settings)

Example 22 with IndexService

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

the class SizeMappingTests method testSizeEnabled.

public void testSizeEnabled() throws Exception {
    IndexService service = createIndex("test", Settings.EMPTY, "type", "_size", "enabled=true");
    DocumentMapper docMapper = service.mapperService().documentMapper("type");
    BytesReference source = XContentFactory.jsonBuilder().startObject().field("field", "value").endObject().bytes();
    ParsedDocument doc = docMapper.parse(SourceToParse.source("test", "type", "1", source, XContentType.JSON));
    boolean stored = false;
    boolean points = false;
    for (IndexableField field : doc.rootDoc().getFields("_size")) {
        stored |= field.fieldType().stored();
        points |= field.fieldType().pointDimensionCount() > 0;
    }
    assertTrue(stored);
    assertTrue(points);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) IndexableField(org.apache.lucene.index.IndexableField) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) IndexService(org.elasticsearch.index.IndexService) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper)

Example 23 with IndexService

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

the class SizeMappingTests method testThatDisablingWorksWhenMerging.

public void testThatDisablingWorksWhenMerging() throws Exception {
    IndexService service = createIndex("test", Settings.EMPTY, "type", "_size", "enabled=true");
    DocumentMapper docMapper = service.mapperService().documentMapper("type");
    assertThat(docMapper.metadataMapper(SizeFieldMapper.class).enabled(), is(true));
    String disabledMapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("_size").field("enabled", false).endObject().endObject().endObject().string();
    docMapper = service.mapperService().merge("type", new CompressedXContent(disabledMapping), MapperService.MergeReason.MAPPING_UPDATE, false);
    assertThat(docMapper.metadataMapper(SizeFieldMapper.class).enabled(), is(false));
}
Also used : IndexService(org.elasticsearch.index.IndexService) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent)

Example 24 with IndexService

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

the class IndexFieldDataServiceTests method testGetForFieldDefaults.

public void testGetForFieldDefaults() {
    final IndexService indexService = createIndex("test");
    final IndexFieldDataService ifdService = indexService.fieldData();
    final BuilderContext ctx = new BuilderContext(indexService.getIndexSettings().getSettings(), new ContentPath(1));
    final MappedFieldType stringMapper = new KeywordFieldMapper.Builder("string").build(ctx).fieldType();
    ifdService.clear();
    IndexFieldData<?> fd = ifdService.getForField(stringMapper);
    assertTrue(fd instanceof SortedSetDVOrdinalsIndexFieldData);
    for (MappedFieldType mapper : Arrays.asList(new NumberFieldMapper.Builder("int", NumberFieldMapper.NumberType.BYTE).build(ctx).fieldType(), new NumberFieldMapper.Builder("int", NumberFieldMapper.NumberType.SHORT).build(ctx).fieldType(), new NumberFieldMapper.Builder("int", NumberFieldMapper.NumberType.INTEGER).build(ctx).fieldType(), new NumberFieldMapper.Builder("long", NumberFieldMapper.NumberType.LONG).build(ctx).fieldType())) {
        ifdService.clear();
        fd = ifdService.getForField(mapper);
        assertTrue(fd instanceof SortedNumericDVIndexFieldData);
    }
    final MappedFieldType floatMapper = new NumberFieldMapper.Builder("float", NumberFieldMapper.NumberType.FLOAT).build(ctx).fieldType();
    ifdService.clear();
    fd = ifdService.getForField(floatMapper);
    assertTrue(fd instanceof SortedNumericDVIndexFieldData);
    final MappedFieldType doubleMapper = new NumberFieldMapper.Builder("double", NumberFieldMapper.NumberType.DOUBLE).build(ctx).fieldType();
    ifdService.clear();
    fd = ifdService.getForField(doubleMapper);
    assertTrue(fd instanceof SortedNumericDVIndexFieldData);
}
Also used : SortedSetDVOrdinalsIndexFieldData(org.elasticsearch.index.fielddata.plain.SortedSetDVOrdinalsIndexFieldData) NumberFieldMapper(org.elasticsearch.index.mapper.NumberFieldMapper) IndexService(org.elasticsearch.index.IndexService) SortedNumericDVIndexFieldData(org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) BuilderContext(org.elasticsearch.index.mapper.Mapper.BuilderContext) ContentPath(org.elasticsearch.index.mapper.ContentPath)

Example 25 with IndexService

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

the class IndexFieldDataServiceTests method testFieldDataCacheListener.

public void testFieldDataCacheListener() throws Exception {
    final IndexService indexService = createIndex("test");
    final IndicesService indicesService = getInstanceFromNode(IndicesService.class);
    // copy the ifdService since we can set the listener only once.
    final IndexFieldDataService ifdService = new IndexFieldDataService(indexService.getIndexSettings(), indicesService.getIndicesFieldDataCache(), indicesService.getCircuitBreakerService(), indexService.mapperService());
    final BuilderContext ctx = new BuilderContext(indexService.getIndexSettings().getSettings(), new ContentPath(1));
    final MappedFieldType mapper1 = new TextFieldMapper.Builder("s").fielddata(true).build(ctx).fieldType();
    final IndexWriter writer = new IndexWriter(new RAMDirectory(), new IndexWriterConfig(new KeywordAnalyzer()));
    Document doc = new Document();
    doc.add(new StringField("s", "thisisastring", Store.NO));
    writer.addDocument(doc);
    DirectoryReader open = DirectoryReader.open(writer);
    final boolean wrap = randomBoolean();
    final IndexReader reader = wrap ? ElasticsearchDirectoryReader.wrap(open, new ShardId("test", "_na_", 1)) : open;
    final AtomicInteger onCacheCalled = new AtomicInteger();
    final AtomicInteger onRemovalCalled = new AtomicInteger();
    ifdService.setListener(new IndexFieldDataCache.Listener() {

        @Override
        public void onCache(ShardId shardId, String fieldName, Accountable ramUsage) {
            if (wrap) {
                assertEquals(new ShardId("test", "_na_", 1), shardId);
            } else {
                assertNull(shardId);
            }
            onCacheCalled.incrementAndGet();
        }

        @Override
        public void onRemoval(ShardId shardId, String fieldName, boolean wasEvicted, long sizeInBytes) {
            if (wrap) {
                assertEquals(new ShardId("test", "_na_", 1), shardId);
            } else {
                assertNull(shardId);
            }
            onRemovalCalled.incrementAndGet();
        }
    });
    IndexFieldData<?> ifd = ifdService.getForField(mapper1);
    LeafReaderContext leafReaderContext = reader.getContext().leaves().get(0);
    AtomicFieldData load = ifd.load(leafReaderContext);
    assertEquals(1, onCacheCalled.get());
    assertEquals(0, onRemovalCalled.get());
    reader.close();
    load.close();
    writer.close();
    assertEquals(1, onCacheCalled.get());
    assertEquals(1, onRemovalCalled.get());
    ifdService.clear();
}
Also used : IndexService(org.elasticsearch.index.IndexService) Matchers.containsString(org.hamcrest.Matchers.containsString) Document(org.apache.lucene.document.Document) ShardId(org.elasticsearch.index.shard.ShardId) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) ElasticsearchDirectoryReader(org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) Accountable(org.apache.lucene.util.Accountable) IndicesService(org.elasticsearch.indices.IndicesService) ContentPath(org.elasticsearch.index.mapper.ContentPath) RAMDirectory(org.apache.lucene.store.RAMDirectory) IndexWriter(org.apache.lucene.index.IndexWriter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StringField(org.apache.lucene.document.StringField) IndexReader(org.apache.lucene.index.IndexReader) BuilderContext(org.elasticsearch.index.mapper.Mapper.BuilderContext) TextFieldMapper(org.elasticsearch.index.mapper.TextFieldMapper) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

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