elaticsearch7.x,8.x入门教程(一)

25 篇文章 0 订阅

我们先来学习7.x

一定要关闭防火墙

安装elaticsearch7.11.1

docker run --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -d elasticsearch:7.11.1

浏览器访问

http://192.168.23.132:9200/

进入容器

docker exec -it elasticsearch /bin/bash
cd /usr/share/elasticsearch/plugins/

安装elasticsearch-analysis-ik(ik分词器)版本与elasticsearch保持一致

elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.11.1/elasticsearch-analysis-ik-7.11.1.zip 

退出容器

exit

重启elaticsearch

docker restart elasticsearch

验证分词器

ik_smart:智能分词,查的数据比较少

ik_max_word:最大化分词法,查询出来的数据比较多

192.168.23.132:9200/_analyze
{
    "tokenizer": "ik_smart",
    "text": "上海东方明珠"
}

{
    "tokenizer": "ik_max_word",
    "text": "上海东方明珠"
}

安装kibana 要和elaticsearch的版本一致,使用–link连接到elasticsearch容器

docker run --name kibana --link=elasticsearch:elasticsearch -p 5601:5601 -d kibana:7.11.1

浏览器访问

http://192.168.23.132:5601/

elaticsearch是面向文档型数据库,一条数据就是一个文档

index(索引)=数据库;

type(类型)=表

documents(文档)=行;

fields(字段)=列;

在elaticsearch7.x中,type的概念已经被删除了;

正向索引:根据id快速查询到内容;

倒排索引: 根据keyword(关键字) 和id关联,这样就可以查询到数据了;

接下来我们来看下索引的创建

创建索引就相当于,创建了数据库,使用put,不能使用其他类型

索引是具有幂等性的,只能创建一个

创建一个order索引

http://192.168.23.132:9200/order

查询一个索引为order,使用get

http://192.168.23.132:9200/order

查询所有的索引,使用get

http://192.168.23.132:9200/_cat/indices?v

删除索引order,使用delete

http://192.168.23.132:9200/order

接下来我们创建文档,并添加数据

必须是在某一个索引下,才能创建文档,相当于你有了数据库,才能在表里面添加数据

注意:我们创建文档的时候,必须是POST类型,和创建索引是不一样的类型

内容为json格式,请求数据,必须要有,不能为空;

在order索引下创建文档_doc

http://192.168.23.132:9200/order/_doc

{
    "ordername":"订单名称",
    "price": 200
}

为啥不能用put创建文档呢?

因为返回内容_id是系统给我们的,我们要想使用put,使用自定义的id

在文档后面加入自定义的主键

http://192.168.23.132:9200/order/_doc/123

put 也可以全量修改,上面返回的result:"updated"就是表示已经把数据修改了

"result": "created",表示创建一个新的数据

查询文档使用get

索引/文档/主键

http://192.168.23.132:9200/order/_doc/123

查询索引下所有的数据 get类型

索引/_search

http://192.168.23.132:9200/order/_search

如果我们想要局部更新可以使用 post类型

索引/_update/主键

http://192.168.23.132:9200/order/_update/456

