private static IndexRequest newIndexRequest(IndexPermissions dto, IndexType indexType) { Map<String, Object> doc = new HashMap<>(); if (dto.isAllowAnyone()) { doc.put(IndexAuthorizationConstants.FIELD_ALLOW_ANYONE, true); // no need to feed users and groups } else { doc.put(IndexAuthorizationConstants.FIELD_ALLOW_ANYONE, false); doc.put(IndexAuthorizationConstants.FIELD_GROUP_IDS, dto.getGroupIds()); doc.put(IndexAuthorizationConstants.FIELD_USER_IDS, dto.getUserIds()); } return new IndexRequest(indexType.getIndex(), indexType.getType()) .id(dto.getProjectUuid()) .routing(dto.getProjectUuid()) .source(doc); }
public void addBulkRequest(String id, XContentBuilder source, String routing, String parent) { bulkProcessor.add(indexRequest(index).type(type).id(id).source(source).routing(routing).parent(parent)); insertedDocuments.incrementAndGet(); }
public void addBulkRequest(String id, Map<?, ?> source, String routing, String parent) { bulkProcessor.add(indexRequest(index).type(type).id(id).source(source).routing(routing).parent(parent)); insertedDocuments.incrementAndGet(); }
private static IndexRequest newIndexRequest(ProjectMeasuresDoc doc) { String projectUuid = doc.getId(); return new IndexRequest(INDEX_TYPE_PROJECT_MEASURES.getIndex(), INDEX_TYPE_PROJECT_MEASURES.getType(), projectUuid) .routing(projectUuid) .parent(projectUuid) .source(doc.getFields()); }
private static IndexRequest newIndexRequest(ComponentDoc doc) { return new IndexRequest(INDEX_TYPE_COMPONENT.getIndex(), INDEX_TYPE_COMPONENT.getType(), doc.getId()) .routing(doc.getRouting()) .parent(doc.getParent()) .source(doc.getFields()); }
private static IndexRequest newRuleDocIndexRequest(RuleForIndexingDto ruleForIndexingDto) { RuleDoc doc = RuleDoc.of(ruleForIndexingDto); return new IndexRequest(INDEX_TYPE_RULE.getIndex(), INDEX_TYPE_RULE.getType()) .id(doc.getId()) .routing(doc.getRouting()) .source(doc.getFields()); }
/** * Controls the shard routing of the request. Using this value to hash the shard * and not the id. */ public IndexRequestBuilder setRouting(String routing) { request.routing(routing); return this; }
private static IndexRequest newRuleExtensionDocIndexRequest(RuleExtensionForIndexingDto ruleExtensionForIndexingDto) { RuleExtensionDoc doc = RuleExtensionDoc.of(ruleExtensionForIndexingDto); return new IndexRequest(INDEX_TYPE_RULE_EXTENSION.getIndex(), INDEX_TYPE_RULE_EXTENSION.getType()) .id(doc.getId()) .routing(doc.getRouting()) .parent(doc.getParent()) .source(doc.getFields()); }
private static IndexRequest newRuleExtensionDocIndexRequest(RuleForIndexingDto ruleForIndexingDto) { RuleExtensionDoc ruleExtensionDoc = RuleExtensionDoc.of(ruleForIndexingDto); return new IndexRequest(INDEX_TYPE_RULE_EXTENSION.getIndex(), INDEX_TYPE_RULE_EXTENSION.getType()) .id(ruleExtensionDoc.getId()) .routing(ruleExtensionDoc.getRouting()) .parent(ruleExtensionDoc.getParent()) .source(ruleExtensionDoc.getFields()); }
private static IndexRequest newIndexRequest(UserDto user, ListMultimap<String, String> organizationUuidsByUserUuid) { UserDoc doc = new UserDoc(Maps.newHashMapWithExpectedSize(8)); // all the keys must be present, even if value is null doc.setUuid(user.getUuid()); doc.setLogin(user.getLogin()); doc.setName(user.getName()); doc.setEmail(user.getEmail()); doc.setActive(user.isActive()); doc.setScmAccounts(UserDto.decodeScmAccounts(user.getScmAccounts())); doc.setOrganizationUuids(organizationUuidsByUserUuid.get(user.getUuid())); return new IndexRequest(INDEX_TYPE_USER.getIndex(), INDEX_TYPE_USER.getType()) .id(doc.getId()) .routing(doc.getRouting()) .source(doc.getFields()); } }
public void putDocuments(IndexType indexType, BaseDoc... docs) { try { BulkRequestBuilder bulk = SHARED_NODE.client().prepareBulk() .setRefreshPolicy(REFRESH_IMMEDIATE); for (BaseDoc doc : docs) { bulk.add(new IndexRequest(indexType.getIndex(), indexType.getType(), doc.getId()) .parent(doc.getParent()) .routing(doc.getRouting()) .source(doc.getFields())); } BulkResponse bulkResponse = bulk.get(); if (bulkResponse.hasFailures()) { throw new IllegalStateException(bulkResponse.buildFailureMessage()); } } catch (Exception e) { throw Throwables.propagate(e); } }
/** * Set the routing to decide which shard the documents need to be routed to */ public ReindexRequest setDestRouting(String routing) { this.getDestination().routing(routing); return this; }
private static IndexRequest newIndexRequest(IndexedActiveRuleDto dto) { ActiveRuleDoc doc = new ActiveRuleDoc(dto.getId()) .setRuleId(dto.getRuleId()) .setRuleProfileUuid(dto.getRuleProfileUuid()) .setSeverity(SeverityUtil.getSeverityFromOrdinal(dto.getSeverity())); // all the fields must be present, even if value is null String inheritance = dto.getInheritance(); doc.setInheritance(inheritance == null ? ActiveRuleInheritance.NONE.name() : inheritance); return new IndexRequest(INDEX_TYPE_ACTIVE_RULE.getIndex(), INDEX_TYPE_ACTIVE_RULE.getType()) .id(doc.getId()) .parent(doc.getParent()) .routing(doc.getRouting()) .source(doc.getFields()); }
public void resolveRouting(MetaData metaData) { routing(metaData.resolveWriteIndexRouting(parent, routing, index)); }
public static Request index(IndexRequest indexRequest) { String method = Strings.hasLength(indexRequest.id()) ? HttpMethod.PUT.name() : HttpMethod.POST.name(); boolean isCreate = (indexRequest.opType() == DocWriteRequest.OpType.CREATE); String endpoint = endpoint(indexRequest.index(), indexRequest.type(), indexRequest.id(), isCreate ? "_create" : null); Request request = new Request(method, endpoint); Params parameters = new Params(request); parameters.withRouting(indexRequest.routing()); parameters.withTimeout(indexRequest.timeout()); parameters.withVersion(indexRequest.version()); parameters.withVersionType(indexRequest.versionType()); parameters.withPipeline(indexRequest.getPipeline()); parameters.withRefreshPolicy(indexRequest.getRefreshPolicy()); parameters.withWaitForActiveShards(indexRequest.waitForActiveShards()); BytesRef source = indexRequest.source().toBytesRef(); ContentType contentType = createContentType(indexRequest.getContentType()); request.setEntity(new ByteArrayEntity(source.bytes, source.offset, source.length, contentType)); return request; }
/** * Calculate a routing value to be used, either the included index request's routing, or retrieved document's routing when defined. */ @Nullable static String calculateRouting(GetResult getResult, @Nullable IndexRequest updateIndexRequest) { if (updateIndexRequest != null && updateIndexRequest.routing() != null) { return updateIndexRequest.routing(); } else if (getResult.getFields().containsKey(RoutingFieldMapper.NAME)) { return getResult.field(RoutingFieldMapper.NAME).getValue().toString(); } else { return null; } }
@Override public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { IndexRequest indexRequest = new IndexRequest(request.param("index"), request.param("type"), request.param("id")); indexRequest.routing(request.param("routing")); indexRequest.parent(request.param("parent")); indexRequest.setPipeline(request.param("pipeline")); indexRequest.source(request.requiredContent(), request.getXContentType()); indexRequest.timeout(request.paramAsTime("timeout", IndexRequest.DEFAULT_TIMEOUT)); indexRequest.setRefreshPolicy(request.param("refresh")); indexRequest.version(RestActions.parseVersion(request)); indexRequest.versionType(VersionType.fromString(request.param("version_type"), indexRequest.versionType())); indexRequest.setIfSeqNo(request.paramAsLong("if_seq_no", indexRequest.ifSeqNo())); indexRequest.setIfPrimaryTerm(request.paramAsLong("if_primary_term", indexRequest.ifPrimaryTerm())); String sOpType = request.param("op_type"); String waitForActiveShards = request.param("wait_for_active_shards"); if (waitForActiveShards != null) { indexRequest.waitForActiveShards(ActiveShardCount.parseString(waitForActiveShards)); } if (sOpType != null) { indexRequest.opType(sOpType); } return channel -> client.index(indexRequest, new RestStatusToXContentListener<>(channel, r -> r.getLocation(indexRequest.routing()))); }
/** Executes index operation on primary shard after updates mapping if dynamic mappings are found */ private static void executeIndexRequestOnPrimary(BulkPrimaryExecutionContext context, MappingUpdatePerformer mappingUpdater) throws Exception { final IndexRequest request = context.getRequestToExecute(); final IndexShard primary = context.getPrimary(); final SourceToParse sourceToParse = SourceToParse.source(request.index(), request.type(), request.id(), request.source(), request.getContentType()) .routing(request.routing()).parent(request.parent()); executeOnPrimaryWhileHandlingMappingUpdates(context, () -> primary.applyIndexOperationOnPrimary(request.version(), request.versionType(), sourceToParse, request.ifSeqNo(), request.ifPrimaryTerm(), request.getAutoGeneratedTimestamp(), request.isRetry()), e -> primary.getFailedIndexResult(e, request.version()), context::markOperationAsExecuted, mapping -> mappingUpdater.updateMappings(mapping, primary.shardId(), request.type())); }
/** * Prepare the request for merging the existing document with a new one, can optionally detect a noop change. Returns a {@code Result} * containing a new {@code IndexRequest} to be executed on the primary and replicas. */ Result prepareUpdateIndexRequest(ShardId shardId, UpdateRequest request, GetResult getResult, boolean detectNoop) { final long updateVersion = calculateUpdateVersion(request, getResult); final IndexRequest currentRequest = request.doc(); final String routing = calculateRouting(getResult, currentRequest); final String parent = calculateParent(getResult, currentRequest); final Tuple<XContentType, Map<String, Object>> sourceAndContent = XContentHelper.convertToMap(getResult.internalSourceRef(), true); final XContentType updateSourceContentType = sourceAndContent.v1(); final Map<String, Object> updatedSourceAsMap = sourceAndContent.v2(); final boolean noop = !XContentHelper.update(updatedSourceAsMap, currentRequest.sourceAsMap(), detectNoop); // We can only actually turn the update into a noop if detectNoop is true to preserve backwards compatibility and to handle cases // where users repopulating multi-fields or adding synonyms, etc. if (detectNoop && noop) { UpdateResponse update = new UpdateResponse(shardId, getResult.getType(), getResult.getId(), getResult.getVersion(), DocWriteResponse.Result.NOOP); update.setGetResult(extractGetResult(request, request.index(), getResult.getSeqNo(), getResult.getPrimaryTerm(), getResult.getVersion(), updatedSourceAsMap, updateSourceContentType, getResult.internalSourceRef())); return new Result(update, DocWriteResponse.Result.NOOP, updatedSourceAsMap, updateSourceContentType); } else { final IndexRequest finalIndexRequest = Requests.indexRequest(request.index()) .type(request.type()).id(request.id()).routing(routing).parent(parent) .source(updatedSourceAsMap, updateSourceContentType).version(updateVersion).versionType(request.versionType()) .waitForActiveShards(request.waitForActiveShards()).timeout(request.timeout()) .setRefreshPolicy(request.getRefreshPolicy()); return new Result(finalIndexRequest, DocWriteResponse.Result.UPDATED, updatedSourceAsMap, updateSourceContentType); } }