private static XContentParser transform(Mapping mapping, XContentParser parser) throws IOException {
     Map<String, Object> transformed;
     try (XContentParser _ = parser) {
         transformed = transformSourceAsMap(mapping, parser.mapOrdered());
     }
     XContentBuilder builder = XContentFactory.contentBuilder(parser.contentType()).value(transformed);
     return parser.contentType().xContent().createParser(builder.bytes());
 }
  public static TemplateContext parse(
      XContentParser parser, String templateFieldname, String paramsFieldname) throws IOException {
    Map<String, Object> params = null;
    String templateNameOrTemplateContent = null;

    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
      if (token == XContentParser.Token.FIELD_NAME) {
        currentFieldName = parser.currentName();
      } else if (templateFieldname.equals(currentFieldName)) {
        if (token == XContentParser.Token.START_OBJECT && !parser.hasTextCharacters()) {
          XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent());
          builder.copyCurrentStructure(parser);
          templateNameOrTemplateContent = builder.string();
        } else {
          templateNameOrTemplateContent = parser.text();
        }
      } else if (paramsFieldname.equals(currentFieldName)) {
        params = parser.map();
      }
    }

    return new TemplateContext(templateNameOrTemplateContent, params);
  }
示例#3
0
  Query parsePercolatorDocument(String id, BytesReference source) {
    String type = null;
    BytesReference querySource = null;

    XContentParser parser = null;
    try {
      parser = XContentHelper.createParser(source);
      String currentFieldName = null;
      XContentParser.Token token = parser.nextToken(); // move the START_OBJECT
      if (token != XContentParser.Token.START_OBJECT) {
        throw new ElasticsearchException(
            "failed to parse query [" + id + "], not starting with OBJECT");
      }
      while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
          currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_OBJECT) {
          if ("query".equals(currentFieldName)) {
            if (type != null) {
              return parseQuery(type, null, parser);
            } else {
              XContentBuilder builder = XContentFactory.contentBuilder(parser.contentType());
              builder.copyCurrentStructure(parser);
              querySource = builder.bytes();
              builder.close();
            }
          } else {
            parser.skipChildren();
          }
        } else if (token == XContentParser.Token.START_ARRAY) {
          parser.skipChildren();
        } else if (token.isValue()) {
          if ("type".equals(currentFieldName)) {
            type = parser.text();
          }
        }
      }
      return parseQuery(type, querySource, null);
    } catch (Exception e) {
      throw new PercolatorException(shardId().index(), "failed to parse query [" + id + "]", e);
    } finally {
      if (parser != null) {
        parser.close();
      }
    }
  }
  @Override
  public Mapper parse(ParseContext context) throws IOException {
    XContentParser parser = context.parser();
    XContentParser.Token token = parser.currentToken();
    if (token == XContentParser.Token.VALUE_NULL) {
      throw new MapperParsingException(
          "completion field [" + fieldType().names().fullName() + "] does not support null values");
    }

    String surfaceForm = null;
    BytesRef payload = null;
    long weight = -1;
    List<String> inputs = Lists.newArrayListWithExpectedSize(4);

    SortedMap<String, ContextConfig> contextConfig = null;

    if (token == XContentParser.Token.VALUE_STRING) {
      inputs.add(parser.text());
      multiFields.parse(this, context);
    } else {
      String currentFieldName = null;
      while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
          currentFieldName = parser.currentName();
          if (!ALLOWED_CONTENT_FIELD_NAMES.contains(currentFieldName)) {
            throw new IllegalArgumentException(
                "Unknown field name["
                    + currentFieldName
                    + "], must be one of "
                    + ALLOWED_CONTENT_FIELD_NAMES);
          }
        } else if (Fields.CONTEXT.equals(currentFieldName)) {
          SortedMap<String, ContextConfig> configs = Maps.newTreeMap();

          if (token == Token.START_OBJECT) {
            while ((token = parser.nextToken()) != Token.END_OBJECT) {
              String name = parser.text();
              ContextMapping mapping = fieldType().getContextMapping().get(name);
              if (mapping == null) {
                throw new ElasticsearchParseException("context [{}] is not defined", name);
              } else {
                token = parser.nextToken();
                configs.put(name, mapping.parseContext(context, parser));
              }
            }
            contextConfig = Maps.newTreeMap();
            for (ContextMapping mapping : fieldType().getContextMapping().values()) {
              ContextConfig config = configs.get(mapping.name());
              contextConfig.put(mapping.name(), config == null ? mapping.defaultConfig() : config);
            }
          } else {
            throw new ElasticsearchParseException("context must be an object");
          }
        } else if (Fields.CONTENT_FIELD_NAME_PAYLOAD.equals(currentFieldName)) {
          if (!isStoringPayloads()) {
            throw new MapperException("Payloads disabled in mapping");
          }
          if (token == XContentParser.Token.START_OBJECT) {
            XContentBuilder payloadBuilder =
                XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser);
            payload = payloadBuilder.bytes().toBytesRef();
            payloadBuilder.close();
          } else if (token.isValue()) {
            payload = parser.utf8BytesOrNull();
          } else {
            throw new MapperException("payload doesn't support type " + token);
          }
        } else if (token == XContentParser.Token.VALUE_STRING) {
          if (Fields.CONTENT_FIELD_NAME_OUTPUT.equals(currentFieldName)) {
            surfaceForm = parser.text();
          }
          if (Fields.CONTENT_FIELD_NAME_INPUT.equals(currentFieldName)) {
            inputs.add(parser.text());
          }
          if (Fields.CONTENT_FIELD_NAME_WEIGHT.equals(currentFieldName)) {
            Number weightValue;
            try {
              weightValue = Long.parseLong(parser.text());
            } catch (NumberFormatException e) {
              throw new IllegalArgumentException(
                  "Weight must be a string representing a numeric value, but was ["
                      + parser.text()
                      + "]");
            }
            weight =
                weightValue.longValue(); // always parse a long to make sure we don't get overflow
            checkWeight(weight);
          }
        } else if (token == XContentParser.Token.VALUE_NUMBER) {
          if (Fields.CONTENT_FIELD_NAME_WEIGHT.equals(currentFieldName)) {
            NumberType numberType = parser.numberType();
            if (NumberType.LONG != numberType && NumberType.INT != numberType) {
              throw new IllegalArgumentException(
                  "Weight must be an integer, but was [" + parser.numberValue() + "]");
            }
            weight = parser.longValue(); // always parse a long to make sure we don't get overflow
            checkWeight(weight);
          }
        } else if (token == XContentParser.Token.START_ARRAY) {
          if (Fields.CONTENT_FIELD_NAME_INPUT.equals(currentFieldName)) {
            while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
              inputs.add(parser.text());
            }
          }
        }
      }
    }

    if (contextConfig == null) {
      contextConfig = Maps.newTreeMap();
      for (ContextMapping mapping : fieldType().getContextMapping().values()) {
        contextConfig.put(mapping.name(), mapping.defaultConfig());
      }
    }

    final ContextMapping.Context ctx = new ContextMapping.Context(contextConfig, context.doc());

    payload = payload == null ? EMPTY : payload;
    if (surfaceForm == null) { // no surface form use the input
      for (String input : inputs) {
        if (input.length() == 0) {
          continue;
        }
        BytesRef suggestPayload =
            fieldType()
                .analyzingSuggestLookupProvider
                .buildPayload(new BytesRef(input), weight, payload);
        context.doc().add(getCompletionField(ctx, input, suggestPayload));
      }
    } else {
      BytesRef suggestPayload =
          fieldType()
              .analyzingSuggestLookupProvider
              .buildPayload(new BytesRef(surfaceForm), weight, payload);
      for (String input : inputs) {
        if (input.length() == 0) {
          continue;
        }
        context.doc().add(getCompletionField(ctx, input, suggestPayload));
      }
    }
    return null;
  }
  @Override
  public void parse(ParseContext context) throws IOException {
    XContentParser parser = context.parser();
    XContentParser.Token token = parser.currentToken();

    String surfaceForm = null;
    BytesRef payload = null;
    long weight = -1;
    List<String> inputs = Lists.newArrayListWithExpectedSize(4);

    if (token == XContentParser.Token.VALUE_STRING) {
      inputs.add(parser.text());
    } else {
      String currentFieldName = null;
      while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
          currentFieldName = parser.currentName();
        } else if ("payload".equals(currentFieldName)) {
          if (!isStoringPayloads()) {
            throw new MapperException("Payloads disabled in mapping");
          }
          if (token == XContentParser.Token.START_OBJECT) {
            XContentBuilder payloadBuilder =
                XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser);
            payload = payloadBuilder.bytes().toBytesRef();
            payloadBuilder.close();
          }
        } else if (token == XContentParser.Token.VALUE_STRING) {
          if ("output".equals(currentFieldName)) {
            surfaceForm = parser.text();
          }
          if ("input".equals(currentFieldName)) {
            inputs.add(parser.text());
          }
        } else if (token == XContentParser.Token.VALUE_NUMBER) {
          if ("weight".equals(currentFieldName)) {
            weight =
                parser.longValue(); // always parse a long to make sure we don't get the overflow
            // value
            if (weight < 0 || weight > Integer.MAX_VALUE) {
              throw new ElasticSearchIllegalArgumentException(
                  "Weight must be in the interval [0..2147483647] but was " + weight);
            }
          }
        } else if (token == XContentParser.Token.START_ARRAY) {
          if ("input".equals(currentFieldName)) {
            while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
              inputs.add(parser.text());
            }
          }
        }
      }
    }
    payload = payload == null ? EMPTY : payload;
    if (surfaceForm == null) { // no surface form use the input
      for (String input : inputs) {
        BytesRef suggestPayload =
            analyzingSuggestLookupProvider.buildPayload(new BytesRef(input), weight, payload);
        context.doc().add(getCompletionField(input, suggestPayload));
      }
    } else {
      BytesRef suggestPayload =
          analyzingSuggestLookupProvider.buildPayload(new BytesRef(surfaceForm), weight, payload);
      for (String input : inputs) {
        context.doc().add(getCompletionField(input, suggestPayload));
      }
    }
  }
  public SearchSourceBuilder fromXContent(XContentParser parser, QueryParseContext context)
      throws IOException {
    SearchSourceBuilder builder = new SearchSourceBuilder();
    XContentParser.Token token = parser.currentToken();
    String currentFieldName = null;
    if (token != XContentParser.Token.START_OBJECT
        && (token = parser.nextToken()) != XContentParser.Token.START_OBJECT) {
      throw new ParsingException(
          parser.getTokenLocation(),
          "Expected [" + XContentParser.Token.START_OBJECT + "] but found [" + token + "]",
          parser.getTokenLocation());
    }
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
      if (token == XContentParser.Token.FIELD_NAME) {
        currentFieldName = parser.currentName();
      } else if (token.isValue()) {
        if (context.parseFieldMatcher().match(currentFieldName, FROM_FIELD)) {
          builder.from = parser.intValue();
        } else if (context.parseFieldMatcher().match(currentFieldName, SIZE_FIELD)) {
          builder.size = parser.intValue();
        } else if (context.parseFieldMatcher().match(currentFieldName, TIMEOUT_FIELD)) {
          builder.timeoutInMillis = parser.longValue();
        } else if (context.parseFieldMatcher().match(currentFieldName, TERMINATE_AFTER_FIELD)) {
          builder.terminateAfter = parser.intValue();
        } else if (context.parseFieldMatcher().match(currentFieldName, MIN_SCORE_FIELD)) {
          builder.minScore = parser.floatValue();
        } else if (context.parseFieldMatcher().match(currentFieldName, VERSION_FIELD)) {
          builder.version = parser.booleanValue();
        } else if (context.parseFieldMatcher().match(currentFieldName, EXPLAIN_FIELD)) {
          builder.explain = parser.booleanValue();
        } else if (context.parseFieldMatcher().match(currentFieldName, TRACK_SCORES_FIELD)) {
          builder.trackScores = parser.booleanValue();
        } else if (context.parseFieldMatcher().match(currentFieldName, _SOURCE_FIELD)) {
          builder.fetchSourceContext = FetchSourceContext.parse(parser, context);
        } else if (context.parseFieldMatcher().match(currentFieldName, FIELDS_FIELD)) {
          List<String> fieldNames = new ArrayList<>();
          fieldNames.add(parser.text());
          builder.fieldNames = fieldNames;
        } else if (context.parseFieldMatcher().match(currentFieldName, SORT_FIELD)) {
          builder.sort(parser.text());
        } else {
          throw new ParsingException(
              parser.getTokenLocation(),
              "Unknown key for a " + token + " in [" + currentFieldName + "].",
              parser.getTokenLocation());
        }
      } else if (token == XContentParser.Token.START_OBJECT) {
        if (context.parseFieldMatcher().match(currentFieldName, QUERY_FIELD)) {
          builder.queryBuilder = context.parseInnerQueryBuilder();
        } else if (context.parseFieldMatcher().match(currentFieldName, POST_FILTER_FIELD)) {
          builder.postQueryBuilder = context.parseInnerQueryBuilder();
        } else if (context.parseFieldMatcher().match(currentFieldName, _SOURCE_FIELD)) {
          builder.fetchSourceContext = FetchSourceContext.parse(parser, context);
        } else if (context.parseFieldMatcher().match(currentFieldName, SCRIPT_FIELDS_FIELD)) {
          List<ScriptField> scriptFields = new ArrayList<>();
          while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            String scriptFieldName = parser.currentName();
            token = parser.nextToken();
            if (token == XContentParser.Token.START_OBJECT) {
              Script script = null;
              boolean ignoreFailure = false;
              while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                  currentFieldName = parser.currentName();
                } else if (token.isValue()) {
                  if (context.parseFieldMatcher().match(currentFieldName, SCRIPT_FIELD)) {
                    script = Script.parse(parser, context.parseFieldMatcher());
                  } else if (context
                      .parseFieldMatcher()
                      .match(currentFieldName, IGNORE_FAILURE_FIELD)) {
                    ignoreFailure = parser.booleanValue();
                  } else {
                    throw new ParsingException(
                        parser.getTokenLocation(),
                        "Unknown key for a " + token + " in [" + currentFieldName + "].",
                        parser.getTokenLocation());
                  }
                } else if (token == XContentParser.Token.START_OBJECT) {
                  if (context.parseFieldMatcher().match(currentFieldName, SCRIPT_FIELD)) {
                    script = Script.parse(parser, context.parseFieldMatcher());
                  } else {
                    throw new ParsingException(
                        parser.getTokenLocation(),
                        "Unknown key for a " + token + " in [" + currentFieldName + "].",
                        parser.getTokenLocation());
                  }
                } else {
                  throw new ParsingException(
                      parser.getTokenLocation(),
                      "Unknown key for a " + token + " in [" + currentFieldName + "].",
                      parser.getTokenLocation());
                }
              }
              scriptFields.add(new ScriptField(scriptFieldName, script, ignoreFailure));
            } else {
              throw new ParsingException(
                  parser.getTokenLocation(),
                  "Expected ["
                      + XContentParser.Token.START_OBJECT
                      + "] in ["
                      + currentFieldName
                      + "] but found ["
                      + token
                      + "]",
                  parser.getTokenLocation());
            }
          }
          builder.scriptFields = scriptFields;
        } else if (context.parseFieldMatcher().match(currentFieldName, INDICES_BOOST_FIELD)) {
          ObjectFloatHashMap<String> indexBoost = new ObjectFloatHashMap<String>();
          while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
              currentFieldName = parser.currentName();
            } else if (token.isValue()) {
              indexBoost.put(currentFieldName, parser.floatValue());
            } else {
              throw new ParsingException(
                  parser.getTokenLocation(),
                  "Unknown key for a " + token + " in [" + currentFieldName + "].",
                  parser.getTokenLocation());
            }
          }
          builder.indexBoost = indexBoost;
        } else if (context.parseFieldMatcher().match(currentFieldName, AGGREGATIONS_FIELD)) {
          List<BytesReference> aggregations = new ArrayList<>();
          while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            currentFieldName = parser.currentName();
            token = parser.nextToken();
            if (token == XContentParser.Token.START_OBJECT) {
              XContentBuilder xContentBuilder =
                  XContentFactory.contentBuilder(parser.contentType());
              xContentBuilder.startObject();
              xContentBuilder.field(currentFieldName);
              xContentBuilder.copyCurrentStructure(parser);
              xContentBuilder.endObject();
              aggregations.add(xContentBuilder.bytes());
            } else {
              throw new ParsingException(
                  parser.getTokenLocation(),
                  "Unknown key for a " + token + " in [" + currentFieldName + "].",
                  parser.getTokenLocation());
            }
          }
          builder.aggregations = aggregations;
        } else if (context.parseFieldMatcher().match(currentFieldName, HIGHLIGHT_FIELD)) {
          XContentBuilder xContentBuilder =
              XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser);
          builder.highlightBuilder = xContentBuilder.bytes();
        } else if (context.parseFieldMatcher().match(currentFieldName, INNER_HITS_FIELD)) {
          XContentBuilder xContentBuilder =
              XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser);
          builder.innerHitsBuilder = xContentBuilder.bytes();
        } else if (context.parseFieldMatcher().match(currentFieldName, SUGGEST_FIELD)) {
          XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType());
          xContentBuilder.copyCurrentStructure(parser);
          builder.suggestBuilder = xContentBuilder.bytes();
        } else if (context.parseFieldMatcher().match(currentFieldName, SORT_FIELD)) {
          List<BytesReference> sorts = new ArrayList<>();
          XContentBuilder xContentBuilder =
              XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser);
          sorts.add(xContentBuilder.bytes());
          builder.sorts = sorts;
        } else if (context.parseFieldMatcher().match(currentFieldName, EXT_FIELD)) {
          XContentBuilder xContentBuilder =
              XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser);
          builder.ext = xContentBuilder.bytes();
        } else {
          throw new ParsingException(
              parser.getTokenLocation(),
              "Unknown key for a " + token + " in [" + currentFieldName + "].",
              parser.getTokenLocation());
        }
      } else if (token == XContentParser.Token.START_ARRAY) {

        if (context.parseFieldMatcher().match(currentFieldName, FIELDS_FIELD)) {
          List<String> fieldNames = new ArrayList<>();
          while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            if (token == XContentParser.Token.VALUE_STRING) {
              fieldNames.add(parser.text());
            } else {
              throw new ParsingException(
                  parser.getTokenLocation(),
                  "Expected ["
                      + XContentParser.Token.VALUE_STRING
                      + "] in ["
                      + currentFieldName
                      + "] but found ["
                      + token
                      + "]",
                  parser.getTokenLocation());
            }
          }
          builder.fieldNames = fieldNames;
        } else if (context.parseFieldMatcher().match(currentFieldName, FIELDDATA_FIELDS_FIELD)) {
          List<String> fieldDataFields = new ArrayList<>();
          while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            if (token == XContentParser.Token.VALUE_STRING) {
              fieldDataFields.add(parser.text());
            } else {
              throw new ParsingException(
                  parser.getTokenLocation(),
                  "Expected ["
                      + XContentParser.Token.VALUE_STRING
                      + "] in ["
                      + currentFieldName
                      + "] but found ["
                      + token
                      + "]",
                  parser.getTokenLocation());
            }
          }
          builder.fieldDataFields = fieldDataFields;
        } else if (context.parseFieldMatcher().match(currentFieldName, SORT_FIELD)) {
          List<BytesReference> sorts = new ArrayList<>();
          while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            XContentBuilder xContentBuilder =
                XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser);
            sorts.add(xContentBuilder.bytes());
          }
          builder.sorts = sorts;
        } else if (context.parseFieldMatcher().match(currentFieldName, RESCORE_FIELD)) {
          List<BytesReference> rescoreBuilders = new ArrayList<>();
          while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            XContentBuilder xContentBuilder =
                XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser);
            rescoreBuilders.add(xContentBuilder.bytes());
          }
          builder.rescoreBuilders = rescoreBuilders;
        } else if (context.parseFieldMatcher().match(currentFieldName, STATS_FIELD)) {
          List<String> stats = new ArrayList<>();
          while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            if (token == XContentParser.Token.VALUE_STRING) {
              stats.add(parser.text());
            } else {
              throw new ParsingException(
                  parser.getTokenLocation(),
                  "Expected ["
                      + XContentParser.Token.VALUE_STRING
                      + "] in ["
                      + currentFieldName
                      + "] but found ["
                      + token
                      + "]",
                  parser.getTokenLocation());
            }
          }
          builder.stats = stats;
        } else if (context.parseFieldMatcher().match(currentFieldName, _SOURCE_FIELD)) {
          builder.fetchSourceContext = FetchSourceContext.parse(parser, context);
        } else {
          throw new ParsingException(
              parser.getTokenLocation(),
              "Unknown key for a " + token + " in [" + currentFieldName + "].",
              parser.getTokenLocation());
        }
      } else {
        throw new ParsingException(
            parser.getTokenLocation(),
            "Unknown key for a " + token + " in [" + currentFieldName + "].",
            parser.getTokenLocation());
      }
    }
    return builder;
  }