org.elasticsearch.index.IndexService Java Examples

The following examples show how to use org.elasticsearch.index.IndexService. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TransportFieldStatsTransportAction.java    From Elasticsearch with Apache License 2.0 7 votes vote down vote up
@Override
protected FieldStatsShardResponse shardOperation(FieldStatsShardRequest request) {
    ShardId shardId = request.shardId();
    Map<String, FieldStats> fieldStats = new HashMap<>();
    IndexService indexServices = indicesService.indexServiceSafe(shardId.getIndex());
    MapperService mapperService = indexServices.mapperService();
    IndexShard shard = indexServices.shardSafe(shardId.id());
    try (Engine.Searcher searcher = shard.acquireSearcher("fieldstats")) {
        for (String field : request.getFields()) {
            MappedFieldType fieldType = mapperService.fullName(field);
            if (fieldType != null) {
                IndexReader reader = searcher.reader();
                Terms terms = MultiFields.getTerms(reader, field);
                if (terms != null) {
                    fieldStats.put(field, fieldType.stats(terms, reader.maxDoc()));
                }
            } else {
                throw new IllegalArgumentException("field [" + field + "] doesn't exist");
            }
        }
    } catch (IOException e) {
        throw ExceptionsHelper.convertToElastic(e);
    }
    return new FieldStatsShardResponse(shardId, fieldStats);
}
 
Example #2
Source File: TransportShardDeleteAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected void processRequestItemsOnReplica(ShardId shardId, ShardDeleteRequest request) {
    IndexService indexService = indicesService.indexServiceSafe(request.index());
    IndexShard indexShard = indexService.shardSafe(shardId.id());
    for (int i = 0; i < request.itemIndices().size(); i++) {
        int location = request.itemIndices().get(i);
        if (request.skipFromLocation() == location) {
            // skipping this and all next items, the primary did not processed them (mostly due to a kill request)
            break;
        }

        ShardDeleteRequest.Item item = request.items().get(i);
        try {
            Engine.Delete delete = indexShard.prepareDeleteOnReplica(request.type(), item.id(), item.version(), item.versionType());
            indexShard.delete(delete);
            logger.trace("{} REPLICA: successfully deleted [{}]/[{}]", request.shardId(), request.type(), item.id());
        } catch (Throwable e) {
            // if its not an ignore replica failure, we need to make sure to bubble up the failure
            // so we will fail the shard
            if (!ignoreReplicaException(e)) {
                throw e;
            }
        }
    }

}
 
Example #3
Source File: FieldReadCallback.java    From deprecated-security-advanced-modules with Apache License 2.0 6 votes vote down vote up
public FieldReadCallback(final ThreadContext threadContext, final IndexService indexService,
        final ClusterService clusterService, final ComplianceConfig complianceConfig, final AuditLog auditLog,
        final Set<String> maskedFields, ShardId shardId) {
    super();
    //this.threadContext = Objects.requireNonNull(threadContext);
    //this.clusterService = Objects.requireNonNull(clusterService);
    this.index = Objects.requireNonNull(indexService).index();
    this.complianceConfig = complianceConfig;
    this.auditLog = auditLog;
    this.maskedFields = maskedFields;
    this.shardId = shardId;
    try {
        sfc = (SourceFieldsContext) HeaderHelper.deserializeSafeFromHeader(threadContext, "_opendistro_security_source_field_context");
        if(sfc != null && sfc.hasIncludesOrExcludes()) {
            if(log.isTraceEnabled()) {
                log.trace("_opendistro_security_source_field_context: "+sfc);
            }

            filterFunction = XContentMapValues.filter(sfc.getIncludes(), sfc.getExcludes());
        }
    } catch (Exception e) {
        if(log.isDebugEnabled()) {
            log.debug("Cannot deserialize _opendistro_security_source_field_context because of {}", e.toString());
        }
    }
}
 
