Elasticsearch+SpringBoot开发搜房Demo(1)--Springboot整合ElasticSearch Client和数据初始化

本文主要是介绍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和数据初始化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/974785

相关文章

SpringBoot中四种AOP实战应用场景及代码实现

《SpringBoot中四种AOP实战应用场景及代码实现》面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护,在SpringBoot应用中,AO... 目录引言场景一:日志记录与性能监控业务需求实现方案使用示例扩展:MDC实现请求跟踪场景二:权限控制与

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Java NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

Java 实用工具类Spring 的 AnnotationUtils详解

《Java实用工具类Spring的AnnotationUtils详解》Spring框架提供了一个强大的注解工具类org.springframework.core.annotation.Annot... 目录前言一、AnnotationUtils 的常用方法二、常见应用场景三、与 JDK 原生注解 API 的

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

Java中的StringBuilder之如何高效构建字符串

《Java中的StringBuilder之如何高效构建字符串》本文将深入浅出地介绍StringBuilder的使用方法、性能优势以及相关字符串处理技术,结合代码示例帮助读者更好地理解和应用,希望对大家... 目录关键点什么是 StringBuilder?为什么需要 StringBuilder?如何使用 St

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格