springboot检索之整合elasticsearch并使用spring-data操作
各种过时,版本不兼容,只能暂时将代码放在这先做参考了。
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
Book.java
package com.gong.spingbootes.bean; import org.springframework.data.elasticsearch.annotations.Document; //这里必须指明索引名字和类型 @Document(indexName = "gong",type = "book") public class Book { private Integer id; private String bookName; private String author; public Book(){} public Book(Integer id, String bookName, String author) { this.id = id; this.bookName = bookName; this.author = author; } @Override public String toString() { return "Book{" + "id=" + id + ", bookName=‘" + bookName + ‘\‘‘ + ", author=‘" + author + ‘\‘‘ + ‘}‘; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } }
BookRepository.java
package com.gong.spingbootes.repository; import com.gong.spingbootes.bean.Book; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import java.util.List; //第一个泛型是对象类型,第二个泛型是book的主键 public interface BookRepository extends ElasticsearchRepository<Book,Integer>{ //这里的名字不是随意取的,而是根据查询需求来取的,具体可参考相关文档 public List<Book> findByBookNameLike(String bookName); }
application.properties
spring.data.elasticsearch.cluster-name=elasticsearch spring.data.elasticsearch.cluster-nodes=192.168.124.22 #这两玩意过时了
SpringbootEsApplicationTests.java
package com.gong.spingbootes; import com.gong.spingbootes.bean.Article; import com.gong.spingbootes.bean.Book; import com.gong.spingbootes.repository.BookRepository; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class SpingbootEsApplicationTests { @Autowired BookRepository bookRepository; @Test public void contextLoads() { } //添加索引 @Test public void index() { bookRepository.index(new Book(1,"java","德玛")); } //进行查询 @Test public void search() { bookRepository.findByBookNameLike("ja"); } }
相关推荐
newbornzhao 2020-09-14
做对一件事很重要 2020-09-07
renjinlong 2020-09-03
明瞳 2020-08-19
李玉志 2020-08-19
mengyue 2020-08-07
molong0 2020-08-06
AFei00 2020-08-03
molong0 2020-08-03
wenwentana 2020-08-03
YYDU 2020-08-03
另外一部分,则需要先做聚类、分类处理,将聚合出的分类结果存入ES集群的聚类索引中。数据处理层的聚合结果存入ES中的指定索引,同时将每个聚合主题相关的数据存入每个document下面的某个field下。
sifeimeng 2020-08-03
心丨悦 2020-08-03
liangwenrong 2020-07-31
sifeimeng 2020-08-01
mengyue 2020-07-30
tigercn 2020-07-29
IceStreamLab 2020-07-29