当前位置: 首页>>代码示例>>Java>>正文


Java XContentParser.map方法代码示例

本文整理汇总了Java中org.elasticsearch.common.xcontent.XContentParser.map方法的典型用法代码示例。如果您正苦于以下问题:Java XContentParser.map方法的具体用法?Java XContentParser.map怎么用?Java XContentParser.map使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.elasticsearch.common.xcontent.XContentParser的用法示例。


在下文中一共展示了XContentParser.map方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: extractRequestSpecificFieldsAndReturnSearchCompatibleParser

import org.elasticsearch.common.xcontent.XContentParser; //导入方法依赖的package包/类
/**
 * We can't send parseSearchRequest REST content that it doesn't support
 * so we will have to remove the content that is valid in addition to
 * what it supports from the content first. This is a temporary hack and
 * should get better when SearchRequest has full ObjectParser support
 * then we can delegate and stuff.
 */
private XContentParser extractRequestSpecificFieldsAndReturnSearchCompatibleParser(XContentParser parser,
                                  Map<String, Consumer<Object>> bodyConsumers) throws IOException {
    if (parser == null) {
        return parser;
    }
    try {
        Map<String, Object> body = parser.map();

        for (Map.Entry<String, Consumer<Object>> consumer : bodyConsumers.entrySet()) {
            Object value = body.remove(consumer.getKey());
            if (value != null) {
                consumer.getValue().accept(value);
            }
        }

        try (XContentBuilder builder = XContentFactory.contentBuilder(parser.contentType())) {
            return parser.contentType().xContent().createParser(parser.getXContentRegistry(), builder.map(body).bytes());
        }
    } finally {
        parser.close();
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:30,代码来源:AbstractBulkByQueryRestHandler.java

示例2: testShuffleXContent

import org.elasticsearch.common.xcontent.XContentParser; //导入方法依赖的package包/类
public void testShuffleXContent() throws IOException {
    Map<String, Object> randomStringObjectMap = randomStringObjectMap(5);
    XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
    builder.map(randomStringObjectMap);
    XContentBuilder shuffleXContent = shuffleXContent(builder);
    XContentParser parser = createParser(shuffleXContent);
    Map<String, Object> resultMap = parser.map();
    assertEquals("both maps should contain the same mappings", randomStringObjectMap, resultMap);
    assertNotEquals("Both builders string representations should be different", builder.bytes(), shuffleXContent.bytes());
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:11,代码来源:ESTestCaseTests.java

示例3: parse

import org.elasticsearch.common.xcontent.XContentParser; //导入方法依赖的package包/类
public static ScriptedMetricAggregationBuilder parse(String aggregationName, QueryParseContext context) throws IOException {
    Script initScript = null;
    Script mapScript = null;
    Script combineScript = null;
    Script reduceScript = null;
    Map<String, Object> params = null;
    XContentParser.Token token;
    String currentFieldName = null;
    Set<String> scriptParameters = new HashSet<>();
    scriptParameters.add(INIT_SCRIPT_FIELD.getPreferredName());
    scriptParameters.add(MAP_SCRIPT_FIELD.getPreferredName());
    scriptParameters.add(COMBINE_SCRIPT_FIELD.getPreferredName());
    scriptParameters.add(REDUCE_SCRIPT_FIELD.getPreferredName());

    XContentParser parser = context.parser();
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_OBJECT || token == XContentParser.Token.VALUE_STRING) {
            if (INIT_SCRIPT_FIELD.match(currentFieldName)) {
                initScript = Script.parse(parser);
            } else if (MAP_SCRIPT_FIELD.match(currentFieldName)) {
                mapScript = Script.parse(parser);
            } else if (COMBINE_SCRIPT_FIELD.match(currentFieldName)) {
                combineScript = Script.parse(parser);
            } else if (REDUCE_SCRIPT_FIELD.match(currentFieldName)) {
                reduceScript = Script.parse(parser);
            } else if (token == XContentParser.Token.START_OBJECT &&
                    PARAMS_FIELD.match(currentFieldName)) {
                params = parser.map();
            } else {
                throw new ParsingException(parser.getTokenLocation(),
                        "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [" + aggregationName + "].");
        }
    }

    if (mapScript == null) {
        throw new ParsingException(parser.getTokenLocation(), "map_script field is required in [" + aggregationName + "].");
    }

    ScriptedMetricAggregationBuilder factory = new ScriptedMetricAggregationBuilder(aggregationName);
    if (initScript != null) {
        factory.initScript(initScript);
    }
    if (mapScript != null) {
        factory.mapScript(mapScript);
    }
    if (combineScript != null) {
        factory.combineScript(combineScript);
    }
    if (reduceScript != null) {
        factory.reduceScript(reduceScript);
    }
    if (params != null) {
        factory.params(params);
    }
    return factory;
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:62,代码来源:ScriptedMetricAggregationBuilder.java

示例4: parse

import org.elasticsearch.common.xcontent.XContentParser; //导入方法依赖的package包/类
public static BucketSelectorPipelineAggregationBuilder parse(String reducerName, QueryParseContext context) throws IOException {
    XContentParser parser = context.parser();
    XContentParser.Token token;
    Script script = null;
    String currentFieldName = null;
    Map<String, String> bucketsPathsMap = null;
    GapPolicy gapPolicy = null;

    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if (BUCKETS_PATH.match(currentFieldName)) {
                bucketsPathsMap = new HashMap<>();
                bucketsPathsMap.put("_value", parser.text());
            } else if (GAP_POLICY.match(currentFieldName)) {
                gapPolicy = GapPolicy.parse(context, parser.text(), parser.getTokenLocation());
            } else if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName)) {
                script = Script.parse(parser);
            } else {
                throw new ParsingException(parser.getTokenLocation(),
                        "Unknown key for a " + token + " in [" + reducerName + "]: [" + currentFieldName + "].");
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (BUCKETS_PATH.match(currentFieldName)) {
                List<String> paths = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    String path = parser.text();
                    paths.add(path);
                }
                bucketsPathsMap = new HashMap<>();
                for (int i = 0; i < paths.size(); i++) {
                    bucketsPathsMap.put("_value" + i, paths.get(i));
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(),
                        "Unknown key for a " + token + " in [" + reducerName + "]: [" + currentFieldName + "].");
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName)) {
                script = Script.parse(parser);
            } else if (BUCKETS_PATH.match(currentFieldName)) {
                Map<String, Object> map = parser.map();
                bucketsPathsMap = new HashMap<>();
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    bucketsPathsMap.put(entry.getKey(), String.valueOf(entry.getValue()));
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(),
                        "Unknown key for a " + token + " in [" + reducerName + "]: [" + currentFieldName + "].");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [" + reducerName + "].");
        }
    }

    if (bucketsPathsMap == null) {
        throw new ParsingException(parser.getTokenLocation(), "Missing required field [" + BUCKETS_PATH.getPreferredName()
                + "] for bucket_selector aggregation [" + reducerName + "]");
    }

    if (script == null) {
        throw new ParsingException(parser.getTokenLocation(), "Missing required field [" + Script.SCRIPT_PARSE_FIELD.getPreferredName()
                + "] for bucket_selector aggregation [" + reducerName + "]");
    }

    BucketSelectorPipelineAggregationBuilder factory =
            new BucketSelectorPipelineAggregationBuilder(reducerName, bucketsPathsMap, script);
    if (gapPolicy != null) {
        factory.gapPolicy(gapPolicy);
    }
    return factory;
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:74,代码来源:BucketSelectorPipelineAggregationBuilder.java

