1.查询报错all shards failed
原因一:可能是因为查询条数过多。
解决办法:设置最大查询数量。
原因二:spring boot提供的elasticsearch依赖,使用createIndex方法时,不会创建mapping。缺少mapping,查询就会报这个错。
可以在kibana中查询创建的索引是否有mapping
正确的index:
{
"pt-elasticsearch-info": {
"aliases": {},
"mappings": {
"info": {
"properties": {
"content": {
"type": "text"
},
"createTime": {
"type": "text"
},
"createUserId": {
"type": "text"
},
"id": {
"type": "long"
},
"indexName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"indexType": {
"type": "text"
},
"infoType": {
"type": "text"
},
"size": {
"type": "integer"
},
"title": {
"type": "text"
}
}
}
},
"settings": {
"index": {
"creation_date": "1606636705734",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "Yc0081jgSj6wxqzLXTA8QQ",
"version": {
"created": "6040299"
},
"provided_name": "pt-elasticsearch-info"
}
}
}
}
错误的index:
{
"pt-elasticsearch-info": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1606636705734",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "Yc0081jgSj6wxqzLXTA8QQ",
"version": {
"created": "6040299"
},
"provided_name": "pt-elasticsearch-info"
}
}
}
}
解决办法:在创建index时候,创建mapping。这里我是使用实体bean转的mapping
package com.shineyue.index;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import lombok.Data;
/**
* 基础index
*
* @author Administrator
*
*/
@Data
@Document(indexName = "pt-elasticsearch-base", type = "base")
public class BaseIndex {
@Id
private String id;
/**
* 时间自增ID
*/
private String indexType;
private String title;
private String content;
private int size;
private String createTime;
}
bean转mapping的方法:
根据类的路径转换成mapping
public JSONObject classToMapping(String className, String indexType) {
Class<?> aClass = null;
try {
aClass = ClassLoader.getSystemClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 这里是需要转换对象
Field[] fields = aClass.getDeclaredFields();
JSONObject jsonObject = new JSONObject();
for (int i = 0; i < fields.length; i++) {
String name = fields[i].getName();
JSONObject sub = new JSONObject();
String type = fields[i].getType().getTypeName();
if (type.contains("Integer") || type.contains("int")) {
sub.put("type", "integer");
} else if (type.contains("Long") || type.contains("long")) {
sub.put("type", "long");
} else if (type.contains("String")) {
// sub.put("type", "keyword");
sub.put("type", "text");
} else if (type.contains("Boolean") || type.contains("boolean")) {
sub.put("type", "boolean");
} else if (type.contains("BigDecimal")) {
sub.put("type", "float");
} else if (type.contains("Date")) {
sub.put("type", "date");
} else {
sub.put("type", "todo");
}
jsonObject.put(name, sub);
}
log.info("转换成的mapping==={}", JSONObject.toJSONString(jsonObject));
JSONObject mapping2 = new JSONObject();
JSONObject mapping3 = new JSONObject();
mapping2.put("properties", jsonObject);
mapping3.put(indexType, mapping2);
return mapping3;
}
然后在创建完成index后,创建mapping
String className = StringTool.concat("com.shineyue.index.", StringTool.InitialCap(bean.getString("indexType")),
"Index");
JSONObject mapping = classToMapping(className, indexType);
Boolean createIndexFlag = elasticsearchTemplate.createIndex(bean.getString("indexName"));
Boolean putMappingFlag = true;
if (createIndexFlag) {
// 创建index成功后,根据类转换成mapping创建mapping
Class cls = null;
try {
cls = Class.forName(className);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
putMappingFlag = elasticsearchTemplate.putMapping(cls, mapping);
// putMappingFlag =
// elasticsearchTemplate.putMapping(CustomerIndex.class, mapping);
}