- Elasticsearch
- 安装配置
- 配置文件
- head插件安装
- ES入门
- IK分词器
- 常用映射类型
- 索引管理
-
新版本要求至少jdk1.8以上。
-
支持tar、zip、rpm等多种安装方式。
在windows下开发建议使用ZIP安装方式
- 使用zip、tar安装,配置文件的地址在安装目录的config下。
- 使用RPM安装,配置文件在/etc/elasticsearch下。
- 使用MSI安装,配置文件的地址在安装目录的config下,并且会自动将config目录地址写入环境变量
ES_PATH_CONF。本教程使用的zip包安装,配置文件在ES安装目录的config下。
配置文件如下:
- elasticsearch.yml : 用于配置Elasticsearch运行参数
- jvm.options : 用于配置Elasticsearch JVM设置
- log4j2.properties: 用于配置Elasticsearch日志
elasticsearch.yml
# 配置集群的名称, 默认是elasticsearch cluster.name: Haha # 节点名,通常一台物理服务器就是一个节点 node.name: hh_node_1 # 设置绑定主机的ip地址,设置为0.0.0.0表示绑定任何ip network.host: 0.0.0.0 # 设置对外服务器的http端口,默认是9200 http.port: 9200 # 集群节点之间通讯端口 transport.tcp.port: 9300 # 指定该节点是否有资格被选举成为master节点,默认是true node.master: true # 指定该节点是否存储索引数据,默认为true node.data: true # 设置集群中master节点的初始化列表 discovery.zen.ping.unicast.hosts: ["0.0.0.0:9300", "0.0.0.0:9301", "0.0.0.0:9302"] # 主节点数量的最少之,此公式为(master_eligible_nodes / 2) + 1 discovery.zen.minimum_master_nodes: 1 # 设置为true可以所著ES使用的内存 bootstrap.memory_lock: false # 单机允许最大存储节点数,通常单机启动一个节点建议设置为1. node.max_local_storage_nodes: 1 # 新建data文件存储的路径 path.data: F:elasticsearch-6.2.1data # 日志文件存储的路径 path.logs: F:elasticsearch-6.2.1logs http.cors.enabled: true http.cors.allow-origin: /.*/head插件安装
启动命令: npm run start
ES入门- 创建索引库
{
"settings":{
"index":{
"number_of_shards":1,
"number_of_replicas":0
}
}
}
number_of_shards:设置分片的数量,在集群中通常设置多个分片,表示一个索引库将拆分成多片分别存储不同的结点,提高了ES的处理能力和高可用性,入门程序使用单机环境,这里设置为1。
number_of_replicas:设置副本的数量,设置副本是为了提高ES的高可靠性,单机环境设置为0.
当然也可以通过head插件来创建
- 创建映射
- post 请求:http://localhost:9200/yh_course/doc/_mapping
{
"properties":{
"name":{
"type":"text"
},
"description":{
"type":"text"
},
"studymodel":{
"type":"keyword"
}
}
}
- 创建文档
- 发送:put 或Post http://localhost:9200/yh_course/doc/id值(如果不指定id值ES会自动生成ID)
{
"name":"Bootstrap开发框架",
"description":"Bootstrap是由Twitter推出的一个前台页面开发框架,在行业之中使用较为广泛。此开发框架包含了大量的CSS、JS程序代码,可以帮助开发者(尤其是不擅长页面开发的程序人员)轻松的实现一个不受浏览器限制的精美界面效果。",
"studymodel":"201001"
}
可以使用postman测试
- 搜索文档
-
发送:get http://localhost:9200/yh_course/doc/4028e58161bcf7f40161bcf8b77c0000
-
查询所有记录
发送 get http://localhost:9200/yh_course/doc/_search注意:body体中不能包含数据
-
查询名称中包括spring 关键字的的记录
发送:get http://localhost:9200/yh_course/doc/_search?q=name:bootstrap -
查询学习模式为201001的记录
发送 get http://localhost:9200/yh_course/doc/_search?q=studymodel:201001
查询结果分析:
IK分词器took:本次操作花费的时间,单位为毫秒。
timed_out:请求是否超时
_shards:说明本次操作共搜索了哪些分片
hits:搜索命中的记录
hits.total : 符合条件的文档总数 hits.hits :匹配度较高的前N个文档
hits.max_score:文档匹配得分,这里为最高分
_score:每个文档都有一个匹配度得分,按照降序排列。
_source:显示了文档的原始内容
安装完成的IK分词器需要放到ES存放插件的包下面 , 重新启动服务器
post 发送:localhost:9200/_analyze
{"text":"测试分词器,后边是测试内容:spring cloud实战"}
两种分词模式
- ik_max_word====>细粒度拆分
- ik_smart====>粗粒度拆分
自定义词库
- 如果要让分词器支持一些专有词语,可以自定义词库。 iK 分词器自带一个 main.dic 的文件,此文件为词库文件
- 在上边的目录中新建一个my.dic文件(注意文件格式为utf-8(不要选择utf-8 BOM)),可以在其中自定义词汇
从新启动服务器
- 查询所有映射
GET:http : //localhost:9200/_mapping
映射创建成功可以添加字段,已有的字段不允许更新
通过删除索引来删除映射
常用映射类型- text文本字段
"name": {
"type": "text",
"analyzer":"ik_max_word"
}
上边指定了analyzer是指在索引和搜索都使用ik_max_word,如果单独想定义搜索时使用的分词器则可以通过
search_analyzer属性。
对于ik分词器建议是索引时使用ik_max_word将搜索内容进行细粒度分词,搜索时使用ik_smart提高搜索精确性
"name": {
"type": "text",
"analyzer":"ik_max_word",
"search_analyzer":"ik_smart"
}
通过index属性指定是否索引。默认为 index=true ,即要进行索引,只有进行索引才可以从索引库搜索到
"pic": {
"type": "text",
"index":false
}
插入文档:
http://localhost:9200/yh_course/doc/4028e58161bcf7f40161bcf8b77c0000
{
"name":"Bootstrap开发框架",
"description":"Bootstrap是由Twitter推出的一个前台页面开发框架,在行业之中使用较为广泛。此开发框架包含了大量的CSS、JS程序代码,可以帮助开发者(尤其是不擅长页面开发的程序人员)轻松的实现一个不受浏览器限制的精美界面效果。",
"studymodel":"201002",
"pic":"group1/M00/00/01/wKhlQFqO4MmAOP53AAAcwDwm6SU490.jpg"
}
查询测试:
Get http://localhost:9200/yh_course/_search?q=name:开发
Get http://localhost:9200/yh_course/_search?q=description:开发
Get http://localhost:9200/yh_course/_search?q=pic:group1/M00/00/01/wKhlQFqO4MmAOP53AAAcwDwm6SU490.jpg
Get http://localhost:9200/yh_course/_search?q=studymodel:201002
上边介绍的text文本字段在映射时要设置分词器,keyword字段为关键字字段,通常搜索keyword是按照整体搜
{
"properties": {
"studymodel":{
"type":"keyword"
},
"name":{
"type":"keyword"
}
}
}
索引管理
添加依赖
org.elasticsearch.client elasticsearch‐rest‐high‐level‐client 6.2.1 org.elasticsearch elasticsearch 6.2.1
配置文件(application.yml)
server:
port: ${port:40100}
spring:
application:
name: yh‐search‐service
yh:
elasticsearch:
hostlist: ${eshostlist:127.0.0.1:9200} #多个结点中间用逗号分隔
配置类
@Configuration
public class ElasticsearchConfig {
@Value("${yh.elasticsearch.hostlist}") private
String hostlist;
@Bean
public RestHighLevelClient restHighLevelClient(){
//解析hostlist配置信息
String[] split = hostlist.split(",");
//创建HttpHost数组,其中存放es主机和端口的配置信息
HttpHost[] httpHostArray = new HttpHost[split.length];
for(int i=0;i
启动类
@SpringBootApplication
@EntityScan("com.yh.framework.domain.search")//扫描实体类
@ComponentScan(basePackages={"com.lxw.api"})//扫描接口
@ComponentScan(basePackages={"com.yh.search"})//扫描本项目下的所有类
@ComponentScan(basePackages={"com.lxw.framework"})//扫描common下的所有类
public class SearchApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(SearchApplication.class, args);
}
}
创建索引库
@SpringBootTest
@RunWith(SpringRunner.class)
public class TestIndex {
@Autowired
RestHighLevelClient client;
@Autowired
RestClient restClient;
//创建索引库
@Test
public void testCreateIndex() throws IOException {
//创建索引请求对象,并设置索引名称
CreateIndexRequest createIndexRequest = new
CreateIndexRequest("yh_course"); //设置索引参数
createIndexRequest.settings(Settings.builder().put("number_of_shards",1)
.put("number_of_replicas",0));
//设置映射
createIndexRequest.mapping("doc"," {n" +
" t"properties": {n" +
" "name": {n" +
" "type": "text",n" +
" "analyzer":"ik_max_word",n" +
" "search_analyzer":"ik_smart"n" +
" },n" +
" "description": {n" +
" "type": "text",n" +
" "analyzer":"ik_max_word",n" +
" "search_analyzer":"ik_smart"n" +
" },n" +
" "studymodel": {n" +
" "type": "keyword"n" +
" },n" +
" "price": {n" +
" "type": "float"n" +
" }n" +
" }n" +
"}", XContentType.JSON);
//创建索引操作客户端
IndicesClient indices = client.indices();
//创建响应对象
CreateIndexResponse createIndexResponse = indices.create(createIndexRequest);
//得到响应结果
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println(acknowledged);
}
//删除索引库
@Test
public void testDeleteIndex() throws IOException {
//删除索引请求对象
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("yh_course");
//删除索引
DeleteIndexResponse deleteIndexResponse = client.indices().delete(deleteIndexRequest);
//删除索引响应结果
boolean acknowledged = deleteIndexResponse.isAcknowledged();
System.out.println(acknowledged);
}
}
添加文档
//添加文档
@Test
public void testAddDoc() throws IOException {
//准备json数据
Map jsonMap = new HashMap<>();
jsonMap.put("name", "spring cloud实战");
jsonMap.put("description", "本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud
基础入门 3.实战Spring Boot 4.注册中心eureka。");
jsonMap.put("studymodel", "201001");
SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy‐MM‐dd HH:mm:ss");
jsonMap.put("timestamp", dateFormat.format(new Date()));
jsonMap.put("price", 5.6f);
//索引请求对象
IndexRequest indexRequest = new IndexRequest("yh_course","doc");
//指定索引文档内容
indexRequest.source(jsonMap);
//索引响应对象
IndexResponse indexResponse = client.index(indexRequest);
//获取响应结果
DocWriteResponse.Result result = indexResponse.getResult();
System.out.println(result);
}
查询文档
//查询文档
@Test
public void getDoc() throws IOException {
GetRequest getRequest = new GetRequest(
"yh_course",
"doc",
"4028e581617f945f01617f9dabc40000");
GetResponse getResponse = client.get(getRequest);
boolean exists = getResponse.isExists();
Map sourceAsMap = getResponse.getSourceAsMap();
System.out.println(sourceAsMap);
}
更新文档
//更新文档
@Test
public void updateDoc() throws IOException {
UpdateRequest updateRequest = new UpdateRequest("yh_course", "doc",
"4028e581617f945f01617f9dabc40000");
Map map = new HashMap<>();
map.put("name", "spring cloud实战");
updateRequest.doc(map);
UpdateResponse update = client.update(updateRequest);
RestStatus status = update.status();
System.out.println(status);
}
删除文档
//根据id删除文档
@Test
public void testDelDoc() throws IOException {
//删除文档id
String id = "eqP_amQBKsGOdwJ4fHiC";
//删除索引请求对象
DeleteRequest deleteRequest = new
DeleteRequest("yh_course","doc",id); //响应对象
DeleteResponse deleteResponse = client.delete(deleteRequest);
//获取响应结果
DocWriteResponse.Result result = deleteResponse.getResult();
System.out.println(result);
}