{
    "doc":{
        "ordername":"aaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    }
}

我们看下数据 已经发生了变化

删除文档 delete类型

索引/文档/主键

http://192.168.23.132:9200/order/_doc/456

我们在看下条件查询,查询索引下的文档 条件查询

http://192.168.23.132:9200/order/_search

{
 "query":{
     "match":{
         "price":200
     }
 }
}

query和match都是固定字段,在下一层的price改成你的文档中的字段

match_all:是匹配所有

from:是当前页起始位置,第一条数据为0,如果要看某一个页的数据,(页码-1)*每页条数

size: 每页条数

我们来看下第0页的5条数据

http://192.168.23.132:9200/order/_search

{
 "query":{
     "match_all":{
        
     }
    },
     "from": 0,
     "size": 5
}

我们要想看下第5页的数据

(页码-1)*每页条数

from=5-1=4*5=20

如果想查询的文档数据 只显示某些字段可以使用

"_source": ["字段名称"]

文档排序

sort 就是排序的意思,下一层就是文档的字段,在一下层order关键字相当于 数据库的order by desc,

order by asc

{
 "query":{
     "match_all":{
        
     }
    },
     "sort":{
         "price":{
             "order" : "desc"   
         }
     }
     
}

接下来我们来看下多条件查询,根据订单名称和价格查询

must相当于sql中的and条件

query 就是查询的意思

在每一个match下面写上你文档中的字段

一个match对应一个字段

bool就是使用多条件查询

{
 "query":{
     "bool":{
        "must":[
            {
                "match":{
                    "ordername":"订单名称"
                }    
            },
            {
                 "match":{
                     "price":200
                 }   
            }
        ]    
     }
    }
}

我们来看下根据订单名称或者价格有一个符合条件的查询

should相当于sql中的or

{
 "query":{
     "bool":{
        "should":[
            {
                "match":{
                    "ordername":"订单名称"
                }    
            },
            {
                 "match":{
                     "price":30
                 }   
            }
        ]    
     }
    }
}

我们在看下范围查询

查询价格大于30的数据

filter 过滤

range 范围

gt 大于

lt 小于

{
 "query":{
     "bool":{
        "should":[
            {
                "match":{
                    "ordername":"订单名称"
                }    
            },
            {
                 "match":{
                     "price":30
                 }   
            }
        ],
        "filter":{
            "range":{
                "price":{
                    "gt": 30
                }
            }
        }    
     }
    }
}

正常情况下match是全文检索 模糊查询

我们要想完全匹配需要改为match_phrase

但是由于我们安装了ik分词器,所以这个match_phrase失效了

我们对字段进行高亮显示

highlight 高亮

fields 对那些字段进行设置

{
 "query":{
     "match_phrase":{
        "ordername":"订单"
     }
    },
    "highlight":{
        "fields":{
            "ordername":{}
        }
    }
     
}

接下来对价格进行分组查询

aggs:聚合操作

price_group:分组名称随便起的名字

terms:分组

field 分组字段

{
    "aggs":{
        "price_group":{
            "terms":{
                "field": "price"
            }
        }
    }
}

key:就是那个字段的值

doc_count:统计数量

上面查询出来的数据,还显示内容,我们可以去掉内容,只显示分组信息

加入size

{
    "aggs":{
        "price_group":{
            "terms":{
                "field": "price"
            }
        }
    },
    "size":0
}

我们还可以求平均值

avg 平均值

{
    "aggs":{
        "price_avg":{
            "avg":{
                "field": "price"
            }
        }
    },
    "size":0
}

接下来我们创建一个新的索引user, put类型

http://192.168.23.132:9200/user

接下来创建一个映射

properties:属性

name,sex,tel 文档的字段

type:text 支持分词查询

type:keyword 只能精确查询,不支持分词

index: true 可以索引查询

index: false 不可以索引查询

http://192.168.23.132:9200/user/_mapping
{
    "properties":{
        "name":{
            "type":"text",
            "index": true
        },
        "sex":{
            "type": "keyword",
            "index": true
        },
        "tel":{
            "type": "keyword",
            "index": false
        }
    }
}

接下来我们创建一些文档的数据

http://192.168.23.132:9200/user/_create/222
{
    "name":"张三1111",
    "sex":"男的",
    "tel": "56456464"
}

我们可以看到name支持分词查询

sex不支持分词查询,必须输入全部才能查询到

可以看到tel字段

无法创建查询:无法搜索字段[tel],因为它未被索引

因为index设置了false

接下来我们在Java代码中使用下


            <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
                <version>7.11.1</version>
            </dependency>
            <!-- elasticsearch 的客户端 -->
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-high-level-client</artifactId>
                <version>7.11.1</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>
            <!-- junit 单元测试 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>

创建索引

package com.example.demo.entity;

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 org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;

import java.io.IOException;

public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );
        //创建索引 索引名为kucun
        CreateIndexRequest request=new CreateIndexRequest("kucun");
        CreateIndexResponse createIndexResponse = client
                .indices().create(request, RequestOptions.DEFAULT);
        //响应状态
        boolean acknowledged = createIndexResponse.isAcknowledged();
        System.out.println("响应状态:"+acknowledged);
        //关闭客户端
        client.close();
    }
}

查询索引

package com.example.demo.entity;

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 org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;

import java.io.IOException;

public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );
        //查询索引 kucun
        GetIndexRequest request=new GetIndexRequest("kucun");
        GetIndexResponse getIndexResponse = client
                .indices().get(request, RequestOptions.DEFAULT);
        //返回内容
        System.out.println(getIndexResponse.getAliases());
        System.out.println(getIndexResponse.getMappings());
        System.out.println(getIndexResponse.getSettings());
        //关闭客户端
        client.close();
    }
}

删除索引

package com.example.demo.entity;

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.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;

import java.io.IOException;

public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );
        //删除索引 kucun
        DeleteIndexRequest request=new DeleteIndexRequest("kucun");
        AcknowledgedResponse delete = client
                .indices().delete(request, RequestOptions.DEFAULT);
        //返回内容
        System.out.println("响应状态:"+delete.isAcknowledged());
        //关闭客户端
        client.close();
    }
}

接下来创建实体类

@Data
public class SysUser {

    private String name;

    private String sex;

    private Integer age;


}

在文档插入数据

package com.example.demo.entity;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
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.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 java.io.IOException;
import java.util.UUID;

public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );
        //为库存索引下的文档插入数据
        IndexRequest request=new IndexRequest();
        request.index("kucun").id("1001");


        SysUser sysUser=new SysUser();
        sysUser.setAge(18);
        sysUser.setName("张三");
        sysUser.setSex("男");
        //向es插入数据必须是json格式
        ObjectMapper objectMapper=new ObjectMapper();
        String s = objectMapper.writeValueAsString(sysUser);
        request.source(s, XContentType.JSON);

        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        //返回结果
        System.out.println(response.getResult());
        //关闭客户端
        client.close();
    }
}

修改文档的数据

package com.example.demo.entity;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
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.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 java.io.IOException;
import java.util.UUID;

public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );
        //为库存索引下的文档修改数据
        UpdateRequest request=new UpdateRequest();
        request.index("kucun").id("1001");
        //修改某个字段的数据 doc就是文档 也是行数据
        request.doc("age",2000);
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
        //返回结果
        System.out.println(response.getResult());
        //关闭客户端
        client.close();
    }
}

查询文档的数据

package com.example.demo.entity;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
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.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.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 java.io.IOException;
import java.util.UUID;

public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );
        //为库存索引下的文档查询数据
        GetRequest request=new GetRequest();
        request.index("kucun").id("1001");
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        //返回结果
        System.out.println(response.getSourceAsString());
        //关闭客户端
        client.close();
    }
}

删除文档的数据

package com.example.demo.entity;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
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.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.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 java.io.IOException;
import java.util.UUID;

