Search in sources :

Example 11 with MapperService

use of org.elasticsearch.index.mapper.MapperService in project crate by crate.

the class MatchQueryBuilderTest method mockMapperService.

private MapperService mockMapperService() {
    Analyzer analyzer = new GermanAnalyzer();
    MapperService mapperService = mock(MapperService.class);
    when(mapperService.searchAnalyzer()).thenReturn(analyzer);
    return mapperService;
}
Also used : GermanAnalyzer(org.apache.lucene.analysis.de.GermanAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) GermanAnalyzer(org.apache.lucene.analysis.de.GermanAnalyzer) MapperService(org.elasticsearch.index.mapper.MapperService)

Example 12 with MapperService

use of org.elasticsearch.index.mapper.MapperService in project elasticsearch by elastic.

the class AggregatorTestCase method createAggregator.

protected <A extends Aggregator, B extends AggregationBuilder> A createAggregator(B aggregationBuilder, IndexSearcher indexSearcher, MappedFieldType... fieldTypes) throws IOException {
    IndexSettings indexSettings = createIndexSettings();
    SearchContext searchContext = createSearchContext(indexSearcher, indexSettings);
    CircuitBreakerService circuitBreakerService = new NoneCircuitBreakerService();
    when(searchContext.bigArrays()).thenReturn(new MockBigArrays(Settings.EMPTY, circuitBreakerService));
    // TODO: now just needed for top_hits, this will need to be revised for other agg unit tests:
    MapperService mapperService = mapperServiceMock();
    when(mapperService.hasNested()).thenReturn(false);
    when(searchContext.mapperService()).thenReturn(mapperService);
    IndexFieldDataService ifds = new IndexFieldDataService(indexSettings, new IndicesFieldDataCache(Settings.EMPTY, new IndexFieldDataCache.Listener() {
    }), circuitBreakerService, mapperService);
    when(searchContext.fieldData()).thenReturn(ifds);
    SearchLookup searchLookup = new SearchLookup(mapperService, ifds, new String[] { "type" });
    when(searchContext.lookup()).thenReturn(searchLookup);
    QueryShardContext queryShardContext = queryShardContextMock(fieldTypes, indexSettings, circuitBreakerService);
    when(searchContext.getQueryShardContext()).thenReturn(queryShardContext);
    @SuppressWarnings("unchecked") A aggregator = (A) aggregationBuilder.build(searchContext, null).create(null, true);
    return aggregator;
}
Also used : IndexSettings(org.elasticsearch.index.IndexSettings) SearchContext(org.elasticsearch.search.internal.SearchContext) MockBigArrays(org.elasticsearch.common.util.MockBigArrays) IndicesFieldDataCache(org.elasticsearch.indices.fielddata.cache.IndicesFieldDataCache) IndexFieldDataService(org.elasticsearch.index.fielddata.IndexFieldDataService) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) NoneCircuitBreakerService(org.elasticsearch.indices.breaker.NoneCircuitBreakerService) MapperService(org.elasticsearch.index.mapper.MapperService) NoneCircuitBreakerService(org.elasticsearch.indices.breaker.NoneCircuitBreakerService) SearchLookup(org.elasticsearch.search.lookup.SearchLookup)

Example 13 with MapperService

use of org.elasticsearch.index.mapper.MapperService in project elasticsearch by elastic.

the class ParentFieldLoadingIT method testChangingEagerParentFieldLoadingAtRuntime.

