Elasticsearch与数据库比较
关系型数据库(比如Mysql) | 非关系型数据库(Elasticsearch) |
数据库Database | 索引Index |
表Table | 类型Type |
数据行Row | 文档Document |
数据列Column | 字段Field |
约束 Schema | 映射Mapping |
安装Elasticsearch
安装Elasticsearch
Elasticsearch6.2.2下载地址
Elasticsearch 6.2.2 | Elastic
学习资料
慕课网 课程介绍,ElasticSearch入门 教程-慕课网
BiBi视频 Spring Boot视频教程(下)整合篇_哔哩哔哩_bilibili
安装kibana插件(推荐)
参考
下载地址
https://www.elastic.co/cn/downloads/past-releases#kibana
安装Elasticsearch-head插件
安装node和npm
yum install -y nodejs npm
将Elasticsearch-head下载下来并且解压
GitHub - mobz/elasticsearch-head: A web front end for an elastic search cluster
进入 解压的目录 elasticsearch-head-master 下执行
npm install
在目录 elasticsearch-head-master 下执行启动es-head
npm run start
将Elasticsearch 和 Elasticsearch-head 绑定在一起
修改 /usr/local/elasticsearch-6.2.2/config/elasticsearch.yml文件
在最后添加一下配置
http.cors.enabled: true
http.cors.allow-origin: "*"
后台启动 Elasticsearch (esuser用户)(esuser用户)(esuser用户)
./bin/elasticsearch -d
启动Elasticsearch -head
npm run start
下下策:在箭头处自己输入
Elasticsearch数据结构
操作
添加
查询
查询一个
http://47.105.132.96:9200/people/man/1
查询全部
http://47.105.132.96:9200/people/man/_search
条件查询
添加
删除
SpringBoot用Jest操作Elasticsearch
POM
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>6.3.1</version>
</dependency>
application.properties
spring.elasticsearch.jest.uris=http://47.105.132.96:9200
Entity
package com.example.demo;
import io.searchbox.annotations.JestId;
public class Article {
@JestId
private Integer id;
private String author;
private String title;
private String content;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Article(Integer id, String author, String title, String content) {
this.id = id;
this.author = author;
this.title = title;
this.content = content;
}
public Article() {
}
@Override
public String toString() {
return "Article [id=" + id + ", author=" + author + ", title=" + title + ", content=" + content + "]";
}
}
Controller
package com.example.demo;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.searchbox.client.JestClient;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
@RestController
public class ArticleController {
@Autowired
private JestClient jestClient;
@RequestMapping("/select")
public String selectArticle() {
/*
* { "query" : { "match" : { "author" : "zhangsan" } } }
*/
String queryStr = "{\n" + " \"query\" : {\n" + " \"match\" : {\n" + " \"author\" : \"zhangsan\"\n"
+ " }\n" + "}\n" + "}";
// 构建搜索功能
Search index = new Search.Builder(queryStr).addIndex("cbeann").addType("news").build();
try {
SearchResult result = jestClient.execute(index);
return result.getJsonString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "error";
}
}
@RequestMapping("/add")
public String addArticle() {
Article a = new Article(100, "zhangsan", "请吃饭", "2019年1月1日我请吃饭");
Index index = new Index.Builder(a).index("cbeann").type("news").build();
try {
jestClient.execute(index);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("--------出错-----------");
}
return "success";
}
@RequestMapping("/update")
public String updateArticle() {
// 添加ID相同的就会覆盖
return "success";
}
@RequestMapping("/delete")
public String deleteArticle() {
// 还未涉及
return "success";
}
}
SpringBoot用SpringDate操作Elasticsearch(注意:版本对应很重要)
SpringBoot 1.5.12 + elasticsearch2.4.6
Docker下载2.4.6
docker pull elasticsearch:2.4.6
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name myes 5e9d896dcc
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ElasticsearchJestDemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
<!-- 根据你的Elasticsearch 的版本选择 jest 的版本 ,比如 你的 ES 是6.x.x ,jest的版本可以使 6.x.x -->
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>6.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
spring.elasticsearch.jest.uris=http://47.105.132.96:9200
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=47.105.132.96:9300
实体类
package com.example.demo;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName="cbeann",type="books")
public class Book {
private Integer id;
private String name;
private String auther;
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", auther=" + auther + "]";
}
public Book(Integer id, String name, String auther) {
super();
this.id = id;
this.name = name;
this.auther = auther;
}
public Book() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuther() {
return auther;
}
public void setAuther(String auther) {
this.auther = auther;
}
}
Repository(类似JPA)
package com.example.demo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface BookRepository extends ElasticsearchRepository<Book, Integer>{
}
BookController
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookRepository bookRepository;
@RequestMapping("/add")
public String add(){
Book book=new Book(1, "西游记", "吴承恩");
bookRepository.index(book);
return "success";
}
}
或者
@RestController
public class OtherController {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
}
elasticsearch读写流程
15_分布式搜索引擎写入和查询的工作流程是什么样的?_哔哩哔哩_bilibili