Java源码示例:org.elasticsearch.index.get.GetResult
示例1
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()
)
);
}
示例2
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;
}
示例3
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;
}
示例4
@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;
}
示例5
@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;
}
示例6
@Override
public void logDocumentWritten(ShardId shardId, GetResult originalResult, Index currentIndex, IndexResult result,
ComplianceConfig complianceConfig) {
if (enabled) {
super.logDocumentWritten(shardId, originalResult, currentIndex, result, complianceConfig);
}
}
示例7
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));
}
}
}
}
}
示例8
@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()));
}
};
}
示例9
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);
}
}
示例10
@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);
}
}
示例11
@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;
}
示例12
@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);
}
示例13
@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);
}
}
示例14
/**
* 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);
}
示例15
@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);
}
示例16
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);
}
示例17
public Context(GetResult getResult) {
super();
this.getResult = getResult;
}
示例18
public GetResult getGetResult() {
return getResult;
}
示例19
/**
* 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);
}
}
示例20
public ExplainResponse(String index, String type, String id, boolean exists, Explanation explanation, GetResult getResult) {
this(index, type, id, exists, explanation);
this.getResult = getResult;
}
示例21
public GetResult getGetResult() {
return getResult;
}
示例22
@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();
}
}
示例23
public GetResponse(GetResult getResult) {
this.getResult = getResult;
}
示例24
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
getResult = GetResult.readGetResult(in);
}
示例25
public void setGetResult(GetResult getResult) {
this.getResult = getResult;
}
示例26
public GetResult getGetResult() {
return this.getResult;
}