Example #4
Source File: OpenDistroSecurityFlsDlsIndexSearcherWrapper.java    From deprecated-security-advanced-modules with Apache License 2.0 6 votes vote down vote up
public OpenDistroSecurityFlsDlsIndexSearcherWrapper(final IndexService indexService, final Settings settings,
        final AdminDNs adminDNs, final ClusterService clusterService, final AuditLog auditlog,
        final ComplianceIndexingOperationListener ciol, final ComplianceConfig complianceConfig, final PrivilegesEvaluator evaluator) {
    super(indexService, settings, adminDNs, evaluator);
    ciol.setIs(indexService);
    this.clusterService = clusterService;
    this.indexService = indexService;
    this.complianceConfig = complianceConfig;
    this.auditlog = auditlog;
    final boolean allowNowinDlsQueries = settings.getAsBoolean(ConfigConstants.OPENDISTRO_SECURITY_UNSUPPORTED_ALLOW_NOW_IN_DLS, false);
    if (allowNowinDlsQueries) {
        nowInMillis = () -> System.currentTimeMillis();
    } else {
        nowInMillis = () -> {throw new IllegalArgumentException("'now' is not allowed in DLS queries");};
    }
}
 
Example #5
Source File: InternalCountOperation.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public long count(String index, int shardId, WhereClause whereClause) throws IOException, InterruptedException {
    IndexService indexService;
    try {
        indexService = indicesService.indexServiceSafe(index);
    } catch (IndexNotFoundException e) {
        if (PartitionName.isPartition(index)) {
            return 0L;
        }
        throw e;
    }

    IndexShard indexShard = indexService.shardSafe(shardId);
    try (Engine.Searcher searcher = indexShard.acquireSearcher("count-operation")) {
        LuceneQueryBuilder.Context queryCtx = queryBuilder.convert(
                whereClause, indexService.mapperService(), indexService.fieldData(), indexService.cache());
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
        return searcher.searcher().count(queryCtx.query());
    }
}
 
Example #6
Source File: SyncedFlushService.java    From crate with Apache License 2.0 6 votes vote down vote up
private ShardSyncedFlushResponse performSyncedFlush(ShardSyncedFlushRequest request) {
    IndexService indexService = indicesService.indexServiceSafe(request.shardId().getIndex());
    IndexShard indexShard = indexService.getShard(request.shardId().id());
    LOGGER.trace("{} performing sync flush. sync id [{}], expected commit id {}", request.shardId(), request.syncId(), request.expectedCommitId());
    Engine.SyncedFlushResult result = indexShard.syncFlush(request.syncId(), request.expectedCommitId());
    LOGGER.trace("{} sync flush done. sync id [{}], result [{}]", request.shardId(), request.syncId(), result);
    switch (result) {
        case SUCCESS:
            return new ShardSyncedFlushResponse();
        case COMMIT_MISMATCH:
            return new ShardSyncedFlushResponse("commit has changed");
        case PENDING_OPERATIONS:
            return new ShardSyncedFlushResponse("pending operations");
        default:
            throw new ElasticsearchException("unknown synced flush result [" + result + "]");
    }
}
 
Example #7
Source File: TransportShardMultiTermsVectorAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected MultiTermVectorsShardResponse shardOperation(MultiTermVectorsShardRequest request, ShardId shardId) {
    MultiTermVectorsShardResponse response = new MultiTermVectorsShardResponse();
    for (int i = 0; i < request.locations.size(); i++) {
        TermVectorsRequest termVectorsRequest = request.requests.get(i);
        try {
            IndexService indexService = indicesService.indexServiceSafe(request.index());
            IndexShard indexShard = indexService.shardSafe(shardId.id());
            TermVectorsResponse termVectorsResponse = indexShard.termVectorsService().getTermVectors(termVectorsRequest, shardId.getIndex());
            termVectorsResponse.updateTookInMillis(termVectorsRequest.startTime());
            response.add(request.locations.get(i), termVectorsResponse);
        } catch (Throwable t) {
            if (TransportActions.isShardNotAvailableException(t)) {
                throw (ElasticsearchException) t;
            } else {
                logger.debug("{} failed to execute multi term vectors for [{}]/[{}]", t, shardId, termVectorsRequest.type(), termVectorsRequest.id());
                response.add(request.locations.get(i),
                        new MultiTermVectorsResponse.Failure(request.index(), termVectorsRequest.type(), termVectorsRequest.id(), t));
            }
        }
    }

    return response;
}
 
