@Nullable
  protected GetResult extractGetResult(
      final UpdateRequest request,
      long version,
      final Map<String, Object> source,
      XContentType sourceContentType,
      @Nullable final BytesReference sourceAsBytes) {
    if (request.fields() == null || request.fields().length == 0) {
      return null;
    }
    boolean sourceRequested = false;
    Map<String, GetField> fields = null;
    if (request.fields() != null && request.fields().length > 0) {
      SourceLookup sourceLookup = new SourceLookup();
      sourceLookup.setNextSource(source);
      for (String field : request.fields()) {
        if (field.equals("_source")) {
          sourceRequested = true;
          continue;
        }
        Object value = sourceLookup.extractValue(field);
        if (value != null) {
          if (fields == null) {
            fields = newHashMapWithExpectedSize(2);
          }
          GetField getField = fields.get(field);
          if (getField == null) {
            getField = new GetField(field, new ArrayList<Object>(2));
            fields.put(field, getField);
          }
          getField.getValues().add(value);
        }
      }
    }

    // TODO when using delete/none, we can still return the source as bytes by generating it (using
    // the sourceContentType)

    return new GetResult(
        request.index(),
        request.type(),
        request.id(),
        version,
        true,
        sourceRequested ? sourceAsBytes : null,
        fields);
  }
 private Fields generateTermVectors(
     Collection<GetField> getFields,
     boolean withOffsets,
     @Nullable Map<String, String> perFieldAnalyzer)
     throws IOException {
   /* store document in memory index */
   MemoryIndex index = new MemoryIndex(withOffsets);
   for (GetField getField : getFields) {
     String field = getField.getName();
     Analyzer analyzer = getAnalyzerAtField(field, perFieldAnalyzer);
     for (Object text : getField.getValues()) {
       index.addField(field, text.toString(), analyzer);
     }
   }
   /* and read vectors from it */
   return MultiFields.getFields(index.createSearcher().getIndexReader());
 }
Example #3
0
 @Override
 public void writeTo(StreamOutput out) throws IOException {
   out.writeString(index);
   out.writeOptionalString(type);
   out.writeString(id);
   out.writeLong(version);
   out.writeBoolean(exists);
   if (exists) {
     out.writeBytesReference(source);
     if (fields == null) {
       out.writeVInt(0);
     } else {
       out.writeVInt(fields.size());
       for (GetField field : fields.values()) {
         field.writeTo(out);
       }
     }
   }
 }
Example #4
0
 @Override
 public void readFrom(StreamInput in) throws IOException {
   index = in.readString();
   type = in.readOptionalString();
   id = in.readString();
   version = in.readLong();
   exists = in.readBoolean();
   if (exists) {
     source = in.readBytesReference();
     if (source.length() == 0) {
       source = null;
     }
     int size = in.readVInt();
     if (size == 0) {
       fields = emptyMap();
     } else {
       fields = new HashMap<>(size);
       for (int i = 0; i < size; i++) {
         GetField field = readGetField(in);
         fields.put(field.getName(), field);
       }
     }
   }
 }
Example #5
0
  public XContentBuilder toXContentEmbedded(XContentBuilder builder, Params params)
      throws IOException {
    List<GetField> metaFields = new ArrayList<>();
    List<GetField> otherFields = new ArrayList<>();
    if (fields != null && !fields.isEmpty()) {
      for (GetField field : fields.values()) {
        if (field.getValues().isEmpty()) {
          continue;
        }
        if (field.isMetadataField()) {
          metaFields.add(field);
        } else {
          otherFields.add(field);
        }
      }
    }

    for (GetField field : metaFields) {
      builder.field(field.getName(), field.getValue());
    }

    builder.field(Fields.FOUND, exists);

    if (source != null) {
      XContentHelper.writeRawField("_source", source, builder, params);
    }

    if (!otherFields.isEmpty()) {
      builder.startObject(Fields.FIELDS);
      for (GetField field : otherFields) {
        builder.startArray(field.getName());
        for (Object value : field.getValues()) {
          builder.value(value);
        }
        builder.endArray();
      }
      builder.endObject();
    }
    return builder;
  }