Loading

SpringBoot整合ElasticSearch

齐先生,我没有对这个世界失望,我只是对自己很失望。

  1. 引入对应的pom依赖(使用es-rest-high-level-client来调用es)

<!-- es -->
       <dependency>
           <groupId>org.elasticsearch.client</groupId>
           <artifactId>elasticsearch-rest-high-level-client</artifactId>
           <version>${elasticsearch.version}</version>
           <exclusions>
               <exclusion>
                   <groupId>org.elasticsearch.client</groupId>
                   <artifactId>elasticsearch-rest-client</artifactId>
               </exclusion>
               <exclusion>
                   <groupId>org.elasticsearch</groupId>
                   <artifactId>elasticsearch</artifactId>
               </exclusion>
           </exclusions>
       </dependency>
       <dependency>
           <groupId>org.elasticsearch</groupId>
           <artifactId>elasticsearch</artifactId>
           <version>7.17.6</version>
       </dependency>
       <dependency>
           <groupId>org.elasticsearch.client</groupId>
           <artifactId>elasticsearch-rest-client</artifactId>
           <version>7.17.6</version>
       </dependency>

注意:这里需要观察里面对应es的版本,如果与你服务器上安装的es版本不一样的话,会报找不到包的异常。

如:

Java ElasticsearchException.toXContent

查看版本对不对应看引入elasticsearch-rest-high-level-client,如果不对应排除掉,重新引入对应的。

  1. 设置配置类

/**
* @Description es请求配置类
* es添加了安全访问规则,访问es需要添加一个安全头,就可以通过requestOptions设置
* @Author lh
* @Date 2022/9/21 21:16
*/
@Configuration
public class GuliESConfig {

   @Value("${es.host}")
   private String ES_HOST;

   public static final RequestOptions COMMON_OPTIONS;

   static {
       RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
       COMMON_OPTIONS = builder.build();
  }

   @Bean
   @SuppressWarnings(value = "all")
   public RestHighLevelClient esRestClient() {
       RestClientBuilder builder = null;
       // 可以指定多个es
       builder = RestClient.builder(new HttpHost(ES_HOST, 9200, "http"));

       return new RestHighLevelClient(builder);
  }
}
  1. 测试保存数据

@SpringBootTest
public class GulimallSearchTest {

   @Autowired
   @SuppressWarnings(value = "all")
   private RestHighLevelClient esRestClient;

   @Test
   public void indexData() throws IOException {
       // 设置索引
       IndexRequest indexRequest = new IndexRequest("user");
       indexRequest.id("1");

       User user = new User();
       user.setUserName("李嘉图");
       user.setAge(18);
       user.setGender("男");
       String jsonString = JSON.toJSONString(user);

       // 设置要保存的内容,指定数据和类型
       indexRequest.source(jsonString, XContentType.JSON);

       // 执行创建索引和保存数据
       IndexResponse index = esRestClient.index(indexRequest, GuliESConfig.COMMON_OPTIONS);
       System.out.println(index);
  }
}

结果:

IndexResponse[index=user,type=_doc,id=1,version=3,result=updated,seqNo=2,primaryTerm=1,shards={"total":2,"successful":1,"failed":0}]
  1. 测试获取数据

   @Test
   public void find() throws IOException {
       // 创建索引请求
       SearchRequest searchRequest = new SearchRequest();
       searchRequest.indices("user");

       // 设置检索条件
       SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//       sourceBuilder.query();
//       sourceBuilder.from();
//       sourceBuilder.size();
//       sourceBuilder.aggregation();
       sourceBuilder.query(QueryBuilders.matchQuery("userName", "李嘉图"));
       System.out.println(sourceBuilder.toString());
       searchRequest.source(sourceBuilder);

       // 执行检索
       SearchResponse searchResponse = esRestClient.search(searchRequest, GuliESConfig.COMMON_OPTIONS);
       System.out.println(searchResponse);
  }

结果:

{"took":24,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":0.8630463,"hits":[{"_index":"user","_type":"_doc","_id":"1","_score":0.8630463,"_source":{"age":18,"gender":"男","userName":"李嘉图"}}]}}
 
posted @ 2022-09-22 16:16  你比从前快乐;  阅读(155)  评论(0编辑  收藏  举报