栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

ES使用Java API创建映射报错:mapping source must be pairs offieldnames and properties definition.

ES使用Java API创建映射报错:mapping source must be pairs offieldnames and properties definition.

ES使用Java API创建映射报错java.lang.IllegalArgumentException: mapping source must be pairs of fieldnames and properties definition.

原来代码如下:

String mapping = "{n" +
                "      "properties" : {n" +
                "        "address" : {n" +
                "          "type" : "text",n" +
                "          "analyzer" : "ik_max_word"n" +
                "        },n" +
                "        "age" : {n" +
                "          "type" : "long"n" +
                "        },n" +
                "        "name" : {n" +
                "          "type" : "keyword"n" +
                "        }n" +
                "      }n" +
                "    }";
createIndexRequest.mapping(mapping, XContentType.JSON);

上述代码运行后会出现异常:

java.lang.IllegalArgumentException: 
mapping source must be pairs offieldnames and properties definition.

通过分析:
原因:导入过时包

import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

底层:

    
    public CreateIndexRequest mapping(String type, Object... source) {
        mapping(type, PutMappingRequest.buildFromSimplifiedDef(type, source));
        return this;
    }

逐步进入方法:buildFromSimplifiedDef(type, source));

 
    public static XContentBuilder buildFromSimplifiedDef(String type, Object... source) {
        if (source.length % 2 != 0) {
            throw new IllegalArgumentException("mapping source must be pairs of fieldnames and properties definition.");
        }

通过上述源码和相关注释文档可以看出第二个source参数是必须成对的,所以在上述代码中使用这个方法是缺少类型的也就是type。ES 7.x以后,将逐步移除type这个概念,现在的操作已经不再使用,默认为_doc。

public CreateIndexRequest mapping(String type, Object... source) {
    mapping(type, PutMappingRequest.buildFromSimplifiedDef(type, source));
    return this;
}
处理方法

应在mapping中添加"_doc"类型参数如下:

        String mapping = "{n" +
                "      "properties" : {n" +
                "        "address" : {n" +
                "          "type" : "text",n" +
                "          "analyzer" : "ik_max_word"n" +
                "        },n" +
                "        "age" : {n" +
                "          "type" : "long"n" +
                "        },n" +
                "        "name" : {n" +
                "          "type" : "keyword"n" +
                "        }n" +
                "      }n" +
                "    }";
        createIndexRequest.mapping("_doc",mapping, XContentType.JSON);

底层方法如下:

   
    public CreateIndexRequest mapping(String type, String source, XContentType xContentType) {
        return mapping(type, new BytesArray(source), xContentType);
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/746497.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号