网上很多都过时了,我总结了一下新版的。
首先,springboot配置elasticsearch,不要再用下面这写了,jest完全过时了,别再用了
现在spring官方推荐我们用High Level REST Client
来配置
具体配置如下
@Configuration
public class ElasticSearchConfig extends AbstractElasticsearchConfiguration {
@Bean
public RestHighLevelClient restHighLevelClient(){
ClientConfiguration configuration = ClientConfiguration.builder(
)
.connectedTo("192.168.100.126:9200")
//.withConnectTimeout(Duration.ofSeconds(5))
//.withSocketTimeout(Duration.ofSeconds(3))
//.useSsl()
//.withDefaultHeaders(defaultHeaders)
//.withBasicAuth(username, password)
// ... other options
.build();
RestHighLevelClient client = RestClients.create(configuration).rest();
return client;
}
}
Transport client
将会再ES8.0中被弃用。可以看到ElasticsearchTemplate
是基于Transport client
//6.0版本后,一个index只有一个type了,这里type也被移除。ES默认type为“_doc”
@Document(indexName = "at")
public class Book {
private Integer id;
private String bookname;
private String author;
//get set 构造函数,toString的得写上,这里太长就删了先
}
操作ES有两大类
先用Elasticsearch Repositories
,注意接口与接口的关系是extends
@Repository
public interface Bookrepository extends ElasticsearchRepository<Book,Integer> {
//ElasticsearchCrudRepository 已经过时
List<Book> findBookById(int i);
}
Elasticsearch Repositories
提供了许多关键字,来帮助我们实现方法。我们只需要写抽象方法即可,Elasticsearch Repositories
会根据方法名自动我们为我们实现,比如上面find
和By
就是关键字。
我们需要在springboot主配置类上加上注解@EnableElasticsearchRepositories
可以使用Elasticsearch提供的的关键字(方法)列表
,常用关键字如下
然后测试一下
@SpringBootTest
class ElasticsearchApplicationTests {
@Autowired
Bookrepository bookrepository;
@Test
void contextLoads() {
Book book=new Book(1,"西游记","吴承恩");
bookrepository.save(book);
}
@Test
void testRepositories(){
//查询
//Elasticsearch Repositories提供and,by等一大堆关键字来连接JAVABEAN属性,我们写接口,他自动变成为实现类。
List<Book> bookById = bookrepository.findBookById(1);
System.out.println(bookById.get(0));
}
}
ElasticsearchTemplate是基于Transport client
的,Transport client
将会再ES8.0中被弃用,用谁不用我多说了吧。
使用Elasticsearch Operations我们需要修改上面的配置类,需要继承AbstractElasticsearchConfiguration
,因为基类AbstractElasticsearchConfiguration
已经提供了ElasticsearchRestTemplate
这个bean
@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {
@Override
public RestHighLevelClient elasticsearchClient() {
ClientConfiguration configuration = ClientConfiguration.builder(
)
.connectedTo("192.168.100.126:9200")//9300会报错
//.withConnectTimeout(Duration.ofSeconds(5))
//.withSocketTimeout(Duration.ofSeconds(3))
//.useSsl()
//.withDefaultHeaders(defaultHeaders)
//.withBasicAuth(username, password)
// ... other options
.build();
RestHighLevelClient client = RestClients.create(configuration).rest();
return client;
}
}
使用方法
@SpringBootTest
class ElasticsearchApplicationTests {
@Autowired
ElasticsearchOperations elasticsearchOperations;
@Test
void testSaveByOperations(){
Book book=new Book(2,"西游记2","吴承恩二世");
IndexQuery indexQuery= new IndexQueryBuilder()
.withId(book.getId().toString())
.withObject(book)
.build();
String documentId = elasticsearchOperations.index(indexQuery, IndexCoordinates.of("at"));//返回_id(并非javabean中的ID,而是hits中的)
System.out.println(documentId);
}
@Test
void testFindByOperations(){
Book book = elasticsearchOperations.get("2",Book.class,IndexCoordinates.of("at"));//IndexCoordinates的参数为Index
System.out.println(book);
}
}