示例5: parse

import org.elasticsearch.common.xcontent.XContentParser; //导入方法依赖的package包/类
public static BucketScriptPipelineAggregationBuilder parse(String reducerName, QueryParseContext context) throws IOException {
    XContentParser parser = context.parser();
    XContentParser.Token token;
    Script script = null;
    String currentFieldName = null;
    Map<String, String> bucketsPathsMap = null;
    String format = null;
    GapPolicy gapPolicy = null;

    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if (FORMAT.match(currentFieldName)) {
                format = parser.text();
            } else if (BUCKETS_PATH.match(currentFieldName)) {
                bucketsPathsMap = new HashMap<>();
                bucketsPathsMap.put("_value", parser.text());
            } else if (GAP_POLICY.match(currentFieldName)) {
                gapPolicy = GapPolicy.parse(context, parser.text(), parser.getTokenLocation());
            } else if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName)) {
                script = Script.parse(parser);
            } else {
                throw new ParsingException(parser.getTokenLocation(),
                        "Unknown key for a " + token + " in [" + reducerName + "]: [" + currentFieldName + "].");
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (BUCKETS_PATH.match(currentFieldName)) {
                List<String> paths = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    String path = parser.text();
                    paths.add(path);
                }
                bucketsPathsMap = new HashMap<>();
                for (int i = 0; i < paths.size(); i++) {
                    bucketsPathsMap.put("_value" + i, paths.get(i));
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(),
                        "Unknown key for a " + token + " in [" + reducerName + "]: [" + currentFieldName + "].");
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (Script.SCRIPT_PARSE_FIELD.match(currentFieldName)) {
                script = Script.parse(parser);
            } else if (BUCKETS_PATH.match(currentFieldName)) {
                Map<String, Object> map = parser.map();
                bucketsPathsMap = new HashMap<>();
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    bucketsPathsMap.put(entry.getKey(), String.valueOf(entry.getValue()));
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(),
                        "Unknown key for a " + token + " in [" + reducerName + "]: [" + currentFieldName + "].");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [" + reducerName + "].");
        }
    }

    if (bucketsPathsMap == null) {
        throw new ParsingException(parser.getTokenLocation(), "Missing required field [" + BUCKETS_PATH.getPreferredName()
                + "] for series_arithmetic aggregation [" + reducerName + "]");
    }

    if (script == null) {
        throw new ParsingException(parser.getTokenLocation(), "Missing required field [" + Script.SCRIPT_PARSE_FIELD.getPreferredName()
                + "] for series_arithmetic aggregation [" + reducerName + "]");
    }

    BucketScriptPipelineAggregationBuilder factory =
            new BucketScriptPipelineAggregationBuilder(reducerName, bucketsPathsMap, script);
    if (format != null) {
        factory.format(format);
    }
    if (gapPolicy != null) {
        factory.gapPolicy(gapPolicy);
    }
    return factory;
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:80,代码来源:BucketScriptPipelineAggregationBuilder.java