Example #8
Source File: TransportShardDeleteActionTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Before
public void prepare() throws Exception {
    indexUUID = UUIDs.randomBase64UUID();
    IndicesService indicesService = mock(IndicesService.class);
    IndexService indexService = mock(IndexService.class);
    when(indicesService.indexServiceSafe(new Index(TABLE_IDENT.indexNameOrAlias(), indexUUID))).thenReturn(indexService);
    indexShard = mock(IndexShard.class);
    when(indexService.getShard(0)).thenReturn(indexShard);


    transportShardDeleteAction = new TransportShardDeleteAction(
        MockTransportService.createNewService(
            Settings.EMPTY, Version.CURRENT, THREAD_POOL, clusterService.getClusterSettings()),
        mock(IndexNameExpressionResolver.class),
        mock(ClusterService.class),
        indicesService,
        mock(ThreadPool.class),
        mock(ShardStateAction.class),
        mock(SchemaUpdateClient.class)
    );
}
 
Example #9
Source File: CrateSearchContext.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public CrateSearchContext(long id,
                          final long nowInMillis,
                          SearchShardTarget shardTarget,
                          Engine.Searcher engineSearcher,
                          IndexService indexService,
                          final IndexShard indexShard,
                          ScriptService scriptService,
                          PageCacheRecycler pageCacheRecycler,
                          BigArrays bigArrays,
                          Counter timeEstimateCounter,
                          Optional<Scroll> scroll) {
    super(id, new CrateSearchShardRequest(nowInMillis, scroll, indexShard),
            shardTarget, engineSearcher, indexService,
            indexShard, scriptService, pageCacheRecycler,
            bigArrays, timeEstimateCounter, ParseFieldMatcher.STRICT, SearchService.NO_TIMEOUT);
    this.engineSearcher = engineSearcher;
}
 
Example #10
Source File: InternalTestCluster.java    From crate with Apache License 2.0 6 votes vote down vote up
synchronized String routingKeyForShard(Index index, int shard, Random random) {
    assertThat(shard, greaterThanOrEqualTo(0));
    assertThat(shard, greaterThanOrEqualTo(0));
    for (NodeAndClient n : nodes.values()) {
        Node node = n.node;
        IndicesService indicesService = getInstanceFromNode(IndicesService.class, node);
        ClusterService clusterService = getInstanceFromNode(ClusterService.class, node);
        IndexService indexService = indicesService.indexService(index);
        if (indexService != null) {
            assertThat(indexService.getIndexSettings().getSettings().getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, -1),
                    greaterThan(shard));
            OperationRouting operationRouting = clusterService.operationRouting();
            while (true) {
                String routing = RandomStrings.randomAsciiOfLength(random, 10);
                final int targetShard = operationRouting
                        .indexShards(clusterService.state(), index.getName(), null, routing)
                        .shardId().getId();
                if (shard == targetShard) {
                    return routing;
                }
            }
        }
    }
    fail("Could not find a node that holds " + index);
    return null;
}
 
Example #11
Source File: TransportClusterStatsAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected ClusterStatsNodeResponse nodeOperation(ClusterStatsNodeRequest nodeRequest) {
    NodeInfo nodeInfo = nodeService.info(false, true, false, true, false, true, false, true);
    NodeStats nodeStats = nodeService.stats(CommonStatsFlags.NONE, true, true, true, false, true, false, false, false, false);
    List<ShardStats> shardsStats = new ArrayList<>();
    for (IndexService indexService : indicesService) {
        for (IndexShard indexShard : indexService) {
            if (indexShard.routingEntry() != null && indexShard.routingEntry().active()) {
                // only report on fully started shards
                shardsStats.add(new ShardStats(indexShard.routingEntry(), indexShard.shardPath(), new CommonStats(indexShard, SHARD_STATS_FLAGS), indexShard.commitStats()));
            }
        }
    }

    ClusterHealthStatus clusterStatus = null;
    if (clusterService.state().nodes().localNodeMaster()) {
        clusterStatus = new ClusterStateHealth(clusterService.state()).getStatus();
    }

    return new ClusterStatsNodeResponse(nodeInfo.getNode(), clusterStatus, nodeInfo, nodeStats, shardsStats.toArray(new ShardStats[shardsStats.size()]));

}
 