public void testChangingEagerParentFieldLoadingAtRuntime() throws Exception {
    assertAcked(prepareCreate("test").setSettings(indexSettings).addMapping("parent").addMapping("child", "_parent", "type=parent"));
    ensureGreen();
    client().prepareIndex("test", "parent", "1").setSource("{}", XContentType.JSON).get();
    client().prepareIndex("test", "child", "1").setParent("1").setSource("{}", XContentType.JSON).get();
    refresh();
    ClusterStatsResponse response = client().admin().cluster().prepareClusterStats().get();
    assertThat(response.getIndicesStats().getFieldData().getMemorySizeInBytes(), equalTo(0L));
    PutMappingResponse putMappingResponse = client().admin().indices().preparePutMapping("test").setType("child").setSource(childMapping(true)).setUpdateAllTypes(true).get();
    assertAcked(putMappingResponse);
    Index test = resolveIndex("test");
    assertBusy(new Runnable() {

        @Override
        public void run() {
            ClusterState clusterState = internalCluster().clusterService().state();
            ShardRouting shardRouting = clusterState.routingTable().index("test").shard(0).getShards().get(0);
            String nodeName = clusterState.getNodes().get(shardRouting.currentNodeId()).getName();
            boolean verified = false;
            IndicesService indicesService = internalCluster().getInstance(IndicesService.class, nodeName);
            IndexService indexService = indicesService.indexService(test);
            if (indexService != null) {
                MapperService mapperService = indexService.mapperService();
                DocumentMapper documentMapper = mapperService.documentMapper("child");
                if (documentMapper != null) {
                    verified = documentMapper.parentFieldMapper().fieldType().eagerGlobalOrdinals();
                }
            }
            assertTrue(verified);
        }
    });
    // Need to add a new doc otherwise the refresh doesn't trigger a new searcher
    // Because it ends up in its own segment, but isn't of type parent or child, this doc doesn't contribute to the size of the fielddata cache
    client().prepareIndex("test", "dummy", "dummy").setSource("{}", XContentType.JSON).get();
    refresh();
    response = client().admin().cluster().prepareClusterStats().get();
    assertThat(response.getIndicesStats().getFieldData().getMemorySizeInBytes(), greaterThan(0L));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexService(org.elasticsearch.index.IndexService) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) IndicesService(org.elasticsearch.indices.IndicesService) Index(org.elasticsearch.index.Index) ClusterStatsResponse(org.elasticsearch.action.admin.cluster.stats.ClusterStatsResponse) PutMappingResponse(org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) MapperService(org.elasticsearch.index.mapper.MapperService)

Example 14 with MapperService

use of org.elasticsearch.index.mapper.MapperService in project crate by crate.

the class TransportBulkCreateIndicesAction method executeCreateIndices.

/**
     * This code is more or less the same as the stuff in {@link MetaDataCreateIndexService}
     * but optimized for bulk operation without separate mapping/alias/index settings.
     */
