spring boot 整合 elasticsearch

1 导入依赖

<dependencies><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>6.2.1</version></dependency><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>6.2.1</version></dependency></dependencies>

2 配置文件

server:port: ${port:40100}
spring:application:name: search
search:elasticsearch:hostlist: ${eshostlist:127.0.0.1:9200} #多个结点中间用逗号分隔

3 创建连接客户端

@Configuration
public class ElasticsearchConfig {@Value("${search.elasticsearch.hostlist}")private String hostlist;@Beanpublic RestHighLevelClient restHighLevelClient(){//解析hostlist配置信息String[] split = hostlist.split(",");//创建HttpHost数组,其中存放es主机和端口的配置信息HttpHost[] httpHostArray = new HttpHost[split.length];for(int i=0;i<split.length;i++){String item = split[i];httpHostArray[i] = new HttpHost(item.split(":")[0], Integer.parseInt(item.split(":")[1]), "http");}//创建RestHighLevelClient客户端return new RestHighLevelClient(RestClient.builder(httpHostArray));}//项目主要使用RestHighLevelClient,对于低级的客户端暂时不用@Beanpublic RestClient restClient(){//解析hostlist配置信息String[] split = hostlist.split(",");//创建HttpHost数组,其中存放es主机和端口的配置信息HttpHost[] httpHostArray = new HttpHost[split.length];for(int i=0;i<split.length;i++){String item = split[i];httpHostArray[i] = new HttpHost(item.split(":")[0], Integer.parseInt(item.split(":")[1]), "http");}return RestClient.builder(httpHostArray).build();}}

创建索引库 映射

注 : es 中索引库相当于数据库中的表 映射相当于数据库中的字段
http方式创建索引库

put http://localhost:9200/索引库名称
{"settings":{"index":{"number_of_shards":1,  分片数量"number_of_replicas":0  副本数量}}
}

http方式创建映射

post:http://localhost:9200/索引库名称/类型名称/_mapping{"properties":{"description":{"type":"text", 数据类型"analyzer":"ik_max_word", 创建索引时细粒度分词"search_analyzer":"ik_smart" 搜索时粗粒度分词},"name":{"type":"text","analyzer":"ik_max_word","search_analyzer":"ik_smart"},"pic":{"type":"text","index":false  不创建索引,搜不到},"price":{"type":"float" 数值类型},"studymodel":{"type":"keyword"  精确匹配},"timestamp":{"type":"date",  日期类型"format":"yyyy‐MM‐dd HH:mm:ss||yyyy‐MM‐dd||epoch_millis"}}

http方式插入文档

put http://localhost:9200/索引库名称/类型名称{"name":"spring开发基础","description":"spring 在java领域非常流行,java程序员都在用。","studymodel":"201001","price":88.6,"timestamp":"2018‐02‐24 19:11:35","pic":"group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg"
}

java api 方式创建 索引库并

package com.search;import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;@SpringBootTest(classes = SearchApplication.class)
@RunWith(SpringRunner.class)
public class Test1 {@AutowiredRestHighLevelClient client;@AutowiredRestClient restClient;//创建索引库@Testpublic void test01() throws IOException {//创建索引请求对象,并设置索引名称CreateIndexRequest createIndexRequest = new CreateIndexRequest("es_article");//设置索引参数 分片数1 副本数0 createIndexRequest.settings(Settings.builder().put("number_of_shards",1).put("number_of_replicas",0));//设置映射createIndexRequest.mapping("doc","{\n" +"    \"properties\":{\n" +"        \"name\":{\n" +"            \"type\":\"text\"\n" +"        },\n" +"        \"description\":{\n" +"            \"type\":\"text\",\n" +"            \"analyzer\":\"ik_max_word\",\n" +"            \"search_analyzer\":\"ik_smart\"\n" +"        },\n" +"        \"studymodel\":{\n" +"            \"type\":\"keyword\"\n" +"        },\n" +"        \"timestamp\":{\n" +"            \"type\":\"date\",\n" +"            \"format\":\"yyyy‐MM‐dd HH:mm:ss||yyyy‐MM‐dd\"\n" +"        }\n" +"    }\n" +"}", XContentType.JSON);//创建索引操作客户端IndicesClient indices = client.indices();//创建响应对象CreateIndexResponse createIndexResponse = indices.create(createIndexRequest);//得到响应结果boolean acknowledged = createIndexResponse.isAcknowledged();System.out.println(acknowledged);}//添加文档@Testpublic void test02() throws IOException {//准备json数据Map<String, Object> jsonMap = new HashMap<>(); jsonMap.put("name", "Bootstrap开发");jsonMap.put("description", "Bootstrap是由Twitter推出的一个前台页面开发框架,是一个非常流行的开发框架,此框架集成了多种页面效果。此开发框架包含了大量的CSS、JS程序代码,可以帮助开发者(尤其是不擅长页面开发的程序人员)轻松    的实现一个不受浏览器限制的精美界面效果");jsonMap.put("studymodel", "201001");jsonMap.put("id","1");SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy‐MM‐dd HH:mm:ss"); jsonMap.put("timestamp", dateFormat.format(new Date())); //索引请求对象IndexRequest indexRequest = new IndexRequest("es_article","doc");//指定索引文档内容indexRequest.source(jsonMap);//索引响应对象IndexResponse indexResponse = client.index(indexRequest);DocWriteResponse.Result result = indexResponse.getResult();System.out.println(result);}//删除文档@Testpublic void test03() throws IOException {//删除文档idString id = "CFpcGnQB4Zx9zTygBfYS";//删除索引请求对象DeleteRequest deleteRequest = new DeleteRequest("es_article","doc",id);//响应对象DeleteResponse deleteResponse = client.delete(deleteRequest);//获取响应结果DocWriteResponse.Result result = deleteResponse.getResult(); System.out.println(result);}}

spring boot 整合 elasticsearch 创建索引库 映射相关推荐