Example #12
Source File: TransportUpgradeStatusAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected ShardUpgradeStatus shardOperation(UpgradeStatusRequest request, ShardRouting shardRouting) {
    IndexService indexService = indicesService.indexServiceSafe(shardRouting.shardId().getIndex());
    IndexShard indexShard = indexService.shardSafe(shardRouting.shardId().id());
    List<Segment> segments = indexShard.engine().segments(false);
    long total_bytes = 0;
    long to_upgrade_bytes = 0;
    long to_upgrade_bytes_ancient = 0;
    for (Segment seg : segments) {
        total_bytes += seg.sizeInBytes;
        if (seg.version.major != Version.CURRENT.luceneVersion.major) {
            to_upgrade_bytes_ancient += seg.sizeInBytes;
            to_upgrade_bytes += seg.sizeInBytes;
        } else if (seg.version.minor != Version.CURRENT.luceneVersion.minor) {
            // TODO: this comparison is bogus! it would cause us to upgrade even with the same format
            // instead, we should check if the codec has changed
            to_upgrade_bytes += seg.sizeInBytes;
        }
    }

    return new ShardUpgradeStatus(indexShard.routingEntry(), total_bytes, to_upgrade_bytes, to_upgrade_bytes_ancient);
}
 
Example #13
Source File: ShadowIndexShard.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Inject
public ShadowIndexShard(ShardId shardId, IndexSettingsService indexSettingsService,
                        IndicesLifecycle indicesLifecycle, Store store, StoreRecoveryService storeRecoveryService,
                        ThreadPool threadPool, MapperService mapperService,
                        IndexQueryParserService queryParserService, IndexCache indexCache,
                        IndexAliasesService indexAliasesService, IndicesQueryCache indicesQueryCache,
                        ShardPercolateService shardPercolateService, CodecService codecService,
                        ShardTermVectorsService termVectorsService, IndexFieldDataService indexFieldDataService,
                        IndexService indexService, @Nullable IndicesWarmer warmer,
                        SnapshotDeletionPolicy deletionPolicy, SimilarityService similarityService,
                        EngineFactory factory, ClusterService clusterService,
                        ShardPath path, BigArrays bigArrays, IndexSearcherWrappingService wrappingService,
                        IndexingMemoryController indexingMemoryController, SearchService shardSearchService) throws IOException {
    super(shardId, indexSettingsService, indicesLifecycle, store, storeRecoveryService,
          threadPool, mapperService, queryParserService, indexCache, indexAliasesService,
          indicesQueryCache, shardPercolateService, codecService,
          termVectorsService, indexFieldDataService, indexService,
          warmer, deletionPolicy, similarityService,
          factory, clusterService, path, bigArrays, wrappingService, indexingMemoryController, shardSearchService);
}
 
Example #14
Source File: PercolateContext.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public PercolateContext(PercolateShardRequest request, SearchShardTarget searchShardTarget, IndexShard indexShard,
                        IndexService indexService, PageCacheRecycler pageCacheRecycler,
                        BigArrays bigArrays, ScriptService scriptService, Query aliasFilter, ParseFieldMatcher parseFieldMatcher) {
    super(parseFieldMatcher, request);
    this.indexShard = indexShard;
    this.indexService = indexService;
    this.fieldDataService = indexService.fieldData();
    this.searchShardTarget = searchShardTarget;
    this.percolateQueries = indexShard.percolateRegistry().percolateQueries();
    this.types = new String[]{request.documentType()};
    this.pageCacheRecycler = pageCacheRecycler;
    this.bigArrays = bigArrays.withCircuitBreaking();
    this.querySearchResult = new QuerySearchResult(0, searchShardTarget);
    this.engineSearcher = indexShard.acquireSearcher("percolate");
    this.searcher = new ContextIndexSearcher(engineSearcher, indexService.cache().query(), indexShard.getQueryCachingPolicy());
    this.scriptService = scriptService;
    this.numberOfShards = request.getNumberOfShards();
    this.aliasFilter = aliasFilter;
    this.startTime = request.getStartTime();
}
 
Example #15
Source File: TransportNodesListShardStoreMetaData.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected NodeStoreFilesMetaData nodeOperation(NodeRequest request) {
    if (request.unallocated) {
        IndexService indexService = indicesService.indexService(request.shardId.index().name());
        if (indexService == null) {
            return new NodeStoreFilesMetaData(clusterService.localNode(), null);
        }
        if (!indexService.hasShard(request.shardId.id())) {
            return new NodeStoreFilesMetaData(clusterService.localNode(), null);
        }
    }
    IndexMetaData metaData = clusterService.state().metaData().index(request.shardId.index().name());
    if (metaData == null) {
        return new NodeStoreFilesMetaData(clusterService.localNode(), null);
    }
    try {
        return new NodeStoreFilesMetaData(clusterService.localNode(), listStoreMetaData(request.shardId));
    } catch (IOException e) {
        throw new ElasticsearchException("Failed to list store metadata for shard [" + request.shardId + "]", e);
    }
}
 
