ElasticSearch
我们要了解哪些内容?
- 安装
声明:JDK1.8,最低要求!ElasticSearch客户端,界面工具!
Java开发,ElasticSearch的版本和我们之后对应的java的核心jar包!
下载
官网:https://www.elastic.co/cn/elasticsearch/
浏览器下载时巨慢的!所以建议到迅雷上面下载安装!
ElasticSearch下载:https://www.elastic.co/cn/downloads/elasticsearch
kibana下载:https://www.elastic.co/cn/downloads/kibana
elasticsearch-head下载:https://github.com/mobz/elasticsearch-head
简单的CRUD
创建一个索引
put /test2 //test2就是一个索引
{
"mappings": {
"properties": { //所有的属性字段
"name": {
"type": "text"
},
"age": {
"type": "long"
},
"birthday": {
"type": "date"
}
}
}
}
删除
DELETE test1
复杂查询
GET jamison/user/_search
{
"query": { //查询的条件
"match": { // match必须
"name": "jamison"
}
},
"_source": ["name", "age", "desc"],//自定义所要显示的字段
"sort": {// 排序
"age": { // 按照age进行排序
"order": "desc"// 排序规则:降序排序
}
},
"from": 0,//分页查询,从第0个索引开始查询
"size": 1 // 所要插叙的结果数
}
多条件查询
GET jamison/user/_search
{
"query": {
"bool": {
"should": [// must:必须所有条件都要满足 should:满足其中一个即可 must_no:不满足条件的都给查询出来
{
"match": {
"name": "jamison"
}
},
{
"match": {
"age": "21"
}
}
]
}
}
}
过滤器
GET jamison/user/_search
{
"query": {
"bool": {
"filter": [
{
"range": { // 范围
"age": { // 按照age进行筛选
"gt": 10 //gt:大于 gte:大于等于 lt:小于 lte:小于等于
}
}
}
]
}
}
}
分词器
按照keywords分词:不会帮你做分解
GET _analyze
{
"analyzer": "keyword",//分词的方式
"text": "狂神说java name"
}
结果:
{
"tokens" : [
{
"token" : "狂神说java name",//只会将text当做为一整个词,不会进行分解,没有被分析
"start_offset" : 0,
"end_offset" : 12,
"type" : "word",
"position" : 0
}
]
}
##按照standart分词:会帮你进行分解
GET _analyze
{
"analyzer": "standard",//分词的方式,standart方式会被分析
"text": "狂神说java name"
}
搜索高亮
GET testdb/_search
{
"query": {
"match": {
"name": "jamison"
}
},
"highlight": {//设置高亮
"fields": {
"name": {}//设置高亮的字段-->>"<em>jamison</em>"
}
}
}
自定义高亮样式
GET testdb/_search
{
"query": {
"match": {
"name": "jamison"// 结果-->>"<p style='color:red;'>jamison</p>"
}
},
"highlight": {
"pre_tags": "<p style='color:red;'>",//前缀
"post_tags": "</p>", //后缀
"fields": {
"name": {}
}
}
}
倒排索引
集成SpringBoot
1、找到原生的依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.9.2</version>
</dependency>
2、找对象
3、分析这个类的方法
配置基本的项目
pom文件
所以我们需要引入自定义的ElasticSearch的版本
自定义高级客户端
测试
测试创建索引
//测试创建索引
@Test
void createIndex() throws IOException {
//创建索引请求
CreateIndexRequest indexRequest = new CreateIndexRequest("jamison_index");
//客户端执行请求,indicesClient,请求后获得响应
CreateIndexResponse response = restHighLevelClient
.indices().create(indexRequest, RequestOptions.DEFAULT);
System.out.println(response);
}
获取索引,判断其是否存在
//测试获取索引,判断其是否存在
@Test
void getIndex() throws IOException {
GetIndexRequest index = new GetIndexRequest("jamison_index");
boolean exists;
exists = restHighLevelClient.indices().exists(index, RequestOptions.DEFAULT);
System.out.println(exists);
}
//测试删除索引
@Test
void deleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("jamison_index");
AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(delete);
}
添加文档
//测试添加文档
@Test
void addDocument() throws IOException {
//创建对象
User user = new User("jamison", 19);
//创建请求
IndexRequest request = new IndexRequest("jamison_index", "user", "1");
//设置规则
request.timeout(TimeValue.timeValueSeconds(1));
//将我们的数据放入请求当中
request.source(JSON.toJSONString(user), XContentType.JSON);
IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
System.out.println(response.status());
}
删除文档
//删除文档
@Test
void deleteDocument() throws IOException {
DeleteRequest deleteRequest = new DeleteRequest("jamison_index");
deleteRequest.type("user");
deleteRequest.id("1");
DeleteResponse delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(delete.toString());
System.out.println(delete.status());
}
判断文档是否存在
@Test
void getExits() throws IOException {
GetRequest request = new GetRequest("jamison_index", "1");
request.fetchSourceContext(new FetchSourceContext(false));
request.storedFields("_none_");
boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
获取文档
@Test
void getDocument() throws IOException {
GetRequest request = new GetRequest("jamison_index", "1");
GetResponse documentFields = restHighLevelClient.get(request, RequestOptions.DEFAULT);
System.out.println(documentFields.getSourceAsString());
System.out.println(documentFields);
}
批量添加数据
//批量添加
@Test
void testBulkDocument() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("10s");
ArrayList<User> users = new ArrayList<>();
for (int i = 0; i < 100; i++){
users.add(new User("jamison" + (i+1), i+1));
}
for (int i = 0; i < users.size(); i++) {
bulkRequest.add(new IndexRequest("jamison_index")
.id(""+i+1)
.source(JSON.toJSONString(users.get(i)), XContentType.JSON));
}
BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulk.status());
}
Lucene是什么
Lucene 是一套信息检索工具包!jar包!不包含搜索引擎系统!
包含的:索引结构!读写索引的工具!排序,搜索规则…工具类
Lucene和ElasticSearch关系:
ElasticSearch是基于Lucene做了一些封装和增强
以后你只要需要用到搜索,就可以用ElasticSearch