mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-28 06:42:29 +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;
|
package org.springframework.data.elasticsearch.core;
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@ -183,27 +182,16 @@ public interface ElasticsearchOperations {
|
|||||||
*/
|
*/
|
||||||
<T> long count(SearchQuery query, Class<T> clazz);
|
<T> long count(SearchQuery query, Class<T> clazz);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute a multiget against elasticsearch for the given ids
|
* Execute a multiget against elasticsearch for the given ids
|
||||||
*
|
*
|
||||||
* @param ids
|
* @param searchQuery
|
||||||
* @param route
|
* @param clazz
|
||||||
* @param clazz
|
* @return
|
||||||
* @return
|
*/
|
||||||
*/
|
<T> LinkedList<T> multiGet(SearchQuery searchQuery, Class<T> clazz);
|
||||||
<T> LinkedList<T> getObjects(Collection<String> ids, String route, 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
|
* Index an object. Will do save or update
|
||||||
*
|
*
|
||||||
* @param query
|
* @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();
|
return countRequestBuilder.execute().actionGet().getCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> LinkedList<T> getObjects(Collection<String> ids, String route, Class<T> clazz) {
|
public <T> LinkedList<T> multiGet(SearchQuery searchQuery, 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
|
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
@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) {
|
public String index(IndexQuery query) {
|
||||||
String documentId = prepareIndex(query).execute().actionGet().getId();
|
String documentId = prepareIndex(query).execute().actionGet().getId();
|
||||||
// We should call this because we are not going through a mapper.
|
// 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 static org.apache.commons.collections.CollectionUtils.*;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.data.domain.Pageable;
|
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> types = new ArrayList<String>();
|
||||||
protected List<String> fields = new ArrayList<String>();
|
protected List<String> fields = new ArrayList<String>();
|
||||||
protected float minScore;
|
protected float minScore;
|
||||||
|
protected Collection<String> ids;
|
||||||
|
protected String route;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Sort getSort() {
|
public Sort getSort() {
|
||||||
@ -108,4 +111,20 @@ abstract class AbstractQuery implements Query {
|
|||||||
public void setMinScore(float minScore) {
|
public void setMinScore(float minScore) {
|
||||||
this.minScore = 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;
|
package org.springframework.data.elasticsearch.core.query;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.commons.collections.CollectionUtils;
|
import org.apache.commons.collections.CollectionUtils;
|
||||||
@ -46,6 +47,8 @@ public class NativeSearchQueryBuilder {
|
|||||||
private String[] types;
|
private String[] types;
|
||||||
private String[] fields;
|
private String[] fields;
|
||||||
private float minScore;
|
private float minScore;
|
||||||
|
private Collection<String> ids;
|
||||||
|
private String route;
|
||||||
|
|
||||||
public NativeSearchQueryBuilder withQuery(QueryBuilder queryBuilder) {
|
public NativeSearchQueryBuilder withQuery(QueryBuilder queryBuilder) {
|
||||||
this.queryBuilder = queryBuilder;
|
this.queryBuilder = queryBuilder;
|
||||||
@ -97,20 +100,34 @@ public class NativeSearchQueryBuilder {
|
|||||||
return this;
|
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() {
|
public NativeSearchQuery build() {
|
||||||
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder, filterBuilder, sortBuilders, highlightFields);
|
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder, filterBuilder, sortBuilders, highlightFields);
|
||||||
if (pageable != null) {
|
if (pageable != null) {
|
||||||
nativeSearchQuery.setPageable(pageable);
|
nativeSearchQuery.setPageable(pageable);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (indices != null) {
|
if (indices != null) {
|
||||||
nativeSearchQuery.addIndices(indices);
|
nativeSearchQuery.addIndices(indices);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (types != null) {
|
if (types != null) {
|
||||||
nativeSearchQuery.addTypes(types);
|
nativeSearchQuery.addTypes(types);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fields != null) {
|
if (fields != null) {
|
||||||
nativeSearchQuery.addFields(fields);
|
nativeSearchQuery.addFields(fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (CollectionUtils.isNotEmpty(facetRequests)) {
|
if (CollectionUtils.isNotEmpty(facetRequests)) {
|
||||||
nativeSearchQuery.setFacets(facetRequests);
|
nativeSearchQuery.setFacets(facetRequests);
|
||||||
}
|
}
|
||||||
@ -118,6 +135,15 @@ public class NativeSearchQueryBuilder {
|
|||||||
if (minScore > 0) {
|
if (minScore > 0) {
|
||||||
nativeSearchQuery.setMinScore(minScore);
|
nativeSearchQuery.setMinScore(minScore);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ids != null) {
|
||||||
|
nativeSearchQuery.setIds(ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (route != null) {
|
||||||
|
nativeSearchQuery.setRoute(route);
|
||||||
|
}
|
||||||
|
|
||||||
return nativeSearchQuery;
|
return nativeSearchQuery;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.elasticsearch.core.query;
|
package org.springframework.data.elasticsearch.core.query;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
@ -115,4 +116,18 @@ public interface Query {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
float getMinScore();
|
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);
|
assertEquals(sampleEntity, sampleEntity1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldReturnObjectForGivenIdUsingGet() {
|
public void shouldReturnObjectsForGivenIdsUsingMultiGet() {
|
||||||
// given
|
// given
|
||||||
String documentId = randomNumeric(5);
|
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
|
||||||
SampleEntity sampleEntity = new SampleEntity();
|
// first document
|
||||||
sampleEntity.setId(documentId);
|
String documentId = randomNumeric(5);
|
||||||
sampleEntity.setMessage("some message");
|
SampleEntity sampleEntity1 = new SampleEntity();
|
||||||
sampleEntity.setVersion(System.currentTimeMillis());
|
sampleEntity1.setId(documentId);
|
||||||
|
sampleEntity1.setMessage("some message");
|
||||||
|
sampleEntity1.setVersion(System.currentTimeMillis());
|
||||||
|
|
||||||
IndexQuery indexQuery = new IndexQuery();
|
IndexQuery indexQuery1 = new IndexQuery();
|
||||||
indexQuery.setId(documentId);
|
indexQuery1.setId(documentId);
|
||||||
indexQuery.setObject(sampleEntity);
|
indexQuery1.setObject(sampleEntity1);
|
||||||
|
indexQueries.add(indexQuery1);
|
||||||
|
|
||||||
elasticsearchTemplate.index(indexQuery);
|
// second document
|
||||||
// when
|
String documentId2 = randomNumeric(5);
|
||||||
SampleEntity sampleEntity1 = elasticsearchTemplate.getObject(documentId, "", SampleEntity.class);
|
SampleEntity sampleEntity2 = new SampleEntity();
|
||||||
// then
|
sampleEntity2.setId(documentId2);
|
||||||
assertNotNull("not null....", sampleEntity1);
|
sampleEntity2.setMessage("some message");
|
||||||
assertEquals(sampleEntity, sampleEntity1);
|
sampleEntity2.setVersion(System.currentTimeMillis());
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
IndexQuery indexQuery2 = new IndexQuery();
|
||||||
public void shouldReturnObjectsForGivenIdsUsingMultiGet() {
|
indexQuery2.setId(documentId2);
|
||||||
// given
|
indexQuery2.setObject(sampleEntity2);
|
||||||
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 indexQuery1 = new IndexQuery();
|
indexQueries.add(indexQuery2);
|
||||||
indexQuery1.setId(documentId);
|
|
||||||
indexQuery1.setObject(sampleEntity1);
|
|
||||||
indexQueries.add(indexQuery1);
|
|
||||||
|
|
||||||
// second document
|
elasticsearchTemplate.bulkIndex(indexQueries);
|
||||||
String documentId2 = randomNumeric(5);
|
elasticsearchTemplate.refresh(SampleEntity.class, true);
|
||||||
SampleEntity sampleEntity2 = new SampleEntity();
|
|
||||||
sampleEntity2.setId(documentId2);
|
|
||||||
sampleEntity2.setMessage("some message");
|
|
||||||
sampleEntity2.setVersion(System.currentTimeMillis());
|
|
||||||
|
|
||||||
IndexQuery indexQuery2 = new IndexQuery();
|
// when
|
||||||
indexQuery2.setId(documentId2);
|
SearchQuery query = new NativeSearchQueryBuilder().withIds(Arrays.asList(documentId, documentId2)).build();
|
||||||
indexQuery2.setObject(sampleEntity2);
|
LinkedList<SampleEntity> sampleEntities = elasticsearchTemplate.multiGet(query, SampleEntity.class);
|
||||||
|
// then
|
||||||
indexQueries.add(indexQuery2);
|
assertThat(sampleEntities.size(), is(equalTo(2)));
|
||||||
|
assertEquals(sampleEntities.get(0), sampleEntity1);
|
||||||
elasticsearchTemplate.bulkIndex(indexQueries);
|
assertEquals(sampleEntities.get(1), sampleEntity2);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldReturnPageForGivenSearchQuery() {
|
public void shouldReturnPageForGivenSearchQuery() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user