目录
1.1.什么是ElasticSearch
1.2.ElasticSearch特点
1.3.Solr与Elasticsearch对比
2.ElasticSearch安装(服务端)
2.1.ElasticSearch部署与启动
2.2.ElasticSearch体系结构
3. elasticsearch-head 插件的安装与使用(客户端)
3.1.Head插件安装
3.2.Head插件操作
3.2.1.新建索引
3.2.2.新建或修改文档
3.2.3.搜索文档
3.2.4.删除文档
4.搜索微服务开发
4.1.整体业务逻辑分析图
4.2.logstash实现ElasticSearch与MySQL数据同步
4.3.spring-ElasticSearch
4.3.1.在pom.xml导入依赖
4.3.2.在application.yml添加配置
4.3.3编写启动类
4.3.4.编写实体映射
4.3.5编写ArticleController
4.3.6ArticleService
4.3.7编写ArticleDao
4.3.8浏览器测试
{
"name" : "bG771C-",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "uUmLXXcGRCaFkHdaxtOyGQ",
"version" : {
"number" : "5.6.8",
"build_hash" : "688ecce",
"build_date" : "2018-02-16T16:46:30.010Z",
"build_snapshot" : false,
"lucene_version" : "6.6.1"
},
"tagline" : "You Know, for Search"
}
下表是 Elasticsearch 与 MySQL 数据库逻辑结构概念的对比
如果都是通过rest请求的方式使用Elasticsearch,未免太过麻烦,而且也不够人性化。我们一般都会使用图形化界面来实现Elasticsearch的日常管理,最常用的就是Head插件。
http.cors.enabled:true
http.cors.allow-origin:"*"
此步为允许elasticsearch跨越访问点击连接即可看到相关信息
选择“索引”选项卡,点击“新建索引”按钮:输入索引名称点击OK
在复合查询-查询中输入提交地址,输入内容http://localhost:9200/索引名/类型名/id,提交方式为PUT
{json中提交文档数据}
点击数据浏览,点击要查询的索引名称,右侧窗格中显示文档信息
点击数据浏览,点击title,输入收缩内容
在复合查询-查询中输入提交地址,输入内容http://localhost:9200/索引名/类型名/id,提交方式为DELETE
logstash下载地址:https://www.elastic.co/cn/downloads/logstash
input {
jdbc {
# mysql jdbc connection string to our backup databse
jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/tensquare_article?characterEncoding=UTF8&useSSL=true"
# the user we wish to excute our statement as
jdbc_user => "root"
jdbc_password => "123456"
# the path to our downloaded jdbc driver
jdbc_driver_library => "D:/logstash-5.6.8/logstash-5.6.8/mysqletc/mysql-connector-java-5.1.46.jar"
# the name of the driver class for mysql
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "10"
#以下对应着要执行的 sql 的绝对路径。
#statement_filepath => ""
statement => "select * from tb_article"
#定时字段 各字段含义(由左至右)分、时、天、月、年,全部为*默认含义为每分钟都更新(测试结果,不同的话请留言指出)
schedule => "* * * * *"
}
}
output {
elasticsearch {
#ESIP地址与端口
hosts => "127.0.0.1:9200"
#ES索引名称(自己定义的)
index => "tensquare"
#自增ID编号
document_id => "%{id}"
document_type => "article"
}
stdout {
#以JSON格式输出
codec => json_lines
}
}
(3)将 mysql 驱动包 mysql-connector-java-5.1.46.jar 拷贝至 D:/logstash-5.6.8/mysqletc/ 下 。
D:/logstash-5.6.8 是你的安装目录
org.springframework.data
spring-data-elasticsearch
server:
port: 9007
spring:
application:
name: tensquare-search
data:
elasticsearch:
cluster-nodes: 127.0.0.1:9300
package com.tensquare.search;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class SearchApplication {
public static void main(String[] args) {
SpringApplication.run(SearchApplication.class, args);
}
}
import org.springframework.data.elasticsearch.annotations.Document;
//此处表明elasticSearch的索引和类型,即对应mysql的数据库和表名
@Document(indexName = "tensquare",type = "article")
public class Article implements Serializable{
......
}
package com.tensquare.search.controller;
import com.tensquare.search.pojo.Article;
import com.tensquare.search.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
/**
* 文章controller
*/
@RestController
@RequestMapping("/article")
@CrossOrigin
public class ArticleController {
@Autowired
private ArticleService articleService;
/**
* 文章搜索
*/
@RequestMapping(value = "/search/{keyword}/{page}/{size}",method = RequestMethod.GET)
public Result search(@PathVariable String keyword,@PathVariable int page,@PathVariable int size){
Page pageData = articleService.search(keyword,page,size);
return new Result(true, StatusCode.OK,"搜索成功",new PageResult<>(pageData.getTotalElements(),pageData.getContent()));
}
}
package com.tensquare.search.service;
import com.tensquare.search.dao.ArticleDao;
import com.tensquare.search.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
/**
* 文章service
*/
@Service
public class ArticleService {
@Autowired
private ArticleDao articleDao;
/**
* 文章搜素
*/
public Page search(String keyword, int page, int size){
return articleDao.findByTitleOrContentLike(keyword,keyword, PageRequest.of(page-1,size));
}
}
继承ElasticsearchRepository接口
package com.tensquare.search.dao;
import com.tensquare.search.pojo.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
/**
* 文章dao
*/
public interface ArticleDao extends ElasticsearchRepository {
/**
* 文章查询
*/
public Page findByTitleOrContentLike(String title, String content, Pageable pageable);
}