其他分享
首页 > 其他分享> > 25-RestHighLevelClient----文档操作

25-RestHighLevelClient----文档操作

作者:互联网

@SpringBootTest
public class RestHighLevelClientForDocumentTest {

    private RestHighLevelClient restHighLevelClient;

    @Autowired
    public RestHighLevelClientForDocumentTest(RestHighLevelClient restHighLevelClient) {
        this.restHighLevelClient = restHighLevelClient;
    }

    /**
     * 索引/插入一条文档
     */
    @Test
    public void testCreated() throws IOException {
        IndexRequest indexRequest = new IndexRequest("products");
        indexRequest.id("2") //手动指定id,不然生成uuid
                .source("{\"title\": \"日本豆\",\"price\": 2.5,\"created_at\": \"2012-12-12\",\"description\": \"日本豆很好吃\"}", XContentType.JSON);//插入的源数据

        //参数1:索引请求对象  参数2:请求配置对象
        IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        System.out.println(index.status());//输出状态
    }

    /**
     * 更新文档
     */
    @Test
    public void testUpdate() throws IOException {
        UpdateRequest updateRequest = new UpdateRequest("products", "1");
        //doc:在保留原始数据的基础上更新
        updateRequest.doc("{\"price\": \"3.5\"}", XContentType.JSON);

        UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
        System.out.println(update.status());
    }

    /**
     * 删除文档
     */
    @Test
    public void testDelete() throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest("products", "2");

        //参数1:删除请求对象  参数2:请求配置对象
        DeleteResponse delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
        System.out.println(delete.status());
    }

    /**
     * 根据id查询文档
     */
    @Test
    public void testQueryById() throws IOException {
        GetRequest getRequest = new GetRequest("products", "1");

        GetResponse response = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
        System.out.println("_id:" + response.getId());
        System.out.println("source:" + response.getSourceAsString());
        System.out.println("source:" + response.getSource());
    }

    /**
     * 查询所有
     */
    @Test
    public void testMatchAll() throws IOException {
        SearchRequest searchRequest = new SearchRequest("products");//指定搜索索引
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();//指定条件对象
        sourceBuilder.query(QueryBuilders.matchAllQuery());//查询所有
        searchRequest.source(sourceBuilder);//指定查询条件

        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);//参数1:搜索请求对象  参数2:请求配置对象

        System.out.println("总条数:" + searchResponse.getHits().getTotalHits().value);
        System.out.println("最大得分:" + searchResponse.getHits().getMaxScore());

        SearchHit[] searchHits = searchResponse.getHits().getHits();
        for (SearchHit searchHit : searchHits) {
            System.out.println("id:"+ searchHit.getId() + "source:" + searchHit.getSourceAsString());
        }
    }

    /**
     * 不同条件查询跟上面如出一辙,只需要改变QueryBuilders条件对象调用的方法即可, 所以我们封装成了query方法
     * 不同条件查询
     */
    @Test
    public void testTermQuery() throws IOException {
        //关键词查询
        this.query(QueryBuilders.termQuery("description", "浣熊"));

        //范围查询
        this.query(QueryBuilders.rangeQuery("price").gt(0).lt(10));

        //前缀查询
        this.query(QueryBuilders.prefixQuery("title", "小浣熊"));

        //通配符查询
        this.query(QueryBuilders.wildcardQuery("title", "小浣熊*"));

        //ids查询 指定多个id
        this.query(QueryBuilders.idsQuery().addIds("1").addIds("2"));

        //multi_match 多字段查询
        //如果查询的字段分词,就把我们输入的关键词分词。否则不会进行分词
        this.query(QueryBuilders.multiMatchQuery("很好吃日本豆").field("title").field("description"));
        this.query(QueryBuilders.multiMatchQuery("很好吃日本豆", "title", "description"));
    }

    //封装的查询方法
    public void query(QueryBuilder queryBuilder) throws IOException {
        SearchRequest searchRequest = new SearchRequest("products");//指定搜索索引
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();//指定条件对象
        sourceBuilder.query(queryBuilder);//关键词查询
        searchRequest.source(sourceBuilder);//指定查询条件

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

        System.out.println("总条数:" + searchResponse.getHits().getTotalHits().value);
        System.out.println("最大得分:" + searchResponse.getHits().getMaxScore());

        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) {
            System.out.println("id:" + hit.getId() + "---source:" + hit.getSourceAsString());
        }
    }
}

标签:25,System,查询,----,println,query,RestHighLevelClient,restHighLevelClient,out
来源: https://www.cnblogs.com/morehair/p/15820414.html