本文主要是介绍Elasticsearch+SpringBoot开发搜房Demo(1)--Springboot整合ElasticSearch Client和数据初始化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本系列文章以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;@Beanpublic 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"}}}}
}
这篇关于Elasticsearch+SpringBoot开发搜房Demo(1)--Springboot整合ElasticSearch Client和数据初始化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!