Example #16
Source File: InternalTestCluster.java    From crate with Apache License 2.0 6 votes vote down vote up
private void assertOpenTranslogReferences() throws Exception {
    assertBusy(() -> {
        for (NodeAndClient nodeAndClient : nodes.values()) {
            IndicesService indexServices = getInstance(IndicesService.class, nodeAndClient.name);
            for (IndexService indexService : indexServices) {
                for (IndexShard indexShard : indexService) {
                    try {
                        if (IndexShardTestCase.getEngine(indexShard) instanceof InternalEngine) {
                            IndexShardTestCase.getTranslog(indexShard).getDeletionPolicy().assertNoOpenTranslogRefs();
                        }
                    } catch (AlreadyClosedException ok) {
                        // all good
                    }
                }
            }
        }
    });
}
 
Example #17
Source File: IndicesClusterStateService.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private void applyAliases(ClusterChangedEvent event) {
    // check if aliases changed
    if (aliasesChanged(event)) {
        // go over and update aliases
        for (IndexMetaData indexMetaData : event.state().metaData()) {
            String index = indexMetaData.getIndex();
            IndexService indexService = indicesService.indexService(index);
            if (indexService == null) {
                // we only create / update here
                continue;
            }
            IndexAliasesService indexAliasesService = indexService.aliasesService();
            indexAliasesService.setAliases(indexMetaData.getAliases());
        }
    }
}
 
Example #18
Source File: IndicesClusterStateService.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void onFailedEngine(final ShardId shardId, final String reason, final @Nullable Throwable failure) {
    ShardRouting shardRouting = null;
    final IndexService indexService = indicesService.indexService(shardId.index().name());
    if (indexService != null) {
        IndexShard indexShard = indexService.shard(shardId.id());
        if (indexShard != null) {
            shardRouting = indexShard.routingEntry();
        }
    }
    if (shardRouting == null) {
        logger.warn("[{}][{}] engine failed, but can't find index shard. failure reason: [{}]", failure,
                shardId.index().name(), shardId.id(), reason);
        return;
    }
    final ShardRouting fShardRouting = shardRouting;
    threadPool.generic().execute(new Runnable() {
        @Override
        public void run() {
            synchronized (mutex) {
                failAndRemoveShard(fShardRouting, indexService, true, "engine failure, reason [" + reason + "]", failure);
            }
        }
    });
}
 
Example #19
Source File: TransportTermVectorsAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected TermVectorsResponse shardOperation(TermVectorsRequest request, ShardId shardId) {
    IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex());
    IndexShard indexShard = indexService.shardSafe(shardId.id());
    TermVectorsResponse response = indexShard.termVectorsService().getTermVectors(request, shardId.getIndex());
    response.updateTookInMillis(request.startTime());
    return response;
}
 
Example #20
Source File: InternalCountOperation.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public long count(TransactionContext txnCtx, Index index, int shardId, Symbol filter) throws IOException, InterruptedException {
    IndexService indexService;
    try {
        indexService = indicesService.indexServiceSafe(index);
    } catch (IndexNotFoundException e) {
        if (IndexParts.isPartitioned(index.getName())) {
            return 0L;
        }
        throw e;
    }

    IndexShard indexShard = indexService.getShard(shardId);
    try (Engine.Searcher searcher = indexShard.acquireSearcher("count-operation")) {
        String indexName = indexShard.shardId().getIndexName();
        var relationName = RelationName.fromIndexName(indexName);
        DocTableInfo table = schemas.getTableInfo(relationName, Operation.READ);
        LuceneQueryBuilder.Context queryCtx = queryBuilder.convert(
            filter,
            txnCtx,
            indexService.mapperService(),
            indexName,
            indexService.newQueryShardContext(),
            table,
            indexService.cache()
        );
        if (Thread.interrupted()) {
            throw new InterruptedException("thread interrupted during count-operation");
        }
        return searcher.searcher().count(queryCtx.query());
    }
}
 