public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );
        //为库存索引下的文档删除数据
        DeleteRequest request=new DeleteRequest();
        request.index("kucun").id("1001");
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
        //返回结果
        System.out.println(response.getResult());
        //关闭客户端
        client.close();
    }
}

接下来看下文档的批量操作

批量新增

package com.example.demo.entity;

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.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.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 java.io.IOException;
import java.util.UUID;

public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );
        //批量新增文档数据
        BulkRequest bulkRequest=new BulkRequest();

        IndexRequest source1 = new IndexRequest().index("kucun").id("1001")
                .source(XContentType.JSON, "name", "李四", "age", "18");

        IndexRequest source2 = new IndexRequest().index("kucun").id("1002")
                .source(XContentType.JSON, "name", "王五", "age", "20");

        IndexRequest source3 = new IndexRequest().index("kucun").id("1003")
                .source(XContentType.JSON, "name", "赵六", "age", "30");

        bulkRequest.add(source1);
        bulkRequest.add(source2);
        bulkRequest.add(source3);

        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println("响应时间:"+bulk.getTook());
        //关闭客户端
        client.close();
    }
}

批量删除文档数据

package com.example.demo.entity;

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.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.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 java.io.IOException;
import java.util.UUID;

public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );
        //批量删除文档数据
        BulkRequest bulkRequest=new BulkRequest();

        DeleteRequest source1 = new DeleteRequest().index("kucun").id("1001");

        DeleteRequest source2 = new DeleteRequest().index("kucun").id("1002");

        DeleteRequest source3 = new DeleteRequest().index("kucun").id("1003");

        bulkRequest.add(source1);
        bulkRequest.add(source2);
        bulkRequest.add(source3);

        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println("响应时间:"+bulk.getTook());
        //关闭客户端
        client.close();
    }
}

查询索引下的所有数据

package com.example.demo.entity;

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.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.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.util.UUID;

public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );
        //查询索引下的所有数据
        SearchRequest searchRequest=new SearchRequest();
        searchRequest.indices("kucun");

        //匹配所有
        searchRequest.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
        SearchResponse response=client.search(searchRequest,RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        System.out.println("条数:"+hits.getTotalHits());
        System.out.println("耗时:"+response.getTook());
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }



        //关闭客户端
        client.close();
    }
}

查询索引下某个字段的数据

package com.example.demo.entity;

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.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.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.util.UUID;

public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );
        //查询索引下的所有数据
        SearchRequest searchRequest=new SearchRequest();
        searchRequest.indices("kucun");

        //查询某个字段
        searchRequest.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age","30")));
        SearchResponse response=client.search(searchRequest,RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        System.out.println("条数:"+hits.getTotalHits());
        System.out.println("耗时:"+response.getTook());
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }



        //关闭客户端
        client.close();
    }
}

分页查询和排序

package com.example.demo.entity;

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.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.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;
import java.util.UUID;

public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );
        //查询索引下的所有数据
        SearchRequest searchRequest=new SearchRequest();
        searchRequest.indices("kucun");

        //分页查询
        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        //当前位置 (页码-1)*每页条数
        //例如我们想看第5页的数据,(5-1)*2=4*2=8
        //页数访问的位置
        int from=(5-1)*2;
        query.from(from);
        //每页条数
        query.size(2);
        //排序 正向ASC  倒叙DESC
        query.sort("age", SortOrder.ASC);

        searchRequest.source(query);
        SearchResponse response=client.search(searchRequest,RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        System.out.println("条数:"+hits.getTotalHits());
        System.out.println("耗时:"+response.getTook());
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }



        //关闭客户端
        client.close();
    }
}

查询的时候不显示name字段

package com.example.demo.entity;

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.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.Nullable;
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.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;
import java.util.UUID;

public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );
        //查询索引下的所有数据
        SearchRequest searchRequest=new SearchRequest();
        searchRequest.indices("kucun");

        //分页查询
        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        //只显示某些字段
        String[] includes={};
        //排除一些字段 不显示
        String[] excludes={"name"};
        query.fetchSource(includes,excludes);

        searchRequest.source(query);
        SearchResponse response=client.search(searchRequest,RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        System.out.println("条数:"+hits.getTotalHits());
        System.out.println("耗时:"+response.getTook());
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }



        //关闭客户端
        client.close();
    }
}

查询的时候只显示name字段

package com.example.demo.entity;

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.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.Nullable;
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.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;
import java.util.UUID;

public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );
        //查询索引下的所有数据
        SearchRequest searchRequest=new SearchRequest();
        searchRequest.indices("kucun");

        //分页查询
        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        //只显示某些字段
        String[] includes={"name"};
        //排除一些字段 不显示
        String[] excludes={};
        query.fetchSource(includes,excludes);

        searchRequest.source(query);
        SearchResponse response=client.search(searchRequest,RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        System.out.println("条数:"+hits.getTotalHits());
        System.out.println("耗时:"+response.getTook());
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }



        //关闭客户端
        client.close();
    }
}

and条件查询

package com.example.demo.entity;

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.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;
import java.util.UUID;

public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );
        //查询索引下的所有数据
        SearchRequest searchRequest=new SearchRequest();
        searchRequest.indices("kucun");

        SearchSourceBuilder query = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //and条件查询
        boolQueryBuilder.must(QueryBuilders.matchQuery("age","30"));
        boolQueryBuilder.must(QueryBuilders.matchQuery("name","赵六"));

        query.query(boolQueryBuilder);



        searchRequest.source(query);
        SearchResponse response=client.search(searchRequest,RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        System.out.println("条数:"+hits.getTotalHits());
        System.out.println("耗时:"+response.getTook());
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }



        //关闭客户端
        client.close();
    }
}

