elasticsearch数据查询,springboot整合es查询数据
一.导言二守则实务2.1。导入依赖2.2。配置环境变量2.3。创建elasticsearch的配置类2.4。指数管理。文件管理。总结。
00-1010在上一篇ElasticSearch文章中,我们详细介绍了ElasticSearch的各种API的使用方法。
在实际的项目开发过程中,我们通常基于一些主流的框架平台进行技术开发,比如SpringBoot。今天,我们与SpringBoot整合。
以ElasticSearch为例,给大家详细介绍一下ElasticSearch的使用方法!
SpringBoot与ElasticSearch连接,主流方式如下
方法一:通过弹性传输客户端客户端连接es服务器,底层通过基于TCP协议的传输模块与远程es服务器通信。但从V7.0开始官方就不建议使用了,从V8.0正式下架.方法二:通过Elastic Java底层Rest客户端连接es服务器,底层通过基于HTTP协议的restful API与远程es服务器通信,只提供最简单最基础的API,类似于上一篇文章给大家介绍的API操作逻辑。方法三:通过弹性Java高层rest客户端连接es服务器,底层基于弹性Java低层rest客户端封装,提供更高级的API,与弹性传输客户端的接口和参数一致。是官方推荐的es客户端。模式四:通过JestClient连接es服务器,jest client是开源社区基于HTTP协议开发的es客户端。官方宣称界面和代码设计比ES提供的Rest客户端更简洁、更合理、更易用,并且具有一定的ES服务器版本兼容性,但更新速度不是很快。目前ES版本已经发布到V7.9,但是JestClient只支持ES从v1.0到v6.x的版本,还有一个地方需要大家注意,就是版本号的兼容性!
在开发过程中,要特别注意客户端和服务器的版本号,尽可能保持一致。例如,如果服务器es的版本号是6.8.2,则连接到es的客户端的版本号应该是6.8.2。即使由于项目原因不能保持一致,客户端的版本号也必须是6.0.0 ~ 6.8.2,并且不能超过服务器的版本号,这样客户端
为什么要这么做?主要原因是es的服务器端,高配版和低配版不兼容;es6和es7的一些API请求参数结构差异较大,所以客户端和服务器的版本号要尽量保持一致。
废话不多说,直接去码!
00-1010本文使用的SpringBoot版本号为2.1.0.RELEASE,server es版本号为6.8.2,客户端推荐的Elastic Java高级Rest客户端版本号为6.4.2,方便与SpringBoot版本兼容。
目录
!-elastic search-dependency groupIdorg.elasticsearch/groupId artifactIdelasticsearch/artifactId版本6.4.2/version/dependency groupIdorg.elasticsearch.client/groupId artifactIdelasticsearch-rest-client/artifactId版本6 . 4 . 2/version/dependency dependency groupIdorg.elasticsearch.client/groupId artifactIdelasticsearch-rest-high-level-client/artifactId版本6 . 4 . 2/version/dependency
00-1010 at应用程序.属性
es全局配置文件中,配置elasticsearch自定义环境变量。
elasticsearch.scheme=httpelasticsearch.address=127.0.0.1:9200elasticsearch.userName=elasticsearch.userPwd=elasticsearch.socketTimeout=5000elasticsearch.connectTimeout=5000elasticsearch.connectionRequestTimeout=5000
2.3、创建 elasticsearch 的 config 类
@Configurationpublic class ElasticsearchConfiguration { private static final Logger log = LoggerFactory.getLogger(ElasticsearchConfiguration.class); private static final int ADDRESS_LENGTH = 2; @Value("${elasticsearch.scheme:http}") private String scheme; @Value("${elasticsearch.address}") private String address; @Value("${elasticsearch.userName}") private String userName; @Value("${elasticsearch.userPwd}") private String userPwd; @Value("${elasticsearch.socketTimeout:5000}") private Integer socketTimeout; @Value("${elasticsearch.connectTimeout:5000}") private Integer connectTimeout; @Value("${elasticsearch.connectionRequestTimeout:5000}") private Integer connectionRequestTimeout; /** * 初始化客户端 * @return */ @Bean(name = "restHighLevelClient") public RestHighLevelClient restClientBuilder() { HttpHost[] hosts = Arrays.stream(address.split(",")) .map(this::buildHttpHost) .filter(Objects::nonNull) .toArray(HttpHost[]::new); RestClientBuilder restClientBuilder = RestClient.builder(hosts); // 异步参数配置 restClientBuilder.setHttpClientConfigCallback(httpClientBuilder -> { httpClientBuilder.setDefaultCredentialsProvider(buildCredentialsProvider()); return httpClientBuilder; }); // 异步连接延时配置 restClientBuilder.setRequestConfigCallback(requestConfigBuilder -> { requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeout); requestConfigBuilder.setSocketTimeout(socketTimeout); requestConfigBuilder.setConnectTimeout(connectTimeout); return requestConfigBuilder; }); return new RestHighLevelClient(restClientBuilder); } /** * 根据配置创建HttpHost * @param s * @return */ private HttpHost buildHttpHost(String s) { String[] address = s.split(":"); if (address.length == ADDRESS_LENGTH) { String ip = address[0]; int port = Integer.parseInt(address[1]); return new HttpHost(ip, port, scheme); } else { return null; } } /** * 构建认证服务 * @return */ private CredentialsProvider buildCredentialsProvider(){ final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, userPwd)); return credentialsProvider; }}
至此,客户端配置完毕,项目启动的时候,会自动注入到Spring的ioc容器里面。
2.4、索引管理
es 中最重要的就是索引库,客户端如何创建呢?请看下文!
创建索引
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class IndexJunit { @Autowired private RestHighLevelClient client; /** * 创建索引(简单模式) * @throws IOException */ @Test public void createIndex() throws IOException { CreateIndexRequest request = new CreateIndexRequest("cs_index"); CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT); System.out.println(response.isAcknowledged()); } /** * 创建索引(复杂模式) * 可以直接把对应的文档结构也一并初始化 * @throws IOException */ @Test public void createIndexComplete() throws IOException { CreateIndexRequest request = new CreateIndexRequest(); //索引名称 request.index("cs_index"); //索引配置 Settings settings = Settings.builder() .put("index.number_of_shards", 3) .put("index.number_of_replicas", 1) .build(); request.settings(settings); //映射结构字段 Map<String, Object> properties = new HashMap(); properties.put("id", ImmutableBiMap.of("type", "text")); properties.put("name", ImmutableBiMap.of("type", "text")); properties.put("sex", ImmutableBiMap.of("type", "text")); properties.put("age", ImmutableBiMap.of("type", "long")); properties.put("city", ImmutableBiMap.of("type", "text")); properties.put("createTime", ImmutableBiMap.of("type", "long")); Map<String, Object> mapping = new HashMap<>(); mapping.put("properties", properties); //添加一个默认类型 System.out.println(JSON.toJSONString(request)); request.mapping("_doc",mapping); CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT); System.out.println(response.isAcknowledged()); }}
删除索引
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class IndexJunit { @Autowired private RestHighLevelClient client; /** * 删除索引 * @throws IOException */ @Test public void deleteIndex() throws IOException { DeleteIndexRequest request = new DeleteIndexRequest("cs_index1"); AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT); System.out.println(response.isAcknowledged()); }}
查询索引
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class IndexJunit { @Autowired private RestHighLevelClient client; /** * 查询索引 * @throws IOException */ @Test public void getIndex() throws IOException { // 创建请求 GetIndexRequest request = new GetIndexRequest(); request.indices("cs_index"); // 执行请求,获取响应 GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT); System.out.println(response.toString()); }}
查询索引是否存在
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class IndexJunit { @Autowired private RestHighLevelClient client; /** * 检查索引是否存在 * @throws IOException */ @Test public void exists() throws IOException { // 创建请求 GetIndexRequest request = new GetIndexRequest(); request.indices("cs_index"); // 执行请求,获取响应 boolean response = client.indices().exists(request, RequestOptions.DEFAULT); System.out.println(response); }}
查询所有的索引名称
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class IndexJunit { @Autowired private RestHighLevelClient client; /** * 查询所有的索引名称 * @throws IOException */ @Test public void getAllIndices() throws IOException { GetAliasesRequest request = new GetAliasesRequest(); GetAliasesResponse response = client.indices().getAlias(request,RequestOptions.DEFAULT); Map<String, Set<AliasMetaData>> map = response.getAliases(); Set<String> indices = map.keySet(); for (String key : indices) { System.out.println(key); } }}
查询索引映射字段
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class IndexJunit { @Autowired private RestHighLevelClient client; /** * 查询索引映射字段 * @throws IOException */ @Test public void getMapping() throws IOException { GetMappingsRequest request = new GetMappingsRequest(); request.indices("cs_index"); request.types("_doc"); GetMappingsResponse response = client.indices().getMapping(request, RequestOptions.DEFAULT); System.out.println(response.toString()); }}
添加索引映射字段
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class IndexJunit { @Autowired private RestHighLevelClient client; /** * 添加索引映射字段 * @throws IOException */ @Test public void addMapping() throws IOException { PutMappingRequest request = new PutMappingRequest(); request.indices("cs_index"); request.type("_doc"); //添加字段 Map<String, Object> properties = new HashMap(); properties.put("accountName", ImmutableBiMap.of("type", "keyword")); Map<String, Object> mapping = new HashMap<>(); mapping.put("properties", properties); request.source(mapping); PutMappingResponse response = client.indices().putMapping(request, RequestOptions.DEFAULT); System.out.println(response.isAcknowledged()); }}
2.5、文档管理
所谓文档,就是向索引里面添加数据,方便进行数据查询,详细操作内容,请看下文!
添加文档
ublic class UserDocument { private String id; private String name; private String sex; private Integer age; private String city; private Date createTime; //省略get、set...}
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class DocJunit { @Autowired private RestHighLevelClient client; /** * 添加文档 * @throws IOException */ @Test public void addDocument() throws IOException { // 创建对象 UserDocument user = new UserDocument(); user.setId("1"); user.setName("里斯"); user.setCity("武汉"); user.setSex("男"); user.setAge(20); user.setCreateTime(new Date()); // 创建索引,即获取索引 IndexRequest request = new IndexRequest(); // 外层参数 request.id("1"); request.index("cs_index"); request.type("_doc"); request.timeout(TimeValue.timeValueSeconds(1)); // 存入对象 request.source(JSON.toJSONString(user), XContentType.JSON); // 发送请求 System.out.println(request.toString()); IndexResponse response = client.index(request, RequestOptions.DEFAULT); System.out.println(response.toString()); }}
更新文档
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class DocJunit { @Autowired private RestHighLevelClient client; /** * 更新文档(按需修改) * @throws IOException */ @Test public void updateDocument() throws IOException { // 创建对象 UserDocument user = new UserDocument(); user.setId("2"); user.setName("程咬金"); user.setCreateTime(new Date()); // 创建索引,即获取索引 UpdateRequest request = new UpdateRequest(); // 外层参数 request.id("2"); request.index("cs_index"); request.type("_doc"); request.timeout(TimeValue.timeValueSeconds(1)); // 存入对象 request.doc(JSON.toJSONString(user), XContentType.JSON); // 发送请求 System.out.println(request.toString()); UpdateResponse response = client.update(request, RequestOptions.DEFAULT); System.out.println(response.toString()); }}
删除文档
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class DocJunit { @Autowired private RestHighLevelClient client; /** * 删除文档 * @throws IOException */ @Test public void deleteDocument() throws IOException { // 创建索引,即获取索引 DeleteRequest request = new DeleteRequest(); // 外层参数 request.id("1"); request.index("cs_index"); request.type("_doc"); request.timeout(TimeValue.timeValueSeconds(1)); // 发送请求 System.out.println(request.toString()); DeleteResponse response = client.delete(request, RequestOptions.DEFAULT); System.out.println(response.toString()); }}
查询文档是不是存在
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class DocJunit { @Autowired private RestHighLevelClient client; /** * 查询文档是不是存在 * @throws IOException */ @Test public void exists() throws IOException { // 创建索引,即获取索引 GetRequest request = new GetRequest(); // 外层参数 request.id("3"); request.index("cs_index"); request.type("_doc"); // 发送请求 System.out.println(request.toString()); boolean response = client.exists(request, RequestOptions.DEFAULT); System.out.println(response); }}
通过 ID 查询指定文档
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class DocJunit { @Autowired private RestHighLevelClient client; /** * 通过ID,查询指定文档 * @throws IOException */ @Test public void getById() throws IOException { // 创建索引,即获取索引 GetRequest request = new GetRequest(); // 外层参数 request.id("1"); request.index("cs_index"); request.type("_doc"); // 发送请求 System.out.println(request.toString()); GetResponse response = client.get(request, RequestOptions.DEFAULT); System.out.println(response.toString()); }}
批量添加文档
@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ElasticSearchApplication.class)public class DocJunit { @Autowired private RestHighLevelClient client; /** * 批量添加文档 * @throws IOException */ @Test public void batchAddDocument() throws IOException { // 批量请求 BulkRequest bulkRequest = new BulkRequest(); bulkRequest.timeout(TimeValue.timeValueSeconds(10)); // 创建对象 List<UserDocument> userArrayList = new ArrayList<>(); userArrayList.add(new UserDocument("张三", "男", 30, "武汉")); userArrayList.add(new UserDocument("里斯", "女", 31, "北京")); userArrayList.add(new UserDocument("王五", "男", 32, "武汉")); userArrayList.add(new UserDocument("赵六", "女", 33, "长沙")); userArrayList.add(new UserDocument("七七", "男", 34, "武汉")); // 添加请求 for (int i = 0; i < userArrayList.size(); i++) { userArrayList.get(i).setId(String.valueOf(i)); IndexRequest indexRequest = new IndexRequest(); // 外层参数 indexRequest.id(String.valueOf(i)); indexRequest.index("cs_index"); indexRequest.type("_doc"); indexRequest.timeout(TimeValue.timeValueSeconds(1)); indexRequest.source(JSON.toJSONString(userArrayList.get(i)), XContentType.JSON); bulkRequest.add(indexRequest); } // 执行请求 BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT); System.out.println(response.status()); }}
三、小结
本文主要围绕 SpringBoot 整合 ElasticSearch 接受数据的插入和搜索使用技巧,在实际的使用过程中,版本号尤其的重要,不同版本的 es,对应的 api 是不一样的。
以上就是SpringBoot+Elasticsearch实现数据搜索的方法详解的详细内容,更多关于SpringBoot Elasticsearch数据搜索的资料请关注盛行IT其它相关文章!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。