  1. Spring Boot整合elasticsearch实现全文检索

    文章目录 1.引入 1.1 Luence 1.2 Solr 1.3 ElasticSearch 2. ElasticSearch安装 2.1 云服务器安装 2.1.1. docker安装 2.1.2 ...

  2. Elasticsearch学习(3) spring boot整合Elasticsearch的原生方式

    前面我们已经介绍了spring boot整合Elasticsearch的jpa方式,这种方式虽然简便,但是依旧无法解决我们较为复杂的业务,所以原生的实现方式学习能够解决这些问题,而原生的学习方式也是E ...

  3. Elasticsearch实战篇——Spring Boot整合ElasticSearch

    2019独角兽企业重金招聘Python工程师标准>>> 当前Spring Boot很是流行,包括我自己,也是在用Spring Boot集成其他框架进行项目开发,所以这一节,我们一起来 ...

  4. ElasticSearch实战篇 - Spring Boot 整合 ElasticSearch

    点击上方 Java后端,选择 设为星标 优质文章,及时送达 作者:冯文议 链接:segmentfault.com/a/1190000018625101 当前Spring Boot很是流行,包括我自己, ...

  5. 怎样合理创建es索引_如何通过Elasticsearch创建索引库?

    今天是刘小爱自学Java的第158天. 感谢你的观看,谢谢你. 学习计划安排如下:Elasticsearch作为一门全文检索技术,那它是如何使用的呢? 先学习Elasticsearch的一些语法,后续 ...

  6. Spring Boot 整合 Elasticsearch,实现 function score query 权重分查询

    运行环境:JDK 7 或 8,Maven 3.0+ 技术栈:SpringBoot 1.5+,ElasticSearch 2.3.2 本文提纲 一.ES 的使用场景 二.运行 springboot-el ...

  7. Elasticsearch创建索引和映射结构详解

    前言 这篇文章详细介绍了如何创建索引和某个类型的映射. 下文中[address]指代elasticsearch服务器访问地址(http://localhost:9200). 1       创建索引 ...

  8. Spring boot整合ElasticSearch

    一.项目目录: 在这里插入代码片 本文用的spring-boot-starter-parent为2.3.0.RELEASE elasticsearch为7.6.2自动配置 二.pom.xml依赖配置: ...

  9. spring boot 整合 ip2region(ip地址库)

    Ip2region是什么? ip2region - 准确率99.9%的离线IP地址定位库,0.0x毫秒级查询,ip2region.db数据库只有数MB,提供了java,php,c,python,nod ...

  10. ElasticSearch教程与实战:从搭建服务到Spring Boot整合

    目录 写在前面 Elasticsearch是什么?可以解决什么问题? 关于Elasticsearch版本的选择 Elasticsearch的几个基本概念 索引(index) 类型(type) 文档(d ...

最新文章

  1. Compiler编译过程
  2. anaconda有什么用?pycharm有什么用?anaconda怎么与pycharm一起联合使用?
  3. 4 款 MySQL Binlog 日志处理工具对比,谁才是王者?
  4. python基础知识资料-Python基础知识梳理 - 第02部分
  5. tornado的资料(暂时没看)
  6. jQuery-1.9.1源码分析系列(二)jQuery选择器续2——筛选
  7. 公安计算机专业就业前景,公安视听技术专业毕业后干什么
  8. Centos 8 RHEL 8 破解root密码
  9. Matlab将数字数组转换为字符数组(用于标明点号)
  10. 谈谈我在敏捷开发中遇到的那些坑
  11. 调用百度地图API进行当前位置定位失败解决方法
  12. 留在一线,逃离一线?我从上海举家回老家的生活经历告诉你!
  13. 在HTML中禁止IE缓存
  14. python提取pdf发票信息_python读取pdf(发票)
  15. Flowable工作流引擎技术方案
  16. 常见的浏览器内核有哪些?
  17. 关于客户端下载文件而不是在服务器生成文件
  18. 计算机操作系统经典进程同步问题
  19. 连接手表_小米手表体验报告(上)
  20. MySQL之——崩溃-修复损坏的innodb:innodb_force_recovery

热门文章

  1. 逻辑学学习.14 --- 谓词逻辑(六):数量量词和摹状词
  2. 代理服务器的工作原理是什么?
  3. 正则表达式:提取数字和小数点
  4. 计算机如何取消自动关机,电脑怎么设置自动关机及取消自动关机
  5. Python爬取豆瓣电影信息并分析
  6. 关于升级短信源码开发接入SMPP通道
  7. [统计学笔记] 统计学中的相关关系和三大相关系数
  8. Git error: unable to create file xxx: Filename too long
  9. 数据集 过滤时 RecordCount 属性
  10. 重复测量方差分析步骤汇总