Пример #1
0
    @Override
    public SignificanceHeuristic parse(XContentParser parser)
        throws IOException, QueryParsingException {
      NAMES_FIELD.match(parser.currentName(), ParseField.EMPTY_FLAGS);
      String script = null;
      String scriptLang;
      XContentParser.Token token;
      Map<String, Object> params = new HashMap<>();
      String currentFieldName = null;
      ScriptService.ScriptType scriptType = ScriptService.ScriptType.INLINE;
      ScriptParameterParser scriptParameterParser = new ScriptParameterParser();
      while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token.equals(XContentParser.Token.FIELD_NAME)) {
          currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_OBJECT) {
          if ("params".equals(currentFieldName)) {
            params = parser.map();
          } else {
            throw new ElasticsearchParseException(
                "unknown object " + currentFieldName + " in script_heuristic");
          }
        } else if (!scriptParameterParser.token(currentFieldName, token, parser)) {
          throw new ElasticsearchParseException(
              "unknown field " + currentFieldName + " in script_heuristic");
        }
      }

      ScriptParameterParser.ScriptParameterValue scriptValue =
          scriptParameterParser.getDefaultScriptParameterValue();
      if (scriptValue != null) {
        script = scriptValue.script();
        scriptType = scriptValue.scriptType();
      }
      scriptLang = scriptParameterParser.lang();

      if (script == null) {
        throw new ElasticsearchParseException("No script found in script_heuristic");
      }
      ExecutableScript searchScript;
      try {
        searchScript =
            scriptService.executable(
                scriptLang, script, scriptType, ScriptContext.Standard.AGGS, params);
      } catch (Exception e) {
        throw new ElasticsearchParseException("The script [" + script + "] could not be loaded", e);
      }
      return new ScriptHeuristic(searchScript, scriptLang, script, scriptType, params);
    }
  @Override
  public void parse(XContentParser parser, SearchContext context) throws Exception {
    XContentParser.Token token;
    String currentFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
      if (token == XContentParser.Token.FIELD_NAME) {
        currentFieldName = parser.currentName();
      } else if (token == XContentParser.Token.START_OBJECT) {
        String fieldName = currentFieldName;
        ScriptParameterParser scriptParameterParser = new ScriptParameterParser();
        String script = null;
        ScriptService.ScriptType scriptType = null;
        Map<String, Object> params = null;
        boolean ignoreException = false;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
          if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
          } else if (token == XContentParser.Token.START_OBJECT) {
            params = parser.map();
          } else if (token.isValue()) {
            if ("ignore_failure".equals(currentFieldName)) {
              ignoreException = parser.booleanValue();
            } else {
              scriptParameterParser.token(currentFieldName, token, parser);
            }
          }
        }

        ScriptParameterValue scriptValue = scriptParameterParser.getDefaultScriptParameterValue();
        if (scriptValue != null) {
          script = scriptValue.script();
          scriptType = scriptValue.scriptType();
        }
        SearchScript searchScript =
            context
                .scriptService()
                .search(
                    context.lookup(),
                    new Script(scriptParameterParser.lang(), script, scriptType, params),
                    ScriptContext.Standard.SEARCH);
        context
            .scriptFields()
            .add(new ScriptFieldsContext.ScriptField(fieldName, searchScript, ignoreException));
      }
    }
  }