GetResult.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Licensed to Elasticsearch under one or more contributor
  3. * license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright
  5. * ownership. Elasticsearch licenses this file to you under
  6. * the Apache License, Version 2.0 (the "License"); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.elasticsearch.index.get;
  20. import com.google.common.collect.ImmutableMap;
  21. import org.elasticsearch.ElasticsearchParseException;
  22. import org.elasticsearch.common.bytes.BytesReference;
  23. import org.elasticsearch.common.compress.CompressorFactory;
  24. import org.elasticsearch.common.io.stream.StreamInput;
  25. import org.elasticsearch.common.io.stream.StreamOutput;
  26. import org.elasticsearch.common.io.stream.Streamable;
  27. import org.elasticsearch.common.xcontent.ToXContent;
  28. import org.elasticsearch.common.xcontent.XContentBuilder;
  29. import org.elasticsearch.common.xcontent.XContentBuilderString;
  30. import org.elasticsearch.common.xcontent.XContentHelper;
  31. import org.elasticsearch.search.lookup.SourceLookup;
  32. import java.io.IOException;
  33. import java.util.Iterator;
  34. import java.util.Map;
  35. import static com.google.common.collect.Iterators.emptyIterator;
  36. import static com.google.common.collect.Maps.newHashMapWithExpectedSize;
  37. import static org.elasticsearch.index.get.GetField.readGetField;
  38. /**
  39. */
  40. public class GetResult implements Streamable, Iterable<GetField>, ToXContent {
  41. private String index;
  42. private String type;
  43. private String id;
  44. private long version;
  45. private boolean exists;
  46. private Map<String, GetField> fields;
  47. private Map<String, Object> sourceAsMap;
  48. private BytesReference source;
  49. private byte[] sourceAsBytes;
  50. GetResult() {
  51. }
  52. public GetResult(String index, String type, String id, long version, boolean exists, BytesReference source, Map<String, GetField> fields) {
  53. this.index = index;
  54. this.type = type;
  55. this.id = id;
  56. this.version = version;
  57. this.exists = exists;
  58. this.source = source;
  59. this.fields = fields;
  60. if (this.fields == null) {
  61. this.fields = ImmutableMap.of();
  62. }
  63. }
  64. /**
  65. * Does the document exists.
  66. */
  67. public boolean isExists() {
  68. return exists;
  69. }
  70. /**
  71. * The index the document was fetched from.
  72. */
  73. public String getIndex() {
  74. return index;
  75. }
  76. /**
  77. * The type of the document.
  78. */
  79. public String getType() {
  80. return type;
  81. }
  82. /**
  83. * The id of the document.
  84. */
  85. public String getId() {
  86. return id;
  87. }
  88. /**
  89. * The version of the doc.
  90. */
  91. public long getVersion() {
  92. return version;
  93. }
  94. /**
  95. * The source of the document if exists.
  96. */
  97. public byte[] source() {
  98. if (source == null) {
  99. return null;
  100. }
  101. if (sourceAsBytes != null) {
  102. return sourceAsBytes;
  103. }
  104. this.sourceAsBytes = sourceRef().toBytes();
  105. return this.sourceAsBytes;
  106. }
  107. /**
  108. * Returns bytes reference, also un compress the source if needed.
  109. */
  110. public BytesReference sourceRef() {
  111. try {
  112. this.source = CompressorFactory.uncompressIfNeeded(this.source);
  113. return this.source;
  114. } catch (IOException e) {
  115. throw new ElasticsearchParseException("failed to decompress source", e);
  116. }
  117. }
  118. /**
  119. * Internal source representation, might be compressed....
  120. */
  121. public BytesReference internalSourceRef() {
  122. return source;
  123. }
  124. /**
  125. * Is the source empty (not available) or not.
  126. */
  127. public boolean isSourceEmpty() {
  128. return source == null;
  129. }
  130. /**
  131. * The source of the document (as a string).
  132. */
  133. public String sourceAsString() {
  134. if (source == null) {
  135. return null;
  136. }
  137. BytesReference source = sourceRef();
  138. try {
  139. return XContentHelper.convertToJson(source, false);
  140. } catch (IOException e) {
  141. throw new ElasticsearchParseException("failed to convert source to a json string");
  142. }
  143. }
  144. /**
  145. * The source of the document (As a map).
  146. */
  147. @SuppressWarnings({"unchecked"})
  148. public Map<String, Object> sourceAsMap() throws ElasticsearchParseException {
  149. if (source == null) {
  150. return null;
  151. }
  152. if (sourceAsMap != null) {
  153. return sourceAsMap;
  154. }
  155. sourceAsMap = SourceLookup.sourceAsMap(source);
  156. return sourceAsMap;
  157. }
  158. public Map<String, Object> getSource() {
  159. return sourceAsMap();
  160. }
  161. public Map<String, GetField> getFields() {
  162. return fields;
  163. }
  164. public GetField field(String name) {
  165. return fields.get(name);
  166. }
  167. @Override
  168. public Iterator<GetField> iterator() {
  169. if (fields == null) {
  170. return emptyIterator();
  171. }
  172. return fields.values().iterator();
  173. }
  174. static final class Fields {
  175. static final XContentBuilderString _INDEX = new XContentBuilderString("_index");
  176. static final XContentBuilderString _TYPE = new XContentBuilderString("_type");
  177. static final XContentBuilderString _ID = new XContentBuilderString("_id");
  178. static final XContentBuilderString _VERSION = new XContentBuilderString("_version");
  179. static final XContentBuilderString FOUND = new XContentBuilderString("found");
  180. static final XContentBuilderString FIELDS = new XContentBuilderString("fields");
  181. }
  182. public XContentBuilder toXContentEmbedded(XContentBuilder builder, Params params) throws IOException {
  183. builder.field(Fields.FOUND, exists);
  184. if (source != null) {
  185. XContentHelper.writeRawField("_source", source, builder, params);
  186. }
  187. if (fields != null && !fields.isEmpty()) {
  188. builder.startObject(Fields.FIELDS);
  189. for (GetField field : fields.values()) {
  190. if (field.getValues().isEmpty()) {
  191. continue;
  192. }
  193. String fieldName = field.getName();
  194. if (field.isMetadataField()) {
  195. builder.field(fieldName, field.getValue());
  196. } else {
  197. builder.startArray(field.getName());
  198. for (Object value : field.getValues()) {
  199. builder.value(value);
  200. }
  201. builder.endArray();
  202. }
  203. }
  204. builder.endObject();
  205. }
  206. return builder;
  207. }
  208. @Override
  209. public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
  210. if (!isExists()) {
  211. builder.field(Fields._INDEX, index);
  212. builder.field(Fields._TYPE, type);
  213. builder.field(Fields._ID, id);
  214. builder.field(Fields.FOUND, false);
  215. } else {
  216. builder.field(Fields._INDEX, index);
  217. builder.field(Fields._TYPE, type);
  218. builder.field(Fields._ID, id);
  219. if (version != -1) {
  220. builder.field(Fields._VERSION, version);
  221. }
  222. toXContentEmbedded(builder, params);
  223. }
  224. return builder;
  225. }
  226. public static GetResult readGetResult(StreamInput in) throws IOException {
  227. GetResult result = new GetResult();
  228. result.readFrom(in);
  229. return result;
  230. }
  231. @Override
  232. public void readFrom(StreamInput in) throws IOException {
  233. index = in.readString();
  234. type = in.readOptionalString();
  235. id = in.readString();
  236. version = in.readLong();
  237. exists = in.readBoolean();
  238. if (exists) {
  239. source = in.readBytesReference();
  240. if (source.length() == 0) {
  241. source = null;
  242. }
  243. int size = in.readVInt();
  244. if (size == 0) {
  245. fields = ImmutableMap.of();
  246. } else {
  247. fields = newHashMapWithExpectedSize(size);
  248. for (int i = 0; i < size; i++) {
  249. GetField field = readGetField(in);
  250. fields.put(field.getName(), field);
  251. }
  252. }
  253. }
  254. }
  255. @Override
  256. public void writeTo(StreamOutput out) throws IOException {
  257. out.writeString(index);
  258. out.writeOptionalString(type);
  259. out.writeString(id);
  260. out.writeLong(version);
  261. out.writeBoolean(exists);
  262. if (exists) {
  263. out.writeBytesReference(source);
  264. if (fields == null) {
  265. out.writeVInt(0);
  266. } else {
  267. out.writeVInt(fields.size());
  268. for (GetField field : fields.values()) {
  269. field.writeTo(out);
  270. }
  271. }
  272. }
  273. }
  274. }