ElasticSearch 7.8.0
安装包
https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-8-0
索引创建
索引相当于创建数据库
发送put请求给http://127.0.0.1:9200/+名称
查询索引
查询指定索引
发送get请求给http://127.0.0.1:9200/+名称
查询全部索引
发送get请求给http://127.0.0.1:9200/_cat/indices?v
删除索引
发送delete请求给http://127.0.0.1:9200/+名称
文档操作
创建文档
发送post请求给http://127.0.0.1:9200/+索引名称+/_doc、并且在body中写入数据
{
"title":"小米手机",
"catrgory":"小米",
"image":"http://www.gulixueyuan.com/xm.jpg",
"price":3999.00
}
创建文档并自定义id
发送post请求给http://127.0.0.1:9200/+索引名称+/_doc+索引、并且在body中写入数据
查询文档数据
查询指定索引指定文档指定数据
发送get请求给http://127.0.0.1:9200/+索引名称+_doc+索引
查询全部数据
发送get请求给http://127.0.0.1:9200/+索引+/_search
修改文档
全量数据的更新
发送put请求给http://127.0.0.1:9200/+索引+/_doc+索引并重新发送body数据
局部数据的更新
发送post请求给http://127.0.0.1:9200/+索引+/_update/+索引
{
"doc":{
"字段名":数据
}
}
删除数据
发送delete请求给http://127.0.0.1:9200/+索引+/_doc/索引
复杂查询
条件查询
通过请求体传递参数
发送git请求给http://127.0.0.1:9200/+索引+/_search?q=category:参数
通过json传递参数
发送git请求http://127.0.0.1:9200/+索引+/_search
{
"query":{
"match":{
"category":参数
}
}
}
分页查询
全部查找
发送git请求http://127.0.0.1:9200/+索引
body里添加如下数据
{
"query":{
"match_all":{
}
}
}
分页查询
请求体这么写from,size
{
"query": {
"match_all": {}
},
"from": 0,
"size": 2
}
查询内容筛选(指定字段查询)
body加上_source
{
"query": {
"match_all": {}
},
"from": 0,
"size": 2,
"_source" : ["字段1"]
}
按指定的规则排序
{
"query":{
"match_all":{
}
},
"from":2,
"size":2,
"_source":["title"],
"sort":{
"price":{
"order":"desc"
}
}
}
多条件查询 and
{
"query":{
"bool":{
"must":[
{
"match":{
"catrgory": "小米"
}
},{
"match":{
"price":3999.00
}
}
]
}
}
}
多条件查询 or
{
"query":{
"bool":{
"should":[
{
"match":{
"catrgory": "小米"
}
},{
"match":{
"price":3999.00
}
}
]
}
}
}
范围查询
{
"query":{
"bool":{
"must":[
{
"match":{
"catrgory": "小米"
}
},{
"match":{
"price":3999.00
}
}
],
"filter":{
"range":{
"price":{
"gt":2000
}
}
}
}
}
}
高亮显示
{
"query":{
"match":{
"catrgory":"大米"
}
},
"highlight":{
"fields":{
"catrgory":{}
}
}
}
分组查询
{
"aggs":{ //聚合操作
"price_group":{ //名称随便起名
"terms":{ //分组
"field":"price"
}
}
},
"size":0
}
求平均值
{
"aggs":{ //聚合操作
"price_avg":{ //名称随便起名
"avg":{ //分组
"field":"price"
}
}
},
"size":0
}
添加映射关系
http://127.0.0.1:9200/user/_mapping
{
"properties":{
"name":{
"type":"text",
"index":true
},
"sex":{
"type":"keyword",
"index":true
},
"tel":{
"type":"keyword",
"index":false
}
}
}
<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-api</artifactId>
<version>2.8.2</version>
</dependency>
<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>
<!
--
j
unit
单元测试
-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
package com.example.demo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
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.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
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.client.eql.EqlSearchResponse;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.naming.directory.SearchResult;
import java.io.IOException;
@SpringBootTest
class DemoApplicationTests {
@Test
void contextLoads() throws IOException {
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
//关闭流
esClient.close();
}
@Test
void test1() throws IOException {
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
CreateIndexRequest createIndexRequest = new CreateIndexRequest("user");
CreateIndexResponse createIndexResponse = esClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
System.out.println(createIndexResponse.isAcknowledged());
//关闭流
esClient.close();
}
@Test
void test2() throws IOException {
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
GetIndexRequest getIndexRequest = new GetIndexRequest("user");
GetIndexResponse getIndexResponse = esClient.indices().get(getIndexRequest, RequestOptions.DEFAULT);
System.out.println(getIndexResponse.getAliases());
System.out.println(getIndexResponse.getMappings());
System.out.println(getIndexResponse.getSettings());
//关闭流
esClient.close();
}
@Test
void test3() throws IOException {
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("user");
AcknowledgedResponse delete = esClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
//关闭流
esClient.close();
}
@Test
void test4() throws IOException {
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
IndexRequest indexRequest = new IndexRequest();
indexRequest.index("user").id("1001");
User user = new User("张三","男",30);
ObjectMapper mapper = new ObjectMapper();
String s = mapper.writeValueAsString(user);
indexRequest.source(s, XContentType.JSON);
esClient.index(indexRequest,RequestOptions.DEFAULT);
IndexResponse index = esClient.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(index.getResult());
//关闭流
esClient.close();
}
@Test
void test5() throws IOException {
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("user").id("1001");
updateRequest.doc(XContentType.JSON,"sex","女");
UpdateResponse update = esClient.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(update.getResult());
//关闭流
esClient.close();
}
@Test
void test6() throws IOException {
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
GetRequest getRequest = new GetRequest();
getRequest.index("user").id("1001");
GetResponse documentFields = esClient.get(getRequest, RequestOptions.DEFAULT);
System.out.println(documentFields.getSourceAsString());
//关闭流
esClient.close();
}
@Test
void test7() throws IOException {
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
DeleteRequest deleteRequest = new DeleteRequest();
deleteRequest.index("user").id("1001");
DeleteResponse delete = esClient.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(delete.getResult());
//关闭流
esClient.close();
}
@Test
void test8() throws IOException {
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(new DeleteRequest().index("user").id("1001"));
bulkRequest.add(new DeleteRequest().index("user").id("1002"));
bulkRequest.add(new DeleteRequest().index("user").id("1003"));
bulkRequest.add(new DeleteRequest().index("user").id("1004"));
BulkResponse bulk = esClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulk.getTook());
//关闭流
esClient.close();
}
@Test
void test9() throws IOException {
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON,"name","zhangsan","age",30,"sex","男"));
bulkRequest.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON,"name","lisi","age",40,"sex","女"));
bulkRequest.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON,"name","wangwu","age",50,"sex","男"));
bulkRequest.add(new IndexRequest().index("user").id("1004").source(XContentType.JSON,"name","zhaoliu","age",60,"sex","女"));
bulkRequest.add(new IndexRequest().index("user").id("1005").source(XContentType.JSON,"name","qianqi","age",70,"sex","男"));
bulkRequest.add(new IndexRequest().index("user").id("1006").source(XContentType.JSON,"name","aobama","age",80,"sex","女"));
BulkResponse bulk = esClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulk.getTook());
//关闭流
esClient.close();
}
@Test
void test10() throws IOException {
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
searchRequest.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(search.getHits());
System.out.println(search.getTook());
for (SearchHit hit:search.getHits() ){
System.out.println(hit.getSourceAsString());
}
//关闭流
esClient.close();
}
@Test
void test11() throws IOException {
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
searchRequest.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age",30)));
SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(search.getHits());
System.out.println(search.getTook());
for (SearchHit hit:search.getHits() ){
System.out.println(hit.getSourceAsString());
}
//关闭流
esClient.close();
}
@Test
void test12() throws IOException {
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
query.from(0);
query.size(2);
searchRequest.source(query);
SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(search.getHits());
System.out.println(search.getTook());
for (SearchHit hit:search.getHits() ){
System.out.println(hit.getSourceAsString());
}
//关闭流
esClient.close();
}
@Test
void test13() throws IOException {
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
query.sort("age", SortOrder.ASC);
searchRequest.source(query);
SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(search.getHits());
System.out.println(search.getTook());
for (SearchHit hit:search.getHits() ){
System.out.println(hit.getSourceAsString());
}
//关闭流
esClient.close();
}
@Test
void test14() throws IOException {
//创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
String[] excludes = {"age"};
String[] includes = {};
query.fetchSource(includes,excludes);
query.sort("age", SortOrder.ASC);
searchRequest.source(query);
SearchResponse search = esClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(search.getHits());
System.out.println(search.getTook());
for (SearchHit hit:search.getHits() ){
System.out.println(hit.getSourceAsString());
}
//关闭流
esClient.close();
}
}