Added support for searchSimilar in ElasticsearchRepository

https://github.com/BioMedCentralLtd/spring-data-elasticsearch/issues/4
This commit is contained in:
Rizwan Idrees 2013-03-19 17:26:32 +00:00
parent 41de820c70
commit c78bfc0f5c
5 changed files with 16 additions and 6 deletions

View File

@ -33,8 +33,6 @@ import static org.apache.commons.collections.CollectionUtils.addAll;
*/ */
abstract class AbstractQuery implements Query{ abstract class AbstractQuery implements Query{
private static final Pageable DEFAULT_PAGE = new PageRequest(0, DEFAULT_PAGE_SIZE);
protected Pageable pageable = DEFAULT_PAGE; protected Pageable pageable = DEFAULT_PAGE;
protected Sort sort; protected Sort sort;
protected List<String> indices = new ArrayList<String>(); protected List<String> indices = new ArrayList<String>();

View File

@ -16,14 +16,13 @@
package org.springframework.data.elasticsearch.core.query; package org.springframework.data.elasticsearch.core.query;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.apache.commons.collections.CollectionUtils.addAll; import static org.apache.commons.collections.CollectionUtils.addAll;
import static org.springframework.data.elasticsearch.core.query.Query.DEFAULT_PAGE_SIZE; import static org.springframework.data.elasticsearch.core.query.Query.DEFAULT_PAGE;
/** /**
* MoreLikeThisQuery * MoreLikeThisQuery
@ -50,7 +49,7 @@ public class MoreLikeThisQuery {
private Integer minWordLen; private Integer minWordLen;
private Integer maxWordLen; private Integer maxWordLen;
private Float boostTerms; private Float boostTerms;
private Pageable pageable = new PageRequest(0, DEFAULT_PAGE_SIZE); private Pageable pageable = DEFAULT_PAGE;
public String getId() { public String getId() {
return id; return id;

View File

@ -17,6 +17,7 @@
package org.springframework.data.elasticsearch.core.query; package org.springframework.data.elasticsearch.core.query;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
@ -29,7 +30,8 @@ import java.util.List;
*/ */
public interface Query { public interface Query {
int DEFAULT_PAGE_SIZE = 10; public static final int DEFAULT_PAGE_SIZE = 10;
public static final Pageable DEFAULT_PAGE = new PageRequest(0, DEFAULT_PAGE_SIZE);
/** /**

View File

@ -42,4 +42,6 @@ public interface ElasticsearchRepository<T, ID extends Serializable> extends Ela
Page<T> search(SearchQuery searchQuery); Page<T> search(SearchQuery searchQuery);
Page<T> searchSimilar(T entity); Page<T> searchSimilar(T entity);
Page<T> searchSimilar(T entity, Pageable pageable);
} }

View File

@ -32,6 +32,7 @@ import java.util.List;
import static org.elasticsearch.index.query.QueryBuilders.inQuery; import static org.elasticsearch.index.query.QueryBuilders.inQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.springframework.data.elasticsearch.core.query.Query.DEFAULT_PAGE;
/** /**
* Elasticsearch specific repository implementation. Likely to be used as target within {@link ElasticsearchRepositoryFactory} * Elasticsearch specific repository implementation. Likely to be used as target within {@link ElasticsearchRepositoryFactory}
@ -185,12 +186,20 @@ public class SimpleElasticsearchRepository<T> implements ElasticsearchRepository
@Override @Override
public Page<T> searchSimilar(T entity) { public Page<T> searchSimilar(T entity) {
return searchSimilar(entity, DEFAULT_PAGE);
}
@Override
public Page<T> searchSimilar(T entity, Pageable pageable) {
Assert.notNull(entity, "Cannot search similar records for 'null'."); Assert.notNull(entity, "Cannot search similar records for 'null'.");
Assert.notNull(entity, "Pageable cannot be 'null'");
MoreLikeThisQuery query = new MoreLikeThisQuery(); MoreLikeThisQuery query = new MoreLikeThisQuery();
query.setId(extractIdFromBean(entity)); query.setId(extractIdFromBean(entity));
query.setPageable(pageable);
return elasticsearchOperations.moreLikeThis(query, getEntityClass()); return elasticsearchOperations.moreLikeThis(query, getEntityClass());
} }
@Override @Override
public void delete(String id) { public void delete(String id) {
Assert.notNull(id, "Cannot delete entity with id 'null'."); Assert.notNull(id, "Cannot delete entity with id 'null'.");