@Override public BytesReference getSourceRef() { return sourceRef(); }
@Override public byte[] source() { if (source == null) { return null; } if (sourceAsBytes != null) { return sourceAsBytes; } this.sourceAsBytes = sourceRef().toBytes(); return this.sourceAsBytes; }
@Override public String sourceAsString() { if (source == null) { return null; } try { return XContentHelper.convertToJson(sourceRef(), false); } catch (IOException e) { throw new ElasticsearchParseException("failed to convert source to a json string"); } }
SearchShardTarget shardTarget = new SearchShardTarget("1", "monitoring", 1); ShardSearchFailure[] shardFailures = new ShardSearchFailure[0]; float score = 0.2345f; BytesReference source = new BytesArray("{\"@timestamp\":\"2014-08-20T15:43:20.762Z\",\"category_name\"" + ":\"cat1111\",\"alert_message\":\"the new cpu threshold has been reached 80%\",\"alert_type\":" + "\"Critical\",\"view_mode\":\"unread\"}"); InternalSearchHit hit = new InternalSearchHit(1, "5YmRf-6OTvelt29V5dphmw", new StringText("quota-management"), null); hit.shardTarget(shardTarget); hit.sourceRef(source); hit.score(score); InternalSearchHit[] hits = new InternalSearchHit[]{hit}; InternalSearchHits internalSearchHits = new InternalSearchHits(hits, 28, score); InternalSearchResponse internalSearchResponse = new InternalSearchResponse(internalSearchHits, null, null, null, false); SearchResponse searchResponse = new SearchResponse(internalSearchResponse, "scrollId", 1, 1, 1000, shardFailures);
internalSearchHit.sourceRef(source);
internalSearchHit.sourceRef(source);
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { BytesReference source = hit.sourceRef(); XContentType contentType = XContentFactory.xContentType(source); XContentParser parser = XContentFactory.xContent(contentType).createParser(source); try { parser.nextToken(); builder.field(Fields._SOURCE); builder.copyCurrentStructure(parser); } finally { parser.close(); } return builder; } }
@Override public void hitExecute(SearchContext context, HitContext hitContext) { FetchSourceContext fetchSourceContext = context.fetchSourceContext(); assert fetchSourceContext.fetchSource(); if (fetchSourceContext.includes().length == 0 && fetchSourceContext.excludes().length == 0) { hitContext.hit().sourceRef(context.lookup().source().internalSourceRef()); return; } SourceLookup source = context.lookup().source(); Object value = source.filter(fetchSourceContext.includes(), fetchSourceContext.excludes()); try { final int initialCapacity = Math.min(1024, source.internalSourceRef().length()); BytesStreamOutput streamOutput = new BytesStreamOutput(initialCapacity); XContentBuilder builder = new XContentBuilder(context.lookup().source().sourceContentType().xContent(), streamOutput); builder.value(value); hitContext.hit().sourceRef(builder.bytes()); } catch (IOException e) { throw new ElasticsearchException("Error filtering source", e); } } }