or条件查询

package com.example.demo.entity;

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.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;
import java.util.UUID;

public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );
        //查询索引下的所有数据
        SearchRequest searchRequest=new SearchRequest();
        searchRequest.indices("kucun");

        SearchSourceBuilder query = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //or条件查询
        boolQueryBuilder.should(QueryBuilders.matchQuery("age","20"));
        boolQueryBuilder.should(QueryBuilders.matchQuery("name","赵六"));

        query.query(boolQueryBuilder);



        searchRequest.source(query);
        SearchResponse response=client.search(searchRequest,RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        System.out.println("条数:"+hits.getTotalHits());
        System.out.println("耗时:"+response.getTook());
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }



        //关闭客户端
        client.close();
    }
}

大于,小于范围查询

package com.example.demo.entity;

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.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;
import java.util.UUID;

public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );
        //查询索引下的所有数据
        SearchRequest searchRequest=new SearchRequest();
        searchRequest.indices("kucun");

        SearchSourceBuilder query = new SearchSourceBuilder();
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age");
        //大于20   大于等于:gte
        rangeQueryBuilder.gt(20);
        //小于30  小于等于: lte
        rangeQueryBuilder.lt(30);
        query.query(rangeQueryBuilder);



        searchRequest.source(query);
        SearchResponse response=client.search(searchRequest,RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        System.out.println("条数:"+hits.getTotalHits());
        System.out.println("耗时:"+response.getTook());
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }



        //关闭客户端
        client.close();
    }
}

模糊查询

package com.example.demo.entity;

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.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;
import java.util.UUID;

public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );
        //查询索引下的所有数据
        SearchRequest searchRequest=new SearchRequest();
        searchRequest.indices("kucun");

        SearchSourceBuilder query = new SearchSourceBuilder();
        //模糊查询
        FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("name", "六")
                //差一个字符也能查询出来 可以去掉这一行 由你来控制
                .fuzziness(Fuzziness.ONE);


        query.query(fuzzyQueryBuilder);



        searchRequest.source(query);
        SearchResponse response=client.search(searchRequest,RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        System.out.println("条数:"+hits.getTotalHits());
        System.out.println("耗时:"+response.getTook());
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }



        //关闭客户端
        client.close();
    }
}

高亮显示字段

package com.example.demo.entity;

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.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;

public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );
        //查询索引下的所有数据
        SearchRequest searchRequest=new SearchRequest();
        searchRequest.indices("kucun");

        SearchSourceBuilder query = new SearchSourceBuilder();
        //精确匹配 如果要使用name中文查询,是查不出来的,要改成name.keyword 如果是age,可以不用加.keyword
        TermsQueryBuilder termQueryBuilder = QueryBuilders.termsQuery("name.keyword", "王五");
        HighlightBuilder highlightBuilder=new HighlightBuilder();
        //前置标签
        highlightBuilder.preTags("<font color='red'>");
        //后置标签
        highlightBuilder.postTags("</font>");
        //对那个字段进行高亮  如果是中文 同样要加.keyword 如果是age不需要
        highlightBuilder.field("name.keyword");

        query.highlighter(highlightBuilder);
        query.query(termQueryBuilder);

        searchRequest.source(query);
        SearchResponse response=client.search(searchRequest,RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        System.out.println("条数:"+hits.getTotalHits());
        System.out.println("耗时:"+response.getTook());
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            System.out.println("高亮数据:"+highlightFields);

        }
        //关闭客户端
        client.close();
    }
}

获取最大的年龄

package com.example.demo.entity;

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.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.Max;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;

public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );
        //查询索引下的所有数据
        SearchRequest searchRequest=new SearchRequest();
        searchRequest.indices("kucun");

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        //maxAge是随便起的名字 对age 取最大的年龄
        AggregationBuilder aggregationBuilder= AggregationBuilders.max("maxAge").field("age");
        searchSourceBuilder.aggregation(aggregationBuilder);

        searchRequest.source(searchSourceBuilder);
        SearchResponse response=client.search(searchRequest,RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        //和上面定义好的名字一致
        Max maxAge = response.getAggregations().get("maxAge");
        System.out.println("最大年龄"+maxAge.getValue());

        System.out.println("条数:"+hits.getTotalHits());
        System.out.println("耗时:"+response.getTook());
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
        //关闭客户端
        client.close();
    }
}

分组查询

package com.example.demo.entity;

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.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.Max;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;

public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );
        //查询索引下的所有数据
        SearchRequest searchRequest=new SearchRequest();
        searchRequest.indices("kucun");

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        //分组查询  age_group随便起的名字
        AggregationBuilder aggregationBuilder= AggregationBuilders.terms("age_group").field("age");
        searchSourceBuilder.aggregation(aggregationBuilder);

        searchRequest.source(searchSourceBuilder);
        SearchResponse response=client.search(searchRequest,RequestOptions.DEFAULT);
        System.out.println(response);


        //关闭客户端
        client.close();
    }
}

创建zhifu索引下的映射,一旦创建 不可以修改,前提是先创建好zhifu这个索引

package com.example.demo.entity;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;

import org.elasticsearch.action.search.SearchResponse;
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.PutMappingRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );

        //前提是先有zhifu这个索引,才能添加zhifu索引下的映射_mapping
        PutMappingRequest request = new PutMappingRequest("zhifu");

