SpringBoot-高级-检索-整合SpringDataElasticsearch

前面整合Jest技术,9200端口来操作ES,接下来整合SpringBoot ES模块,我们从pom文件把SpringData打开,org.springframework.bootspring-boot-starter-data-elasticsearch
这Jest来留在这,然后我们来配置一下,来到配置文件里面,前面这个是Jest的配置,SpringData的ES在SpringData里面,一个是节点的名字,还有一个是节点的信息http://59.110.158.145:9200/{"name" : "Clint Barton","cluster_name" : "elasticsearch","cluster_uuid" : "PDTFBfQpSRuPYlwdKI7e-Q","version" : {"number" : "2.4.6","build_hash" : "5376dca9f70f3abef96a77f4bb22720ace8240fd","build_timestamp" : "2017-07-18T12:17:44Z","build_snapshot" : false,"lucene_version" : "5.5.4"},"tagline" : "You Know, for Search"
}spring.data.elasticsearch.cluster-name=elasticsearch主机地址加上9300,spring.data.elasticsearch.cluster-nodes=59.110.158.145:9300把这个配上以后直接启动但是我们发现这有报错,他说Connection refused,连接拒绝,是什么问题呢,我们这个节点传输有问题,这问题的原因在哪,我们来给大家说一下,我们看SpringData引入的ES相关的模块,来往下翻,ES用的是246版本,而我们的ES是569版本,ES版本有可能不合适,我们需要找到一个合适的SPRING DATA版本才行,我们来参照官方文档,我们来搜索一下SpringData我们来到SPRING的官网,我们来看他们是怎么来进行版本适配的https://spring.io/projects/spring-data-elasticsearch我们来打开他的文档https://docs.spring.io/spring-data/elasticsearch/docs/2.1.23.RELEASE/reference/html/这块有一个Version ControlVersion Control - https://github.com/spring-projects/spring-data-elasticsearch这块有一个表格

package com.learn.repository;import java.util.List;import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;import com.learn.bean.Book;public interface BookRepository extends ElasticsearchRepository {public List findByBookNameLike(String bookName);
}
package com.learn.bean;import org.springframework.data.elasticsearch.annotations.Document;@Document(indexName="china",type="book")
public class Book {private String bookName;private String author;private Integer id;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public Book() {super();}public Book(String bookName, String author) {super();this.bookName = bookName;this.author = author;}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;}@Overridepublic String toString() {return "Book [bookName=" + bookName + ", author=" + author + ", id=" + id + "]";}}
/** Copyright 2013-2016 the original author or authors.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/
package org.springframework.data.elasticsearch.repository;import java.io.Serializable;import org.elasticsearch.index.query.QueryBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.data.repository.NoRepositoryBean;/*** @param * @param * @author Rizwan Idrees* @author Mohsin Husen*/
@NoRepositoryBean
public interface ElasticsearchRepository extendsElasticsearchCrudRepository { S index(S entity);Iterable search(QueryBuilder query);Page search(QueryBuilder query, Pageable pageable);Page search(SearchQuery searchQuery);Page searchSimilar(T entity, String[] fields, Pageable pageable);void refresh();Class getEntityClass();
}
我们就发现有id为1的,我们直接来查Book,一般的增删改查BookRepository里面都有,比如有计数,删除,保存,搜索,包括他也支持写自定义方法public List findByBookNameLike(String bookName);就是模糊查询的,传入bookName,就写好方法名就行了,不用写实现,我们就默认去ES中找图书,按照书名来给我们模糊查询,来测试一下,看能不能查出西游记Book [bookName=西游记, author=吴承恩, id=1]@AutowiredBookRepository bookRepository;@Testpublic void test02() {
//		Book book = new Book();
//		book.setId(1);
//		book.setBookName("西游记");
//		book.setAuthor("吴承恩");
//		bookRepository.index(book);List list = bookRepository.findByBookNameLike("游");for (Book book : list) {System.out.println(book);}}大家可以来参考官方文档https://docs.spring.io/spring-data/elasticsearch/docs/2.1.23.RELEASE/reference/html/
#repositories.query-methodshttps://docs.spring.io/spring-data/elasticsearch/docs/2.1.23.RELEASE/reference/html/
#elasticsearch.misc.filter

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部