栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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   
http://localhost:9200/books/_doc/1   
http://localhost:9200/books/_create/2    


查询ES文档 GET请求:

http://localhost:9200/books/_doc/3  
http://localhost:9200/books/_search  
http://localhost:9200/books/_search?q=name:springboot  

删除ES文档 DELETE请求

`http://localhost:9200/books/_doc/3`  

修改ES文档 PUT请求 内容是全覆盖
第一种方式:

第二种方式:

springboot 整合 ES high level cilent
添加相关依赖:

        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
        

硬编码

@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 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);
        }
    }
}



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/856511.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号