1.ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。
2.解压jar包
3.解压后的目录结构
Bin:elasticSearcher 所有的可以执行的命令
Config: elasticsearcher 配置文件
Data: elasticsearcher 数据文件
Lib:elasticsearcher 运行使用的jar
Logs:elasticsearcher日志文件
Modules: elasticsearcher 使用模型案例
Plugins:elasticsearcher 的插件
4.认识下elasticsearch的一些配置 :打开config文件夹下的elasticsearcher.yml文件
cluster.name:my-application:当前elasticsearcher集群服务器名称
node.name:node-1:当前节点的名称
network.host:192.168.0.1 :当前节点绑定的IP地址
http.port: 9200 :当前服务器绑定的端口
5.打开bin目录:运行elasticsearcher.bat
6.打开浏览器输入:http://localhost:9200/ 发现运行成功
7.安装elasticsearch插件head
7.1安装node.js
http://nodejs.cn/download/
7.2解压缩后,将Node.js目录配置到Path路径下
7.3打开cmd看看是否安装成功
8.安装git
9.在cmd切换到head插件安装目录下:输入 git clone https://github.com/mobz/elasticsearch-head.git
10.注意elasticsearch-head不要放置到elasticsearch\plugins\目录下,挪出来
11.输入npm install 安装
12.安装完成后,配置elasticsearch允许head插件连接(默认是不允许连接的)
打开elasticsearch/config/elasticsearch.yml 在文件的最后添加:
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-methods: OPTIONS,HEAD,GET,POST,PUT,DELETE
http.cors.allow-headers: "X-Requested-With,Content-Type,Content-Length,X-User"
13.保存,并进入到elasticsearch-head中启动head插件
14.使用浏览器访问 :http://localhost:9100
15.导入依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.1.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.1.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.1.0</version>
</dependency>
16.测试一下
public class TestElasticSearch {
//和ElasticSearch交互的客户端类,高级别的客户端类,其内部是低级别的客户端,所以需要低级别客户端来创建
//低级客户端维护一个连接池,并启动一些线程,因此当你用完以后应该关闭高级客户端,并且在内部它将会关闭低级客户端,以释放这些资源。关闭客户端可以使用close()方法
private RestHighLevelClient client;
@Before
public void initRestHighLevelClient(){
//初始化RestHighLevelClient22
client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"Http")));
}
public List<Book> loadAll() throws Exception {
List<Book> bookList = new ArrayList<>();
Class.forName("com.mysql.cj.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/demo?useSSL=true&characterEncoding=utf-8&serverTimezone=UTC";
Connection conn= DriverManager.getConnection(url,"root","root");
PreparedStatement ps=conn.prepareStatement("select * from book");
ResultSet rs=ps.executeQuery();
while(rs.next()){
Book b=new Book();
b.setBookId(rs.getInt("bookId"));
b.setBookName(rs.getString("bookName"));
b.setBookAuthor(rs.getString("bookAuthor"));
b.setBookPrice(rs.getDouble("bookPrice"));
b.setBookInfo(rs.getString("bookInfo"));
bookList.add(b);
}
conn.close();
return bookList;
}
@Test
public void addData() throws Exception {
//批量操作对象
BulkRequest request=new BulkRequest();
List<Book> bookList=loadAll();
for(Book book : bookList){
//向这个批量操作对象中添加数据,同时还支持删除、修改
//添加文档source
request.add(new IndexRequest("book").source(XContentType.JSON,"bookId",book.getBookId(),"bookName",book.getBookName(),"bookAuthor",book.getBookAuthor(),"bookPrice",book.getBookPrice(),"bookInfo",book.getBookInfo()));
}
//客户端等待BulkResponse返回
BulkResponse responses= client.bulk(request, RequestOptions.DEFAULT);
System.out.println(responses.status());//返回状态码
}
@Test
public void loadElasticAll() throws IOException{
//查询请求
SearchRequest request = new SearchRequest();
//指定索引
request.indices("book");
//查询条件
//设置查询范围
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
//绑定查询条件
request.source(searchSourceBuilder);
//得到响应对象
SearchResponse response = client.search(request,RequestOptions.DEFAULT);
//得到查询结果集
SearchHits hits = response.getHits();
for (SearchHit hit:hits) {
//查询的结果 JSON字符串形式
System.out.println(hit.getSourceAsString());
}
}
}