安装
es安装
- 下载:官网、历史版本,或者国内镜像。
- 教程。
- 配置
config/elasticsearch.yml
。参考。
cluster.name: my-cluster
node.name: my-node1
network.host: 0.0.0.0
http.port: 9200
cluster.initial_master_nodes: ["my-node1"]
bin/elasticsearch.bat
启动。
- 访问:
http://localhost:9200
。
kibana安装
- 下载:官网,或者国内镜像。
- 教程。
- 配置
config/kibana.yml
。
server.port: 5601
server.host: "localhost"
server.name: "my-kibana-server"
elasticsearch.hosts: ["http://localhost:9200"]
i18n.locale: "zh-CN"
bin/kibana.bat
启动。
- 访问:
http://localhost:5601
。
命令行使用
索引操作
GET /_cat/indices
PUT /second_hand_house
{
"mappings" : {
"properties" : {
"houseCode" : {
"type" : "text"
},
"title" : {
"type" : "text"
},
"priceTotal" : {
"type" : "integer"
},
"unitPrice" : {
"type" : "integer"
},
"area" : {
"type" : "double"
},
"fromTime" : {
"type" : "long"
},
"toTime" : {
"type" : "long"
}
}
},
"settings" : {
"index" : {
"refresh_interval" : "1s",
"number_of_shards" : "3",
"number_of_replicas" : "1"
}
}
}
GET second_hand_house
DELETE second_hand_house
文档操作
PUT /second_hand_house/_doc/001?routing=SH001
{
"houseCode" : "SH001",
"title" : "房屋1",
"priceTotal" : 20000000,
"unitPrice" : 100000,
"area" : 200.00,
"fromTime" : 1597680310,
"toTime" : 1597690308
}
PUT /second_hand_house/_doc/002?routing=SH002
{
"houseCode" : "SH002",
"title" : "房屋2",
"priceTotal" : 10000000,
"unitPrice" : 100000,
"area" : 100.00,
"fromTime" : 1596680310,
"toTime" : 1596690308
}
PUT /second_hand_house/_doc/001?routing=SH001
{
"houseCode" : "SH001",
"title" : "房屋123",
"priceTotal" : 20000000,
"unitPrice" : 100000,
"area" : 200.00,
"fromTime" : 1597680310,
"toTime" : 1597690308
}
DELETE second_hand_house/_doc/001
GET second_hand_house/_doc/001?routing=SH001
搜索操作
GET second_hand_house/_search
{
"query": {
"match_all": {}
}
}
GET second_hand_house/_search?size=10&from=0
{
"query": {
"match":{
"title": "房"
}
}
}
java客户端使用
初始化
- 引入依赖。es客户端版本交给springboot管理,否则容易冲突。
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>elasticsearch-rest-high-level-clientartifactId>
dependency>
@Bean
public RestHighLevelClient restHighLevelClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
return client;
}
文档操作
Map<String, Object> map = BeanUtils.objectToMap(secondHandHouseEO);
IndexRequest request = new IndexRequest("second_hand_house", "_doc", secondHandHouseEO.getHouseCode())
.source(map);
request.routing(secondHandHouseEO.getHouseCode());
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
if (indexResponse.getResult() == Result.CREATED || indexResponse.getResult() == Result.UPDATED) {
return true;
}
ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
if (shardInfo.getFailed() > 0) {
for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) {
String reason = failure.reason();
LOGGER.error("put es error, {}", reason);
}
return false;
}
return true;
DeleteRequest request = new DeleteRequest("second_hand_house", "_doc", houseCode);
request.routing(houseCode);
DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
ReplicationResponse.ShardInfo shardInfo = deleteResponse.getShardInfo();
if (shardInfo.getFailed() > 0) {
for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) {
String reason = failure.reason();
LOGGER.error("delete es error, {}", reason);
}
return false;
}
return true;
搜索操作
SearchRequest request = new SearchRequest("second_hand_house");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.from(offset);
sourceBuilder.size(limit);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
request.source(sourceBuilder);
SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
List<SecondHandHouseEO> list = new ArrayList<>();
for (SearchHit hit : searchResponse.getHits()) {
Map<String, Object> map = hit.getSourceAsMap();
list.add(BeanUtils.mapToObject(map, SecondHandHouseEO.class));
}
SearchRequest request = new SearchRequest("second_hand_house");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.from(offset);
sourceBuilder.size(limit);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
MatchQueryBuilder queryBuilder = new MatchQueryBuilder("title", title);
sourceBuilder.query(queryBuilder);
request.source(sourceBuilder);
SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
List<SecondHandHouseEO> list = new ArrayList<>();
for (SearchHit hit : searchResponse.getHits()) {
Map<String, Object> map = hit.getSourceAsMap();
list.add(BeanUtils.mapToObject(map, SecondHandHouseEO.class));
}