org.elasticsearch.index.get.GetResult Java Examples

The following examples show how to use org.elasticsearch.index.get.GetResult. 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: ExplainRequestBuilder.java    From elasticshell with Apache License 2.0 6 votes vote down vote up
@Override
protected XContentBuilder toXContent(ExplainRequest request, ExplainResponse response, XContentBuilder builder) throws IOException {
    builder.startObject();
    builder.field(Fields.OK, response.isExists())
            .field(Fields._INDEX, request.index())
            .field(Fields._TYPE, request.type())
            .field(Fields._ID, request.id())
            .field(Fields.MATCHED, response.isMatch());

    if (response.hasExplanation()) {
        builder.startObject(Fields.EXPLANATION);
        buildExplanation(builder, response.getExplanation());
        builder.endObject();
    }
    GetResult getResult = response.getGetResult();
    if (getResult != null) {
        builder.startObject(Fields.GET);
        getResult.toXContentEmbedded(builder, ToXContent.EMPTY_PARAMS);
        builder.endObject();
    }
    builder.endObject();
    return builder;
}
 
Example #2
Source File: TestHelpers.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public static GetResponse createGetResponse(ToXContentObject o, String id) throws IOException {
    XContentBuilder content = o.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS);

    return new GetResponse(
        new GetResult(
            AnomalyDetector.ANOMALY_DETECTORS_INDEX,
            MapperService.SINGLE_MAPPING_NAME,
            id,
            UNASSIGNED_SEQ_NO,
            0,
            -1,
            true,
            BytesReference.bytes(content),
            Collections.emptyMap(),
            Collections.emptyMap()
        )
    );
}
 
Example #3
Source File: ElasticSearchServiceMapper.java    From vertx-elasticsearch-service with Apache License 2.0 6 votes vote down vote up
private static com.hubrick.vertx.elasticsearch.model.GetResult mapToGetResult(GetResult esGetResult) {
    final com.hubrick.vertx.elasticsearch.model.GetResult getResult = new com.hubrick.vertx.elasticsearch.model.GetResult()
            .setId(esGetResult.getId())
            .setIndex(esGetResult.getIndex())
            .setType(esGetResult.getType())
            .setVersion(esGetResult.getVersion())
            .setExists(esGetResult.isExists());

    if (esGetResult.getFields() != null) {
        getResult.setFields(
                esGetResult.getFields()
                        .entrySet()
                        .stream()
                        .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().getValues()))
        );
    }

    if (esGetResult.getSource() != null) {
        getResult.setSource(new JsonObject(esGetResult.getSource()));
    }

    return getResult;
}
 
Example #4
Source File: ElasticSearchServiceMapper.java    From vertx-elasticsearch-service with Apache License 2.0 6 votes vote down vote up
private static com.hubrick.vertx.elasticsearch.model.GetResult mapToGetResult(GetResponse getResponse) {
    final com.hubrick.vertx.elasticsearch.model.GetResult getResult = new com.hubrick.vertx.elasticsearch.model.GetResult()
            .setId(getResponse.getId())
            .setIndex(getResponse.getIndex())
            .setType(getResponse.getType())
            .setVersion(getResponse.getVersion())
            .setExists(getResponse.isExists());

    if (getResponse.getFields() != null) {
        getResult.setFields(
                getResponse.getFields()
                        .entrySet()
                        .stream()
                        .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().getValues()))
        );
    }

    if (getResponse.getSource() != null) {
        getResult.setSource(new JsonObject(getResponse.getSource()));
    }

    return getResult;
}
 
Example #5
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 #6
Source File: KibanaSeedTest.java    From openshift-elasticsearch-plugin with Apache License 2.0 5 votes vote down vote up
private void givenKibanaConfigWithDefaultIndex(String index) {
    GetResponse response = new GetResponse(new GetResult(context.getKibanaIndex(), 
            "config", ConfigurationSettings.DEFAULT_KIBANA_VERSION, 1L, true, 
            new BytesArray("{\"defaultIndex\":\"" + index + "\"}"), null));
    when(pluginClient.getDocument(eq(context.getKibanaIndex()), eq("config"),
            eq(ConfigurationSettings.DEFAULT_KIBANA_VERSION))).thenReturn(response);
}
 