//        {
//            "properties":{
//            "name":{
//                "type":"text",
//                        "index": true
//            },
//            "age":{
//                "type": "keyword",
//                        "index": true
//            }
//        }
//        }

        Map<String,Object> map=new HashMap<>();
        Map<String,Object>properties=new HashMap<>();
        Map<String,Object>nameMap=new HashMap<>();
        Map<String,Object>ageMap=new HashMap<>();
        //支持分词搜索
        nameMap.put("type","text");
        //可以索引查询
        nameMap.put("index",true);

        //只能精确查询 不能分词
        ageMap.put("type","keyword");
        //可以索引查询
        ageMap.put("index",true);

        properties.put("name",nameMap);
        properties.put("age",ageMap);
        map.put("properties",properties);

        ObjectMapper objectMapper=new ObjectMapper();
        String s = objectMapper.writeValueAsString(map);
        //把mappings的json加载到来源中
        request.source(s,XContentType.JSON);

        AcknowledgedResponse response = client.indices().putMapping(request, RequestOptions.DEFAULT);
        System.out.println("返回结果:"+response.isAcknowledged());

        //关闭客户端
        client.close();
    }
}

查询索引下的映射

package com.example.demo.entity;

import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.search.SearchResponse;
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.GetMappingsRequest;
import org.elasticsearch.client.indices.GetMappingsResponse;
import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;


public class EsClient {
    public static void main(String[] args) throws IOException {
        //创建es客户端
        RestHighLevelClient client=new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.23.132",9200,"http"))
        );

        //查询zhifu索引下的映射信息_mapping
        GetMappingsRequest request = new GetMappingsRequest();
        request.indices("zhifu");


        GetMappingsResponse mapping = client.indices().getMapping(request, RequestOptions.DEFAULT);
        Map<String, MappingMetadata> mappings = mapping.mappings();
        Set<Map.Entry<String, MappingMetadata>> entries = mappings.entrySet();
        for (Map.Entry<String, MappingMetadata> entry : entries) {
            MappingMetadata value = entry.getValue();
            Map<String, Object> sourceAsMap = value.getSourceAsMap();
            System.out.println(sourceAsMap);
        }
        //关闭客户端
        client.close();
    }
}

接下来我们来安装es集群,找一台新的服务器

先关掉防火墙

systemctl stop firewalld.service

拉取es镜像

docker pull elasticsearch:7.11.1

创建挂载目录

    mkdir -p  /root/elasticsearch
    mkdir -p  /root/elasticsearch/config/data1
    mkdir -p  /root/elasticsearch/config/data2
    mkdir -p  /root/elasticsearch/config/data3
    mkdir -p  /root/elasticsearch/config/plugins1
    mkdir -p  /root/elasticsearch/config/plugins2
    mkdir -p  /root/elasticsearch/config/plugins3

设置执行权限

    chmod 777 /root/elasticsearch/config/data1 
    chmod 777 /root/elasticsearch/config/data2 
    chmod 777 /root/elasticsearch/config/data3

创建es1.yml,ip地址换成你自己的

 vim /root/elasticsearch/config/es1.yml
cluster.name: elasticsearch-cluster 
cluster.initial_master_nodes : es-node1
node.name: es-node1 
network.bind_host: 0.0.0.0 
network.publish_host: 192.168.23.131 
http.port: 9201 
transport.tcp.port: 9301
http.cors.enabled: true 
http.cors.allow-origin: "*" 
node.master: true 
node.data: true  
discovery.zen.ping.unicast.hosts:  ["192.168.23.131:9301","192.168.23.131:9302","192.168.23.131:9303"]

创建es2.yml,ip地址换成你自己的

 vim /root/elasticsearch/config/es2.yml
cluster.name: elasticsearch-cluster 
node.name: es-node2
network.bind_host: 0.0.0.0 
network.publish_host: 192.168.23.131 
http.port: 9202
transport.tcp.port: 9302 
http.cors.enabled: true 
http.cors.allow-origin: "*" 
node.master: true 
node.data: true  
discovery.zen.ping.unicast.hosts:  ["192.168.23.131:9301","192.168.23.131:9302","192.168.23.131:9303"]

创建es3.yml,ip地址换成你自己的

    vim /root/elasticsearch/config/es3.yml
cluster.name: elasticsearch-cluster 
node.name: es-node3
network.bind_host: 0.0.0.0 
network.publish_host: 192.168.23.131 
http.port: 9203
transport.tcp.port: 9303
http.cors.enabled: true 
http.cors.allow-origin: "*" 
node.master: true 
node.data: true  
discovery.zen.ping.unicast.hosts:  ["192.168.23.131:9301","192.168.23.131:9302","192.168.23.131:9303"]

调高JVM线程数限制数量

vim /etc/sysctl.conf
vm.max_map_count=262144

保存退出,刷新配置

sysctl -p

transport.tcp.port:用于配置节点间互相通信的端口号,默认是9300,范围在9300~9400之间,优先绑定9300,如果被占用,则用9301,以此类推

http.port,es对外暴露的http api接口的端口号,默认在9200~9300之间选择一个,优先选择9200,如果被绑定,则选择9201,以此类推

启动es集群

docker run -e ES_JAVA_OPTS="-Xms2048m -Xmx2048m" \
        -d -p 9201:9201 -p 9301:9301 \
        -e ES_MIN_MEM=128m \
        -e ES_MAX_MEM=4096m \
        -v /root/elasticsearch/config/es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
        -v /root/elasticsearch/config/data1/:/usr/share/elasticsearch/data/ \
        -v /root/elasticsearch/config/plugins1/:/usr/share/elasticsearch/plugins  \
        --restart=always \
        --name es01 \
        elasticsearch:7.11.1
