一.ElasticSearch的客户端操作
1.所需要的资料链接链接:https://pan.baidu.com/s/11Sxwu-M5VO2UAzzlzwdUIw?pwd=dyjf
提取码:dyjf
2.nodejs安装下载链接Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.https://nodejs.org/en/download/node -v查看版本
在cmd控制台中输入如下执行命令:
cnpm install -g grunt-cli
结果如下
进入elasticsearch-head-master目录启动head,在命令提示符下输入命令:
npm install
grunt server
默认端口9100
输入localhost:9100,在连接处填上elasticsearch的ip地址,以及对应的端口
2.Kibana下载安装
下载镜像
docker pull docker.io/kibana:5.6.8
创建容器
docker run -it -d -e ELASTICSEARCH_URL=http://192.168.220.100:9200 --name kibana -p 5601:5601 kibana:5.6.8
在输入网址一栏,输入elasticserch的ip地址+5601,就会进入上图所示的页面。
快捷键
ctrl+i 自动缩进
ctrl+enter 提交请求
down 打开自动补全菜单
enter或tab 选中项自动补全
esc 关闭补全菜单
3,操作命令
get表示查找,
put表示修改,
post表示创建,
delete表示删除
4.Java API使用elasticserach
创建springboot工程,并创建一些类和接口,如下图所示
添加配置,application.yml
spring:
data:
elasticsearch:
cluster-name: my-es
//ip地址,端口是9300
cluster-nodes: 10.34.19.191:9300
pom.xml配置
4.0.0 org.springframework.boot spring-boot-starter-parent2.1.2.RELEASE com.pzz es-demo20.0.1-SNAPSHOT es-demo2 Demo project for Spring Boot 11 org.springframework.boot spring-boot-starter-data-elasticsearchorg.springframework.boot spring-boot-starter-testtest org.junit.jupiter junit-jupiterRELEASE test org.springframework.boot spring-boot-maven-plugin
实体类Artical
@Document(indexName = "pfg_blog",type = "article")
public class Article {
@Id
@Field(type = FieldType.Long, store = true)
private long id;
@Field(type = FieldType.Text, store = true, analyzer = "ik_smart")
private String title;
@Field(type = FieldType.Text, store = true, analyzer = "ik_smart")
private String content;
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 getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "Article{" +
"序号=" + id +
", 标题='" + title + ''' +
", 内容='" + content + ''' +
'}';
}
}
dao类
package com.pzz.dao;
import com.pzz.domain.Article;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;
import java.util.List;
public interface ArticleDao extends ElasticsearchCrudRepository {
//findby表示查询,Title表示要查找的属性
public List findByTitle(String title);
//findby表示查询,Title表示要查找的属性,or表示或者,Title与content满足其中一个就行
public List findByTitleOrContent(String title, String content);
//findby表示查询,Title表示要查找的属性,or表示或者,Title与content满足其中一个就行
//Pageable 表示分页
public List findByTitleOrContent(String title, String content, Pageable pageable);
}
创建测试类
package com.pzz.esdemo2;
import com.pzz.dao.ArticleDao;
import com.pzz.domain.Article;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest
class EsDemo2ApplicationTests {
@Autowired
private ElasticsearchTemplate template;
@Autowired
private ArticleDao dao;
@Test
void contextLoads() {
}
@Test
public void creatIndex(){
System.out.println(template.createIndex(Article.class));
}
//创建文档
@Test
public void addDocument() throws Exception {
for (long i = 10; i <= 20; i++) {
//创建一个Article对象
Article article = new Article();
article.setId(i);
article.setTitle("女护士路遇昏迷男子跪地抢救:救人是职责更是本能" + i);
article.setContent("这是一个美丽的女护士妹妹" + i);
//把文档写入索引库
dao.save(article);
}
}
//根据id删除文档
@Test
public void deleteDocumentById() throws Exception {
dao.deleteById(11l);
}
//查询所有文档
@Test
public void findAll(){
Iterable all = dao.findAll();
all.forEach(System.out::println);
}
//更具title属性查找
@Test
public void findByTitle() {
dao.findByTitle("女护士").forEach(System.out::println);
}
//满足title或content的条件查询,并且进行分页
@Test
public void findByTitlePage(){
PageRequest of = PageRequest.of(1, 5);
dao.findByTitleOrContent("女护士","美丽",of).forEach(System.out::println);
}
}
最终效果展示



