原来代码如下:
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);
}