docker run -e ES_JAVA_OPTS="-Xms2048m -Xmx2048m" \
        -d -p 9202:9202 -p 9302:9302 \
        -e ES_MIN_MEM=128m \
        -e ES_MAX_MEM=4096m \
        -v /root/elasticsearch/config/es2.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
        -v /root/elasticsearch/config/data2/:/usr/share/elasticsearch/data/ \
        -v /root/elasticsearch/config/plugins2/:/usr/share/elasticsearch/plugins  \
        --restart=always \
        --name es02 \
        elasticsearch:7.11.1
docker run -e ES_JAVA_OPTS="-Xms2048m -Xmx2048m" \
        -d -p 9203:9203 -p 9303:9303 \
        -e ES_MIN_MEM=128m \
        -e ES_MAX_MEM=4096m \
        -v /root/elasticsearch/config/es3.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
        -v /root/elasticsearch/config/data3/:/usr/share/elasticsearch/data/ \
        -v /root/elasticsearch/config/plugins3/:/usr/share/elasticsearch/plugins  \
        --restart=always \
        --name es03 \
        elasticsearch:7.11.1

确认集群配置

http://192.168.23.131:9201/_cat/nodes?pretty

*号代表master节点 负责整个集群管理的

接下来为每一个节点安装ik分词器

进入所有es容器 安装

docker exec -it es01 /bin/bash
cd plugins/
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.11.1/elasticsearch-analysis-ik-7.11.1.zip
docker exec -it es02 /bin/bash
cd plugins/
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.11.1/elasticsearch-analysis-ik-7.11.1.zip
docker exec -it es03 /bin/bash
cd plugins/
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.11.1/elasticsearch-analysis-ik-7.11.1.zip

重启es容器

docker restart es01
docker restart es02
docker restart es03

ik分词器验证

192.168.23.131:9201/_analyze

192.168.23.131:9202/_analyze

192.168.23.131:9203/_analyze

{
    "tokenizer": "ik_smart",
    "text": "上海东方明珠"
}

接下来创建kibana

docker pull kibana:7.11.1

mkdir -p /root/kibana/config

cd /root/kibana/config

vi /root/kibana/config/kibana.yml

配置kibana连接到es集群,这里改成你的ip

server.name: kibana
server.host: "0"
elasticsearch.hosts: [ "http://192.168.23.131:9201","http://192.168.23.131:9202","http://192.168.23.131:9203" ]
i18n.locale: "zh-CN"

启动kibana

docker run -d --name=kibana --restart=always -p 5601:5601 -v /root/kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml kibana:7.11.1

验证kibana

http://192.168.23.131:5601/

先创建一个索引

我们给文档添加一下数据

可以看到已经在kibana刷新数据了

我们在9203节点创建的索引,在9201节点添加的数据,集群就搭建好了

es中的索引就是为了提高搜索的性能

es里面的分片,可以理解为把一张表,拆分成多个表,例如把张三放入A表,把李四放入B表;

es里面的分片副本,就是把分片复制一份;

创建一个bbb的索引,分片3个主分片和一份副本(每个主分片拥有一个副本分片)

http://192.168.23.131:9201/bbb
{
  "settings":{
      "number_of_shards": 3,
      "number_of_replicas": 1
  }
}

查看一下分片副本

接下来我们安装下elasticsearch-head

docker run -d -p 9100:9100 --name head5  mobz/elasticsearch-head:5

访问http://192.168.23.131:9100

这里的22是包含所有的索引,不只是bbb的索引

集群健康值:yellow,表示当前集群的全部主分片都是正常运行,但是副本分片没有全部处于正常状态,如果副本显示unassigned表示副本未分片,有丢数据的风险

集群健康值:green,表示所有分片都在正常运行,这样不会数据丢失,也可以解决单点故障之后的转移;

可以看到我们的集群分片是水平扩容的,不是都放在同一台节点上,这样性能就会提升

数字框加粗的就是主分片

主分片的数量在索引创建的时候就已经确定下来了,不可更改,只能改副本的数量

接下来我们副本改成2,就是在加3个分片的备份

http://192.168.23.131:9201/bbb/_settings
{

      "number_of_replicas": 2
  
}

可以看到从22变成了25

我们可以看到现在es-node3是主节点

我们把他停掉

docker stop es03

可以看到变成了yellow,第一个数字变成了16说明出现了故障

主节点也变成了es-node2,但是现在还能进行通信,这就是出现了故障,转移到其他节点上

我们在启动es03节点

 docker restart es03

可以看到节点恢复了,但是master不会转移,还是我们的es-node2

存放数据是从hash(id)%主分片数据 得到节点的主分片,写入数据,然后再写入到副本

取数据的时候,从那个节点取数据都可以

接下来我们集成下springboot项目

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>client</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 添加MyBatisPlus的依赖 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>
        <!-- MySQL数据 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>
        <!-- druid  连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.14</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath:mapper/*.xml
server:
  port: 8080
spring:
  application:
    name: yewu
  datasource:
    driverClassName: com.mysql.cj.jdbc.Driver
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=true
    username: root


logging:
  level:
    root: debug
#es集群配置
elasticsearch:
  host:
    - 192.168.23.131
    - 192.168.23.131
    - 192.168.23.131
  port:
    - 9201
    - 9202
    - 9203


package com.example.client.entity;

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

/**
 *
 * 索引为商品 indexName必须是小写字母
 * 当项目启动的时候,就自动创建了索引到es
 * @param
 * @return
 * @throws Exception
 */
