ElasticSearch入门教程
全文检索
试想一下结构化数据的查询方式,例如数据库的查找方式,我们知道在数据库中构建索引可以极大程度的提高查询速度,这是因为结构化数据有一定程度固定的结构使得我们可以采取某些搜索算法加速查询速度,那既然如此,为什么我们不可以尝试在非结构化数据中,将一部分结构化信息抽取出来,重新组织,然后针对这部分有结构的数据进行索引建立,从而达到加速查询的目的;
上述想法很天然,但却构成了全文检索的基本思路
从非结构化数据中抽取部分结构化数据,并建立索引,再对索引进行搜索的过程,我们成为全文索引
顺序扫描法
比如我们想要在成千上万的文档中,查找包含某一字符串的所有文档,顺序扫描法就必须逐个的扫描每个文档,并且每个文档都要从头看到尾,如果找到,就继续找下一个,直至遍历所有的文档;这种方法通常应用于数据量较小的场景,比如经常使用的
windows安装ElasticSearch
版本对应关系:
springboot2.1.5
spring-boot-starter-data-elasticsearch
elasticsearch-6.4.3
ik-6.4.3
-
下载es
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.3.zip
解压到D:\Java\
-
下载ik
解压文件到D:\Java\elasticsearch-6.4.3\plugins
创建ik文件夹
解压
-
修改D:\Java\elasticsearch-6.4.3\config\elasticsearch.yml配置文件
cluster.name -
运行D:\Java\elasticsearch-6.4.3\bin\elasticsearch.bat
linux安装es
-
下载
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.3.tar.gz -
cd /opt/elasticsearch-6.4.0/config
vim jvm.options
{
-Xmx256m
-Xmx256m
}
vim elasticsearch.yml
{
path.data: /tmp/es/data
cluster.name: nowcoder
path.logs: /tmp/es/logs
} -
添加ik
mkdir opt/elasticsearch-6.4.0/plugins/ik && cd opt/elasticsearch-6.4.0/plugins/ik
yum install unzip
unzip /root/elasticsearch-analysis-ik-6.4.0.zip /opt/elasticsearch-6.4.0/plugins/ik -
创建用户
groupadd nowcoder
useradd nowcoder1 -p 123456 -g nowcoder
-
设置权限并登陆
cd /opt
chown -R nowcoder1:nowcoder *
cd /tmp
chown -R nowcoder1:nowcoder *
sudo - nowcoder1
{
Enter Password:
...
}
-
启动es
cd opt/elasticsearch-6.4.0/bin
./elasticsearch
-
测试es
su -
{
Enter Password:
..
}
action1: curl -X Get "localhost:9200/_cat/health?v"
action2: curl http://localhost:9200/
使用PostMan操作ES
GET localhsot:9200 查询es状态
PUT localhsot:9200/test 创建索引
GET localhost:9200/_cat/indices?v 查看索引状态
DELETE localhsot:9200/test 删除索引
PUT 创建文档
localhsot:9200/test/_doc/1
row-JSON
{
"title":"互联网求职",
"content":"我这里需要招聘运营岗位数名"
}
GET localhsot:9200/test/_search 查询文档
Spring整合ElasticSearch
sql
CREATE TABLE `discuss_post` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` varchar(45) DEFAULT NULL,
`title` varchar(100) DEFAULT NULL,
`content` text,
`type` int DEFAULT NULL COMMENT '0-普通; 1-置顶;',
`status` int DEFAULT NULL COMMENT '0-正常; 1-精华; 2-拉黑;',
`create_time` timestamp NULL DEFAULT NULL,
`comment_count` int DEFAULT NULL,
`score` double DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `index_user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=281 DEFAULT CHARSET=utf8mb3;
-
添加maven依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency> -
添加properties配置项
#elasticsearchProperties
spring.data.elasticsearch.cluster-name=nowcoder
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300 -
解决springboot整合redis+es冲突问题
-
单元测试项
//建立es与实体类的关系 -
创建es仓库
-
基于es的全文检索第一种方法