mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-23 20:42:11 +00:00
Added support for searchSimilar in ElasticsearchRepository
https://github.com/BioMedCentralLtd/spring-data-elasticsearch/issues/4
This commit is contained in:
parent
93a512f014
commit
41de820c70
@ -22,7 +22,6 @@ import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
|||||||
import org.springframework.data.repository.NoRepositoryBean;
|
import org.springframework.data.repository.NoRepositoryBean;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param <T>
|
* @param <T>
|
||||||
@ -41,4 +40,6 @@ public interface ElasticsearchRepository<T, ID extends Serializable> extends Ela
|
|||||||
Page<T> search(QueryBuilder elasticsearchQuery, Pageable pageable);
|
Page<T> search(QueryBuilder elasticsearchQuery, Pageable pageable);
|
||||||
|
|
||||||
Page<T> search(SearchQuery searchQuery);
|
Page<T> search(SearchQuery searchQuery);
|
||||||
|
|
||||||
|
Page<T> searchSimilar(T entity);
|
||||||
}
|
}
|
||||||
|
@ -19,14 +19,10 @@ import org.elasticsearch.index.query.QueryBuilder;
|
|||||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||||
import org.springframework.data.domain.*;
|
import org.springframework.data.domain.*;
|
||||||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
||||||
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
|
import org.springframework.data.elasticsearch.core.query.*;
|
||||||
import org.springframework.data.elasticsearch.core.query.GetQuery;
|
|
||||||
import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
|
||||||
import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
|
||||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
|
||||||
import java.lang.reflect.ParameterizedType;
|
import java.lang.reflect.ParameterizedType;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -187,6 +183,14 @@ public class SimpleElasticsearchRepository<T> implements ElasticsearchRepository
|
|||||||
return elasticsearchOperations.queryForPage(query, getEntityClass());
|
return elasticsearchOperations.queryForPage(query, getEntityClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page<T> searchSimilar(T entity) {
|
||||||
|
Assert.notNull(entity, "Cannot search similar records for 'null'.");
|
||||||
|
MoreLikeThisQuery query = new MoreLikeThisQuery();
|
||||||
|
query.setId(extractIdFromBean(entity));
|
||||||
|
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'.");
|
||||||
|
@ -29,7 +29,9 @@ import org.springframework.test.context.ContextConfiguration;
|
|||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static org.apache.commons.lang.RandomStringUtils.randomNumeric;
|
import static org.apache.commons.lang.RandomStringUtils.randomNumeric;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.*;
|
import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||||
@ -381,4 +383,40 @@ public class RepositoryTest {
|
|||||||
//then
|
//then
|
||||||
assertThat(sampleEntities,is(notNullValue()));
|
assertThat(sampleEntities,is(notNullValue()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnSimilarEntities(){
|
||||||
|
//given
|
||||||
|
String sampleMessage = "So we build a web site or an application and want to add search to it, " +
|
||||||
|
"and then it hits us: getting search working is hard. We want our search solution to be fast," +
|
||||||
|
" we want a painless setup and a completely free search schema, we want to be able to index data simply using JSON over HTTP, " +
|
||||||
|
"we want our search server to be always available, we want to be able to start with one machine and scale to hundreds, " +
|
||||||
|
"we want real-time search, we want simple multi-tenancy, and we want a solution that is built for the cloud.";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
List<SampleEntity> sampleEntities = createSampleEntitiesWithMessage(sampleMessage, 30);
|
||||||
|
repository.save(sampleEntities);
|
||||||
|
|
||||||
|
//when
|
||||||
|
Page<SampleEntity> results = repository.searchSimilar(sampleEntities.get(0));
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertThat(results.getTotalElements(), is(greaterThanOrEqualTo(1L)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<SampleEntity> createSampleEntitiesWithMessage(String message, int numberOfEntities){
|
||||||
|
List<SampleEntity> sampleEntities = new ArrayList<SampleEntity>();
|
||||||
|
for(int i = 0; i < numberOfEntities; i++){
|
||||||
|
String documentId = randomNumeric(5);
|
||||||
|
SampleEntity sampleEntity = new SampleEntity();
|
||||||
|
sampleEntity.setId(documentId);
|
||||||
|
sampleEntity.setMessage(message);
|
||||||
|
sampleEntity.setVersion(System.currentTimeMillis());
|
||||||
|
sampleEntities.add(sampleEntity);
|
||||||
|
}
|
||||||
|
return sampleEntities;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user