示例6: parse

import org.elasticsearch.common.xcontent.XContentParser; //导入方法依赖的package包/类
@Override
public PipelineAggregatorFactory parse(String reducerName, XContentParser parser, SearchContext context) throws IOException {
    XContentParser.Token token;
    Script script = null;
    String currentFieldName = null;
    Map<String, String> bucketsPathsMap = null;
    GapPolicy gapPolicy = GapPolicy.SKIP;

    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if (context.parseFieldMatcher().match(currentFieldName, BUCKETS_PATH)) {
                bucketsPathsMap = new HashMap<>();
                bucketsPathsMap.put("_value", parser.text());
            } else if (context.parseFieldMatcher().match(currentFieldName, GAP_POLICY)) {
                gapPolicy = GapPolicy.parse(context, parser.text(), parser.getTokenLocation());
            } else if (context.parseFieldMatcher().match(currentFieldName, ScriptField.SCRIPT)) {
                script = Script.parse(parser, context.parseFieldMatcher());
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + reducerName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (context.parseFieldMatcher().match(currentFieldName, BUCKETS_PATH)) {
                List<String> paths = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    String path = parser.text();
                    paths.add(path);
                }
                bucketsPathsMap = new HashMap<>();
                for (int i = 0; i < paths.size(); i++) {
                    bucketsPathsMap.put("_value" + i, paths.get(i));
                }
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + reducerName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (context.parseFieldMatcher().match(currentFieldName, ScriptField.SCRIPT)) {
                script = Script.parse(parser, context.parseFieldMatcher());
            } else if (context.parseFieldMatcher().match(currentFieldName, BUCKETS_PATH)) {
                Map<String, Object> map = parser.map();
                bucketsPathsMap = new HashMap<>();
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    bucketsPathsMap.put(entry.getKey(), String.valueOf(entry.getValue()));
                }
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + reducerName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else {
            throw new SearchParseException(context, "Unexpected token " + token + " in [" + reducerName + "].",
                    parser.getTokenLocation());
        }
    }

    if (bucketsPathsMap == null) {
        throw new SearchParseException(context, "Missing required field [" + BUCKETS_PATH.getPreferredName()
                + "] for bucket_selector aggregation [" + reducerName + "]", parser.getTokenLocation());
    }

    if (script == null) {
        throw new SearchParseException(context, "Missing required field [" + ScriptField.SCRIPT.getPreferredName()
                + "] for bucket_selector aggregation [" + reducerName + "]", parser.getTokenLocation());
    }

    return new BucketSelectorPipelineAggregator.Factory(reducerName, bucketsPathsMap, script, gapPolicy);
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:70,代码来源:BucketSelectorParser.java

示例7: parse

import org.elasticsearch.common.xcontent.XContentParser; //导入方法依赖的package包/类
@Override
public PipelineAggregatorFactory parse(String reducerName, XContentParser parser, SearchContext context) throws IOException {
    XContentParser.Token token;
    Script script = null;
    String currentFieldName = null;
    Map<String, String> bucketsPathsMap = null;
    String format = null;
    GapPolicy gapPolicy = GapPolicy.SKIP;

    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if (context.parseFieldMatcher().match(currentFieldName, FORMAT)) {
                format = parser.text();
            } else if (context.parseFieldMatcher().match(currentFieldName, BUCKETS_PATH)) {
                bucketsPathsMap = new HashMap<>();
                bucketsPathsMap.put("_value", parser.text());
            } else if (context.parseFieldMatcher().match(currentFieldName, GAP_POLICY)) {
                gapPolicy = GapPolicy.parse(context, parser.text(), parser.getTokenLocation());
            } else if (context.parseFieldMatcher().match(currentFieldName, ScriptField.SCRIPT)) {
                script = Script.parse(parser, context.parseFieldMatcher());
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + reducerName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (context.parseFieldMatcher().match(currentFieldName, BUCKETS_PATH)) {
                List<String> paths = new ArrayList<>();
                while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                    String path = parser.text();
                    paths.add(path);
                }
                bucketsPathsMap = new HashMap<>();
                for (int i = 0; i < paths.size(); i++) {
                    bucketsPathsMap.put("_value" + i, paths.get(i));
                }
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + reducerName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (context.parseFieldMatcher().match(currentFieldName, ScriptField.SCRIPT)) {
                script = Script.parse(parser, context.parseFieldMatcher());
            } else if (context.parseFieldMatcher().match(currentFieldName, BUCKETS_PATH)) {
                Map<String, Object> map = parser.map();
                bucketsPathsMap = new HashMap<>();
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    bucketsPathsMap.put(entry.getKey(), String.valueOf(entry.getValue()));
                }
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + reducerName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else {
            throw new SearchParseException(context, "Unexpected token " + token + " in [" + reducerName + "].",
                    parser.getTokenLocation());
        }
    }

    if (bucketsPathsMap == null) {
        throw new SearchParseException(context, "Missing required field [" + BUCKETS_PATH.getPreferredName()
                + "] for series_arithmetic aggregation [" + reducerName + "]", parser.getTokenLocation());
    }

    if (script == null) {
        throw new SearchParseException(context, "Missing required field [" + ScriptField.SCRIPT.getPreferredName()
                + "] for series_arithmetic aggregation [" + reducerName + "]", parser.getTokenLocation());
    }

    ValueFormatter formatter = null;
    if (format != null) {
        formatter = ValueFormat.Patternable.Number.format(format).formatter();
    } else {
        formatter = ValueFormatter.RAW;
    }

    return new BucketScriptPipelineAggregator.Factory(reducerName, bucketsPathsMap, script, formatter, gapPolicy);
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:80,代码来源:BucketScriptParser.java

示例8: handleRequest

import org.elasticsearch.common.xcontent.XContentParser; //导入方法依赖的package包/类
@Override
protected void handleRequest(RestRequest request, RestChannel channel, Client client) throws Exception {
    RenderSearchTemplateRequest renderSearchTemplateRequest;
    BytesReference source = RestActions.getRestContent(request);
    XContentParser parser = XContentFactory.xContent(source).createParser(source);
    String templateId = request.param("id");
    final Template template;
    if (templateId == null) {
        template = Template.parse(parser, parseFieldMatcher);
    } else {
        Map<String, Object> params = null;
        String currentFieldName = null;
        XContentParser.Token token = parser.nextToken();
        if (token != XContentParser.Token.START_OBJECT) {
            throw new ElasticsearchParseException("failed to parse request. request body must be an object but found [{}] instead", token);
        }
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else if (parseFieldMatcher.match(currentFieldName, ScriptField.PARAMS)) {
                if (token == XContentParser.Token.START_OBJECT) {
                    params = parser.map();
                } else {
                    throw new ElasticsearchParseException("failed to parse request. field [{}] is expected to be an object, but found [{}] instead", currentFieldName, token);
                }
            } else {
                throw new ElasticsearchParseException("failed to parse request. unknown field [{}] of type [{}]", currentFieldName, token);
            }
        }
        template = new Template(templateId, ScriptType.INDEXED, MustacheScriptEngineService.NAME, null, params);
    }
    renderSearchTemplateRequest = new RenderSearchTemplateRequest();
    renderSearchTemplateRequest.template(template);
    client.admin().cluster().renderSearchTemplate(renderSearchTemplateRequest, new RestBuilderListener<RenderSearchTemplateResponse>(channel) {

        @Override
        public RestResponse buildResponse(RenderSearchTemplateResponse response, XContentBuilder builder) throws Exception {
            builder.prettyPrint();
            response.toXContent(builder, ToXContent.EMPTY_PARAMS);
            return new BytesRestResponse(OK, builder);
        }});
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:43,代码来源:RestRenderSearchTemplateAction.java


注:本文中的org.elasticsearch.common.xcontent.XContentParser.map方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。