ClusterState executeCreateIndices(ClusterState currentState, BulkCreateIndicesRequest request) throws Exception {
    List<String> indicesToCreate = new ArrayList<>(request.indices().size());
    String removalReason = null;
    String testIndex = null;
    try {
        validateAndFilterExistingIndices(currentState, indicesToCreate, request);
        if (indicesToCreate.isEmpty()) {
            return currentState;
        }
        Map<String, IndexMetaData.Custom> customs = Maps.newHashMap();
        Map<String, Map<String, Object>> mappings = Maps.newHashMap();
        Map<String, AliasMetaData> templatesAliases = Maps.newHashMap();
        List<String> templateNames = Lists.newArrayList();
        List<IndexTemplateMetaData> templates = findTemplates(request, currentState, indexTemplateFilter);
        applyTemplates(customs, mappings, templatesAliases, templateNames, templates);
        File mappingsDir = new File(environment.configFile().toFile(), "mappings");
        if (mappingsDir.isDirectory()) {
            addMappingFromMappingsFile(mappings, mappingsDir, request);
        }
        Settings indexSettings = createIndexSettings(currentState, templates);
        testIndex = indicesToCreate.get(0);
        indicesService.createIndex(testIndex, indexSettings, clusterService.localNode().getId());
        // now add the mappings
        IndexService indexService = indicesService.indexServiceSafe(testIndex);
        MapperService mapperService = indexService.mapperService();
        // first, add the default mapping
        if (mappings.containsKey(MapperService.DEFAULT_MAPPING)) {
            try {
                mapperService.merge(MapperService.DEFAULT_MAPPING, new CompressedXContent(XContentFactory.jsonBuilder().map(mappings.get(MapperService.DEFAULT_MAPPING)).string()), MapperService.MergeReason.MAPPING_UPDATE, false);
            } catch (Exception e) {
                removalReason = "failed on parsing default mapping on index creation";
                throw new MapperParsingException("mapping [" + MapperService.DEFAULT_MAPPING + "]", e);
            }
        }
        for (Map.Entry<String, Map<String, Object>> entry : mappings.entrySet()) {
            if (entry.getKey().equals(MapperService.DEFAULT_MAPPING)) {
                continue;
            }
            try {
                // apply the default here, its the first time we parse it
                mapperService.merge(entry.getKey(), new CompressedXContent(XContentFactory.jsonBuilder().map(entry.getValue()).string()), MapperService.MergeReason.MAPPING_UPDATE, false);
            } catch (Exception e) {
                removalReason = "failed on parsing mappings on index creation";
                throw new MapperParsingException("mapping [" + entry.getKey() + "]", e);
            }
        }
        IndexQueryParserService indexQueryParserService = indexService.queryParserService();
        for (AliasMetaData aliasMetaData : templatesAliases.values()) {
            if (aliasMetaData.filter() != null) {
                aliasValidator.validateAliasFilter(aliasMetaData.alias(), aliasMetaData.filter().uncompressed(), indexQueryParserService);
            }
        }
        // now, update the mappings with the actual source
        Map<String, MappingMetaData> mappingsMetaData = Maps.newHashMap();
        for (DocumentMapper mapper : mapperService.docMappers(true)) {
            MappingMetaData mappingMd = new MappingMetaData(mapper);
            mappingsMetaData.put(mapper.type(), mappingMd);
        }
        MetaData.Builder newMetaDataBuilder = MetaData.builder(currentState.metaData());
        for (String index : indicesToCreate) {
            Settings newIndexSettings = createIndexSettings(currentState, templates);
            final IndexMetaData.Builder indexMetaDataBuilder = IndexMetaData.builder(index).settings(newIndexSettings);
            for (MappingMetaData mappingMd : mappingsMetaData.values()) {
                indexMetaDataBuilder.putMapping(mappingMd);
            }
            for (AliasMetaData aliasMetaData : templatesAliases.values()) {
                indexMetaDataBuilder.putAlias(aliasMetaData);
            }
            for (Map.Entry<String, IndexMetaData.Custom> customEntry : customs.entrySet()) {
                indexMetaDataBuilder.putCustom(customEntry.getKey(), customEntry.getValue());
            }
            indexMetaDataBuilder.state(IndexMetaData.State.OPEN);
            final IndexMetaData indexMetaData;
            try {
                indexMetaData = indexMetaDataBuilder.build();
            } catch (Exception e) {
                removalReason = "failed to build index metadata";
                throw e;
            }
            logger.info("[{}] creating index, cause [bulk], templates {}, shards [{}]/[{}], mappings {}", index, templateNames, indexMetaData.getNumberOfShards(), indexMetaData.getNumberOfReplicas(), mappings.keySet());
            indexService.indicesLifecycle().beforeIndexAddedToCluster(new Index(index), indexMetaData.getSettings());
            newMetaDataBuilder.put(indexMetaData, false);
        }
        MetaData newMetaData = newMetaDataBuilder.build();
        ClusterState updatedState = ClusterState.builder(currentState).metaData(newMetaData).build();
        RoutingTable.Builder routingTableBuilder = RoutingTable.builder(updatedState.routingTable());
        for (String index : indicesToCreate) {
            routingTableBuilder.addAsNew(updatedState.metaData().index(index));
        }
        RoutingAllocation.Result routingResult = allocationService.reroute(ClusterState.builder(updatedState).routingTable(routingTableBuilder).build(), "bulk-index-creation");
        updatedState = ClusterState.builder(updatedState).routingResult(routingResult).build();
        removalReason = "cleaning up after validating index on master";
        return updatedState;
    } finally {
        if (testIndex != null) {
            // index was partially created - need to clean up
            indicesService.deleteIndex(testIndex, removalReason != null ? removalReason : "failed to create index");
        }
    }
}
Also used : MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) IndexService(org.elasticsearch.index.IndexService) Index(org.elasticsearch.index.Index) IndexQueryParserService(org.elasticsearch.index.query.IndexQueryParserService) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Settings(org.elasticsearch.common.settings.Settings) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) ElasticsearchException(org.elasticsearch.ElasticsearchException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) IndexAlreadyExistsException(org.elasticsearch.indices.IndexAlreadyExistsException) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) IOException(java.io.IOException) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) RoutingAllocation(org.elasticsearch.cluster.routing.allocation.RoutingAllocation) File(java.io.File) MapperService(org.elasticsearch.index.mapper.MapperService)

