elasticsearch搜索引擎笔记一
- spring官网地址
- Elastics earch 的官方地址: https://www.elastic.co/cn/
- 数据类型种类
结构化数据:数据库mysql
非结构化数据:比如音频,视频等(nosql,redis,mangdb)
半结构化数据:xml,html等(redis,mangdb)
- 数据格式
Elasticsearcs是面向文档型数据库,一条数据在这里就是一个文档。
ES里的 Index 可以看做一个库,而 Types 相当于表, Documents 则相当于表的行。
这里Types 的概念已经被逐渐弱化, Elasticsearch 6.X 中,一个 index 下已经只能包含一个
type Elasticsearch 7.X 中 , Type 的概念已经被删除了。
es基础操作各种请求发送url以及格式(postman)------------------------------------------------------------------------------------------------------------------------------------------
- 创建索引
在Postman 中,向 ES 服务器发 PUT 请求 http://127.0.0.1:9200/shopping


-
查看单个索引


-
查看所有索引
http://127.0.0.1:9200/_cat/indices?v
这里请求路径中的_cat 表示查看的意思, indices 表示索引,所以整体含义就是查看当前 ES
服务器中的所有索引,就好像 MySQL 中的 show tables 的感觉,服务器响应结果如下


- 删除索引

重新访问索引时,服务器返回响应:索引不存在

- 创建文档
向 ES 服务器发 POST 请求 http://127.0.0.1:9200/sh opping /_doc


此处发送请求的方式必须为POST ,不能是 PUT ,否则会发生错误
如果想要自定义唯一性标识,需要在创建时指定http://127.0.0.1:9200/shopping/_doc/ 1001

此处需要注意:如果增加数据时明确数据主键,那么请求方式也可以为PUT

- 查看文档
向 ES 服务器发 GET 请求 http://127.0.0.1:9200/sh opping /_doc/1


- 查询全部文档
http://127.0.0.1:9200/shopping/_search

- 全量修改文档
向 ES 服 务器发 POST 请求 http://127.0.0.1:9200/shopping /_doc/1


- 局部修改字段
向 ES 服务器发 POST 请求 http://127.0.0.1:9200/sh opping/opping/_update/1
{"doc":{"address":"北京"}
}

- 删除文档
向 ES 服务器发 DELETE 请求 http://127.0.0.1:9200/sh opping /_doc/1


如果删除一个并不存在的文档


- 条件查询文档
http://127.0.0.1:9200/shopping/_search
{"query":{"match":{"address":"重庆"}
}
}

- 条件全量查询
http://127.0.0.1:9200/shopping/_search
{"query":{"match_all":{}
}
}

- 条件分页查询加排序
http://127.0.0.1:9200/shopping/_search
{"query":{"match_all":{}
},"from":5,"size":5,"_source":["address","age"],"sort":{"age":{"order":"asc"}}
}

- 多个条件组合查询(and)
http://127.0.0.1:9200/shopping/_search
{"query":{"bool":{"must":[{"match":{"address":"北京"}},{"match":{"name":"冉述保"}}]}}
}

- 多条件组合查询(or)
http://127.0.0.1:9200/shopping/_search
{"query":{"bool":{"should":[{"match":{"address":"北京"}},{"match":{"address":"重庆"}}]}}
}

- 多条件范围查询
http://127.0.0.1:9200/shopping/_search
{"query":{"bool":{"should":[{"match":{"address":"北京"}},{"match":{"address":"重庆"}}],"filter":{"range":{"age":{"gt":26}}}}}
}


- 不分词,完全匹配查询且高亮显示
http://127.0.0.1:9200/shopping/_search
{"query":{"match_phrase":{"address":"北京"}},"highlight":{"fields":{"address":{}}}
}

- 分组查询
http://127.0.0.1:9200/shopping/_search
{"aggs":{"age_group":{"terms":{"field":"age"}}},"size":0
}

- 求平均值
http://127.0.0.1:9200/shopping/_search
{"aggs":{"age_avg":{"avg":{"field":"age"}}},"size":0
}

- 新增映射关系
http://127.0.0.1:9200/user/_mapping
{"properties":{"name":{"type":"text","index":true},"sex":{"type":"keyword","index":true},"phone":{"type":"keyword","index":false}}
}

- 查询映射关系
http://127.0.0.1:9200/user/_mapping

- 查询text(分词)-keyword(不分词,必须精准匹配)
http://127.0.0.1:9200/user/_search
{"query":{"match":{"name":"张"}
}
}
有结果
{"query":{"match":{"sex":"男"}
}
}
无结果