Example #7
Source File: KibanaSeedTest.java    From openshift-elasticsearch-plugin with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    KibanaUtils utils = new KibanaUtils(settings, pluginClient);
    seeder = new KibanaSeed(settings, loader, pluginClient, utils);
    context = new OpenshiftRequestContextFactory.OpenshiftRequestContext(USER, TOKEN, true, 
            new HashSet<Project>(), ".kibana_123", KibanaIndexMode.SHARED_OPS, Collections.emptyList());
    when(loader.getOperationsMappingsTemplate()).thenReturn("{\"foo\":\"bar\"");
    when(pluginClient.updateDocument(anyString(), anyString(), anyString(), anyString())).thenReturn(mock(UpdateResponse.class));
   
    GetResponse response = new GetResponse(new GetResult(context.getKibanaIndex(), 
            "config", ConfigurationSettings.DEFAULT_KIBANA_VERSION, 1L, false, 
            new BytesArray("{\"defaultIndex\":\"\"}"), null));
    when(pluginClient.getDocument(eq(context.getKibanaIndex()), eq("config"),
            eq(ConfigurationSettings.DEFAULT_KIBANA_VERSION))).thenReturn(response);
}
 
Example #8
Source File: UpdateHelper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Prepares an update request by converting it into an index or delete request or an update response (no action).
 */
@SuppressWarnings("unchecked")
public Result prepare(UpdateRequest request, IndexShard indexShard) {
    final GetResult getResult = indexShard.getService().get(request.type(), request.id(),
            new String[]{RoutingFieldMapper.NAME, ParentFieldMapper.NAME, TTLFieldMapper.NAME, TimestampFieldMapper.NAME},
            true, request.version(), request.versionType(), FetchSourceContext.FETCH_SOURCE, false);
    return prepare(request, getResult);
}
 
Example #9
Source File: UpdateResponse.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    index = in.readString();
    type = in.readString();
    id = in.readString();
    version = in.readLong();
    created = in.readBoolean();
    if (in.readBoolean()) {
        getResult = GetResult.readGetResult(in);
    }
}
 
Example #10
Source File: TransportGetAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected GetResponse shardOperation(GetRequest request, ShardId shardId) {
    IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex());
    IndexShard indexShard = indexService.shardSafe(shardId.id());

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

    GetResult result = indexShard.getService().get(request.type(), request.id(), request.fields(),
            request.realtime(), request.version(), request.versionType(), request.fetchSourceContext(), request.ignoreErrorsOnGeneratedFields());
    return new GetResponse(result);
}
 
Example #11
Source File: ExplainResponse.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    index = in.readString();
    type = in.readString();
    id = in.readString();
    exists = in.readBoolean();
    if (in.readBoolean()) {
        explanation = readExplanation(in);
    }
    if (in.readBoolean()) {
        getResult = GetResult.readGetResult(in);
    }
}
 
Example #12
Source File: ShardTermVectorsService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private Fields addGeneratedTermVectors(Engine.GetResult get, Fields termVectorsByField, TermVectorsRequest request, Set<String> selectedFields) throws IOException {
    /* only keep valid fields */
    Set<String> validFields = new HashSet<>();
    for (String field : selectedFields) {
        MappedFieldType fieldType = indexShard.mapperService().smartNameFieldType(field);
        if (!isValidField(fieldType)) {
            continue;
        }
        // already retrieved, only if the analyzer hasn't been overridden at the field
        if (fieldType.storeTermVectors() &&
                (request.perFieldAnalyzer() == null || !request.perFieldAnalyzer().containsKey(field))) {
            continue;
        }
        validFields.add(field);
    }

    if (validFields.isEmpty()) {
        return termVectorsByField;
    }

    /* generate term vectors from fetched document fields */
    GetResult getResult = indexShard.getService().get(
            get, request.id(), request.type(), validFields.toArray(Strings.EMPTY_ARRAY), null, false);
    Fields generatedTermVectors = generateTermVectors(getResult.getFields().values(), request.offsets(), request.perFieldAnalyzer(), validFields);

    /* merge with existing Fields */
    if (termVectorsByField == null) {
        return generatedTermVectors;
    } else {
        return mergeFields(termVectorsByField, generatedTermVectors);
    }
}
 
Example #13
Source File: TransportShardUpsertAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Function<GetResult, Object> build(final Reference reference, SymbolToFieldExtractor.Context context) {
    return new Function<GetResult, Object>() {
        @Override
        public Object apply(GetResult getResult) {
            if (getResult == null) {
                return null;
            }
            return reference.valueType().value(XContentMapValues.extractValue(
                    reference.info().ident().columnIdent().fqn(), getResult.sourceAsMap()));
        }
    };
}
 
