springboot 合入elasticsearch测试

该代码示例展示了如何在Java中配置和使用Elasticsearch的High-LevelREST客户端进行索引创建、查询、删除等操作。配置类通过SpringBoot整合,测试类包括了创建索引、检查索引存在性、删除索引、添加文档、获取文档、更新文档、删除文档以及搜索操作。
摘要由CSDN通过智能技术生成

添加依赖

		<!-- es -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.2.0</version>
        </dependency>

配置类

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class ElasticSearchClientConfig {
    @Value("${es.hostname}")
    private String hostname;
    @Value("${es.port}")
    private Integer port;
    @Value("${es.scheme}")
    private String scheme;
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(hostname, port, scheme)));
        return client;
    }
}

测试类

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reptile.dao.AnnouncementDao;
import com.reptile.dao.BidItemDao;
import com.reptile.dto.AnnouncementAndItemDto;
import com.reptile.dto.AnnouncementDto;
import com.reptile.po.AnnouncementPo;
import com.reptile.po.BidItemPo;
import com.reptile.vo.AnnouncementReq;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;

import com.alibaba.fastjson.JSON;
import org.springframework.test.context.junit4.SpringRunner;

/**
 *  es7.6.x 高级客户端测试 API
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticsearchJdApplicationTests {
    // 面向对象来操作
    @Autowired
    private RestHighLevelClient client;

    @Autowired
    private AnnouncementDao announcementDao;

    @Autowired
    private BidItemDao bidItemDao;

    // 测试索引的创建 Request PUT kuang_index
    @Test
    void testCreateIndex() throws IOException {
        // 1、创建索引请求
        CreateIndexRequest request = new CreateIndexRequest("biding");
        request.settings(Settings.builder()
                // 创建分片数为3
                .put("index.number_of_shards", 1)
                // 副本数为2
                .put("index.number_of_replicas", 0)
        );
        request.source("{\n" +
                "    \"mappings\": {\n" +
                "        \"properties\": {\n" +
                "            \"addressId\": {\n" +
                "                \"type\": \"long\"\n" +
                "            },\n" +
                "            \"announcementName\": {\n" +
                "                \"type\": \"text\",\n" +
                "                \"fields\": {\n" +
                "                    \"keyword\": {\n" +
                "                        \"type\": \"keyword\"\n" +
                "                    }\n" +
                "                },\n" +
                "                \"boost\": 10\n" +
                "            },\n" +
                "            \"bidItems\": {\n" +
                "                \"type\": \"nested\",\n" +
                "                \"properties\": {\n" +
                "                    \"announcementNumber\": {\n" +
                "                        \"type\": \"keyword\"\n" +
                "                    },\n" +
                "                    \"bidContent\": {\n" +
                "                        \"type\": \"text\"\n" +
                "                    },\n" +
                "                    \"startDate\": {\n" +
                "                        \"type\": \"date\",\n" +
                "                        \"format\": \"yyyy-MM-dd\"\n" +
                "                    },\n" +
                "                    \"endDate\": {\n" +
                "                        \"type\": \"date\",\n" +
                "                        \"format\": \"yyyy-MM-dd\"\n" +
                "                    },\n" +
                "                    \"bidItemNumber\": {\n" +
                "                        \"type\": \"keyword\"\n" +
                "                    },\n" +
                "                    \"id\": {\n" +
                "                        \"type\": \"long\"\n" +
                "                    },\n" +
                "                    \"packetNumber\": {\n" +
                "                        \"type\": \"integer\"\n" +
                "                    },\n" +
                "                    \"itemName\": {\n" +
                "                        \"type\": \"text\",\n" +
                "                        \"fields\": {\n" +
                "                            \"keyword\": {\n" +
                "                                \"type\": \"keyword\"\n" +
                "                            }\n" +
                "                        }\n" +
                "                    },\n" +
                "                    \"performanceRequirements\": {\n" +
                "                        \"type\": \"keyword\"\n" +
                "                    },\n" +
                "                    \"projectConstructionUnit\": {\n" +
                "                        \"type\": \"keyword\"\n" +
                "                    },\n" +
                "                    \"qualificationRequirement\": {\n" +
                "                        \"type\": \"keyword\"\n" +
                "                    },\n" +
                "                    \"smallClassName\": {\n" +
                "                        \"type\": \"keyword\"\n" +
                "                    },\n" +
                "                    \"projectCategory\": {\n" +
                "                        \"type\": \"keyword\"\n" +
                "                    },\n" +
                "                    \"biddingScope\": {\n" +
                "                        \"type\": \"keyword\"\n" +
                "                    },\n" +
                "                    \"licensingRequirements\": {\n" +
                "                        \"type\": \"keyword\"\n" +
                "                    },\n" +
                "                    \"bidLimitPrice\": {\n" +
                "                        \"type\": \"keyword\"\n" +
                "                    },\n" +
                "                    \"bidBond\": {\n" +
                "                        \"type\": \"keyword\"\n" +
                "                    },\n" +
                "                    \"contactInformation\": {\n" +
                "                        \"type\": \"keyword\"\n" +
                "                    },\n" +
                "                    \"demandUnit\": {\n" +
                "                        \"type\": \"keyword\"\n" +
                "                    },\n" +
                "                    \"materialName\": {\n" +
                "                        \"type\": \"keyword\"\n" +
                "                    },\n" +
                "                    \"materialDesc\": {\n" +
                "                        \"type\": \"keyword\"\n" +
                "                    },\n" +
                "                    \"location\": {\n" +
                "                        \"type\": \"keyword\"\n" +
                "                    },\n" +
                "                    \"remark\": {\n" +
                "                        \"type\": \"text\"\n" +
                "                    },\n" +
                "                    \"projectDetail\": {\n" +
                "                        \"type\": \"text\"\n" +
                "                    }\n" +
                "                }\n" +
                "            },\n" +
                "            \"fileAcquireEndTime\": {\n" +
                "                \"type\": \"date\",\n" +
                "                \"format\": \"yyyy-MM-dd||yyyy-MM-dd HH:mm:ss\"\n" +
                "            },\n" +
                "            \"fileName\": {\n" +
                "                \"type\": \"text\"\n" +
                "            },\n" +
                "            \"flag\": {\n" +
                "                \"type\": \"byte\",\n" +
                "                \"index\": false\n" +
                "            },\n" +
                "            \"id\": {\n" +
                "                \"type\": \"long\"\n" +
                "            },\n" +
                "            \"insertTime\": {\n" +
                "                \"type\": \"date\",\n" +
                "                \"format\": \"yyyy-MM-dd||yyyy-MM-dd HH:mm:ss\"\n" +
                "            },\n" +
                "            \"itemNumber\": {\n" +
                "                \"type\": \"keyword\"\n" +
                "            },\n" +
                "            \"labels\": {\n" +
                "                \"type\": \"keyword\"\n" +
                "            },\n" +
                "            \"noticeUrl\": {\n" +
                "                \"type\": \"keyword\"\n" +
                "            },\n" +
                "            \"openTime\": {\n" +
                "                \"type\": \"date\",\n" +
                "                \"format\": \"yyyy-MM-dd||yyyy-MM-dd HH:mm:ss\"\n" +
                "            },\n" +
                "            \"publishTime\": {\n" +
                "                \"type\": \"date\",\n" +
                "                \"format\": \"yyyy-MM-dd||yyyy-MM-dd HH:mm:ss\"\n" +
                "            },\n" +
                "            \"city\": {\n" +
                "                \"type\": \"keyword\"\n" +
                "            },\n" +
                "            \"resultType\": {\n" +
                "                \"type\": \"byte\"\n" +
                "            },\n" +
                "            \"type\": {\n" +
                "                \"type\": \"keyword\"\n" +
                "            }\n" +
                "        }\n" +
                "    }\n" +
                "}", XContentType.JSON);
        // 2、客户端执行请求 IndicesClient,请求后获得响应
        CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse);
    }

    // 测试获取索引,判断其是否存在
    @Test
    void testExistIndex() throws IOException {
        GetIndexRequest request = new GetIndexRequest("biding");
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

    // 测试删除索引
    @Test
    void testDeleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("biding");
        // 删除
        AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
        System.out.println(delete.isAcknowledged());
    }

    // 测试添加文档
    @Test
    void testAddDocument() throws IOException {
        // for(int i = 0; i < 10; i++)
        AnnouncementReq announcementReq = new AnnouncementReq();
        announcementReq.setPageNo(1);
        announcementReq.setAddressId(1);
        announcementReq.setPageSize(3);
        Page<AnnouncementPo> announcementPoPage = announcementDao.pageAnnouncement(announcementReq);
        if(announcementPoPage.getRecords() == null || announcementPoPage.getRecords().size() <= 0) return;

        List<AnnouncementAndItemDto> announcementAndItemDtos = announcementPoPage.getRecords().stream().map(item -> {
            AnnouncementAndItemDto announcementAndItemDto = new AnnouncementAndItemDto();
            BeanUtils.copyProperties(item, announcementAndItemDto);
            return announcementAndItemDto;
        }).collect(Collectors.toList());
        List<String> announcementNumbers = announcementAndItemDtos.stream().map(AnnouncementAndItemDto::getItemNumber).collect(Collectors.toList());

        // 组织BidItem和公告的对应关系
        HashMap<String, List<BidItemPo>> announcementNumberBidItemsMap = new HashMap<>();
        bidItemDao.lambdaQuery().in(BidItemPo::getAnnouncementNumber, announcementNumbers).list().forEach(item -> {
            List<BidItemPo> orDefault = announcementNumberBidItemsMap.getOrDefault(item.getAnnouncementNumber(), new ArrayList<>());
            orDefault.add(item);
            announcementNumberBidItemsMap.put(item.getAnnouncementNumber(), orDefault);
        });

        announcementAndItemDtos.forEach(item -> {
            item.setBidItems(announcementNumberBidItemsMap.get(item.getItemNumber()));
        });

        // 批处理请求
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("30s");
        for (int i = 0; i < announcementAndItemDtos.size(); i++) {
            // 批量更新和批量删除,就在这里修改对应的请求就可以了
            bulkRequest.add(new IndexRequest("biding").id("" + (i + 1))
                .source(JSON.toJSONString(announcementAndItemDtos.get(i)), XContentType.JSON));
        }

        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulkResponse.hasFailures()); // 是否失败,返回 false 代表 成功!
        System.out.println(bulkResponse.status());
        System.out.println(bulkResponse.toString());
    }

    // 获取文档,判断是否存在 get /index/doc/1
    @Test
    void testIsExists() throws IOException {
        GetRequest getRequest = new GetRequest("kuang_index", "1");
        // 不获取返回的 _source 的上下文了
        getRequest.fetchSourceContext(new FetchSourceContext(false));
        getRequest.storedFields("_none_");
        boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

    // 获得文档的信息
    @Test
    void testGetDocument() throws IOException {
        GetRequest getRequest = new GetRequest("kuang_index", "1");
        GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
        System.out.println(getResponse.getSourceAsString()); // 打印文档的内容
        System.out.println(getResponse); // 返回的全部内容和命令式一样的
    }

    // 更新文档的信息
    @Test
    void testUpdateRequest() throws IOException {
        UpdateRequest updateRequest = new UpdateRequest("kuang_index", "1");
        updateRequest.timeout("1s");
        // User user = new User("狂神说Java", 18);
        // updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);
        UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
        System.out.println(updateResponse.status());
    }

    // 删除文档记录
    @Test
    void testDeleteRequest() throws IOException {
        DeleteRequest request = new DeleteRequest("kuang_index", "1");
        request.timeout("1s");
        DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
        System.out.println(deleteResponse.status());
    }

    // 特殊的,真的项目一般都会批量插入数据!
    @Test
    void testBulkRequest() throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("10s");
        // ArrayList<User> userList = new ArrayList<>();
        // userList.add(new User("kuangshen1", 3));
        // userList.add(new User("kuangshen2", 3));
        // userList.add(new User("kuangshen3", 3));
        // userList.add(new User("qinjiang1", 3));
        // userList.add(new User("qinjiang1", 3));
        // userList.add(new User("qinjiang1", 3));
        // 批处理请求
        // for (int i = 0; i < userList.size(); i++) {
        //     // 批量更新和批量删除,就在这里修改对应的请求就可以了
        //     bulkRequest.add(new IndexRequest("kuang_index").id("" + (i + 1))
        //         .source(JSON.toJSONString(userList.get(i)), XContentType.JSON));
        // }
        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulkResponse.hasFailures()); // 是否失败,返回 false 代表 成功!
    }

    // 查询
    // SearchRequest 搜索请求
    // SearchSourceBuilder 条件构造
    // HighlightBuilder 构建高亮
    // TermQueryBuilder 精确查询
    // MatchAllQueryBuilder
    // xxx QueryBuilder 对应我们刚才看到的命令!
    @Test
    void testSearch() throws IOException {
        SearchRequest searchRequest = new SearchRequest("kuang_index");
        // 构建搜索条件
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.highlighter();
        // 查询条件,我们可以使用 QueryBuilders 工具来实现
        // QueryBuilders.termQuery 精确
        // QueryBuilders.matchAllQuery() 匹配所有
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "qinjiang1");
        // MatchAllQueryBuilder matchAllQueryBuilder =
        QueryBuilders.matchAllQuery();
        sourceBuilder.query(termQueryBuilder);
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(JSON.toJSONString(searchResponse.getHits()));
        System.out.println("=================================");
        for (SearchHit documentFields : searchResponse.getHits().getHits()) {
            System.out.println(documentFields.getSourceAsMap());
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

脸是真的白

如果对你有用的话,可以支持一下

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

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

打赏作者

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

抵扣说明:

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

余额充值