springboot整合ES和IK分词器及使用ES文档的基本操作 high level cilent

ES应用场景:全文分布式搜索引擎
倒排索引===>创建文档===>使用文档

ES安装包:
链接:https://pan.baidu.com/s/1oO56WOc0s-Me6wfobL4CEw
提取码:71zz

下载IK分词器,解压至ES的plugins目录下
链接:https://pan.baidu.com/s/1USCUEBoxxqjGqdkdmtA2dw
提取码:t1qm

运行
elasticsearch.bat

浏览器输入http://localhost:9200/,出现以下字符则运行成功

{
  "name" : "LAPTOP-II2DNBO4",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "jSUK96vzS1SRenRXK33o2A",
  "version" : {
    "number" : "7.16.2",
    "build_flavor" : "default",
    "build_type" : "zip",
    "build_hash" : "2b937c44140b6559905130a8650c64dbd0879cfb",
    "build_date" : "2021-12-18T19:42:46.604893745Z",
    "build_snapshot" : false,
    "lucene_version" : "8.10.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline : "You Know, for Search"

通过 postman 访问ES(一直发送请求需要重启ES)
1.创建指定索引 写方法体

{
    "mappings":{
        "properties":{
            "id":{
                "type":"keyword"  
            },
            "name":{
                "type":"text",     
                "analyzer":"ik_max_word",  
                "copy_to":"all"  
            },
            "description":{
                "type":"text", 
                "analyzer":"ik_max_word",
                "copy_to":"all" 
            },
            "type":{
                "type":"keyword" 
            },
            "all":{
                "type":"text", 
                "analyzer":"ik_max_word
            }
        }
    }
}

在这里插入图片描述
2.查询指定索引
在这里插入图片描述

创建ES文档 POST请求:

http://localhost:9200/books/_doc   <!--生成随机id-->
http://localhost:9200/books/_doc/1   <!--生成指定id-->
http://localhost:9200/books/_create/2    <!--生成指定id-->

在这里插入图片描述
查询ES文档 GET请求:

http://localhost:9200/books/_doc/3  <!--查询id为3的ES文档-->
http://localhost:9200/books/_search  <!--查询所有ES文档-->
http://localhost:9200/books/_search?q=name:springboot  <!--查询包含 指定内容的ES文档-->

删除ES文档 DELETE请求

`http://localhost:9200/books/_doc/3`  <!--删除id为3的ES文档-->

修改ES文档 PUT请求 内容是全覆盖
第一种方式:
在这里插入图片描述
第二种方式:
在这里插入图片描述
springboot 整合 ES high level cilent
添加相关依赖:

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
        </dependency>

硬编码

@SpringBootTest
class Springboot14EsApplicationTests {

    private RestHighLevelClient client;

    @BeforeEach
    void setUp() {
        HttpHost host = HttpHost.create("http://localhost:9200");
        RestClientBuilder builder = RestClient.builder(host);
        client = new RestHighLevelClient(builder);
    }

    @AfterEach
    void tearDown() throws IOException {
        client.close();
    }
    

    @Test //创建索引
    void testCreateIndex() throws IOException {
        CreateIndexRequest request = new CreateIndexRequest("books");
        client.indices().create(request, RequestOptions.DEFAULT);
    }

  @Test  //创建索引
    void testCreateIndexByIK() throws IOException {

        CreateIndexRequest request = new CreateIndexRequest("books");
        //设置请求中的参数
        String json = "";//写入json数据
        request.source(json, XContentType.JSON);
        client.indices().create(request, RequestOptions.DEFAULT);
    }

  //创建文档
    @Test
    void testCreateDoc() throws IOException {
        Book book = bookDao.selectById(2);
        IndexRequest request = new IndexRequest("books").id(book.getId().toString());
        String json = JSON.toJSONString(book);
        request.source(json,XContentType.JSON);
        client.index(request,RequestOptions.DEFAULT);
    }

	 //创建多个文档
    @Test
    void testCreateDocAll() throws IOException {
        List<Book> bookList = bookDao.selectList(null);
        BulkRequest bulk  = new BulkRequest();

        for (Book book : bookList) {
            IndexRequest request = new IndexRequest("books").id(book.getId().toString());
            String json = JSON.toJSONString(book);
            request.source(json,XContentType.JSON);
            bulk.add(request);
        }
        client.bulk(bulk,RequestOptions.DEFAULT);//批处理
    }

	 //按照id查询文档
    @Test
    void testselectDocById() throws IOException {
        GetRequest request = new GetRequest("books","2");
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        String sourceAsString = response.getSourceAsString();
        System.out.println(sourceAsString);
    }
	
    //按照条件查询文档
    @Test
    void testselectDocByCondition() throws IOException {

        SearchRequest request = new SearchRequest("books");

        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.matchPhraseQuery("name","测试"));
        request.source(builder);

        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        for (SearchHit hit : hits) {
            String source = hit.getSourceAsString();
            Book book = JSON.parseObject(source, Book.class);
            System.out.println(book);
        }
    }
}



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值