Elasticsearch(二)Text和Keyword类型的区别、Springboot整合ES

1.Text和Keyword类型的区别

text: 它会为该字段的内容进行拆词操作,并放入倒排索引表中

keyword: 它不会进行拆词操作

使用match匹配查询---对匹配的关键字进行拆词操作,并和倒排索引表中对应。

使用term精准匹配---它不会对关键字进行拆词操作,而且把关键字作为一个整体和倒排索引表进行匹配

2.Springboot整合ES

1.创建一个Springboot工程并加入相关的依赖

<dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <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>
    </dependencies>

2.创建一个配置,获取ES工具类对象。

@Configuration
public class ESConfig {

    //该对象可以对ES进行相关的操作
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1",9200,"http")));
        return client;
    }
}

3.进行相关对ES操作

3.1 操作索引(indices)---创建索引

@Test
    void testCreateIndex() throws Exception{
        CreateIndexRequest createIndexRequest = new CreateIndexRequest("testindex_02");
        CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse.isAcknowledged());
    }

3.2 操作索引--删除索引

    //删除索引
    @Test
    void testDeleteIndex() throws Exception{
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("testindex_02");
        AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        System.out.println(delete.isAcknowledged());
    }

3.3 索引操作--判断索引是否存在

    //查询索引
    @Test
    void testExistIndex() throws Exception{
        GetIndexRequest getIndexRequest = new GetIndexRequest("testindex_02");
        boolean b = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
        System.out.println(b);
    }

3.4.对文档的操作(index)---添加文档

    //添加文档
    @Test
    void testInsetDoc() throws Exception{
        IndexRequest indexRequest = new IndexRequest("testindex_02");
        //指定文档的内容:String文档的json内容,XContentType xContentType:以什么格式
        indexRequest.source(JSON.toJSONString(new User("1", "张三", "北京", 17)),XContentType.JSON);
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println(indexResponse.getResult());
    }

3.5.查询文档

    //查询文档
    @Test
    void testGetDoc() throws Exception{
        GetRequest getRequest = new GetRequest("testindex_02");
        getRequest.id("Fk38pYIB_tpG9hpoQyI3");
        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);

        System.out.println(getResponse.getSourceAsMap());
    }

3.6.判断文档是否存在

    //判断文档是否存在
    @Test
    void testExistDoc() throws Exception{
        GetRequest getRequest = new GetRequest("testindex_02");
        getRequest.id("Fk38pYIB_tpG9hpoQyI3");
        boolean b = client.exists(getRequest, RequestOptions.DEFAULT);
        System.out.println(b);
    }

3.7.修改文档

    //修改文档
    @Test
    void testUpdateDoc() throws Exception{
        UpdateRequest updateRequest = new UpdateRequest("testindex_02","Fk38pYIB_tpG9hpoQyI3");
        User user = new User();
        user.setName("阿大");
        updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
        UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
        System.out.println(updateResponse.getResult());
    }

3.8.删除文档

    //删除文档
    @Test
    void testDeleteDoc() throws Exception{
        DeleteRequest deleteRequest = new DeleteRequest("testindex_02");
        deleteRequest.id("Fk38pYIB_tpG9hpoQyI3");
        DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
        System.out.println(deleteResponse.getResult());
    }

3.9.批量添加文档

    //批量添加文档
    @Test
    void testBulkDoc() throws Exception {
        BulkRequest bulkRequest = new BulkRequest("testindex_02");
        List<User> list = new ArrayList<>();
        list.add(new User("1","张三","北京",12));
        list.add(new User("2","李四","上海",14));
        list.add(new User("3","王五","武汉",17));
        /*list.stream().forEach(item->
        bulkRequest.add(new IndexRequest().id(item.getId()).source(JSON.toJSONString(item),XContentType.JSON)));*/
        for(User user : list){
            IndexRequest indexRequest = new IndexRequest();
            indexRequest.id(user.getId());
            indexRequest.source(JSON.toJSONString(user),XContentType.JSON);
            bulkRequest.add(indexRequest);
        }
        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulkResponse.hasFailures());
    }

3.10.复杂查询

    /*搜索查询---GET /索引/_search
     {
         "query":{
             "":{}
          },
      "from":
        "size":
         "_source":["",""],
         "sort":{}
     }
    1. 搜索请求对象SearchRequest
    2. 构建一个条件对象SearchSourceBuilder
    3. 把条件对象放入搜索请求对象中
    4. 执行搜索功能*/
    @Test
    void testSearch() throws Exception{
        SearchRequest searchRequest = new SearchRequest("testindex_02");
        //创建一个条件对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "张");
        searchSourceBuilder.query(termQueryBuilder);

        //分页
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(1);

        //排序
        searchSourceBuilder.sort("age");

        //高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("name");
        //首
        highlightBuilder.preTags("<font color='red'>");
        //尾
        highlightBuilder.postTags("</font>");
        searchSourceBuilder.highlighter(highlightBuilder);

        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        System.out.println("总条数:"+searchResponse.getHits().getTotalHits().value);
        SearchHit[] searchHits = searchResponse.getHits().getHits();

        Arrays.stream(searchHits).forEach(item-> System.out.println(item.getSourceAsString()));

        Arrays.stream(searchHits).forEach(item-> System.out.println(item.getHighlightFields()));
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Naaaaa.a

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值