本系列文章以ElasticSearch技术栈为核心,结合当下最流行的互联网技术的租房网站实战,解决企业“搜索”业务难题。
Springboot整合ElasticSearch Client
修改Maven依赖,加入
<elasticsearch.version>5.6.1</elasticsearch.version>
<!-- ES -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
配置文件
application.properties
elasticsearch.cluster.name=xunwu
elasticsearch.host=127.0.0.1
elasticsearch.port=9300
读取配置
package com.imooc.config;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticSearchConfig {
@Value("${elasticsearch.host}")
private String esHost;
@Value("${elasticsearch.port}")
private int esPort;
@Value("${elasticsearch.cluster.name}")
private String esName;
@Bean
public TransportClient esClient() throws UnknownHostException {
Settings settings = Settings.builder()
.put("cluster.name", this.esName)
// .put("cluster.name", "elasticsearch")
.put("client.transport.sniff", true)
.build();
InetSocketTransportAddress master = new InetSocketTransportAddress(
InetAddress.getByName(esHost), esPort
// InetAddress.getByName("10.99.207.76"), 8999
);
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(master);
return client;
}
}
Elasticsearch 创建索引
PUT http://:/<索引>
在用Kibana的时候不需要http://:
settings 设置
number_of_shards 分片数
number_of_replicas 副本数
mappings 结构化数据设置
mappings下面的一级属性 是自定义的类型
properties 类型的属性设置节点,下面都是属性
epoch_millis表示时间戳
例如
本例子使用的json
{
"settings": {
"number_of_replicas": 0
},
"mappings": {
"house": {
"dynamic": false,
"properties": {
"houseId": {
"type": "long"
},
"title": {
"type": "text",
"index": "analyzed",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"price": {
"type": "integer"
},
"area": {
"type": "integer"
},
"createTime": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"lastUpdateTime": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"cityEnName": {
"type": "keyword"
},
"regionEnName": {
"type": "keyword"
},
"direction": {
"type": "integer"
},
"distanceToSubway": {
"type": "integer"
},
"subwayLineName": {
"type": "keyword"
},
"subwayStationName": {
"type": "keyword"
},
"tags": {
"type": "text"
},
"street": {
"type": "keyword"
},
"district": {
"type": "keyword"
},
"description": {
"type": "text",
"index": "analyzed",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"layoutDesc" : {
"type": "text",
"index": "analyzed",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"traffic": {
"type": "text",
"index": "analyzed",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"roundService": {
"type": "text",
"index": "analyzed",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"rentWay": {
"type": "integer"
},
"suggest": {
"type": "completion"
},
"location": {
"type": "geo_point"
}
}
}
}
}