栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

ElasticSearch

ElasticSearch

索引库操作 初始化索引库

①引入es的RestHighLevelClient依赖:


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

②因为SpringBoot默认的ES版本是7.6.2,所以我们需要覆盖默认的ES版本:


    1.8
    7.12.1

③初始化RestHighLevelClient:

@Bean
    public RestHighLevelClient client(){
        return  new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://119.23.248.50:9200")
        ));
    }

创建索引库

①创建一个类,定义mapping映射的JSON字符串常量

package cn.itcast.hotel.constants;

public class HotelConstants {
    public static final String MAPPING_TEMPLATE = "{n" +
            "  "mappings": {n" +
            "    "properties": {n" +
            "      "id": {n" +
            "        "type": "keyword"n" +
            "      },n" +
            "      "name":{n" +
            "        "type": "text",n" +
            "        "analyzer": "ik_max_word",n" +
            "        "copy_to": "all"n" +
            "      },n" +
            "      "address":{n" +
            "        "type": "keyword",n" +
            "        "index": falsen" +
            "      },n" +
            "      "price":{n" +
            "        "type": "integer"n" +
            "      },n" +
            "      "score":{n" +
            "        "type": "integer"n" +
            "      },n" +
            "      "brand":{n" +
            "        "type": "keyword",n" +
            "        "copy_to": "all"n" +
            "      },n" +
            "      "city":{n" +
            "        "type": "keyword",n" +
            "        "copy_to": "all"n" +
            "      },n" +
            "      "starName":{n" +
            "        "type": "keyword"n" +
            "      },n" +
            "      "business":{n" +
            "        "type": "keyword"n" +
            "      },n" +
            "      "location":{n" +
            "        "type": "geo_point"n" +
            "      },n" +
            "      "pic":{n" +
            "        "type": "keyword",n" +
            "        "index": falsen" +
            "      },n" +
            "      "all":{n" +
            "        "type": "text",n" +
            "        "analyzer": "ik_max_word"n" +
            "      }n" +
            "    }n" +
            "  }n" +
            "}";
}

②编写代码

@SpringBootTest
public class Index {

    @Autowired
    private RestHighLevelClient client;

    //新增索引库
    @Test
    void createIndex() throws IOException {
        //准备request请求
        CreateIndexRequest request = new CreateIndexRequest("hotel");
        //准备参数
        request.source(MAPPING_TEMPLATE, XContentType.JSON);
        //发送请求
        client.indices().create(request, RequestOptions.DEFAULT);
    }
}

删除索引库

编写代码

@SpringBootTest
public class Index {

    @Autowired
    private RestHighLevelClient client;

    //删除索引库
    @Test
    void deleteIndex() throws IOException {
        //准备request请求
        DeleteIndexRequest request = new DeleteIndexRequest("hotel");
        //准备参数(删除操作不需要参数)
        //发送请求
        client.indices().delete(request,RequestOptions.DEFAULT);
    }
}

判断索引库是否存在
@SpringBootTest
public class Index {

    @Autowired
    private RestHighLevelClient client;

    //判断索引库是否存在
    @Test
    void exitsIndex() throws IOException {
        //准备request请求
        GetIndexRequest request = new GetIndexRequest("hotel");
        //准备参数(判断索引库是否存在不需要参数)
        //发送请求
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        //打印一下
        System.out.println(exists?"索引库存在":"索引库不存在");
    }
}

总结

索引库操作的基本步骤

  • 初始化RestHighLevelClient。
  • 创建XXXIndexRequest,XXX是Create、Get、Delete。
  • 准备DSL参数(只有Create的时候需要,其它都是无参)。
  • 发送请求。调用 **indices( ).xxx( )**方法,xxx是create、delete、exists。

文档操作 新增文档
  1. 根据 id 查询酒店数据 Hotel
  2. 将 Hotel 封装为 HotelDoc
  3. 将 HotelDoc 序列化为 JSON
  4. 创建 IndexRequest,指定索引库名称和id
  5. 准备请求参数,也就是JSON文档
  6. 发送请求
@SpringBootTest
public class Index {

    @Autowired
    private RestHighLevelClient client;

    @Autowired
    private IHotelService hotelService;

    //新增文档
    @Test
    void createdocument() throws IOException {
        //根据id查询酒店数据
        Hotel hotel = hotelService.getById(61083L);
        //转换为文档类型
        HotelDoc hotelDoc = new HotelDoc(hotel);
        //序列化为JSON
        String json = JSON.toJSONString(hotelDoc);

        //准备request请求
        IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
        //准备参数
        request.source(json,XContentType.JSON);
        //发送请求
        client.index(request,RequestOptions.DEFAULT);
    }
}

删除文档
@SpringBootTest
public class Index {

    @Autowired
    private RestHighLevelClient client;

    @Autowired
    private IHotelService hotelService;

    //删除文档
    @Test
    void deletedocument() throws IOException {
        //准备request请求
        DeleteRequest request = new DeleteRequest("hotel", "61083");
        //准备参数(删除文档不需要参数)
        //发送请求
        client.delete(request,RequestOptions.DEFAULT);
    }
}

修改文档
@SpringBootTest
public class Index {

    @Autowired
    private RestHighLevelClient client;

    @Autowired
    private IHotelService hotelService;

    //修改文档
    @Test
    void deletedocument() throws IOException {
        //准备request请求
        UpdateRequest request = new UpdateRequest("hotel", "61083");
        //准备参数
        request.doc(
                "age",18,
                "name","Tom"
        );
        //发送请求
        client.update(request,RequestOptions.DEFAULT);
    }
}

