七仔的博客

七仔的博客GithubPages分博

0%

springboot集成Elasticsearch进行操作

springboot集成Elasticsearch进行操作

springboot集成Elasticsearch进行操作

使用SpringBootStarterDataElasticsearch进行操作

Maven引入

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

配置连接信息

1
2
3
4
5
6
7
8
9
spring:
elasticsearch:
rest:
# 地址
uris: http://*.*.*.*:host
# 用户名
username: *
# 密码
password: *

这里要说一下,9200/9300端口最好不要用,换成别的端口,然后用户名密码的限制一定要有,不然数据可能会被删除(详见另一篇博客:服务器被meow攻击,Elasticsearch数据被删除
x-pake在elasticsearch的7.0.0没有免费开放,7.3.1是默认有的,中间几个版本没有试过

实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;

import java.util.List;

@Data
@NoArgsConstructor
@Accessors(chain = true)
@Document(indexName = "test_index", shards = 1, replicas = 0)
public class PatientRecipeInfo {

@Id
private String id;

@Field
private Object content;

@Field
private Long time;
}

数据持久层

1
2
3
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface MyElasticRepository extends ElasticsearchRepository<DemoEntity, Long>  {
}

服务层

1
2
3
4
5
6
7
8
9
10
11
12
public interface ESYPRelateDealService {

/**
* 根据名称查询排序
**/
Page<DemoEntity> sort(String test, Integer page, Integer size);

/**
* 保存数据
**/
boolean save(DemoEntity demoEntity);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Service
public class ESYPRelateDealServiceImpl implements ESYPRelateDealService {

@Autowired
private MyElasticRepository myElasticRepository;

/**
* 根据名称查询分页
**/
Page<DemoEntity> sort(String test, Integer page, Integer size){
Sort sort = Sort.by(Sort.Order.desc(test))
Pageable pageable = PageRequest.of(page, size, sort);
Page<DemoEntity> page = myElasticRepository.findAll(pageable);
return page;
}

/**
* 保存数据
**/
boolean save(DemoEntity demoEntity){
myElasticRepository.save(demoEntity);
}
}

使用High Level Rest Client进行操作

引入Maven

1
2
3
4
5
<dependency>
<groupId>org.elasticsearch.module</groupId>
<artifactId>reindex</artifactId>
<version>2.4.6</version>
</dependency>

配置连接信息

1
2
3
4
5
6
7
8
9
spring:
elasticsearch:
rest:
# 地址
uris: http://*.*.*.*:host
# 用户名
username: *
# 密码
password: *

引入配置信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
public class ESProperties {

@Value("${spring.elasticsearch.rest.uris}")
private String uris;

@Value("${spring.elasticsearch.rest.username}")
private String username;

@Value("${spring.elasticsearch.rest.password}")
private String password;
}

服务层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class ESDealServiceImpl {
@Autowired
ESProperties esProperties;

public RestHighLevelClient client;

//ESHighLevel初始化
@PostConstruct
public void init() {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(esProperties.getUsername(), esProperties.getPassword()));
client = new RestHighLevelClient(
RestClient
.builder(HttpHost.create(esProperties.getUris()))
.setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.disableAuthCaching();
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
})
);
}

}

未完待续。。。

此为博主副博客,留言请去主博客,转载请注明出处:https://www.baby7blog.com/myBlog/95.html

欢迎关注我的其它发布渠道