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

1.Text和Keyword类型的区别

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

keyword: 它不会进行拆词操作

Elasticsearch(二)Text和Keyword类型的区别、Springboot整合ES_第1张图片

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

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

2.Springboot整合ES

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


        
            com.alibaba
            fastjson
            1.2.75
        
        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

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)---创建索引

Elasticsearch(二)Text和Keyword类型的区别、Springboot整合ES_第2张图片

@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 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("");
        //尾
        highlightBuilder.postTags("");
        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()));
    }

你可能感兴趣的:(Elasticsearch,elasticsearch,搜索引擎,大数据)