SpringBoot整合ElasticSearch7.6.2
SpringBoot整合ElasticSearch7.6.2
0、前置條件
之前使用SpringBoot整合過ES的低版本,ES各個大版本之間有較大的變化。
ES中值得注意的事項:
type逐漸移除,預計版本8中將消失
head外掛在高等級的版本中,不支援直接安裝,需要nodejs支援。
SpringBoot與Es的整合,需要注意版本支援,且在7.x的ES版本中客戶端更新為 High Level REST Client,在 SpringBoot中的ElasticSearchTemplate過時,建議使用 High Level REST Client或者ElasticSearchRestTemplate。
版本如果不適配,也無法執行。
從Spring的官網可以看到版本的資訊:
這次使用的版本是:
SpringBoot 2.3.0
ES7.6.2
這個SpringBoot版本中自帶的是7.6.2版本的客戶端,依賴如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
ES資料如下:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "boot",
"_type" : "user",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "Smish",
"age" : 16,
"gender" : "male",
"desc" : [
"產品經理",
"藝術家"
]
}
},
{
"_index" : "boot",
"_type" : "user",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "Jaskson",
"age" : 18,
"gender" : "male",
"desc" : [
"碼農",
"直男",
"女裝大佬"
]
}
}
]
}
}
1、使用High Level REST Client
高階客戶端是ES提供的客戶端,支援多種語言。
在SpringBoot中使用只需要使用一個配置類設定好引數即可。
直接繼承Springboot依賴中提供的配置類或者自建,保證有高階客戶端物件即可。
@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {
@Override
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("localhost:9200")
.build();
return RestClients.create(clientConfiguration).rest();
}
}
這個客戶端提供了非常多的api供使用,測試如下:
//判斷索引是否存在:
@SpringBootTest
@RunWith(SpringRunner.class)
public class ElasticTest {
@Autowired
RestHighLevelClient restHighLevelClient;
@Test
public void test() throws IOException {
final GetIndexRequest indexRequest = new GetIndexRequest("boot");
final boolean exists = restHighLevelClient.indices().exists(indexRequest,RequestOptions.DEFAULT)
System.out.println(exists);
}
}
//通過id或者索引查詢資料:
@Test
public void test01() throws Exception {
final GetRequest request = new GetRequest("boot", "1");
final GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
final String string = response.toString();
System.out.println(string);
System.out.println("-------------------------");
}
{"_index":"boot","_type":"_doc","_id":"1","_version":2,"_seq_no":4,"_primary_term":3,"found":true,"_source":{"name":"Jaskson","age":18,"gender":"male","desc":["碼農","直男","女裝大佬"]}}
@Test
public void test02() throws Exception {
SearchResponse search = restHighLevelClient.search(new SearchRequest("boot"), RequestOptions.DEFAULT);
System.out.println(search);
}
高階客戶端內建api較多,這些操作也可以使用ElasticsearchRestTemplate操作。
2、ElasticsearchRestTemplate操作
ElasticsearchRestTemplate是使用高階 REST 客戶端實現的介面,ElasticsearchTemplate自版本 4.0 開始棄用,不過其實操作差別不大。
首先需要一個實體類,加入註解。
@Document(indexName = "boot")
public class User {
@Id
private int id;
@Field
private String name;
@Field
private Integer age;
@Field
private String gender;
@Field
private String desc;
public User(int id, String name, Integer age, String gender, String desc) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.desc = desc;
}
public User(){};
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", desc='" + desc + '\'' +
'}';
}
}
測試查詢:
@Autowired
ElasticsearchRestTemplate template;
@Test
public void test(){
IndexOperations ops = template.indexOps(User.class);
boolean exists = ops.exists();
System.out.println(exists);
//通過id查詢,已經廢棄
User user = template.queryForObject(GetQuery.getById("1"), User.class);
System.out.println(user);
//和上一樣
User user1 = template.get("1", User.class);
System.out.println(user1);
//查詢所有
final SearchHits<User> search = template.search(Query.findAll(), User.class);
final Iterator<SearchHit<User>> iterator = search.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
相關文章
- SpringBoot整合系列-整合JPASpring Boot
- SpringBoot 整合 rabbitmqSpring BootMQ
- SpringBoot 整合 elkSpring Boot
- SpringBoot 整合 elasticsearchSpring BootElasticsearch
- SpringBoot 整合 apolloSpring Boot
- springboot整合redis?Spring BootRedis
- SpringBoot整合RedisSpring BootRedis
- flowable 整合 springbootSpring Boot
- SpringBoot整合MQTTSpring BootMQQT
- ElasticSearch 整合 SpringBootElasticsearchSpring Boot
- SpringBoot整合ESSpring Boot
- Springboot整合pagehelperSpring Boot
- springBoot整合thymeleafSpring Boot
- SpringBoot 整合 RedisSpring BootRedis
- SpringBoot整合NacosSpring Boot
- SpringBoot 整合 dockerSpring BootDocker
- Springboot整合RabbitMQSpring BootMQ
- springBoot整合flowableSpring Boot
- Springboot整合MyabitsSpring Boot
- springBoot 整合 mybatisSpring BootMyBatis
- SpringBoot整合dubboSpring Boot
- SpringBoot整合elasticsearchSpring BootElasticsearch
- RocketMQ整合SpringBootMQSpring Boot
- 【SpringBoot】整合RedisSpring BootRedis
- springboot 整合jeagerSpring Boot
- Springboot整合MybatisSpring BootMyBatis
- Windows下安裝ElasticSearch7.6.2WindowsElasticsearch
- SpringBoot(19)---SpringBoot整合ApolloSpring Boot
- SpringBoot(17)---SpringBoot整合RocketMQSpring BootMQ
- SpringBoot(十六)_springboot整合JasperReSpring Boot
- SpringBoot整合系列-整合H2Spring Boot
- SpringBoot整合系列–整合MyBatis-plusSpring BootMyBatis
- Java SpringBoot 整合 ActiveMQJavaSpring BootMQ
- Java SpringBoot 整合 RabbitMQJavaSpring BootMQ
- springboot+mybatis整合Spring BootMyBatis
- SpringBoot | 3.2 整合MyBatisSpring BootMyBatis
- springboot整合druidSpring BootUI
- SpringBoot整合測試Spring Boot