Example #14
Source File: TransportShardUpsertAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private void processGeneratedColumns(final DocTableInfo tableInfo,
                                     Map<String, Object> updatedColumns,
                                     Map<String, Object> updatedGeneratedColumns,
                                     boolean validateExpressionValue,
                                     @Nullable GetResult getResult) {
    SymbolToFieldExtractorContext ctx = new SymbolToFieldExtractorContext(functions, updatedColumns);

    for (GeneratedReferenceInfo referenceInfo : tableInfo.generatedColumns()) {
        // partitionedBy columns cannot be updated
        if (!tableInfo.partitionedByColumns().contains(referenceInfo)) {
            Object givenValue = updatedGeneratedColumns.get(referenceInfo.ident().columnIdent().fqn());
            if ((givenValue != null && validateExpressionValue)
                ||
                generatedExpressionEvaluationNeeded(referenceInfo.referencedReferenceInfos(), updatedColumns.keySet())) {
                // at least one referenced column was updated, need to evaluate expression and update column
                Function<GetResult, Object> extractor = SYMBOL_TO_FIELD_EXTRACTOR.convert(referenceInfo.generatedExpression(), ctx);
                Object value = extractor.apply(getResult);
                if (givenValue == null) {
                    // add column & value
                    updatedColumns.put(referenceInfo.ident().columnIdent().fqn(), value);
                } else if (validateExpressionValue && referenceInfo.type().compareValueTo(value, givenValue) != 0) {
                    throw new IllegalArgumentException(String.format(Locale.ENGLISH,
                            "Given value %s for generated column does not match defined generated expression value %s",
                            givenValue, value));
                }
            }
        }
    }
}
 
Example #15
Source File: AuditLogImpl.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
@Override
public void logDocumentWritten(ShardId shardId, GetResult originalResult, Index currentIndex, IndexResult result,
							   ComplianceConfig complianceConfig) {
	if (enabled) {
		super.logDocumentWritten(shardId, originalResult, currentIndex, result, complianceConfig);
	}
}
 
Example #16
Source File: ComplianceIndexingOperationListenerImpl.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
@Override
public Index preIndex(final ShardId shardId, final Index index) {
    if(complianceConfig.isEnabled() && complianceConfig.logDiffsForWrite()) {
        Objects.requireNonNull(is);

        final IndexShard shard;

        if (index.origin() != org.elasticsearch.index.engine.Engine.Operation.Origin.PRIMARY) {
            return index;
        }

        if((shard = is.getShardOrNull(shardId.getId())) == null) {
            return index;
        }

        if (shard.isReadAllowed()) {
            try {

                final GetResult getResult = shard.getService().getForUpdate(index.type(), index.id(),
                        index.getIfSeqNo(), index.getIfPrimaryTerm());

                if (getResult.isExists()) {
                    threadContext.set(new Context(getResult));
                } else {
                    threadContext.set(new Context(null));
                }
            } catch (Exception e) {
                if (log.isDebugEnabled()) {
                    log.debug("Cannot retrieve original document due to {}", e.toString());
                }
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Cannot read from shard {}", shardId);
            }
        }
    }

    return index;
}
 
Example #17
Source File: ExplainResponse.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public GetResult getGetResult() {
    return getResult;
}
 
Example #18
Source File: TransportExplainAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected ExplainResponse shardOperation(ExplainRequest request, ShardId shardId) {
    IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex());
    IndexShard indexShard = indexService.shardSafe(shardId.id());
    Term uidTerm = new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(request.type(), request.id()));
    Engine.GetResult result = indexShard.get(new Engine.Get(false, uidTerm));
    if (!result.exists()) {
        return new ExplainResponse(shardId.getIndex(), request.type(), request.id(), false);
    }

    SearchContext context = new DefaultSearchContext(
            0, new ShardSearchLocalRequest(new String[]{request.type()}, request.nowInMillis, request.filteringAlias()),
            null, result.searcher(), indexService, indexShard,
            scriptService, pageCacheRecycler,
            bigArrays, threadPool.estimatedTimeInMillisCounter(), parseFieldMatcher,
            SearchService.NO_TIMEOUT
    );
    SearchContext.setCurrent(context);

    try {
        context.parsedQuery(indexService.queryParserService().parseQuery(request.source()));
        context.preProcess();
        int topLevelDocId = result.docIdAndVersion().docId + result.docIdAndVersion().context.docBase;
        Explanation explanation = context.searcher().explain(context.query(), topLevelDocId);
        for (RescoreSearchContext ctx : context.rescore()) {
            Rescorer rescorer = ctx.rescorer();
            explanation = rescorer.explain(topLevelDocId, context, ctx, explanation);
        }
        if (request.fields() != null || (request.fetchSourceContext() != null && request.fetchSourceContext().fetchSource())) {
            // Advantage is that we're not opening a second searcher to retrieve the _source. Also
            // because we are working in the same searcher in engineGetResult we can be sure that a
            // doc isn't deleted between the initial get and this call.
            GetResult getResult = indexShard.getService().get(result, request.id(), request.type(), request.fields(), request.fetchSourceContext(), false);
            return new ExplainResponse(shardId.getIndex(), request.type(), request.id(), true, explanation, getResult);
        } else {
            return new ExplainResponse(shardId.getIndex(), request.type(), request.id(), true, explanation);
        }
    } catch (IOException e) {
        throw new ElasticsearchException("Could not explain", e);
    } finally {
        context.close();
        SearchContext.removeCurrent();
    }
}
 
