{
"device_log_2020-05": {
"mappings": {
"log": {
"properties": {
"AttamentFileMD5": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"AttamentFilePath": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"CalledSource": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"Description": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"DevLogFileMD5": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"DevLogFilePath": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"Item": {
"properties": {
"Name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"ThredHoldMax": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"ThredHoldMin": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"Unit": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"Value": {
"type": "text"
}
}
},
"LogGuid": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"LogId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"LogLevel": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"LogPriority": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"LogType": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"OperationDateTime": {
"type": "date"
},
"ProductNumber": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"SerialNumber": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"ServiceCode": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"SourceName": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"SystemId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"TempGuid": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"Uid": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"UploadDateTime": {
"type": "date"
}
}
}
}
}
}
查询当前的mapping
get /device_log_2019-09/log/_mapping
日期字段
https://blog.csdn.net/weixin_39723544/article/details/104237033
ignore_above
https://www.letianbiji.com/elasticsearch/es7-ignore-above.html
https://blog.csdn.net/Z446136354/article/details/97245114
https://blog.csdn.net/ifenggege/article/details/86103918
ES Java High Level REST Client 聚合返回
https://blog.csdn.net/chedan666/article/details/82429947
package com.uih.uplus.solar.log.inquirer.esmanager.model;
import java.util.ArrayList;
import java.util.Map;
public class EsDocumentList extends ArrayList<Map<String, Object>> {
private static final long serialVersionUID = -2227654367227324689L;
private long numFound = 0;
private long start = 0;
private Float maxScore = null;
public Float getMaxScore() {
return maxScore;
}
public void setMaxScore(Float maxScore) {
this.maxScore = maxScore;
}
public long getNumFound() {
return numFound;
}
public void setNumFound(long numFound) {
this.numFound = numFound;
}
public long getStart() {
return start;
}
public void setStart(long start) {
this.start = start;
}
@Override
public String toString() {
return "{numFound="+numFound
+",start="+start
+ (maxScore!=null ? ",maxScore="+maxScore : "")
+",docs="+super.toString()
+"}";
}
@Override
public boolean equals(Object object) {
if (object == this) {
return true;
}
if (!(object instanceof EsDocumentList)) {
return false;
}
EsDocumentList list = (EsDocumentList) object;
if (!list.getMaxScore().equals(this.getMaxScore())) {
return false;
}
if (list.getNumFound() != this.getNumFound()) {
return false;
}
if (list.getStart() != this.getStart()) {
return false;
}
if (list.size() != this.size()) {
return false;
}
for (int i = 0; i < list.size(); i++) {
if (!list.get(i).equals(this.get(i))) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
return Long.hashCode(start);
}
}
<!-- ES -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
@Override
public SearchResponse get(String[] index, QueryBuilder queryBuilder, String sortName, String sortOrder,
int startIndex, int size) {
try {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(queryBuilder).from(startIndex).size(size).timeout(new TimeValue(EsConst.TIMEOUT_VALUE, TimeUnit.SECONDS));
if(StringUtils.isNotEmpty(sortName) && StringUtils.isNotEmpty(sortOrder)){
searchSourceBuilder.sort(new FieldSortBuilder(sortName).order(sortOrder.equals("asc") ? SortOrder.ASC : SortOrder.DESC));
}
SearchRequest searchRequest=new SearchRequest();
if(index!=null&&index.length>0){
searchRequest=new SearchRequest(index,searchSourceBuilder);
}else {
searchRequest.source(searchSourceBuilder);
}
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
return searchResponse;
} catch (IOException e) {
logger.log(LogCst.INQUIRER_DEV_ERROR,"org.elasticsearch.action.search.SearchResponse 发生异常:",e);
}
return new SearchResponse();
}
SearchResponse searchResponse=esDao.get(index,queryBuilder,sotName,sortOrder,startIndex,size);
if (searchResponse.status() != RestStatus.OK || searchResponse.getHits().getTotalHits() <= 0) {
return esDocumentList;
}
for (SearchHit hit : searchResponse.getHits().getHits()) {
Map<String, Object> mapResult = hit.getSourceAsMap();
esDocumentList.add(mapResult);
}
esDocumentList.setNumFound(searchResponse.getHits().getTotalHits());
esDocumentList.setStart(startIndex);
return esDocumentList;
@Configuration
public class EsRestClientConfig {
@Value("${elasticSearch.hosts}")
private String hosts; // 集群地址,多个用,隔开
private static String schema = "http"; // 使用的协议
private static int connectTimeOut = 1000; // 连接超时时间
private static int socketTimeOut = 30000; // 连接超时时间
private static int connectionRequestTimeOut = 500; // 获取连接的超时时间
private static int maxConnectNum = 100; // 最大连接数
private static int maxConnectPerRoute = 100; // 最大路由连接数
private ArrayList<HttpHost> getHttpHost(){
ArrayList<HttpHost> hostList = new ArrayList<>();
String[] hostStrs = hosts.split(",");
for (String host : hostStrs) {
String[] address = host.split(":");
String ip = address[0].trim();
int port = Integer.parseInt(address[1]);
hostList.add(new HttpHost(ip, port, schema));
}
return hostList;
}
@Bean
public RestHighLevelClient client() {
ArrayList<HttpHost> hostList = getHttpHost();
RestClientBuilder builder = RestClient.builder(hostList.toArray(new HttpHost[0]));
// 异步httpclient连接延时配置
builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
@Override
public Builder customizeRequestConfig(Builder requestConfigBuilder) {
requestConfigBuilder.setConnectTimeout(connectTimeOut);
requestConfigBuilder.setSocketTimeout(socketTimeOut);
requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);
return requestConfigBuilder;
}
});
// 异步httpclient连接数配置
builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.setMaxConnTotal(maxConnectNum);
httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
return httpClientBuilder;
}
});
RestHighLevelClient client = new RestHighLevelClient(builder);
return client;
}
# ES配置
elasticSearch:
hosts: 10.6.5.1:9200