查询文档
@SpringBootTest
public class Index {

    @Autowired
    private RestHighLevelClient client;

    @Autowired
    private IHotelService hotelService;

    //查询文档
    @Test
    void getdocument() throws IOException {
        //准备request请求
        GetRequest request = new GetRequest("hotel", "61083");
        //发送请求
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        //解析响应结果
        String json = response.getSourceAsString();
        //反序列化成对象
        HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
        //打印
        System.out.println(hotelDoc);
    }
}

批量导入文档
@SpringBootTest
public class Index {

    @Autowired
    private RestHighLevelClient client;

    @Autowired
    private IHotelService hotelService;

    //批量插入文档
    @Test
    void getdocument() throws IOException {
       //先把数据库中,酒店的所有信息查出来
        List hotels = hotelService.list();
        //创建request请求
        BulkRequest request = new BulkRequest();
        //准备参数,这里的参数就是多个request对象
        for (Hotel hotel : hotels) {
            //转换为文档类型
            HotelDoc hotelDoc = new HotelDoc(hotel);
            //转换为JSON
            String json = JSON.toJSONString(hotelDoc);
            //创建新增文档的request对象
            request.add(new IndexRequest("hotel")
                    .id(hotelDoc.getId().toString())//id
                    .source(json, XContentType.JSON));
        }
        //发送请求
        client.bulk(request,RequestOptions.DEFAULT);
    }
}

文档查询 处理响应结果

编写一个方法,处理响应结果(不含高亮)

 //这里编写一个解析响应结果的方法
    private void parseResponse(SearchResponse response){
        //解析响应
        SearchHits searchHits = response.getHits();
        //查询到的条数
        long total = searchHits.getTotalHits().value;
        System.out.println("查询总条数为:"+total);
        //获取文档,得到一个数组
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            //获取文档信息(json)
            String json = hit.getSourceAsString();
            //将JSON反序列化为对象
            HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
            //这里直接打印
            System.out.println(hotelDoc);
        }
    }

编写一个方法,处理响应结果(含高亮)

//这里编写一个解析响应结果的方法(高亮)
    private void parseResponseHighlight(SearchResponse response){
        //解析响应
        SearchHits searchHits = response.getHits();
        //查询到的条数
        long total = searchHits.getTotalHits().value;
        System.out.println("查询总条数为:"+total);
        //获取文档,得到一个数组
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            //获取文档信息(json)
            String json = hit.getSourceAsString();
            //将JSON反序列化为对象
            HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
            //获取高亮结果
            Map highlightFields = hit.getHighlightFields();
            if (highlightFields!=null){
                //不为空,那就覆盖,这里以brand高亮为例
                String value = highlightFields.get("brand").getFragments()[0].string();
                hotelDoc.setBrand(value);
            }
            //这里直接打印
            System.out.println(hotelDoc);
        }
    }

match查询

全文检索的 match 和 multi_match 查询与match_all的API基本一致。差别是查询条件,也就是query的部分。

@Autowired
    private RestHighLevelClient client;

    //match查询
    @Test
    void match() throws IOException {
        //准备request
        SearchRequest request = new SearchRequest("hotel");
        //准备参数
        request.source().query(QueryBuilders.matchQuery("all","如家"));
        //发送请求,得到响应
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //解析响应结果
        parseResponse(response);
    }

term精确查询
//Term精确查询
    @Test
    void term() throws IOException {
        //准备request请求
        SearchRequest request = new SearchRequest("hotel");
        //准备参数
        request.source().query(QueryBuilders.termQuery("brand","如家"));
        //发送请求,得到响应
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //解析响应结果
        parseResponse(response);
    }

bool查询
//bool查询
    @Test
    void bool() throws IOException {
        //准备request请求
        SearchRequest request = new SearchRequest("hotel");
        //准备参数
        //准备BoolQuery
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        //添加term
        boolQuery.must(QueryBuilders.termQuery("brand","如家"));
        //添加range
        boolQuery.filter(QueryBuilders.rangeQuery("price").lte(200));
        //装进去
        request.source().query(boolQuery);
        //发送请求,得到响应
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //解析响应结果
        parseResponse(response);
    }

排序
//排序
    @Test
    void sort() throws IOException {
        //准备request
        SearchRequest request = new SearchRequest("hotel");
        //准备参数
        request.source().query(QueryBuilders.termQuery("brand","如家"));
        //排序
        request.source().sort("price", SortOrder.DESC);
        //发送请求,得到响应
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //解析响应结果
        parseResponse(response);
    }

分页
//分页
    @Test
    void page() throws IOException {
        //准备request
        SearchRequest request = new SearchRequest("hotel");
        //准备参数
        request.source().query(QueryBuilders.termQuery("brand","如家"));
        //分页
        //当前页码
        int pageNum=1;
        //每页显示条数
        int pageSize=6;
        request.source().from((pageNum-1)*pageSize).size(pageSize);
        //发送请求,得到响应
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //解析响应结果
        parseResponse(response);
    }

高亮
//高亮
    @Test
    void highlight() throws IOException {
        //准备request
        SearchRequest request = new SearchRequest("hotel");
        //准备参数
        request.source().query(QueryBuilders.matchQuery("all","如家"));
        //高亮
        request.source().highlighter(new HighlightBuilder()
                .field("brand")//高亮字段
                .preTags("")//标签前缀
                .postTags("")//标签后缀
                .requireFieldMatch(false)//是否需要匹配
        );
        //发送请求,得到响应
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //解析响应结果(高亮)
        parseResponseHighlight(response);
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/487833.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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