@Data
@Document(indexName = "shangpin")
public class ShangPin {

    //等同于es的_id
    @Id
    private String id;

    //商品名称  可以进行分词  analyzer 分词器类型
    //Text可以分词
    // ik_smart 查的数据比较少
    // ik_max_word 查的数据比较多
    @Field(type = FieldType.Text,analyzer = "ik_max_word")
    private String title;

    //分类 不进行分词
    @Field(type = FieldType.Keyword)
    private String fenLei;

    //商品价格
    private Double price;
}
package com.example.client.controller;

import com.example.client.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BlueController {

    @Autowired
    private TestService testService;

    @GetMapping("/aa")
    public void aa(){
        testService.aa();
    }
    @GetMapping("/bb")
    public void bb(){
        testService.bb();
    }

}
package com.example.client.mapper;

import com.example.client.entity.ShangPin;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ShangPinMapper extends ElasticsearchRepository<ShangPin,String> {

}
package com.example.client.service;

public interface TestService {

    public void aa();

    public void bb();
}
package com.example.client.service.impl;

import com.example.client.entity.Actor;
import com.example.client.entity.ShangPin;
import com.example.client.mapper.ShangPinMapper;
import com.example.client.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@Slf4j
@Service
public class TestServiceImpl implements TestService {

    @Autowired
    private ShangPinMapper shangPinMapper;
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;


    @Override
    public void aa() {
        // 删除文档数据 参数主键id
        String delete = elasticsearchRestTemplate
                .delete("77bdd17a-4bcd-4dfa-bf61-2faa99de4100",ShangPin.class);
        log.info("返回结果:{}",delete);
    }


    @Override
    public void bb() {
        //添加文档数据
        ShangPin shangPin=new ShangPin();
        String uuid= UUID.randomUUID().toString();
        shangPin.setId(uuid);
        shangPin.setTitle("这是一个标题");
        shangPin.setFenLei("一个分类");
        shangPin.setPrice(200.23);
        shangPinMapper.save(shangPin);
    }
}

调用http://localhost:8080/bb 添加文档数据

然后我们在看一下索引,发现文档中有数据了

调用http://localhost:8080/aa 删除索引下的文档数据

然后我们在查看一下索引,发现数据没有了

我们把id拿过来,修改一条数据,然后还是调用save方法,那么就是修改

可以看到已经修改数据了

根据id获取文档数据

@Override
    public void bb() {
        //根据id获取文档数据
        ShangPin shangPin = shangPinMapper.findById("076d2f77-aea0-4943-a53d-93c72275e416").get();
        log.info("======================={}",shangPin);
    }

获取索引下所有文档数据

@Override
    public void bb() {
        //获取索引下所有文档数据
        Iterable<ShangPin> all = shangPinMapper.findAll();
        for (ShangPin shangPin : all) {
            log.info("======================={}",shangPin);
        }
    }

还可以通过下面的方式 删除文档数据

  @Override
    public void bb() {
        //根据id删除文档数据
         shangPinMapper.deleteById("076d2f77-aea0-4943-a53d-93c72275e416");
    }

批量添加数据

@Override
    public void bb() {
        //批量添加数据
        List<ShangPin> list=new ArrayList<>();
        for (int i = 0; i <10 ; i++) {
           ShangPin shangPin=new ShangPin();
           shangPin.setId(UUID.randomUUID().toString());
           shangPin.setFenLei("分类:"+i);
           shangPin.setTitle("标题:"+i);
           shangPin.setPrice(i+1.0);
           list.add(shangPin);
        }
        shangPinMapper.saveAll(list);
    }

可以看到数据都插入进来了

再来看下分页搜索+排序

 @Override
    public void bb() {
        //按照id排序, asc:顺序 desc:倒叙
        Sort sort = Sort.by(Sort.Direction.DESC,"price");
        //当前页,第一页从 0 开始, 1 表示第二页 ,2表示第三页 依次类推
        int currentPage=1;
        //每页显示多少条
        int pageSize = 5;
        //设置查询分页
        PageRequest pageRequest = PageRequest.of(currentPage, pageSize,sort);
        //分页查询
        Page<ShangPin> page = shangPinMapper.findAll(pageRequest);
        for (ShangPin x : page.getContent()) {
            log.info("{}",x);
        }
    }

注意,这里不能使用id排序,否则会报下面的错误

org.elasticsearch.ElasticsearchException: Elasticsearch exception [type=illegal_argument_exception, reason=Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [id] in order to load field data by uninverting the inverted index. Note that this can use significant memory.]

要想解决,会特别耗费内存,所以就不使用id来排序

在看下价格的条件查询

@Override
    public void bb() {
        //按照id排序, asc:顺序 desc:倒叙
        Sort sort = Sort.by(Sort.Direction.DESC,"price");
        //当前页,第一页从 0 开始, 1 表示第二页 ,2表示第三页 依次类推
        int currentPage=0;
        //每页显示多少条
        int pageSize = 5;
        //多条件查询
        BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();
        //条件查询
        TermQueryBuilder termQueryBuilder=QueryBuilders.termQuery("price","10");
        //and 查询
        boolQueryBuilder.must(termQueryBuilder);

        //设置查询分页
        PageRequest pageRequest = PageRequest.of(currentPage, pageSize,sort);

        //创建query请求
        NativeSearchQuery searchQuery=new NativeSearchQueryBuilder()
                //bool参数查询
                .withQuery(boolQueryBuilder)
                //分页参数
                .withPageable(pageRequest)
                //构建
                .build()
                ;

        //查询
        SearchHits<ShangPin> list = elasticsearchRestTemplate.search(searchQuery, ShangPin.class);
        //解析数据
        List<SearchHit<ShangPin>> searchHits = list.getSearchHits();

        for (SearchHit<ShangPin> x :searchHits) {
            ShangPin sp=x.getContent();
            log.info("=========================={}",sp);
        }
    }

