elasticsearch5.6
索引模板
curl cmserver:9200/track_20190809?pretty
{
"track_20190809" : {
"aliases" : { },
"mappings" : {
"track" : {
"_all" : {
"enabled" : false
},
"properties" : {
"dataSource" : {
"type" : "keyword"
},
"dtTime" : {
"type" : "date"
},
"idtype" : {
"type" : "keyword"
},
"linkId" : {
"type" : "keyword"
},
"strEqpId" : {
"type" : "keyword"
},
"strId" : {
"type" : "keyword"
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "1566555759568",
"number_of_shards" : "4",
"number_of_replicas" : "0",
"uuid" : "X0e_Xj-ERB--GJedj6hsJw",
"version" : {
"created" : "5061699"
},
"provided_name" : "track_20190809"
}
}
}
}
maven
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.6.16</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.6.16</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
客户端创建
public class ESRepository {
public TransportClient client = null;
public void init() throws Exception {
buildClient();
}
private void buildClient() throws Exception {
String[] hosts = { "192.168.0.82" };
Settings settings = Settings.builder().put("cluster.name", "elasticsearch").put("client.transport.sniff", true)
.put("client.transport.ping_timeout", "60s").put("action.auto_create_index", true).build();
client = new PreBuiltTransportClient(settings);
for (String strTransportHostName : hosts) {
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(strTransportHostName), 9300));
}
}
}
查询代码
public class QueryData {
private ESRepository esr = new ESRepository();
private int total = 0;
public void doRun(String query) {
try {
esr.init();
} catch (Exception e) {
log.error("", e);
}
log.info("query data start...");
long a = System.currentTimeMillis();
String scrollId = getScrollId(query);
queryByScrollId(scrollId);
clearScroll(scrollId);
long b = System.currentTimeMillis();
log.info("query data end total:" + total + ", time:" + (b-a));
}
private void queryByScrollId(String scrollId) {
SearchScrollRequestBuilder searchScrollRequestBuilder;
SearchResponse response;
int i = 0;
while(true) {
i++;
searchScrollRequestBuilder = esr.client.prepareSearchScroll(scrollId);
searchScrollRequestBuilder.setScroll(TimeValue.timeValueMinutes(5));
response = searchScrollRequestBuilder.get();
long count = response.getHits().getHits().length;
log.info("--------- queryByScrollId size {}, i={}", count, i);
total += count;
if (count == 0) {
break;
} else {
/*for (SearchHit searchHit : searchHits) {
log.info("--------- queryByScrollId source {}", searchHit.getSourceAsString());
}*/
}
scrollId = response.getScrollId();
}
}
private String getScrollId(String query) {
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
if(query != null) {
queryBuilder.must(QueryBuilders.wildcardQuery("strId", "*" + query + "*"));
}
SearchRequestBuilder searchRequestBuilder = esr.client.prepareSearch("track_20190809");
searchRequestBuilder.setTypes("track");
searchRequestBuilder.setScroll(TimeValue.timeValueMinutes(5));
searchRequestBuilder.setSearchType(SearchType.DEFAULT);
searchRequestBuilder.setSize(10000);
//searchRequestBuilder.addSort(SortBuilders.fieldSort("strId"));
if(query != null) {
searchRequestBuilder.setQuery(queryBuilder);
}
SearchResponse response = searchRequestBuilder.get();
long count = response.getHits().getHits().length;
log.info("--------- getScrollId size {}", count);
total += count;
/*for (SearchHit searchHit : searchHits) {
log.info("--------- getScrollId source {}", searchHit.getSourceAsString());
}*/
return response.getScrollId();
}
private void clearScroll(String scrollId) {
ClearScrollRequestBuilder clearScrollRequestBuilder = esr.client.prepareClearScroll();
clearScrollRequestBuilder.addScrollId(scrollId);
ClearScrollResponse response = clearScrollRequestBuilder.get();
log.info("--------- clearScroll result {}", response.isSucceeded());
}
}
elasticsearch2.3
索引模板
curl cmserver:9200/track_20190809?pretty
{
"track_20190809" : {
"aliases" : { },
"mappings" : {
"track" : {
"_all" : {
"enabled" : false
},
"properties" : {
"dataSource" : {
"type" : "string",
"index" : "not_analyzed"
},
"dtTime" : {
"type" : "date",
"format" : "strict_date_optional_time||epoch_millis"
},
"idtype" : {
"type" : "string",
"index" : "not_analyzed"
},
"linkId" : {
"type" : "string",
"index" : "not_analyzed"
},
"strEqpId" : {
"type" : "string",
"index" : "not_analyzed"
},
"strId" : {
"type" : "string",
"index" : "not_analyzed"
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "1566465473765",
"number_of_shards" : "4",
"number_of_replicas" : "0",
"uuid" : "MfT_bzy1TO-67aVocEt5uQ",
"version" : {
"created" : "2030399"
}
}
}
}
}
maven
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
查询代码
和elasticsearch5.6一样
两个es内存配置都为1G,数据总条数1000万
全部查出来,5.6需要300多秒,而2.3需要130秒左右,不清楚为什么,求大神帮忙