/** 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