DATAES-52 - Support Get & Multi Get
Added support for MultiGetResultMapper for custom fields
This commit is contained in:
@@ -22,9 +22,12 @@ import java.lang.reflect.Method;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.elasticsearch.action.get.GetResponse;
|
||||
import org.elasticsearch.action.get.MultiGetItemResponse;
|
||||
import org.elasticsearch.action.get.MultiGetResponse;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.common.base.Strings;
|
||||
import org.elasticsearch.common.jackson.core.JsonEncoding;
|
||||
@@ -129,6 +132,18 @@ public class DefaultResultMapper extends AbstractResultMapper {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> LinkedList<T> mapResults(MultiGetResponse responses, Class<T> clazz) {
|
||||
LinkedList<T> list = new LinkedList<T>();
|
||||
for (MultiGetItemResponse response : responses.getResponses()) {
|
||||
if (!response.isFailed() && response.getResponse().isExists()) {
|
||||
T result = mapEntity(response.getResponse().getSourceAsString(), clazz);
|
||||
list.add(result);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private <T> void setPersistentEntityId(T result, String id, Class<T> clazz) {
|
||||
if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {
|
||||
PersistentProperty<ElasticsearchPersistentProperty> idProperty = mappingContext.getPersistentEntity(clazz).getIdProperty();
|
||||
|
||||
@@ -183,7 +183,7 @@ public interface ElasticsearchOperations {
|
||||
<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 searchQuery
|
||||
* @param clazz
|
||||
@@ -191,6 +191,16 @@ public interface ElasticsearchOperations {
|
||||
*/
|
||||
<T> LinkedList<T> multiGet(SearchQuery searchQuery, Class<T> clazz);
|
||||
|
||||
/**
|
||||
* Execute a multiGet against elasticsearch for the given ids with MultiGetResultMapper
|
||||
*
|
||||
* @param searchQuery
|
||||
* @param clazz
|
||||
* @param multiGetResultMapper
|
||||
* @return
|
||||
*/
|
||||
<T> LinkedList<T> multiGet(SearchQuery searchQuery, Class<T> clazz, MultiGetResultMapper multiGetResultMapper);
|
||||
|
||||
/**
|
||||
* Index an object. Will do save or update
|
||||
*
|
||||
|
||||
@@ -37,7 +37,10 @@ import org.elasticsearch.action.bulk.BulkItemResponse;
|
||||
import org.elasticsearch.action.bulk.BulkRequestBuilder;
|
||||
import org.elasticsearch.action.bulk.BulkResponse;
|
||||
import org.elasticsearch.action.count.CountRequestBuilder;
|
||||
import org.elasticsearch.action.get.*;
|
||||
import org.elasticsearch.action.get.GetResponse;
|
||||
import org.elasticsearch.action.get.MultiGetRequest;
|
||||
import org.elasticsearch.action.get.MultiGetRequestBuilder;
|
||||
import org.elasticsearch.action.get.MultiGetResponse;
|
||||
import org.elasticsearch.action.index.IndexRequestBuilder;
|
||||
import org.elasticsearch.action.mlt.MoreLikeThisRequestBuilder;
|
||||
import org.elasticsearch.action.search.SearchRequestBuilder;
|
||||
@@ -248,6 +251,10 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
||||
|
||||
@Override
|
||||
public <T> LinkedList<T> multiGet(SearchQuery searchQuery, Class<T> clazz) {
|
||||
return resultsMapper.mapResults(getMultiResponse(searchQuery, clazz), clazz);
|
||||
}
|
||||
|
||||
private <T> MultiGetResponse getMultiResponse(SearchQuery searchQuery, Class<T> clazz) {
|
||||
|
||||
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
|
||||
|
||||
@@ -266,14 +273,12 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
||||
}
|
||||
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;
|
||||
return builder.execute().actionGet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> LinkedList<T> multiGet(SearchQuery searchQuery, Class<T> clazz, MultiGetResultMapper getResultMapper) {
|
||||
return getResultMapper.mapResults(getMultiResponse(searchQuery, clazz), clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.elasticsearch.core;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
import org.elasticsearch.action.get.MultiGetResponse;
|
||||
|
||||
/**
|
||||
* @author Mohsin Husen
|
||||
*/
|
||||
public interface MultiGetResultMapper {
|
||||
|
||||
<T> LinkedList<T> mapResults(MultiGetResponse responses, Class<T> clazz);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013 the original author or authors.
|
||||
* Copyright 2013-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,7 +23,7 @@ package org.springframework.data.elasticsearch.core;
|
||||
* @author Artur Konczak
|
||||
*/
|
||||
|
||||
public interface ResultsMapper extends SearchResultMapper, GetResultMapper {
|
||||
public interface ResultsMapper extends SearchResultMapper, GetResultMapper, MultiGetResultMapper {
|
||||
|
||||
EntityMapper getEntityMapper();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user