您好,登錄后才能下訂單哦!
這篇文章主要講解了“spring+springdata+elasticsearch的配置方法”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“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,項目啟動時默認(rèn)自動創(chuàng)建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的配置方法”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對spring+springdata+elasticsearch的配置方法這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。