es的索引和文档都是存储在磁盘上的;

我们在配置es的jvm调优的时候,不要超过虚拟机内存的50%;

例如虚拟机是64GB内存,那么xmx,xms不要超过31G;

es可以通过版本号来使用乐观锁,保证读写一致性

接下来我们进入kibana

http://192.168.23.131:5601/

我们可以控制台编写索引的操作 crud,左边的操作结果会在右边展示出来

#创建索引
#put 索引名称  索引名称必须是小写
put suoyin
#head 索引名称 判断是否存在 200 存在, 404 不存在
head suoyin
#查询索引名称
get suoyin
#查询所有索引
get _cat/indices
#删除索引
DELETE suoyin
#创建文档数据,修改文档数据
PUT suoyin/_doc/1001
{
  "name":"张三",
  "age":18
}
#查询文档数据
GET suoyin/_doc/1001
#查询索引下所有的文档数据
GET suoyin/_search
#删除文档数据
DELETE suoyin/_doc/1001
#批量添加文档数据
PUT suoyin/_bulk
{"index": {"_index": "suoyin","_id": "1001"}}
{"name":"zhangsan","age":30}
{"index": {"_index": "suoyin","_id": "1002"}}
{"name":"lisi","age":40}
{"index": {"_index": "suoyin","_id": "1003"}}
{"name":"wangwu","age":50}
{"index": {"_index": "suoyin","_id": "1004"}}
{"name":"zhaoliu","age":60}
#条件查询 match支持分词,也就是模糊搜索
#前提是你的数据是以空格 隔开的 才是分词
GET suoyin/_search
{
  "query": {
    "match": {
      "name": "lisi"
    }
  }
}
#条件查询 term不支持分词,也就是不支持模糊搜索
#数据如果有空格,那么也不能查询出来
GET suoyin/_search
{
  "query": {
    "term": {
      "name": {
        "value": "lisi"
      }
    }
  }
}
#条件查询 只显示age字段
GET suoyin/_search
{
  "_source": ["age"], 
  "query": {
    "match": {
      "name": "lisi"
    }
  }
}

#条件查询 or 
GET suoyin/_search
{

  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "zhangsan"
          }
        },
        {
          "match": {
            "age": 40
          }
        }
      ]
    }
  }
}
#条件查询 排序 asc正序 desc 倒叙
GET suoyin/_search
{

 "query":{
   "match": {
     "name":"zhangsan"
   }
 },
 "sort":[
     {
       "age":{
         "order": "desc"
       }
     }
   
   ]
}
#分页查询 from=(页数-1)*size
GET suoyin/_search
{

 "query":{
   "match_all": {}
 },
 "from":0,
 "size":2
}

#age 分组查询 
# ageGroup 随便起的名字
# size 是不显示任何内容
GET suoyin/_search
{
  "aggs": {
    "ageGroup": {
      "terms": {
        "field": "age"
      }
    }
  },
  "size": 0
  
}
#age 分组查询 求和
# ageGroup 随便起的名字
# size 是不显示任何内容
# ageSum 随便起的名字
# sum 对age分组后的数据 进行求和
GET suoyin/_search
{
  "aggs": {
    "ageGroup": {
      "terms": {
        "field": "age"
      },
      "aggs": {
        "ageSum": {
          "sum": {
            "field": "age"
          }
        }
      }
    }
  },
  "size": 0
  
}
#age 求平均值
GET suoyin/_search
{
  "aggs": {
    "ageAvg": {
      "avg": {
        "field": "age"
      }
    }
  },
  "size": 0
  
}
#按照年龄倒叙 查询前2名
GET suoyin/_search
{
  "aggs": {
    "top2": {
      "top_hits": {
        "sort": [{
          "age": {
            "order":"desc"
          }
          
        }
        ],
        "size": 2
      }
    }
  },
  "size": 0
  
}
#创建模版 修改模版 mytemp 模版名称随便起
#创建索引时候 使用my开头才能匹配上这个模版
PUT _template/mytemp
{
  "index_patterns": ["my*"],
  "settings": {
    "index":{
       "number_of_shards" : "2"
    }
  },
  "mappings": {
    "properties": {
      "now":{
        "type": "date",
        "format": "yyyy/MM/dd"
      }
    }
  }
}

#查看模版
GET _template/mytemp
#创建索引 并匹配自定义模版
PUT my_sy

#查看索引
GET my_sy
#删除模版
DELETE _template/mytemp
#默认分词 查看zhangsan的分词效果 分词就是把一个分成多份
GET _analyze
{
  "analyzer": "standard",
  "text": ["zhangsan"]
}
#查看zhangsan的分词效果 使用ik分词器
GET _analyze
{
  "analyzer": "ik_smart",
  "text": ["我是一个张三"]
}

#查看zhangsan的分词效果 使用ik分词器
GET _analyze
{
  "analyzer": "ik_max_word",
  "text": ["我是一个张三"]
}

elaticsearch7.x,8.x入门教程(二)

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值