搜索引擎知识和搜索框架elasticsearch基本介绍
elasticsearch:针对数据量特别大,PB,TB
纯java开发,springboot使用,5.6版本
es升级4->5版本,改动大,但是5版本后,改动不大
elasticSearch主要特点
1、特点:全文检索,结构化检索,数据统计、分析,接近实时处理,分布式搜索(可部署数百台服务器),处理PB级别的数据
搜索纠错,自动完成
2、使用场景:日志搜索,数据聚合,数据监控,报表统计分析
3、国内外使用者:维基百科,Stack Overflow,GitHub
新特性讲解
1、6.2.x版本基于Lucene 7.x,更快,性能进一步提升,对应的序列化组件,升级到Jackson 2.8
mysql:database table rocord
es : index type(只能存在一个) document
2、推荐使用5.0版本推出的Java REST/HTTP客户端,依赖少,比Transport使用更方便,在基准测试中,性能并不输于Transport客户端,
在5.0到6.0版本中,每次有对应的API更新, 文档中也说明,推荐使用这种方式进行开发使用,所有可用节点间的负载均衡
在节点故障和特定响应代码的情况下进行故障转移,失败的连接处罚(失败的节点是否重试取决于失败的连续次数;失败的失败次数越多,客户端在再次尝试同一节点之前等待的时间越长)
3、(重要)不再支持一个索引库里面多个type,6.x版本已经禁止一个index里面多个type,所以一个index索引库只能存在1个type
官方文档:
1、6.0更新特性
https://www.elastic.co/guide/en/elasticsearch/reference/6.0/release-notes-6.0.0.html#breaking-java-6.0.0
2、6.1更新特性
https://www.elastic.co/guide/en/elasticsearch/reference/6.1/release-notes-6.1.0.html
#解压命令
tar -zxvf jdk-8u131-linux-x64.tar.gz
#进入/etc/profile
vim /etc/profile
#在profile文件末尾输入以下文字
JAVA_HOME=/usr/local/java/jdk1.8.0_131
CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME
export CLASSPATH
export PATH
#保存并退出
esc : wq
source /etc/profile
java -version
vi elasticsearch.yml
修改配置项为 network.host: 0.0.0.0 如下图,保存并退出。
PS:elasticserach所需内存比较大,如果服务器的内存不够会报内存溢出的错误,此时可以通过修改conf下的jvm.options文件来解决问题。修改下图位置,将启动内存设置成128M
创建用户
adduser 用户名
设置密码
passwd 用户名
./elasticsearch
此时启动之后就无法再执行其他命令了,这样会很不方便。可以执行如下命令,进行后台启动
./elasticsearch -d
启动成功之后本地浏览器输入http://IP:9200进行访问,出现下图信息即可
1、错误:the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
解决方法:
编辑elasticsearch.yml:
cluster.initial_master_nodes: ["node-1"] ,这里的node-1是上面一个默认的打开就可以了
2、错误:max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
解决方法:
在 /etc/sysctl.conf文件最后添加一行
vm.max_map_count=262144
org.springframework.boot
spring-boot-starter-data-elasticsearch
org.apache.commons
commons-lang3
3.1
##=========Elasticsearch配置=========
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=IP:9300
spring.data.elasticsearch.repositories.enabled=true
import org.springframework.data.elasticsearch.annotations.Document;
import java.io.Serializable;
/**
* @Author:Lyd
* @Date 2020/1/8 11:08
* @Description 文章对象
*/
@Document(indexName="blog",type = "article")
public class Article implements Serializable {
private long id;
private String title;
private String summary;
private String content;
private int pv;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getPv() {
return pv;
}
public void setPv(int pv) {
this.pv = pv;
}
}
import com.lyd.pojo.Article;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;
/**
* @Author:Lyd
* @Date 2020/1/8 14:35
* @Description
*/
@Component
//@Repository
public interface ArticleRespository extends ElasticsearchRepository {
}
import com.lyd.pojo.Article;
import com.lyd.pojo.JsonData;
import com.lyd.respository.ArticleRespository;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author:Lyd
* @Date 2020/1/8 14:50
* @Description es搜索引擎控制层实现
*/
@RestController
@RequestMapping("/api/v1/artical")
public class ArticalController {
@Autowired
private ArticleRespository articleRespository;
/**
* 保存
* @return
*/
@GetMapping(value = "save")
public Object save(){
Article article=new Article();
article.setId(1L);
article.setContent("this is 内容");
article.setPv(888);
article.setTitle("springboot集成Elasticsearch 搜索引擎学习");
article.setSummary("概要搜索");
articleRespository.save(article);
return JsonData.buildSuccess();
}
/**
* ES查询数据
* @param title
* @return json格式查询数据
*/
@GetMapping(value="search")
public Object search(String title){
QueryBuilder queryBuilder= QueryBuilders.matchQuery("title",title);
Iterable list=articleRespository.search(queryBuilder);
return JsonData.buildSuccess(list);
}
}
启动项目浏览器中访问localhost:8080/api/v1/artical/save,执行数据保存操作,然后通过localhost:8080/api/v1/artical/search进行查询操作
PS:如果服务器在阿里云上需要开发9200、9300端口
配置es出现相关问题处理(阿里云、腾讯云,亚马逊云安装问题集合)
1、问题一
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c5330000, 986513408, 0) failed; error='Cannot allocate memory' (errno=12)
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 986513408 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /usr/local/software/temp/elasticsearch-6.2.2/hs_err_pid1912.log
解决:内存不够,购买阿里云的机器可以动态增加内存
2、问题二
[root@iZwz95j86y235aroi85ht0Z bin]# ./elasticsearch
[2018-02-22T20:14:04,870][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:125) ~[elasticsearch-6.2.2.jar:6.2.2]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:112) ~[elasticsearch-6.2.2.jar:6.2.2]
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-6.2.2.jar:6.2.2]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-6.2.2.jar:6.2.2]
解决:用非root用户
添加用户:useradd -m 用户名 然后设置密码 passwd 用户名
3、问题三
./elasticsearch
Exception in thread "main" java.nio.file.AccessDeniedException: /usr/local/software/temp/elasticsearch-6.2.2/config/jvm.options
解决:权限不够 chmod 777 -R 当前es目录
常见配置问题资料:https://www.jianshu.com/p/c5d6ec0f35e0