mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-28 14:52:20 +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
16
src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchOperations.java
Normal file → Executable file
16
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;
|
||||||
@ -186,22 +185,11 @@ public interface ElasticsearchOperations {
|
|||||||
/**
|
/**
|
||||||
* 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> getObjects(Collection<String> ids, String route, Class<T> clazz);
|
<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
|
* Index an object. Will do save or update
|
||||||
|
31
src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java
Normal file → Executable file
31
src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java
Normal file → Executable file
@ -247,11 +247,24 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@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);
|
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
|
||||||
|
|
||||||
MultiGetRequestBuilder builder = client.prepareMultiGet();
|
MultiGetRequestBuilder builder = client.prepareMultiGet();
|
||||||
for (String id : ids) {
|
|
||||||
builder.add(new MultiGetRequest.Item(persistentEntity.getIndexName(), persistentEntity.getIndexType(), id).routing(route));
|
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();
|
MultiGetResponse responses = builder.execute().actionGet();
|
||||||
final LinkedList<T> result = new LinkedList<T>();
|
final LinkedList<T> result = new LinkedList<T>();
|
||||||
@ -263,18 +276,6 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@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;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@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();
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
|
24
src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java
Normal file → Executable file
24
src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java
Normal file → Executable file
@ -112,27 +112,6 @@ public class ElasticsearchTemplateTests {
|
|||||||
assertEquals(sampleEntity, sampleEntity1);
|
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());
|
|
||||||
|
|
||||||
IndexQuery indexQuery = new IndexQuery();
|
|
||||||
indexQuery.setId(documentId);
|
|
||||||
indexQuery.setObject(sampleEntity);
|
|
||||||
|
|
||||||
elasticsearchTemplate.index(indexQuery);
|
|
||||||
// when
|
|
||||||
SampleEntity sampleEntity1 = elasticsearchTemplate.getObject(documentId, "", SampleEntity.class);
|
|
||||||
// then
|
|
||||||
assertNotNull("not null....", sampleEntity1);
|
|
||||||
assertEquals(sampleEntity, sampleEntity1);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldReturnObjectsForGivenIdsUsingMultiGet() {
|
public void shouldReturnObjectsForGivenIdsUsingMultiGet() {
|
||||||
// given
|
// given
|
||||||
@ -166,7 +145,8 @@ public class ElasticsearchTemplateTests {
|
|||||||
elasticsearchTemplate.refresh(SampleEntity.class, true);
|
elasticsearchTemplate.refresh(SampleEntity.class, true);
|
||||||
|
|
||||||
// when
|
// when
|
||||||
LinkedList<SampleEntity> sampleEntities = elasticsearchTemplate.getObjects(Arrays.asList(documentId, documentId2), "", SampleEntity.class);
|
SearchQuery query = new NativeSearchQueryBuilder().withIds(Arrays.asList(documentId, documentId2)).build();
|
||||||
|
LinkedList<SampleEntity> sampleEntities = elasticsearchTemplate.multiGet(query, SampleEntity.class);
|
||||||
// then
|
// then
|
||||||
assertThat(sampleEntities.size(), is(equalTo(2)));
|
assertThat(sampleEntities.size(), is(equalTo(2)));
|
||||||
assertEquals(sampleEntities.get(0), sampleEntity1);
|
assertEquals(sampleEntities.get(0), sampleEntity1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user