Example #21
Source File: TransportShardMultiGetAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected MultiGetShardResponse shardOperation(MultiGetShardRequest request, ShardId shardId) {
    IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex());
    IndexShard indexShard = indexService.shardSafe(shardId.id());

    if (request.refresh() && !request.realtime()) {
        indexShard.refresh("refresh_flag_mget");
    }

    MultiGetShardResponse response = new MultiGetShardResponse();
    for (int i = 0; i < request.locations.size(); i++) {
        MultiGetRequest.Item item = request.items.get(i);
        try {
            GetResult getResult = indexShard.getService().get(item.type(), item.id(), item.fields(), request.realtime(), item.version(), item.versionType(), item.fetchSourceContext(), request.ignoreErrorsOnGeneratedFields());
            response.add(request.locations.get(i), new GetResponse(getResult));
        } catch (Throwable t) {
            if (TransportActions.isShardNotAvailableException(t)) {
                throw (ElasticsearchException) t;
            } else {
                logger.debug("{} failed to execute multi_get for [{}]/[{}]", t, shardId, item.type(), item.id());
                response.add(request.locations.get(i), new MultiGetResponse.Failure(request.index(), item.type(), item.id(), t));
            }
        }
    }

    return response;
}
 
Example #22
Source File: LangdetectMappingTests.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testSimpleMapping() throws Exception {
    IndexService indexService = createIndex("some_index", Settings.EMPTY,
            "someType", getMapping("simple-mapping.json"));
    DocumentMapper docMapper = indexService.mapperService().documentMapper("someType");
    String sampleText = copyToStringFromClasspath("english.txt");
    BytesReference json = BytesReference.bytes(XContentFactory.jsonBuilder()
            .startObject().field("someField", sampleText).endObject());
    SourceToParse sourceToParse = SourceToParse.source("some_index", "someType", "1", json, XContentType.JSON);
    ParsedDocument doc = docMapper.parse(sourceToParse);
    assertEquals(1, doc.rootDoc().getFields("someField").length);
    assertEquals("en", doc.rootDoc().getFields("someField")[0].stringValue());
}
 
Example #23
Source File: LangdetectMappingTests.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testToFields() throws Exception {
    IndexService indexService = createIndex("some_index", Settings.EMPTY,
            "someType", getMapping("mapping-to-fields.json"));
    DocumentMapper docMapper = indexService.mapperService().documentMapper("someType");
    String sampleText = copyToStringFromClasspath("english.txt");
    BytesReference json = BytesReference.bytes(XContentFactory.jsonBuilder()
            .startObject().field("someField", sampleText).endObject());
    SourceToParse sourceToParse = SourceToParse.source("some_index", "someType", "1", json, XContentType.JSON);
    ParsedDocument doc = docMapper.parse(sourceToParse);
    assertEquals(1, doc.rootDoc().getFields("someField").length);
    assertEquals("en", doc.rootDoc().getFields("someField")[0].stringValue());
    assertEquals(1, doc.rootDoc().getFields("english_field").length);
    assertEquals("This is a very small example of a text", doc.rootDoc().getFields("english_field")[0].stringValue());
}
 
Example #24
Source File: IndicesTTLService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the shards to purge, i.e. the local started primary shards that have ttl enabled and disable_purge to false
 */
private List<IndexShard> getShardsToPurge() {
    List<IndexShard> shardsToPurge = new ArrayList<>();
    MetaData metaData = clusterService.state().metaData();
    for (IndexService indexService : indicesService) {
        // check the value of disable_purge for this index
        IndexMetaData indexMetaData = metaData.index(indexService.index().name());
        if (indexMetaData == null) {
            continue;
        }
        boolean disablePurge = indexMetaData.getSettings().getAsBoolean(INDEX_TTL_DISABLE_PURGE, false);
        if (disablePurge) {
            continue;
        }

        // check if ttl is enabled for at least one type of this index
        boolean hasTTLEnabled = false;
        for (String type : indexService.mapperService().types()) {
            DocumentMapper documentType = indexService.mapperService().documentMapper(type);
            if (documentType.TTLFieldMapper().enabled()) {
                hasTTLEnabled = true;
                break;
            }
        }
        if (hasTTLEnabled) {
            for (IndexShard indexShard : indexService) {
                if (indexShard.state() == IndexShardState.STARTED && indexShard.routingEntry().primary() && indexShard.routingEntry().started()) {
                    shardsToPurge.add(indexShard);
                }
            }
        }
    }
    return shardsToPurge;
}
 
