这篇文章主要讲解了“spring+springdata+elasticsearch的配置方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“spring+springdata+elasticsearch的配置方法”吧!
pom配置:
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>3.1.0.RELEASE</version></dependency>
es配置文件:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd"> <elasticsearch:repositories base-package="com.test.repositories"/> <elasticsearch:transport-client id="client" cluster-nodes="127.0.0.1:9300" cluster-name="elasticsearch"/> <bean name="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate"> <constructor-arg name="client" ref="client" /> </bean></beans>
实体Book.java,项目启动时默认自动创建es索引
import lombok.Data;import org.springframework.data.annotation.Id;import org.springframework.data.annotation.Version;import org.springframework.data.elasticsearch.annotations.Document;import org.springframework.data.elasticsearch.annotations.Field;import org.springframework.data.elasticsearch.annotations.FieldType;@Data@Document(indexName= "book", type= "book", shards = 3, replicas = 1)public class Book {@Id private String id; @Field(type = FieldType.Keyword)private String name; @Field private Long price; @Version private Long version;}
ElasticsearchCrudRepository.java
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;import org.springframework.data.repository.NoRepositoryBean;import org.springframework.data.repository.PagingAndSortingRepository;import java.io.Serializable;@NoRepositoryBeanpublic interface ElasticsearchCrudRepository<T, ID extends Serializable> extends ElasticsearchRepository<T, ID>, PagingAndSortingRepository<T, ID> { }
BookRepository.java
@Repositorypublic interface BookRepository extends ElasticsearchCrudRepository<Book, String> { List<Book> findBookByName(String name);}
test类
@Controller@RequestMapping("es")public class EsController {@Autowired private BookService bookService; @RequestMapping("book/{name}")public CommonResponse bookSearch(@PathVariable String name){ CommonResponse response = new CommonResponse<>(); List<Book> list = bookService.findBookByName(name); response.setData(list); return response; } }
配置时注意spring和es的版本兼容问题,可查看官方文档https://github.com/spring-projects/spring-data-elasticsearch
感谢各位的阅读,以上就是“spring+springdata+elasticsearch的配置方法”的内容了,经过本文的学习后,相信大家对spring+springdata+elasticsearch的配置方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。