Example 15 with MapperService

use of org.elasticsearch.index.mapper.MapperService in project crate by crate.

the class AlterTableClusterStateExecutor method updateMapping.

private ClusterState updateMapping(ClusterState currentState, AlterTableRequest request, Index[] concreteIndices) throws Exception {
    if (request.mappingDelta() == null) {
        return currentState;
    }
    Map<Index, MapperService> indexMapperServices = new HashMap<>();
    Map<String, Object> currentMeta = new HashMap<>();
    for (Index index : concreteIndices) {
        final IndexMetadata indexMetadata = currentState.metadata().getIndexSafe(index);
        Map<String, Object> sourceAsMap = indexMetadata.mapping().sourceAsMap();
        Map<String, Object> meta = (Map<String, Object>) sourceAsMap.get("_meta");
        if (meta != null) {
            Maps.extendRecursive(currentMeta, meta);
        }
        if (indexMapperServices.containsKey(indexMetadata.getIndex()) == false) {
            MapperService mapperService = indicesService.createIndexMapperService(indexMetadata);
            indexMapperServices.put(index, mapperService);
            // add mappings for all types, we need them for cross-type validation
            mapperService.merge(indexMetadata, MapperService.MergeReason.MAPPING_RECOVERY);
        }
    }
    String mappingDelta = addExistingMeta(request, currentMeta);
    PutMappingClusterStateUpdateRequest updateRequest = new PutMappingClusterStateUpdateRequest(mappingDelta).ackTimeout(request.timeout()).masterNodeTimeout(request.masterNodeTimeout()).indices(concreteIndices);
    return metadataMappingService.putMappingExecutor.applyRequest(currentState, updateRequest, indexMapperServices);
}
Also used : HashMap(java.util.HashMap) PutMappingClusterStateUpdateRequest(org.elasticsearch.action.admin.indices.mapping.put.PutMappingClusterStateUpdateRequest) Index(org.elasticsearch.index.Index) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Map(java.util.Map) HashMap(java.util.HashMap) MapperService(org.elasticsearch.index.mapper.MapperService)

Aggregations

MapperService (org.elasticsearch.index.mapper.MapperService)46 Settings (org.elasticsearch.common.settings.Settings)16 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)14 IndexSettings (org.elasticsearch.index.IndexSettings)13 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)12 IOException (java.io.IOException)10 Store (org.elasticsearch.index.store.Store)9 Matchers.containsString (org.hamcrest.Matchers.containsString)9 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)8 Index (org.elasticsearch.index.Index)8 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)8 HashMap (java.util.HashMap)7 Map (java.util.Map)7 IndexService (org.elasticsearch.index.IndexService)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 IndexAnalyzers (org.elasticsearch.index.analysis.IndexAnalyzers)6 Collections (java.util.Collections)5 HashSet (java.util.HashSet)5 List (java.util.List)5 Analyzer (org.apache.lucene.analysis.Analyzer)5