Example #25
Source File: IndicesService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an IndexService for the specified index if exists otherwise a {@link IndexNotFoundException} is thrown.
 */
public IndexService indexServiceSafe(String index) {
    IndexService indexService = indexService(index);
    if (indexService == null) {
        throw new IndexNotFoundException(index);
    }
    return indexService;
}
 
Example #26
Source File: IndicesService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an IndexService for the specified index if exists otherwise returns <code>null</code>.
 *
 */
@Nullable
public IndexService indexService(String index) {
    IndexServiceInjectorPair indexServiceInjectorPair = indices.get(index);
    if (indexServiceInjectorPair == null) {
        return null;
    } else {
        return indexServiceInjectorPair.getIndexService();
    }
}
 
Example #27
Source File: IndicesService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Iterator<IndexService> iterator() {
    return Iterators.transform(indices.values().iterator(), new Function<IndexServiceInjectorPair, IndexService>() {
        @Override
        public IndexService apply(IndexServiceInjectorPair input) {
            return input.getIndexService();
        }
    });
}
 
Example #28
Source File: InternalTestCluster.java    From crate with Apache License 2.0 5 votes vote down vote up
private void assertSameSyncIdSameDocs() {
    Map<String, Long> docsOnShards = new HashMap<>();
    final Collection<NodeAndClient> nodesAndClients = nodes.values();
    for (NodeAndClient nodeAndClient : nodesAndClients) {
        IndicesService indexServices = getInstance(IndicesService.class, nodeAndClient.name);
        for (IndexService indexService : indexServices) {
            for (IndexShard indexShard : indexService) {
                try {
                    CommitStats commitStats = indexShard.commitStats();
                    String syncId = commitStats.getUserData().get(Engine.SYNC_COMMIT_ID);
                    if (syncId != null) {
                        long liveDocsOnShard = commitStats.getNumDocs();
                        if (docsOnShards.get(syncId) != null) {
                            assertThat("sync id is equal but number of docs does not match on node "
                                + nodeAndClient.name + ". expected " + docsOnShards.get(syncId) + " but got "
                                + liveDocsOnShard, docsOnShards.get(syncId), equalTo(liveDocsOnShard));
                        } else {
                            docsOnShards.put(syncId, liveDocsOnShard);
                        }
                    }
                } catch (AlreadyClosedException e) {
                    // the engine is closed or if the shard is recovering
                }
            }
        }
    }
}
 
Example #29
Source File: ReferenceMappingTests.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testRefFromID() throws Exception {
    IndexService indexService = createIndex("docs", Settings.EMPTY,
            "docs", getMapping("ref-mapping-from-id.json"));
    DocumentMapper docMapper = indexService.mapperService().documentMapper("docs");
    BytesReference json = BytesReference.bytes(jsonBuilder().startObject()
            .field("title", "A title")
            .field("authorID", "1")
            .endObject());
    SourceToParse sourceToParse = SourceToParse.source("docs", "docs", "1", json, XContentType.JSON);
    ParseContext.Document doc = docMapper.parse(sourceToParse).rootDoc();
    assertEquals(1, doc.getFields("ref").length, 1);
    assertEquals("John Doe", doc.getFields("ref")[0].stringValue());
}
 
Example #30
Source File: IndicesStore.java    From crate with Apache License 2.0 5 votes vote down vote up
private IndexShard getShard(ShardActiveRequest request) {
    ClusterName thisClusterName = clusterService.getClusterName();
    if (!thisClusterName.equals(request.clusterName)) {
        LOGGER.trace("shard exists request meant for cluster[{}], but this is cluster[{}], ignoring request", request.clusterName, thisClusterName);
        return null;
    }
    ShardId shardId = request.shardId;
    IndexService indexService = indicesService.indexService(shardId.getIndex());
    if (indexService != null && indexService.indexUUID().equals(request.indexUUID)) {
        return indexService.getShardOrNull(shardId.id());
    }
    return null;
}