JavaAPI操作es(基于代码)---------------------------------------------------------------------------------------------------------------------
- 准备工作- maven
<?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"><parent><artifactId>web_demo</artifactId><groupId>com.zgs.test</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>es-test</artifactId><dependencies><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>7.8.0</version></dependency><!--elasticsearch的客户端--><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.8.0</version></dependency><!--elasticsearch 依赖 2.x 的 log4j--><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>2.8.2</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.9</version></dependency><!--单元测试--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies></project>
- 新建索引
package com.zgs.es.test;import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;import java.io.IOException;/*** @author zgs* @date 2021年12月01日 18:01:00*/
@Slf4j
public class EsTest_Index_Create {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"http")));//创建索引CreateIndexRequest request = new CreateIndexRequest("zgs1");CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);log.info("-----------"+response.toString());boolean acknowledged = response.isAcknowledged();System.out.println("acknowledged-------"+acknowledged);client.close();}
}
- 查询索引
package com.zgs.es.test;import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;import java.io.IOException;/*** @author zgs* @date 2021年12月02日 15:32:00*/
@Slf4j
public class EsTest_Index_Search {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"http")));//查询索引GetIndexRequest request = new GetIndexRequest("zgs");GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);log.info("#########################"+response.getAliases());log.info("#########################"+response.getDataStreams());log.info("#########################"+response.getMappings());log.info("#########################"+response.getSettings());client.close();}
}
- 删除索引
package com.zgs.es.test;import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;import java.io.IOException;/*** @author zgs* @date 2021年12月02日 15:41:00*/
@Slf4j
public class EsTest_Index_Delete {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"http")));//删除索引DeleteIndexRequest request = new DeleteIndexRequest("zgs");AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);log.info("#########################"+response.isAcknowledged());client.close();}
}
- 新增文本信息
package com.zgs.es.test;import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;import java.io.IOException;/*** @author zgs* @date 2021年12月02日 16:24:00*/
@Slf4j
public class EsTest_Doc_Insert {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"http")));IndexRequest request = new IndexRequest();request.index("zgs").id("1001");ZgsVo zgsVo = new ZgsVo();zgsVo.setName("张贵松");zgsVo.setAge(24);zgsVo.setSex("男");//转换为jsonObjectMapper mappers = new ObjectMapper();String zgsJson = mappers.writeValueAsString(zgsVo);request.source(zgsJson, XContentType.JSON);IndexResponse response = client.index(request, RequestOptions.DEFAULT);log.info("########################"+response.getResult());client.close();}
}
- 修改文本信息
package com.zgs.es.test;import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;import java.io.IOException;/*** @author zgs* @date 2021年12月02日 17:28:00*/
@Slf4j
public class EsTest_Doc_Update {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"http")));UpdateRequest request = new UpdateRequest();request.index("zgs").id("1001");request.doc(XContentType.JSON,"name","张帅");UpdateResponse response = client.update(request, RequestOptions.DEFAULT);log.info("###########################"+response.getGetResult());client.close();}
}
- 查询文本信息
package com.zgs.es.test;import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;import java.io.IOException;/*** @author zgs* @date 2021年12月02日 17:38:00*/
@Slf4j
public class EsTest_Doc_Get {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"http")));GetRequest request = new GetRequest();request.index("zgs").id("1001");GetResponse response = client.get(request, RequestOptions.DEFAULT);log.info("###########################"+response.getSourceAsString());client.close();}
}
- 删除文本信息
package com.zgs.es.test;import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;import java.io.IOException;/*** @author zgs* @date 2021年12月02日 17:40:00*/
@Slf4j
public class EsTest_Doc_Delete {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"http")));DeleteRequest request = new DeleteRequest();request.index("zgs").id("1001");DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);log.info("###########################"+response.toString());client.close();}
}
- 批量新增数据
package com.zgs.es.test;import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;import java.io.IOException;/*** @author zgs* @date 2021年12月02日 17:55:00*/
@Slf4j
public class EsTest_Doc_Insert_Batch {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"http")));BulkRequest bulkRequest = new BulkRequest();bulkRequest.add(new IndexRequest().index("zgs").id("1001").source(XContentType.JSON,"name","张帅1"));bulkRequest.add(new IndexRequest().index("zgs").id("1002").source(XContentType.JSON,"name","张帅2"));bulkRequest.add(new IndexRequest().index("zgs").id("1003").source(XContentType.JSON,"name","张帅3"));BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);log.info("#########################"+response.getIngestTook());log.info("#########################"+response.getItems());client.close();}
}
- 批量删除数据
package com.zgs.es.test;import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;import java.io.IOException;/*** @author zgs* @date 2021年12月02日 18:08:00*/
@Slf4j
public class EsTest_Doc_Delete_Batch {public static void main(String[] args) throws IOException {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost",9200,"http")));BulkRequest bulkRequest = new BulkRequest();bulkRequest.add(new DeleteRequest().index("zgs").id("1002"));bulkRequest.add(new DeleteRequest().index("zgs").id("1003"));BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);log.info("#########################"+response.getIngestTook());log.info("#########################"+response.getItems());client.close();}
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!



