/**
  * Indicates whether the response should contain the stored _source.
  *
  * @return this for chaining
  */
 public GetRequestBuilder setFetchSource(boolean fetch) {
   FetchSourceContext context =
       request.fetchSourceContext() == null
           ? FetchSourceContext.FETCH_SOURCE
           : request.fetchSourceContext();
   request.fetchSourceContext(
       new FetchSourceContext(fetch, context.includes(), context.excludes()));
   return this;
 }
Пример #2
0
  public static void parseDocuments(
      XContentParser parser,
      List<Item> items,
      @Nullable String defaultIndex,
      @Nullable String defaultType,
      @Nullable String[] defaultFields,
      @Nullable FetchSourceContext defaultFetchSource,
      @Nullable String defaultRouting,
      boolean allowExplicitIndex)
      throws IOException {
    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
      if (token != XContentParser.Token.START_OBJECT) {
        throw new IllegalArgumentException("docs array element should include an object");
      }
      String index = defaultIndex;
      String type = defaultType;
      String id = null;
      String routing = defaultRouting;
      String parent = null;
      List<String> storedFields = null;
      long version = Versions.MATCH_ANY;
      VersionType versionType = VersionType.INTERNAL;

      FetchSourceContext fetchSourceContext = FetchSourceContext.FETCH_SOURCE;

      while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
          currentFieldName = parser.currentName();
        } else if (token.isValue()) {
          if ("_index".equals(currentFieldName)) {
            if (!allowExplicitIndex) {
              throw new IllegalArgumentException("explicit index in multi get is not allowed");
            }
            index = parser.text();
          } else if ("_type".equals(currentFieldName)) {
            type = parser.text();
          } else if ("_id".equals(currentFieldName)) {
            id = parser.text();
          } else if ("_routing".equals(currentFieldName) || "routing".equals(currentFieldName)) {
            routing = parser.text();
          } else if ("_parent".equals(currentFieldName) || "parent".equals(currentFieldName)) {
            parent = parser.text();
          } else if ("fields".equals(currentFieldName)) {
            throw new ParsingException(
                parser.getTokenLocation(),
                "Unsupported field [fields] used, expected [stored_fields] instead");
          } else if ("stored_fields".equals(currentFieldName)) {
            storedFields = new ArrayList<>();
            storedFields.add(parser.text());
          } else if ("_version".equals(currentFieldName) || "version".equals(currentFieldName)) {
            version = parser.longValue();
          } else if ("_version_type".equals(currentFieldName)
              || "_versionType".equals(currentFieldName)
              || "version_type".equals(currentFieldName)
              || "versionType".equals(currentFieldName)) {
            versionType = VersionType.fromString(parser.text());
          } else if ("_source".equals(currentFieldName)) {
            if (parser.isBooleanValue()) {
              fetchSourceContext =
                  new FetchSourceContext(
                      parser.booleanValue(),
                      fetchSourceContext.includes(),
                      fetchSourceContext.excludes());
            } else if (token == XContentParser.Token.VALUE_STRING) {
              fetchSourceContext =
                  new FetchSourceContext(
                      fetchSourceContext.fetchSource(),
                      new String[] {parser.text()},
                      fetchSourceContext.excludes());
            } else {
              throw new ElasticsearchParseException("illegal type for _source: [{}]", token);
            }
          }
        } else if (token == XContentParser.Token.START_ARRAY) {
          if ("fields".equals(currentFieldName)) {
            throw new ParsingException(
                parser.getTokenLocation(),
                "Unsupported field [fields] used, expected [stored_fields] instead");
          } else if ("stored_fields".equals(currentFieldName)) {
            storedFields = new ArrayList<>();
            while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
              storedFields.add(parser.text());
            }
          } else if ("_source".equals(currentFieldName)) {
            ArrayList<String> includes = new ArrayList<>();
            while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
              includes.add(parser.text());
            }
            fetchSourceContext =
                new FetchSourceContext(
                    fetchSourceContext.fetchSource(),
                    includes.toArray(Strings.EMPTY_ARRAY),
                    fetchSourceContext.excludes());
          }

        } else if (token == XContentParser.Token.START_OBJECT) {
          if ("_source".equals(currentFieldName)) {
            List<String> currentList = null, includes = null, excludes = null;

            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
              if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
                if ("includes".equals(currentFieldName) || "include".equals(currentFieldName)) {
                  currentList = includes != null ? includes : (includes = new ArrayList<>(2));
                } else if ("excludes".equals(currentFieldName)
                    || "exclude".equals(currentFieldName)) {
                  currentList = excludes != null ? excludes : (excludes = new ArrayList<>(2));
                } else {
                  throw new ElasticsearchParseException(
                      "source definition may not contain [{}]", parser.text());
                }
              } else if (token == XContentParser.Token.START_ARRAY) {
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                  currentList.add(parser.text());
                }
              } else if (token.isValue()) {
                currentList.add(parser.text());
              } else {
                throw new ElasticsearchParseException(
                    "unexpected token while parsing source settings");
              }
            }

            fetchSourceContext =
                new FetchSourceContext(
                    fetchSourceContext.fetchSource(),
                    includes == null
                        ? Strings.EMPTY_ARRAY
                        : includes.toArray(new String[includes.size()]),
                    excludes == null
                        ? Strings.EMPTY_ARRAY
                        : excludes.toArray(new String[excludes.size()]));
          }
        }
      }
      String[] aFields;
      if (storedFields != null) {
        aFields = storedFields.toArray(new String[storedFields.size()]);
      } else {
        aFields = defaultFields;
      }
      items.add(
          new Item(index, type, id)
              .routing(routing)
              .storedFields(aFields)
              .parent(parent)
              .version(version)
              .versionType(versionType)
              .fetchSourceContext(
                  fetchSourceContext == FetchSourceContext.FETCH_SOURCE
                      ? defaultFetchSource
                      : fetchSourceContext));
    }
  }