Example #19
Source File: ComplianceIndexingOperationListenerImpl.java    From deprecated-security-advanced-modules with Apache License 2.0 4 votes vote down vote up
public Context(GetResult getResult) {
    super();
    this.getResult = getResult;
}
 
Example #20
Source File: GetResponse.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public GetResponse(GetResult getResult) {
    this.getResult = getResult;
}
 
Example #21
Source File: GetResponse.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    getResult = GetResult.readGetResult(in);
}
 
Example #22
Source File: ExplainResponse.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public ExplainResponse(String index, String type, String id, boolean exists, Explanation explanation, GetResult getResult) {
    this(index, type, id, exists, explanation);
    this.getResult = getResult;
}
 
Example #23
Source File: UpdateResponse.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public void setGetResult(GetResult getResult) {
    this.getResult = getResult;
}
 
Example #24
Source File: UpdateResponse.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public GetResult getGetResult() {
    return this.getResult;
}
 
Example #25
Source File: TransportShardUpsertAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Prepares an update request by converting it into an index request.
 * <p/>
 * TODO: detect a NOOP and return an update response if true
 */
@SuppressWarnings("unchecked")
private SourceAndVersion prepareUpdate(DocTableInfo tableInfo,
                                       ShardUpsertRequest request,
                                       ShardUpsertRequest.Item item,
                                       IndexShard indexShard) throws ElasticsearchException {
    final GetResult getResult = indexShard.getService().get(request.type(), item.id(),
            new String[]{RoutingFieldMapper.NAME, ParentFieldMapper.NAME, TTLFieldMapper.NAME},
            true, Versions.MATCH_ANY, VersionType.INTERNAL, FetchSourceContext.FETCH_SOURCE, false);

    if (!getResult.isExists()) {
        throw new DocumentMissingException(new ShardId(request.index(), request.shardId().id()), request.type(), item.id());
    }

    if (getResult.internalSourceRef() == null) {
        // no source, we can't do nothing, through a failure...
        throw new DocumentSourceMissingException(new ShardId(request.index(), request.shardId().id()), request.type(), item.id());
    }

    if (item.version() != Versions.MATCH_ANY && item.version() != getResult.getVersion()) {
        throw new VersionConflictEngineException(
                indexShard.shardId(), Constants.DEFAULT_MAPPING_TYPE, item.id(), getResult.getVersion(), item.version());
    }

    Tuple<XContentType, Map<String, Object>> sourceAndContent = XContentHelper.convertToMap(getResult.internalSourceRef(), true);
    final Map<String, Object> updatedSourceAsMap;
    final XContentType updateSourceContentType = sourceAndContent.v1();

    updatedSourceAsMap = sourceAndContent.v2();

    SymbolToFieldExtractorContext ctx = new SymbolToFieldExtractorContext(functions, item.insertValues());

    Map<String, Object> pathsToUpdate = new LinkedHashMap<>();
    Map<String, Object> updatedGeneratedColumns = new LinkedHashMap<>();
    for (int i = 0; i < request.updateColumns().length; i++) {
        /**
         * NOTE: mapping isn't applied. So if an Insert was done using the ES Rest Endpoint
         * the data might be returned in the wrong format (date as string instead of long)
         */
        String columnPath = request.updateColumns()[i];
        Object value = SYMBOL_TO_FIELD_EXTRACTOR.convert(item.updateAssignments()[i], ctx).apply(getResult);
        ReferenceInfo referenceInfo = tableInfo.getReferenceInfo(ColumnIdent.fromPath(columnPath));
        if (referenceInfo instanceof GeneratedReferenceInfo) {
            updatedGeneratedColumns.put(columnPath, value);

        } else {
            pathsToUpdate.put(columnPath, value);
        }
    }

    processGeneratedColumns(tableInfo, pathsToUpdate, updatedGeneratedColumns, request.validateGeneratedColumns(), getResult);

    updateSourceByPaths(updatedSourceAsMap, pathsToUpdate);

    try {
        XContentBuilder builder = XContentFactory.contentBuilder(updateSourceContentType);
        builder.map(updatedSourceAsMap);
        return new SourceAndVersion(builder.bytes(), getResult.getVersion());
    } catch (IOException e) {
        throw new ElasticsearchGenerationException("Failed to generate [" + updatedSourceAsMap + "]", e);
    }
}
 
Example #26
Source File: ComplianceIndexingOperationListenerImpl.java    From deprecated-security-advanced-modules with Apache License 2.0 4 votes vote down vote up
public GetResult getGetResult() {
    return getResult;
}