mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-22 03:52:10 +00:00
DATAES-52 - Support Get & Multi Get
Removed getObject() as it was already implemented using queryForObject() Changed Method name to multiGet() from getObjects() Made method to accept searchQuery instead of individual params
This commit is contained in:
parent
ab7c01b37c
commit
c2a53715d8
30
src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchOperations.java
Normal file → Executable file
30
src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchOperations.java
Normal file → Executable file
@ -15,7 +15,6 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@ -183,27 +182,16 @@ public interface ElasticsearchOperations {
|
||||
*/
|
||||
<T> long count(SearchQuery query, Class<T> clazz);
|
||||
|
||||
/**
|
||||
* Execute a multiget against elasticsearch for the given ids
|
||||
*
|
||||
* @param ids
|
||||
* @param route
|
||||
* @param clazz
|
||||
* @return
|
||||
*/
|
||||
<T> LinkedList<T> getObjects(Collection<String> ids, String route, Class<T> clazz);
|
||||
/**
|
||||
* Execute a multiget against elasticsearch for the given ids
|
||||
*
|
||||
* @param searchQuery
|
||||
* @param clazz
|
||||
* @return
|
||||
*/
|
||||
<T> LinkedList<T> multiGet(SearchQuery searchQuery, Class<T> clazz);
|
||||
|
||||
/**
|
||||
* Execute a get against elasticsearch for the given id
|
||||
*
|
||||
* @param id
|
||||
* @param route
|
||||
* @param clazz
|
||||
* @return
|
||||
*/
|
||||
<T> T getObject(String id, String route, Class<T> clazz);
|
||||
|
||||
/**
|
||||
/**
|
||||
* Index an object. Will do save or update
|
||||
*
|
||||
* @param query
|
||||
|
57
src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java
Normal file → Executable file
57
src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java
Normal file → Executable file
@ -246,36 +246,37 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
||||
return countRequestBuilder.execute().actionGet().getCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> LinkedList<T> getObjects(Collection<String> ids, String route, Class<T> clazz) {
|
||||
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
|
||||
MultiGetRequestBuilder builder = client.prepareMultiGet();
|
||||
for (String id : ids) {
|
||||
builder.add(new MultiGetRequest.Item(persistentEntity.getIndexName(), persistentEntity.getIndexType(), id).routing(route));
|
||||
}
|
||||
MultiGetResponse responses = builder.execute().actionGet();
|
||||
final LinkedList<T> result = new LinkedList<T>();
|
||||
for (MultiGetItemResponse response : responses.getResponses()) {
|
||||
if (!response.isFailed() && response.getResponse().isExists()) {
|
||||
result.add(resultsMapper.mapResult(response.getResponse(), clazz));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@Override
|
||||
public <T> LinkedList<T> multiGet(SearchQuery searchQuery, Class<T> clazz) {
|
||||
|
||||
@Override
|
||||
public <T> T getObject(String id, String route, Class<T> clazz) {
|
||||
if (id != null) {
|
||||
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
|
||||
GetResponse response = client
|
||||
.prepareGet(persistentEntity.getIndexName(), persistentEntity.getIndexType(), id).setRouting(route)
|
||||
.execute().actionGet();
|
||||
return resultsMapper.mapResult(response, clazz);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
|
||||
|
||||
@Override
|
||||
MultiGetRequestBuilder builder = client.prepareMultiGet();
|
||||
|
||||
for (String id : searchQuery.getIds()) {
|
||||
|
||||
MultiGetRequest.Item item = new MultiGetRequest.Item(persistentEntity.getIndexName(), persistentEntity.getIndexType(), id);
|
||||
|
||||
if (searchQuery.getRoute() != null) {
|
||||
item = item.routing(searchQuery.getRoute());
|
||||
}
|
||||
|
||||
if (searchQuery.getFields() != null && !searchQuery.getFields().isEmpty()) {
|
||||
item = item.fields(toArray(searchQuery.getFields()));
|
||||
}
|
||||
builder.add(item);
|
||||
}
|
||||
MultiGetResponse responses = builder.execute().actionGet();
|
||||
final LinkedList<T> result = new LinkedList<T>();
|
||||
for (MultiGetItemResponse response : responses.getResponses()) {
|
||||
if (!response.isFailed() && response.getResponse().isExists()) {
|
||||
result.add(resultsMapper.mapResult(response.getResponse(), clazz));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String index(IndexQuery query) {
|
||||
String documentId = prepareIndex(query).execute().actionGet().getId();
|
||||
// We should call this because we are not going through a mapper.
|
||||
|
19
src/main/java/org/springframework/data/elasticsearch/core/query/AbstractQuery.java
Normal file → Executable file
19
src/main/java/org/springframework/data/elasticsearch/core/query/AbstractQuery.java
Normal file → Executable file
@ -18,6 +18,7 @@ package org.springframework.data.elasticsearch.core.query;
|
||||
import static org.apache.commons.collections.CollectionUtils.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Pageable;
|
||||
@ -38,6 +39,8 @@ abstract class AbstractQuery implements Query {
|
||||
protected List<String> types = new ArrayList<String>();
|
||||
protected List<String> fields = new ArrayList<String>();
|
||||
protected float minScore;
|
||||
protected Collection<String> ids;
|
||||
protected String route;
|
||||
|
||||
@Override
|
||||
public Sort getSort() {
|
||||
@ -108,4 +111,20 @@ abstract class AbstractQuery implements Query {
|
||||
public void setMinScore(float minScore) {
|
||||
this.minScore = minScore;
|
||||
}
|
||||
|
||||
public Collection<String> getIds() {
|
||||
return ids;
|
||||
}
|
||||
|
||||
public void setIds(Collection<String> ids) {
|
||||
this.ids = ids;
|
||||
}
|
||||
|
||||
public String getRoute() {
|
||||
return route;
|
||||
}
|
||||
|
||||
public void setRoute(String route) {
|
||||
this.route = route;
|
||||
}
|
||||
}
|
||||
|
26
src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQueryBuilder.java
Normal file → Executable file
26
src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQueryBuilder.java
Normal file → Executable file
@ -16,6 +16,7 @@
|
||||
package org.springframework.data.elasticsearch.core.query;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
@ -46,6 +47,8 @@ public class NativeSearchQueryBuilder {
|
||||
private String[] types;
|
||||
private String[] fields;
|
||||
private float minScore;
|
||||
private Collection<String> ids;
|
||||
private String route;
|
||||
|
||||
public NativeSearchQueryBuilder withQuery(QueryBuilder queryBuilder) {
|
||||
this.queryBuilder = queryBuilder;
|
||||
@ -97,20 +100,34 @@ public class NativeSearchQueryBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
public NativeSearchQueryBuilder withIds(Collection<String> ids) {
|
||||
this.ids = ids;
|
||||
return this;
|
||||
}
|
||||
|
||||
public NativeSearchQueryBuilder withRoute(String route) {
|
||||
this.route = route;
|
||||
return this;
|
||||
}
|
||||
|
||||
public NativeSearchQuery build() {
|
||||
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder, filterBuilder, sortBuilders, highlightFields);
|
||||
if (pageable != null) {
|
||||
nativeSearchQuery.setPageable(pageable);
|
||||
}
|
||||
|
||||
if (indices != null) {
|
||||
nativeSearchQuery.addIndices(indices);
|
||||
}
|
||||
|
||||
if (types != null) {
|
||||
nativeSearchQuery.addTypes(types);
|
||||
}
|
||||
|
||||
if (fields != null) {
|
||||
nativeSearchQuery.addFields(fields);
|
||||
}
|
||||
|
||||
if (CollectionUtils.isNotEmpty(facetRequests)) {
|
||||
nativeSearchQuery.setFacets(facetRequests);
|
||||
}
|
||||
@ -118,6 +135,15 @@ public class NativeSearchQueryBuilder {
|
||||
if (minScore > 0) {
|
||||
nativeSearchQuery.setMinScore(minScore);
|
||||
}
|
||||
|
||||
if (ids != null) {
|
||||
nativeSearchQuery.setIds(ids);
|
||||
}
|
||||
|
||||
if (route != null) {
|
||||
nativeSearchQuery.setRoute(route);
|
||||
}
|
||||
|
||||
return nativeSearchQuery;
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core.query;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
@ -115,4 +116,18 @@ public interface Query {
|
||||
* @return
|
||||
*/
|
||||
float getMinScore();
|
||||
|
||||
/**
|
||||
* Get Ids
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Collection<String> getIds();
|
||||
|
||||
/**
|
||||
* Get route
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String getRoute();
|
||||
}
|
||||
|
88
src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java
Normal file → Executable file
88
src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java
Normal file → Executable file
@ -112,66 +112,46 @@ public class ElasticsearchTemplateTests {
|
||||
assertEquals(sampleEntity, sampleEntity1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnObjectForGivenIdUsingGet() {
|
||||
// given
|
||||
String documentId = randomNumeric(5);
|
||||
SampleEntity sampleEntity = new SampleEntity();
|
||||
sampleEntity.setId(documentId);
|
||||
sampleEntity.setMessage("some message");
|
||||
sampleEntity.setVersion(System.currentTimeMillis());
|
||||
@Test
|
||||
public void shouldReturnObjectsForGivenIdsUsingMultiGet() {
|
||||
// given
|
||||
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
|
||||
// first document
|
||||
String documentId = randomNumeric(5);
|
||||
SampleEntity sampleEntity1 = new SampleEntity();
|
||||
sampleEntity1.setId(documentId);
|
||||
sampleEntity1.setMessage("some message");
|
||||
sampleEntity1.setVersion(System.currentTimeMillis());
|
||||
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(documentId);
|
||||
indexQuery.setObject(sampleEntity);
|
||||
IndexQuery indexQuery1 = new IndexQuery();
|
||||
indexQuery1.setId(documentId);
|
||||
indexQuery1.setObject(sampleEntity1);
|
||||
indexQueries.add(indexQuery1);
|
||||
|
||||
elasticsearchTemplate.index(indexQuery);
|
||||
// when
|
||||
SampleEntity sampleEntity1 = elasticsearchTemplate.getObject(documentId, "", SampleEntity.class);
|
||||
// then
|
||||
assertNotNull("not null....", sampleEntity1);
|
||||
assertEquals(sampleEntity, sampleEntity1);
|
||||
}
|
||||
// second document
|
||||
String documentId2 = randomNumeric(5);
|
||||
SampleEntity sampleEntity2 = new SampleEntity();
|
||||
sampleEntity2.setId(documentId2);
|
||||
sampleEntity2.setMessage("some message");
|
||||
sampleEntity2.setVersion(System.currentTimeMillis());
|
||||
|
||||
@Test
|
||||
public void shouldReturnObjectsForGivenIdsUsingMultiGet() {
|
||||
// given
|
||||
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
|
||||
// first document
|
||||
String documentId = randomNumeric(5);
|
||||
SampleEntity sampleEntity1 = new SampleEntity();
|
||||
sampleEntity1.setId(documentId);
|
||||
sampleEntity1.setMessage("some message");
|
||||
sampleEntity1.setVersion(System.currentTimeMillis());
|
||||
IndexQuery indexQuery2 = new IndexQuery();
|
||||
indexQuery2.setId(documentId2);
|
||||
indexQuery2.setObject(sampleEntity2);
|
||||
|
||||
IndexQuery indexQuery1 = new IndexQuery();
|
||||
indexQuery1.setId(documentId);
|
||||
indexQuery1.setObject(sampleEntity1);
|
||||
indexQueries.add(indexQuery1);
|
||||
indexQueries.add(indexQuery2);
|
||||
|
||||
// second document
|
||||
String documentId2 = randomNumeric(5);
|
||||
SampleEntity sampleEntity2 = new SampleEntity();
|
||||
sampleEntity2.setId(documentId2);
|
||||
sampleEntity2.setMessage("some message");
|
||||
sampleEntity2.setVersion(System.currentTimeMillis());
|
||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||
elasticsearchTemplate.refresh(SampleEntity.class, true);
|
||||
|
||||
IndexQuery indexQuery2 = new IndexQuery();
|
||||
indexQuery2.setId(documentId2);
|
||||
indexQuery2.setObject(sampleEntity2);
|
||||
|
||||
indexQueries.add(indexQuery2);
|
||||
|
||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||
elasticsearchTemplate.refresh(SampleEntity.class, true);
|
||||
|
||||
// when
|
||||
LinkedList<SampleEntity> sampleEntities = elasticsearchTemplate.getObjects(Arrays.asList(documentId, documentId2), "", SampleEntity.class);
|
||||
// then
|
||||
assertThat(sampleEntities.size(), is(equalTo(2)));
|
||||
assertEquals(sampleEntities.get(0), sampleEntity1);
|
||||
assertEquals(sampleEntities.get(1), sampleEntity2);
|
||||
}
|
||||
// when
|
||||
SearchQuery query = new NativeSearchQueryBuilder().withIds(Arrays.asList(documentId, documentId2)).build();
|
||||
LinkedList<SampleEntity> sampleEntities = elasticsearchTemplate.multiGet(query, SampleEntity.class);
|
||||
// then
|
||||
assertThat(sampleEntities.size(), is(equalTo(2)));
|
||||
assertEquals(sampleEntities.get(0), sampleEntity1);
|
||||
assertEquals(sampleEntities.get(1), sampleEntity2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnPageForGivenSearchQuery() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user