mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-07-06 02:32:12 +00:00
DATAES-328 - Integrate Data Commons Java 8 upgrade branch.
This commit is contained in:
parent
64bad236da
commit
0cada7f374
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2014-2016 the original author or authors.
|
* Copyright 2014-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -15,7 +15,6 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.elasticsearch.core;
|
package org.springframework.data.elasticsearch.core;
|
||||||
|
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
@ -23,10 +22,8 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonEncoding;
|
|
||||||
import com.fasterxml.jackson.core.JsonFactory;
|
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.elasticsearch.action.get.GetResponse;
|
import org.elasticsearch.action.get.GetResponse;
|
||||||
import org.elasticsearch.action.get.MultiGetItemResponse;
|
import org.elasticsearch.action.get.MultiGetItemResponse;
|
||||||
@ -42,14 +39,18 @@ import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
|
|||||||
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
|
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
|
||||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
|
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
|
||||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
||||||
import org.springframework.data.mapping.PersistentProperty;
|
|
||||||
import org.springframework.data.mapping.context.MappingContext;
|
import org.springframework.data.mapping.context.MappingContext;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonEncoding;
|
||||||
|
import com.fasterxml.jackson.core.JsonFactory;
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Artur Konczak
|
* @author Artur Konczak
|
||||||
* @author Petar Tahchiev
|
* @author Petar Tahchiev
|
||||||
* @author Young Gu
|
* @author Young Gu
|
||||||
* @author Oliver Gierke
|
* @author Oliver Gierke
|
||||||
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
public class DefaultResultMapper extends AbstractResultMapper {
|
public class DefaultResultMapper extends AbstractResultMapper {
|
||||||
|
|
||||||
@ -119,7 +120,6 @@ public class DefaultResultMapper extends AbstractResultMapper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private <T> T mapEntity(Collection<SearchHitField> values, Class<T> clazz) {
|
private <T> T mapEntity(Collection<SearchHitField> values, Class<T> clazz) {
|
||||||
return mapEntity(buildJSONFromFields(values), clazz);
|
return mapEntity(buildJSONFromFields(values), clazz);
|
||||||
}
|
}
|
||||||
@ -175,13 +175,17 @@ public class DefaultResultMapper extends AbstractResultMapper {
|
|||||||
|
|
||||||
if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {
|
if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {
|
||||||
|
|
||||||
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(clazz);
|
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(clazz);
|
||||||
PersistentProperty<?> idProperty = persistentEntity.getIdProperty();
|
Optional<ElasticsearchPersistentProperty> idProperty = persistentEntity.getIdProperty();
|
||||||
|
|
||||||
// Only deal with String because ES generated Ids are strings !
|
// Only deal with String because ES generated Ids are strings !
|
||||||
if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
|
|
||||||
persistentEntity.getPropertyAccessor(result).setProperty(idProperty, id);
|
idProperty.ifPresent(property -> {
|
||||||
}
|
if (property.getType().isAssignableFrom(String.class)) {
|
||||||
|
persistentEntity.getPropertyAccessor(result).setProperty(property, Optional.ofNullable(id));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2016 the original author or authors.
|
* Copyright 2013-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -15,8 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.elasticsearch.core;
|
package org.springframework.data.elasticsearch.core;
|
||||||
|
|
||||||
import static org.apache.commons.lang.StringUtils.isNotBlank;
|
import static org.apache.commons.lang.StringUtils.*;
|
||||||
import static org.apache.commons.lang.StringUtils.isBlank;
|
|
||||||
import static org.elasticsearch.action.search.SearchType.*;
|
import static org.elasticsearch.action.search.SearchType.*;
|
||||||
import static org.elasticsearch.client.Requests.*;
|
import static org.elasticsearch.client.Requests.*;
|
||||||
import static org.elasticsearch.cluster.metadata.AliasAction.Type.*;
|
import static org.elasticsearch.cluster.metadata.AliasAction.Type.*;
|
||||||
@ -28,8 +27,14 @@ import static org.springframework.util.CollectionUtils.isEmpty;
|
|||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.lang.reflect.Method;
|
import java.util.ArrayList;
|
||||||
import java.util.*;
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.elasticsearch.action.ListenableActionFuture;
|
import org.elasticsearch.action.ListenableActionFuture;
|
||||||
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
|
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
|
||||||
@ -76,7 +81,10 @@ import org.springframework.beans.BeansException;
|
|||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.ApplicationContextAware;
|
import org.springframework.context.ApplicationContextAware;
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
import org.springframework.data.domain.*;
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.data.elasticsearch.ElasticsearchException;
|
import org.springframework.data.elasticsearch.ElasticsearchException;
|
||||||
import org.springframework.data.elasticsearch.annotations.Document;
|
import org.springframework.data.elasticsearch.annotations.Document;
|
||||||
import org.springframework.data.elasticsearch.annotations.Mapping;
|
import org.springframework.data.elasticsearch.annotations.Mapping;
|
||||||
@ -87,9 +95,9 @@ import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverte
|
|||||||
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
|
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
|
||||||
import org.springframework.data.elasticsearch.core.facet.FacetRequest;
|
import org.springframework.data.elasticsearch.core.facet.FacetRequest;
|
||||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
|
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
|
||||||
|
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
||||||
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
|
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
|
||||||
import org.springframework.data.elasticsearch.core.query.*;
|
import org.springframework.data.elasticsearch.core.query.*;
|
||||||
import org.springframework.data.mapping.PersistentProperty;
|
|
||||||
import org.springframework.data.util.CloseableIterator;
|
import org.springframework.data.util.CloseableIterator;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
@ -104,8 +112,8 @@ import org.springframework.util.Assert;
|
|||||||
* @author Young Gu
|
* @author Young Gu
|
||||||
* @author Oliver Gierke
|
* @author Oliver Gierke
|
||||||
* @author Mark Janssen
|
* @author Mark Janssen
|
||||||
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class ElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware {
|
public class ElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ElasticsearchTemplate.class);
|
private static final Logger logger = LoggerFactory.getLogger(ElasticsearchTemplate.class);
|
||||||
@ -122,8 +130,10 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
this(client, new MappingElasticsearchConverter(new SimpleElasticsearchMappingContext()), entityMapper);
|
this(client, new MappingElasticsearchConverter(new SimpleElasticsearchMappingContext()), entityMapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ElasticsearchTemplate(Client client, ElasticsearchConverter elasticsearchConverter, EntityMapper entityMapper) {
|
public ElasticsearchTemplate(Client client, ElasticsearchConverter elasticsearchConverter,
|
||||||
this(client, elasticsearchConverter, new DefaultResultMapper(elasticsearchConverter.getMappingContext(), entityMapper));
|
EntityMapper entityMapper) {
|
||||||
|
this(client, elasticsearchConverter,
|
||||||
|
new DefaultResultMapper(elasticsearchConverter.getMappingContext(), entityMapper));
|
||||||
}
|
}
|
||||||
|
|
||||||
public ElasticsearchTemplate(Client client, ResultsMapper resultsMapper) {
|
public ElasticsearchTemplate(Client client, ResultsMapper resultsMapper) {
|
||||||
@ -134,7 +144,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
this(client, elasticsearchConverter, new DefaultResultMapper(elasticsearchConverter.getMappingContext()));
|
this(client, elasticsearchConverter, new DefaultResultMapper(elasticsearchConverter.getMappingContext()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public ElasticsearchTemplate(Client client, ElasticsearchConverter elasticsearchConverter, ResultsMapper resultsMapper) {
|
public ElasticsearchTemplate(Client client, ElasticsearchConverter elasticsearchConverter,
|
||||||
|
ResultsMapper resultsMapper) {
|
||||||
|
|
||||||
Assert.notNull(client, "Client must not be null!");
|
Assert.notNull(client, "Client must not be null!");
|
||||||
Assert.notNull(elasticsearchConverter, "ElasticsearchConverter must not be null!");
|
Assert.notNull(elasticsearchConverter, "ElasticsearchConverter must not be null!");
|
||||||
@ -162,9 +173,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
@Override
|
@Override
|
||||||
public boolean createIndex(String indexName) {
|
public boolean createIndex(String indexName) {
|
||||||
Assert.notNull(indexName, "No index defined for Query");
|
Assert.notNull(indexName, "No index defined for Query");
|
||||||
return client.admin().indices()
|
return client.admin().indices().create(Requests.createIndexRequest(indexName)).actionGet().isAcknowledged();
|
||||||
.create(Requests.createIndexRequest(indexName))
|
|
||||||
.actionGet().isAcknowledged();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -183,8 +192,13 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
|
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
|
||||||
XContentBuilder xContentBuilder = null;
|
XContentBuilder xContentBuilder = null;
|
||||||
try {
|
try {
|
||||||
xContentBuilder = buildMapping(clazz, persistentEntity.getIndexType(), persistentEntity
|
|
||||||
.getIdProperty().getFieldName(), persistentEntity.getParentType());
|
Optional<ElasticsearchPersistentProperty> idProperty = persistentEntity.getIdProperty();
|
||||||
|
|
||||||
|
ElasticsearchPersistentProperty property = idProperty.orElseThrow(() -> new IllegalArgumentException(String.format("No Id property for %s found", clazz.getSimpleName())));
|
||||||
|
|
||||||
|
xContentBuilder = buildMapping(clazz, persistentEntity.getIndexType(),
|
||||||
|
property.getFieldName(), persistentEntity.getParentType().orElse(null));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ElasticsearchException("Failed to build mapping for " + clazz.getSimpleName(), e);
|
throw new ElasticsearchException("Failed to build mapping for " + clazz.getSimpleName(), e);
|
||||||
}
|
}
|
||||||
@ -193,15 +207,15 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> boolean putMapping(Class<T> clazz, Object mapping) {
|
public <T> boolean putMapping(Class<T> clazz, Object mapping) {
|
||||||
return putMapping(getPersistentEntityFor(clazz).getIndexName(), getPersistentEntityFor(clazz).getIndexType(), mapping);
|
return putMapping(getPersistentEntityFor(clazz).getIndexName(), getPersistentEntityFor(clazz).getIndexType(),
|
||||||
|
mapping);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean putMapping(String indexName, String type, Object mapping) {
|
public boolean putMapping(String indexName, String type, Object mapping) {
|
||||||
Assert.notNull(indexName, "No index defined for putMapping()");
|
Assert.notNull(indexName, "No index defined for putMapping()");
|
||||||
Assert.notNull(type, "No type defined for putMapping()");
|
Assert.notNull(type, "No type defined for putMapping()");
|
||||||
PutMappingRequestBuilder requestBuilder = client.admin().indices()
|
PutMappingRequestBuilder requestBuilder = client.admin().indices().preparePutMapping(indexName).setType(type);
|
||||||
.preparePutMapping(indexName).setType(type);
|
|
||||||
if (mapping instanceof String) {
|
if (mapping instanceof String) {
|
||||||
requestBuilder.setSource(String.valueOf(mapping));
|
requestBuilder.setSource(String.valueOf(mapping));
|
||||||
} else if (mapping instanceof Map) {
|
} else if (mapping instanceof Map) {
|
||||||
@ -221,7 +235,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
mappings = client.admin().indices().getMappings(new GetMappingsRequest().indices(indexName).types(type))
|
mappings = client.admin().indices().getMappings(new GetMappingsRequest().indices(indexName).types(type))
|
||||||
.actionGet().getMappings().get(indexName).get(type).getSourceAsMap();
|
.actionGet().getMappings().get(indexName).get(type).getSourceAsMap();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ElasticsearchException("Error while getting mapping for indexName : " + indexName + " type : " + type + " " + e.getMessage());
|
throw new ElasticsearchException(
|
||||||
|
"Error while getting mapping for indexName : " + indexName + " type : " + type + " " + e.getMessage());
|
||||||
}
|
}
|
||||||
return mappings;
|
return mappings;
|
||||||
}
|
}
|
||||||
@ -311,7 +326,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
@Override
|
@Override
|
||||||
public <T> Page<T> queryForPage(CriteriaQuery criteriaQuery, Class<T> clazz) {
|
public <T> Page<T> queryForPage(CriteriaQuery criteriaQuery, Class<T> clazz) {
|
||||||
QueryBuilder elasticsearchQuery = new CriteriaQueryProcessor().createQueryFromCriteria(criteriaQuery.getCriteria());
|
QueryBuilder elasticsearchQuery = new CriteriaQueryProcessor().createQueryFromCriteria(criteriaQuery.getCriteria());
|
||||||
QueryBuilder elasticsearchFilter = new CriteriaFilterProcessor().createFilterFromCriteria(criteriaQuery.getCriteria());
|
QueryBuilder elasticsearchFilter = new CriteriaFilterProcessor()
|
||||||
|
.createFilterFromCriteria(criteriaQuery.getCriteria());
|
||||||
SearchRequestBuilder searchRequestBuilder = prepareSearch(criteriaQuery, clazz);
|
SearchRequestBuilder searchRequestBuilder = prepareSearch(criteriaQuery, clazz);
|
||||||
|
|
||||||
if (elasticsearchQuery != null) {
|
if (elasticsearchQuery != null) {
|
||||||
@ -330,8 +346,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
logger.debug("doSearch query:\n" + searchRequestBuilder.toString());
|
logger.debug("doSearch query:\n" + searchRequestBuilder.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
SearchResponse response = getSearchResponse(searchRequestBuilder
|
SearchResponse response = getSearchResponse(searchRequestBuilder.execute());
|
||||||
.execute());
|
|
||||||
return resultsMapper.mapResults(response, clazz, criteriaQuery.getPageable());
|
return resultsMapper.mapResults(response, clazz, criteriaQuery.getPageable());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -365,7 +380,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
return doStream(initScrollId, scrollTimeInMillis, clazz, mapper);
|
return doStream(initScrollId, scrollTimeInMillis, clazz, mapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> CloseableIterator<T> doStream(final String initScrollId, final long scrollTimeInMillis, final Class<T> clazz, final SearchResultMapper mapper) {
|
private <T> CloseableIterator<T> doStream(final String initScrollId, final long scrollTimeInMillis,
|
||||||
|
final Class<T> clazz, final SearchResultMapper mapper) {
|
||||||
return new CloseableIterator<T>() {
|
return new CloseableIterator<T>() {
|
||||||
|
|
||||||
/** As we couldn't retrieve single result with scroll, store current hits. */
|
/** As we couldn't retrieve single result with scroll, store current hits. */
|
||||||
@ -399,10 +415,10 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
// Test if it remains hits
|
// Test if it remains hits
|
||||||
if (currentHits == null || !currentHits.hasNext()) {
|
if (currentHits == null || !currentHits.hasNext()) {
|
||||||
// Do a new request
|
// Do a new request
|
||||||
SearchResponse response = getSearchResponse(client.prepareSearchScroll(scrollId)
|
SearchResponse response = getSearchResponse(
|
||||||
.setScroll(TimeValue.timeValueMillis(scrollTimeInMillis)).execute());
|
client.prepareSearchScroll(scrollId).setScroll(TimeValue.timeValueMillis(scrollTimeInMillis)).execute());
|
||||||
// Save hits and scroll id
|
// Save hits and scroll id
|
||||||
currentHits = mapper.mapResults(response, clazz, null).iterator();
|
currentHits = mapper.mapResults(response, clazz, Pageable.NONE).iterator();
|
||||||
finished = !currentHits.hasNext();
|
finished = !currentHits.hasNext();
|
||||||
scrollId = response.getScrollId();
|
scrollId = response.getScrollId();
|
||||||
}
|
}
|
||||||
@ -427,7 +443,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
@Override
|
@Override
|
||||||
public <T> long count(CriteriaQuery criteriaQuery, Class<T> clazz) {
|
public <T> long count(CriteriaQuery criteriaQuery, Class<T> clazz) {
|
||||||
QueryBuilder elasticsearchQuery = new CriteriaQueryProcessor().createQueryFromCriteria(criteriaQuery.getCriteria());
|
QueryBuilder elasticsearchQuery = new CriteriaQueryProcessor().createQueryFromCriteria(criteriaQuery.getCriteria());
|
||||||
QueryBuilder elasticsearchFilter = new CriteriaFilterProcessor().createFilterFromCriteria(criteriaQuery.getCriteria());
|
QueryBuilder elasticsearchFilter = new CriteriaFilterProcessor()
|
||||||
|
.createFilterFromCriteria(criteriaQuery.getCriteria());
|
||||||
|
|
||||||
if (elasticsearchFilter == null) {
|
if (elasticsearchFilter == null) {
|
||||||
return doCount(prepareCount(criteriaQuery, clazz), elasticsearchQuery);
|
return doCount(prepareCount(criteriaQuery, clazz), elasticsearchQuery);
|
||||||
@ -467,7 +484,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
return countRequestBuilder.execute().actionGet().getCount();
|
return countRequestBuilder.execute().actionGet().getCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
private long doCount(SearchRequestBuilder searchRequestBuilder, QueryBuilder elasticsearchQuery, QueryBuilder elasticsearchFilter) {
|
private long doCount(SearchRequestBuilder searchRequestBuilder, QueryBuilder elasticsearchQuery,
|
||||||
|
QueryBuilder elasticsearchFilter) {
|
||||||
if (elasticsearchQuery != null) {
|
if (elasticsearchQuery != null) {
|
||||||
searchRequestBuilder.setQuery(elasticsearchQuery);
|
searchRequestBuilder.setQuery(elasticsearchQuery);
|
||||||
} else {
|
} else {
|
||||||
@ -481,8 +499,11 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
}
|
}
|
||||||
|
|
||||||
private <T> CountRequestBuilder prepareCount(Query query, Class<T> clazz) {
|
private <T> CountRequestBuilder prepareCount(Query query, Class<T> clazz) {
|
||||||
String indexName[] = !isEmpty(query.getIndices()) ? query.getIndices().toArray(new String[query.getIndices().size()]) : retrieveIndexNameFromPersistentEntity(clazz);
|
String indexName[] = !isEmpty(query.getIndices())
|
||||||
String types[] = !isEmpty(query.getTypes()) ? query.getTypes().toArray(new String[query.getTypes().size()]) : retrieveTypeFromPersistentEntity(clazz);
|
? query.getIndices().toArray(new String[query.getIndices().size()])
|
||||||
|
: retrieveIndexNameFromPersistentEntity(clazz);
|
||||||
|
String types[] = !isEmpty(query.getTypes()) ? query.getTypes().toArray(new String[query.getTypes().size()])
|
||||||
|
: retrieveTypeFromPersistentEntity(clazz);
|
||||||
|
|
||||||
Assert.notNull(indexName, "No index defined for Query");
|
Assert.notNull(indexName, "No index defined for Query");
|
||||||
|
|
||||||
@ -501,8 +522,10 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
|
|
||||||
private <T> MultiGetResponse getMultiResponse(Query searchQuery, Class<T> clazz) {
|
private <T> MultiGetResponse getMultiResponse(Query searchQuery, Class<T> clazz) {
|
||||||
|
|
||||||
String indexName = !isEmpty(searchQuery.getIndices()) ? searchQuery.getIndices().get(0) : getPersistentEntityFor(clazz).getIndexName();
|
String indexName = !isEmpty(searchQuery.getIndices()) ? searchQuery.getIndices().get(0)
|
||||||
String type = !isEmpty(searchQuery.getTypes()) ? searchQuery.getTypes().get(0) : getPersistentEntityFor(clazz).getIndexType();
|
: getPersistentEntityFor(clazz).getIndexName();
|
||||||
|
String type = !isEmpty(searchQuery.getTypes()) ? searchQuery.getTypes().get(0)
|
||||||
|
: getPersistentEntityFor(clazz).getIndexType();
|
||||||
|
|
||||||
Assert.notNull(indexName, "No index defined for Query");
|
Assert.notNull(indexName, "No index defined for Query");
|
||||||
Assert.notNull(type, "No type define for Query");
|
Assert.notNull(type, "No type define for Query");
|
||||||
@ -547,8 +570,10 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
}
|
}
|
||||||
|
|
||||||
private UpdateRequestBuilder prepareUpdate(UpdateQuery query) {
|
private UpdateRequestBuilder prepareUpdate(UpdateQuery query) {
|
||||||
String indexName = isNotBlank(query.getIndexName()) ? query.getIndexName() : getPersistentEntityFor(query.getClazz()).getIndexName();
|
String indexName = isNotBlank(query.getIndexName()) ? query.getIndexName()
|
||||||
String type = isNotBlank(query.getType()) ? query.getType() : getPersistentEntityFor(query.getClazz()).getIndexType();
|
: getPersistentEntityFor(query.getClazz()).getIndexName();
|
||||||
|
String type = isNotBlank(query.getType()) ? query.getType()
|
||||||
|
: getPersistentEntityFor(query.getClazz()).getIndexType();
|
||||||
Assert.notNull(indexName, "No index defined for Query");
|
Assert.notNull(indexName, "No index defined for Query");
|
||||||
Assert.notNull(type, "No type define for Query");
|
Assert.notNull(type, "No type define for Query");
|
||||||
Assert.notNull(query.getId(), "No Id define for Query");
|
Assert.notNull(query.getId(), "No Id define for Query");
|
||||||
@ -559,8 +584,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
if (query.getUpdateRequest().script() == null) {
|
if (query.getUpdateRequest().script() == null) {
|
||||||
// doc
|
// doc
|
||||||
if (query.DoUpsert()) {
|
if (query.DoUpsert()) {
|
||||||
updateRequestBuilder.setDocAsUpsert(true)
|
updateRequestBuilder.setDocAsUpsert(true).setDoc(query.getUpdateRequest().doc());
|
||||||
.setDoc(query.getUpdateRequest().doc());
|
|
||||||
} else {
|
} else {
|
||||||
updateRequestBuilder.setDoc(query.getUpdateRequest().doc());
|
updateRequestBuilder.setDoc(query.getUpdateRequest().doc());
|
||||||
}
|
}
|
||||||
@ -587,8 +611,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
}
|
}
|
||||||
throw new ElasticsearchException(
|
throw new ElasticsearchException(
|
||||||
"Bulk indexing has failures. Use ElasticsearchException.getFailedDocuments() for detailed messages ["
|
"Bulk indexing has failures. Use ElasticsearchException.getFailedDocuments() for detailed messages ["
|
||||||
+ failedDocuments + "]", failedDocuments
|
+ failedDocuments + "]",
|
||||||
);
|
failedDocuments);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -607,8 +631,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
}
|
}
|
||||||
throw new ElasticsearchException(
|
throw new ElasticsearchException(
|
||||||
"Bulk indexing has failures. Use ElasticsearchException.getFailedDocuments() for detailed messages ["
|
"Bulk indexing has failures. Use ElasticsearchException.getFailedDocuments() for detailed messages ["
|
||||||
+ failedDocuments + "]", failedDocuments
|
+ failedDocuments + "]",
|
||||||
);
|
failedDocuments);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -624,8 +648,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean typeExists(String index, String type) {
|
public boolean typeExists(String index, String type) {
|
||||||
return client.admin().cluster().prepareState().execute().actionGet()
|
return client.admin().cluster().prepareState().execute().actionGet().getState().metaData().index(index)
|
||||||
.getState().metaData().index(index).getMappings().containsKey(type);
|
.getMappings().containsKey(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -656,16 +680,16 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
@Override
|
@Override
|
||||||
public <T> void delete(DeleteQuery deleteQuery, Class<T> clazz) {
|
public <T> void delete(DeleteQuery deleteQuery, Class<T> clazz) {
|
||||||
|
|
||||||
String indexName = isNotBlank(deleteQuery.getIndex()) ? deleteQuery.getIndex() : getPersistentEntityFor(clazz).getIndexName();
|
String indexName = isNotBlank(deleteQuery.getIndex()) ? deleteQuery.getIndex()
|
||||||
String typeName = isNotBlank(deleteQuery.getType()) ? deleteQuery.getType() : getPersistentEntityFor(clazz).getIndexType();
|
: getPersistentEntityFor(clazz).getIndexName();
|
||||||
|
String typeName = isNotBlank(deleteQuery.getType()) ? deleteQuery.getType()
|
||||||
|
: getPersistentEntityFor(clazz).getIndexType();
|
||||||
Integer pageSize = deleteQuery.getPageSize() != null ? deleteQuery.getPageSize() : 1000;
|
Integer pageSize = deleteQuery.getPageSize() != null ? deleteQuery.getPageSize() : 1000;
|
||||||
Long scrollTimeInMillis = deleteQuery.getScrollTimeInMillis() != null ? deleteQuery.getScrollTimeInMillis() : 10000l;
|
Long scrollTimeInMillis = deleteQuery.getScrollTimeInMillis() != null ? deleteQuery.getScrollTimeInMillis()
|
||||||
|
: 10000l;
|
||||||
|
|
||||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(deleteQuery.getQuery())
|
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(deleteQuery.getQuery()).withIndices(indexName)
|
||||||
.withIndices(indexName)
|
.withTypes(typeName).withPageable(new PageRequest(0, pageSize)).build();
|
||||||
.withTypes(typeName)
|
|
||||||
.withPageable(new PageRequest(0, pageSize))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
String scrollId = scan(searchQuery, scrollTimeInMillis, true);
|
String scrollId = scan(searchQuery, scrollTimeInMillis, true);
|
||||||
|
|
||||||
@ -694,11 +718,11 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for(String id : ids) {
|
for (String id : ids) {
|
||||||
bulkRequestBuilder.add(client.prepareDelete(indexName, typeName, id));
|
bulkRequestBuilder.add(client.prepareDelete(indexName, typeName, id));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(bulkRequestBuilder.numberOfActions() > 0) {
|
if (bulkRequestBuilder.numberOfActions() > 0) {
|
||||||
bulkRequestBuilder.execute().actionGet();
|
bulkRequestBuilder.execute().actionGet();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -748,9 +772,11 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
|
|
||||||
private SearchRequestBuilder prepareScan(Query query, long scrollTimeInMillis, boolean noFields) {
|
private SearchRequestBuilder prepareScan(Query query, long scrollTimeInMillis, boolean noFields) {
|
||||||
SearchRequestBuilder requestBuilder = client.prepareSearch(toArray(query.getIndices())).setSearchType(SCAN)
|
SearchRequestBuilder requestBuilder = client.prepareSearch(toArray(query.getIndices())).setSearchType(SCAN)
|
||||||
.setTypes(toArray(query.getTypes()))
|
.setTypes(toArray(query.getTypes())).setScroll(TimeValue.timeValueMillis(scrollTimeInMillis)).setFrom(0);
|
||||||
.setScroll(TimeValue.timeValueMillis(scrollTimeInMillis)).setFrom(0)
|
|
||||||
.setSize(query.getPageable().getPageSize());
|
if(query.getPageable() != Pageable.NONE){
|
||||||
|
requestBuilder.setSize(query.getPageable().getPageSize());
|
||||||
|
}
|
||||||
|
|
||||||
if (!isEmpty(query.getFields())) {
|
if (!isEmpty(query.getFields())) {
|
||||||
requestBuilder.addFields(toArray(query.getFields()));
|
requestBuilder.addFields(toArray(query.getFields()));
|
||||||
@ -768,7 +794,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
Assert.notNull(criteriaQuery.getPageable(), "Query.pageable is required for scan & scroll");
|
Assert.notNull(criteriaQuery.getPageable(), "Query.pageable is required for scan & scroll");
|
||||||
|
|
||||||
QueryBuilder elasticsearchQuery = new CriteriaQueryProcessor().createQueryFromCriteria(criteriaQuery.getCriteria());
|
QueryBuilder elasticsearchQuery = new CriteriaQueryProcessor().createQueryFromCriteria(criteriaQuery.getCriteria());
|
||||||
QueryBuilder elasticsearchFilter = new CriteriaFilterProcessor().createFilterFromCriteria(criteriaQuery.getCriteria());
|
QueryBuilder elasticsearchFilter = new CriteriaFilterProcessor()
|
||||||
|
.createFilterFromCriteria(criteriaQuery.getCriteria());
|
||||||
|
|
||||||
if (elasticsearchQuery != null) {
|
if (elasticsearchQuery != null) {
|
||||||
requestBuilder.setQuery(elasticsearchQuery);
|
requestBuilder.setQuery(elasticsearchQuery);
|
||||||
@ -797,16 +824,16 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> Page<T> scroll(String scrollId, long scrollTimeInMillis, Class<T> clazz) {
|
public <T> Page<T> scroll(String scrollId, long scrollTimeInMillis, Class<T> clazz) {
|
||||||
SearchResponse response = getSearchResponse(client.prepareSearchScroll(scrollId)
|
SearchResponse response = getSearchResponse(
|
||||||
.setScroll(TimeValue.timeValueMillis(scrollTimeInMillis)).execute());
|
client.prepareSearchScroll(scrollId).setScroll(TimeValue.timeValueMillis(scrollTimeInMillis)).execute());
|
||||||
return resultsMapper.mapResults(response, clazz, null);
|
return resultsMapper.mapResults(response, clazz, Pageable.NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> Page<T> scroll(String scrollId, long scrollTimeInMillis, SearchResultMapper mapper) {
|
public <T> Page<T> scroll(String scrollId, long scrollTimeInMillis, SearchResultMapper mapper) {
|
||||||
SearchResponse response = getSearchResponse(client.prepareSearchScroll(scrollId)
|
SearchResponse response = getSearchResponse(
|
||||||
.setScroll(TimeValue.timeValueMillis(scrollTimeInMillis)).execute());
|
client.prepareSearchScroll(scrollId).setScroll(TimeValue.timeValueMillis(scrollTimeInMillis)).execute());
|
||||||
return mapper.mapResults(response, null, null);
|
return mapper.mapResults(response, null, Pageable.NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -960,8 +987,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
@Override
|
@Override
|
||||||
public Map getSetting(String indexName) {
|
public Map getSetting(String indexName) {
|
||||||
Assert.notNull(indexName, "No index defined for getSettings");
|
Assert.notNull(indexName, "No index defined for getSettings");
|
||||||
return client.admin().indices().getSettings(new GetSettingsRequest())
|
return client.admin().indices().getSettings(new GetSettingsRequest()).actionGet().getIndexToSettings()
|
||||||
.actionGet().getIndexToSettings().get(indexName).getAsMap();
|
.get(indexName).getAsMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> SearchRequestBuilder prepareSearch(Query query, Class<T> clazz) {
|
private <T> SearchRequestBuilder prepareSearch(Query query, Class<T> clazz) {
|
||||||
@ -982,7 +1009,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
searchRequestBuilder.setFetchSource(sourceFilter.getIncludes(), sourceFilter.getExcludes());
|
searchRequestBuilder.setFetchSource(sourceFilter.getIncludes(), sourceFilter.getExcludes());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (query.getPageable() != null) {
|
if (query.getPageable() != null && query.getPageable() != Pageable.NONE) {
|
||||||
startRecord = query.getPageable().getPageNumber() * query.getPageable().getPageSize();
|
startRecord = query.getPageable().getPageNumber() * query.getPageable().getPageSize();
|
||||||
searchRequestBuilder.setSize(query.getPageable().getPageSize());
|
searchRequestBuilder.setSize(query.getPageable().getPageSize());
|
||||||
}
|
}
|
||||||
@ -994,8 +1021,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
|
|
||||||
if (query.getSort() != null) {
|
if (query.getSort() != null) {
|
||||||
for (Sort.Order order : query.getSort()) {
|
for (Sort.Order order : query.getSort()) {
|
||||||
searchRequestBuilder.addSort(order.getProperty(), order.getDirection() == Sort.Direction.DESC ? SortOrder.DESC
|
searchRequestBuilder.addSort(order.getProperty(),
|
||||||
: SortOrder.ASC);
|
order.getDirection() == Sort.Direction.DESC ? SortOrder.DESC : SortOrder.ASC);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1007,8 +1034,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
|
|
||||||
private IndexRequestBuilder prepareIndex(IndexQuery query) {
|
private IndexRequestBuilder prepareIndex(IndexQuery query) {
|
||||||
try {
|
try {
|
||||||
String indexName = isBlank(query.getIndexName()) ? retrieveIndexNameFromPersistentEntity(query.getObject()
|
String indexName = isBlank(query.getIndexName())
|
||||||
.getClass())[0] : query.getIndexName();
|
? retrieveIndexNameFromPersistentEntity(query.getObject().getClass())[0] : query.getIndexName();
|
||||||
String type = isBlank(query.getType()) ? retrieveTypeFromPersistentEntity(query.getObject().getClass())[0]
|
String type = isBlank(query.getType()) ? retrieveTypeFromPersistentEntity(query.getObject().getClass())[0]
|
||||||
: query.getType();
|
: query.getType();
|
||||||
|
|
||||||
@ -1026,7 +1053,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
} else if (query.getSource() != null) {
|
} else if (query.getSource() != null) {
|
||||||
indexRequestBuilder = client.prepareIndex(indexName, type, query.getId()).setSource(query.getSource());
|
indexRequestBuilder = client.prepareIndex(indexName, type, query.getId()).setSource(query.getSource());
|
||||||
} else {
|
} else {
|
||||||
throw new ElasticsearchException("object or source is null, failed to index the document [id: " + query.getId() + "]");
|
throw new ElasticsearchException(
|
||||||
|
"object or source is null, failed to index the document [id: " + query.getId() + "]");
|
||||||
}
|
}
|
||||||
if (query.getVersion() != null) {
|
if (query.getVersion() != null) {
|
||||||
indexRequestBuilder.setVersion(query.getVersion());
|
indexRequestBuilder.setVersion(query.getVersion());
|
||||||
@ -1077,40 +1105,44 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
public Boolean removeAlias(AliasQuery query) {
|
public Boolean removeAlias(AliasQuery query) {
|
||||||
Assert.notNull(query.getIndexName(), "No index defined for Alias");
|
Assert.notNull(query.getIndexName(), "No index defined for Alias");
|
||||||
Assert.notNull(query.getAliasName(), "No alias defined");
|
Assert.notNull(query.getAliasName(), "No alias defined");
|
||||||
return client.admin().indices().prepareAliases().removeAlias(query.getIndexName(), query.getAliasName())
|
return client.admin().indices().prepareAliases().removeAlias(query.getIndexName(), query.getAliasName()).execute()
|
||||||
.execute().actionGet().isAcknowledged();
|
.actionGet().isAcknowledged();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<AliasMetaData> queryForAlias(String indexName) {
|
public List<AliasMetaData> queryForAlias(String indexName) {
|
||||||
return client.admin().indices().getAliases(new GetAliasesRequest().indices(indexName))
|
return client.admin().indices().getAliases(new GetAliasesRequest().indices(indexName)).actionGet().getAliases()
|
||||||
.actionGet().getAliases().get(indexName);
|
.get(indexName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ElasticsearchPersistentEntity getPersistentEntityFor(Class clazz) {
|
public ElasticsearchPersistentEntity getPersistentEntityFor(Class clazz) {
|
||||||
Assert.isTrue(clazz.isAnnotationPresent(Document.class), "Unable to identify index name. " + clazz.getSimpleName()
|
Assert.isTrue(clazz.isAnnotationPresent(Document.class), "Unable to identify index name. " + clazz.getSimpleName()
|
||||||
+ " is not a Document. Make sure the document class is annotated with @Document(indexName=\"foo\")");
|
+ " is not a Document. Make sure the document class is annotated with @Document(indexName=\"foo\")");
|
||||||
return elasticsearchConverter.getMappingContext().getPersistentEntity(clazz);
|
return elasticsearchConverter.getMappingContext().getRequiredPersistentEntity(clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getPersistentEntityId(Object entity) {
|
private String getPersistentEntityId(Object entity) {
|
||||||
|
|
||||||
ElasticsearchPersistentEntity<?> persistentEntity = getPersistentEntityFor(entity.getClass());
|
ElasticsearchPersistentEntity<?> persistentEntity = getPersistentEntityFor(entity.getClass());
|
||||||
Object identifier = persistentEntity.getIdentifierAccessor(entity).getIdentifier();
|
Optional<Object> identifier = persistentEntity.getIdentifierAccessor(entity).getIdentifier();
|
||||||
|
|
||||||
return identifier == null ? null : String.valueOf(identifier);
|
return identifier.map(String::valueOf).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setPersistentEntityId(Object entity, String id) {
|
private void setPersistentEntityId(Object entity, String id) {
|
||||||
|
|
||||||
ElasticsearchPersistentEntity<?> persistentEntity = getPersistentEntityFor(entity.getClass());
|
ElasticsearchPersistentEntity<?> persistentEntity = getPersistentEntityFor(entity.getClass());
|
||||||
PersistentProperty<?> idProperty = persistentEntity.getIdProperty();
|
Optional<ElasticsearchPersistentProperty> idProperty = persistentEntity.getIdProperty();
|
||||||
|
|
||||||
// Only deal with String because ES generated Ids are strings !
|
// Only deal with String because ES generated Ids are strings !
|
||||||
if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
|
|
||||||
persistentEntity.getPropertyAccessor(entity).setProperty(idProperty,id);
|
idProperty.ifPresent(property -> {
|
||||||
}
|
|
||||||
|
if (property.getType().isAssignableFrom(String.class)) {
|
||||||
|
persistentEntity.getPropertyAccessor(entity).setProperty(property, Optional.ofNullable(id));
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setPersistentEntityIndexAndType(Query query, Class clazz) {
|
private void setPersistentEntityIndexAndType(Query query, Class clazz) {
|
||||||
@ -1124,14 +1156,14 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
|||||||
|
|
||||||
private String[] retrieveIndexNameFromPersistentEntity(Class clazz) {
|
private String[] retrieveIndexNameFromPersistentEntity(Class clazz) {
|
||||||
if (clazz != null) {
|
if (clazz != null) {
|
||||||
return new String[]{getPersistentEntityFor(clazz).getIndexName()};
|
return new String[] { getPersistentEntityFor(clazz).getIndexName() };
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String[] retrieveTypeFromPersistentEntity(Class clazz) {
|
private String[] retrieveTypeFromPersistentEntity(Class clazz) {
|
||||||
if (clazz != null) {
|
if (clazz != null) {
|
||||||
return new String[]{getPersistentEntityFor(clazz).getIndexType()};
|
return new String[] { getPersistentEntityFor(clazz).getIndexType() };
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2014 the original author or authors.
|
* Copyright 2013-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -15,6 +15,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.elasticsearch.core.mapping;
|
package org.springframework.data.elasticsearch.core.mapping;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.springframework.data.mapping.PersistentEntity;
|
import org.springframework.data.mapping.PersistentEntity;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -22,8 +24,8 @@ import org.springframework.data.mapping.PersistentEntity;
|
|||||||
*
|
*
|
||||||
* @author Rizwan Idrees
|
* @author Rizwan Idrees
|
||||||
* @author Mohsin Husen
|
* @author Mohsin Husen
|
||||||
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, ElasticsearchPersistentProperty> {
|
public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, ElasticsearchPersistentProperty> {
|
||||||
|
|
||||||
String getIndexName();
|
String getIndexName();
|
||||||
@ -40,11 +42,11 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
|
|||||||
|
|
||||||
String getIndexStoreType();
|
String getIndexStoreType();
|
||||||
|
|
||||||
ElasticsearchPersistentProperty getVersionProperty();
|
Optional<ElasticsearchPersistentProperty> getVersionProperty();
|
||||||
|
|
||||||
String getParentType();
|
Optional<String> getParentType();
|
||||||
|
|
||||||
ElasticsearchPersistentProperty getParentIdProperty();
|
Optional<ElasticsearchPersistentProperty> getParentIdProperty();
|
||||||
|
|
||||||
String settingPath();
|
String settingPath();
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013 the original author or authors.
|
* Copyright 2013-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -15,13 +15,11 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.elasticsearch.core.mapping;
|
package org.springframework.data.elasticsearch.core.mapping;
|
||||||
|
|
||||||
import java.beans.PropertyDescriptor;
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.ApplicationContextAware;
|
import org.springframework.context.ApplicationContextAware;
|
||||||
import org.springframework.data.mapping.context.AbstractMappingContext;
|
import org.springframework.data.mapping.context.AbstractMappingContext;
|
||||||
|
import org.springframework.data.mapping.model.Property;
|
||||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||||
import org.springframework.data.util.TypeInformation;
|
import org.springframework.data.util.TypeInformation;
|
||||||
|
|
||||||
@ -30,8 +28,8 @@ import org.springframework.data.util.TypeInformation;
|
|||||||
*
|
*
|
||||||
* @author Rizwan Idrees
|
* @author Rizwan Idrees
|
||||||
* @author Mohsin Husen
|
* @author Mohsin Husen
|
||||||
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class SimpleElasticsearchMappingContext extends
|
public class SimpleElasticsearchMappingContext extends
|
||||||
AbstractMappingContext<SimpleElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> implements ApplicationContextAware {
|
AbstractMappingContext<SimpleElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> implements ApplicationContextAware {
|
||||||
|
|
||||||
@ -39,8 +37,8 @@ public class SimpleElasticsearchMappingContext extends
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected <T> SimpleElasticsearchPersistentEntity<?> createPersistentEntity(TypeInformation<T> typeInformation) {
|
protected <T> SimpleElasticsearchPersistentEntity<?> createPersistentEntity(TypeInformation<T> typeInformation) {
|
||||||
final SimpleElasticsearchPersistentEntity<T> persistentEntity =
|
final SimpleElasticsearchPersistentEntity<T> persistentEntity = new SimpleElasticsearchPersistentEntity<T>(
|
||||||
new SimpleElasticsearchPersistentEntity<T>(typeInformation);
|
typeInformation);
|
||||||
if (context != null) {
|
if (context != null) {
|
||||||
persistentEntity.setApplicationContext(context);
|
persistentEntity.setApplicationContext(context);
|
||||||
}
|
}
|
||||||
@ -48,9 +46,9 @@ public class SimpleElasticsearchMappingContext extends
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ElasticsearchPersistentProperty createPersistentProperty(Field field, PropertyDescriptor descriptor,
|
protected ElasticsearchPersistentProperty createPersistentProperty(Property property,
|
||||||
SimpleElasticsearchPersistentEntity<?> owner, SimpleTypeHolder simpleTypeHolder) {
|
SimpleElasticsearchPersistentEntity<?> owner, SimpleTypeHolder simpleTypeHolder) {
|
||||||
return new SimpleElasticsearchPersistentProperty(field, descriptor, owner, simpleTypeHolder);
|
return new SimpleElasticsearchPersistentProperty(property, owner, simpleTypeHolder);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2014 the original author or authors.
|
* Copyright 2013-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -18,6 +18,7 @@ package org.springframework.data.elasticsearch.core.mapping;
|
|||||||
import static org.springframework.util.StringUtils.*;
|
import static org.springframework.util.StringUtils.*;
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
@ -41,6 +42,7 @@ import org.springframework.util.Assert;
|
|||||||
* @param <T>
|
* @param <T>
|
||||||
* @author Rizwan Idrees
|
* @author Rizwan Idrees
|
||||||
* @author Mohsin Husen
|
* @author Mohsin Husen
|
||||||
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntity<T, ElasticsearchPersistentProperty>
|
public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntity<T, ElasticsearchPersistentProperty>
|
||||||
implements ElasticsearchPersistentEntity<T>, ApplicationContextAware {
|
implements ElasticsearchPersistentEntity<T>, ApplicationContextAware {
|
||||||
@ -55,8 +57,8 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
|
|||||||
private short replicas;
|
private short replicas;
|
||||||
private String refreshInterval;
|
private String refreshInterval;
|
||||||
private String indexStoreType;
|
private String indexStoreType;
|
||||||
private String parentType;
|
private Optional<String> parentType = Optional.empty();
|
||||||
private ElasticsearchPersistentProperty parentIdProperty;
|
private Optional<ElasticsearchPersistentProperty> parentIdProperty = Optional.empty();
|
||||||
private String settingPath;
|
private String settingPath;
|
||||||
private boolean createIndexAndMapping;
|
private boolean createIndexAndMapping;
|
||||||
|
|
||||||
@ -129,12 +131,12 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getParentType() {
|
public Optional<String> getParentType() {
|
||||||
return parentType;
|
return parentType;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ElasticsearchPersistentProperty getParentIdProperty() {
|
public Optional<ElasticsearchPersistentProperty> getParentIdProperty() {
|
||||||
return parentIdProperty;
|
return parentIdProperty;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,16 +154,15 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
|
|||||||
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
|
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
|
||||||
super.addPersistentProperty(property);
|
super.addPersistentProperty(property);
|
||||||
|
|
||||||
if (property.getField() != null) {
|
Optional<Parent> annotation = property.findAnnotation(Parent.class);
|
||||||
Parent parent = property.getField().getAnnotation(Parent.class);
|
|
||||||
if (parent != null) {
|
annotation.ifPresent(parent -> {
|
||||||
Assert.isNull(this.parentIdProperty, "Only one field can hold a @Parent annotation");
|
Assert.isTrue(!this.parentIdProperty.isPresent(), "Only one field can hold a @Parent annotation");
|
||||||
Assert.isNull(this.parentType, "Only one field can hold a @Parent annotation");
|
Assert.isTrue(!this.parentType.isPresent(), "Only one field can hold a @Parent annotation");
|
||||||
Assert.isTrue(property.getType() == String.class, "Parent ID property should be String");
|
Assert.isTrue(property.getType() == String.class, "Parent ID property should be String");
|
||||||
this.parentIdProperty = property;
|
this.parentIdProperty = Optional.of(property);
|
||||||
this.parentType = parent.type();
|
this.parentType = Optional.of(parent.type());
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
||||||
if (property.isVersionProperty()) {
|
if (property.isVersionProperty()) {
|
||||||
Assert.isTrue(property.getType() == Long.class, "Version property should be Long");
|
Assert.isTrue(property.getType() == Long.class, "Version property should be Long");
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013 the original author or authors.
|
* Copyright 2013-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -15,14 +15,13 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.elasticsearch.core.mapping;
|
package org.springframework.data.elasticsearch.core.mapping;
|
||||||
|
|
||||||
import java.beans.PropertyDescriptor;
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.springframework.data.mapping.Association;
|
import org.springframework.data.mapping.Association;
|
||||||
import org.springframework.data.mapping.PersistentEntity;
|
import org.springframework.data.mapping.PersistentEntity;
|
||||||
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
|
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
|
||||||
|
import org.springframework.data.mapping.model.Property;
|
||||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -30,6 +29,7 @@ import org.springframework.data.mapping.model.SimpleTypeHolder;
|
|||||||
*
|
*
|
||||||
* @author Rizwan Idrees
|
* @author Rizwan Idrees
|
||||||
* @author Mohsin Husen
|
* @author Mohsin Husen
|
||||||
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
public class SimpleElasticsearchPersistentProperty extends
|
public class SimpleElasticsearchPersistentProperty extends
|
||||||
AnnotationBasedPersistentProperty<ElasticsearchPersistentProperty> implements ElasticsearchPersistentProperty {
|
AnnotationBasedPersistentProperty<ElasticsearchPersistentProperty> implements ElasticsearchPersistentProperty {
|
||||||
@ -43,19 +43,19 @@ public class SimpleElasticsearchPersistentProperty extends
|
|||||||
SUPPORTED_ID_PROPERTY_NAMES.add("documentId");
|
SUPPORTED_ID_PROPERTY_NAMES.add("documentId");
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimpleElasticsearchPersistentProperty(Field field, PropertyDescriptor propertyDescriptor,
|
public SimpleElasticsearchPersistentProperty(Property property,
|
||||||
PersistentEntity<?, ElasticsearchPersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
|
PersistentEntity<?, ElasticsearchPersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
|
||||||
super(field, propertyDescriptor, owner, simpleTypeHolder);
|
super(property, owner, simpleTypeHolder);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getFieldName() {
|
public String getFieldName() {
|
||||||
return field.getName();
|
return getProperty().getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isIdProperty() {
|
public boolean isIdProperty() {
|
||||||
return super.isIdProperty() || (field != null ? SUPPORTED_ID_PROPERTY_NAMES.contains(getFieldName()) : false);
|
return super.isIdProperty() || SUPPORTED_ID_PROPERTY_NAMES.contains(getFieldName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013 the original author or authors.
|
* Copyright 2013-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -23,24 +23,26 @@ import org.springframework.util.Assert;
|
|||||||
*
|
*
|
||||||
* @author Rizwan Idrees
|
* @author Rizwan Idrees
|
||||||
* @author Mohsin Husen
|
* @author Mohsin Husen
|
||||||
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
public class CriteriaQuery extends AbstractQuery {
|
public class CriteriaQuery extends AbstractQuery {
|
||||||
|
|
||||||
private Criteria criteria;
|
private Criteria criteria;
|
||||||
|
|
||||||
private CriteriaQuery() {
|
private CriteriaQuery() {}
|
||||||
}
|
|
||||||
|
|
||||||
public CriteriaQuery(Criteria criteria) {
|
public CriteriaQuery(Criteria criteria) {
|
||||||
this(criteria, null);
|
this(criteria, Pageable.NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CriteriaQuery(Criteria criteria, Pageable pageable) {
|
public CriteriaQuery(Criteria criteria, Pageable pageable) {
|
||||||
|
|
||||||
|
Assert.notNull(criteria, "Criteria must not be null!");
|
||||||
|
Assert.notNull(pageable, "Pageable must not be null!");
|
||||||
|
|
||||||
this.criteria = criteria;
|
this.criteria = criteria;
|
||||||
this.pageable = pageable;
|
this.pageable = pageable;
|
||||||
if (pageable != null) {
|
this.addSort(pageable.getSort());
|
||||||
this.addSort(pageable.getSort());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final Query fromQuery(CriteriaQuery source) {
|
public static final Query fromQuery(CriteriaQuery source) {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2014 the original author or authors.
|
* Copyright 2013-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -15,11 +15,13 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.elasticsearch.repository.cdi;
|
package org.springframework.data.elasticsearch.repository.cdi;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.enterprise.context.spi.CreationalContext;
|
import javax.enterprise.context.spi.CreationalContext;
|
||||||
import javax.enterprise.inject.spi.Bean;
|
import javax.enterprise.inject.spi.Bean;
|
||||||
import javax.enterprise.inject.spi.BeanManager;
|
import javax.enterprise.inject.spi.BeanManager;
|
||||||
import java.lang.annotation.Annotation;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
||||||
import org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactory;
|
import org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactory;
|
||||||
@ -28,7 +30,8 @@ import org.springframework.data.repository.config.CustomRepositoryImplementation
|
|||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uses CdiRepositoryBean to create ElasticsearchRepository instances.
|
* Uses {@link CdiRepositoryBean} to create
|
||||||
|
* {@link org.springframework.data.elasticsearch.repository.ElasticsearchRepository} instances.
|
||||||
*
|
*
|
||||||
* @author Rizwan Idrees
|
* @author Rizwan Idrees
|
||||||
* @author Mohsin Husen
|
* @author Mohsin Husen
|
||||||
@ -46,24 +49,36 @@ public class ElasticsearchRepositoryBean<T> extends CdiRepositoryBean<T> {
|
|||||||
* @param repositoryType must not be {@literal null}.
|
* @param repositoryType must not be {@literal null}.
|
||||||
* @param beanManager must not be {@literal null}.
|
* @param beanManager must not be {@literal null}.
|
||||||
* @param detector detector for the custom {@link org.springframework.data.repository.Repository} implementations
|
* @param detector detector for the custom {@link org.springframework.data.repository.Repository} implementations
|
||||||
* {@link CustomRepositoryImplementationDetector}, can be {@literal null}.
|
* {@link CustomRepositoryImplementationDetector}, can be {@literal null}.
|
||||||
*/
|
*/
|
||||||
public ElasticsearchRepositoryBean(Bean<ElasticsearchOperations> operations, Set<Annotation> qualifiers,
|
public ElasticsearchRepositoryBean(Bean<ElasticsearchOperations> operations, Set<Annotation> qualifiers,
|
||||||
Class<T> repositoryType, BeanManager beanManager, CustomRepositoryImplementationDetector detector) {
|
Class<T> repositoryType, BeanManager beanManager, Optional<CustomRepositoryImplementationDetector> detector) {
|
||||||
super(qualifiers, repositoryType, beanManager, detector);
|
super(qualifiers, repositoryType, beanManager, detector);
|
||||||
|
|
||||||
Assert.notNull(operations, "Cannot create repository with 'null' for ElasticsearchOperations.");
|
Assert.notNull(operations, "Cannot create repository with 'null' for ElasticsearchOperations.");
|
||||||
this.elasticsearchOperationsBean = operations;
|
this.elasticsearchOperationsBean = operations;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see org.springframework.data.repository.cdi.CdiRepositoryBean#create(javax.enterprise.context.spi.CreationalContext, java.lang.Class, java.util.Optional)
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType, Object customImplementation) {
|
protected T create(CreationalContext<T> creationalContext, Class<T> repositoryType,
|
||||||
|
Optional<Object> customImplementation) {
|
||||||
|
|
||||||
ElasticsearchOperations elasticsearchOperations = getDependencyInstance(elasticsearchOperationsBean,
|
ElasticsearchOperations elasticsearchOperations = getDependencyInstance(elasticsearchOperationsBean,
|
||||||
ElasticsearchOperations.class);
|
ElasticsearchOperations.class);
|
||||||
return new ElasticsearchRepositoryFactory(elasticsearchOperations).getRepository(repositoryType,
|
|
||||||
customImplementation);
|
ElasticsearchRepositoryFactory factory = new ElasticsearchRepositoryFactory(elasticsearchOperations);
|
||||||
|
|
||||||
|
return customImplementation //
|
||||||
|
.map(o -> factory.getRepository(repositoryType, o)) //
|
||||||
|
.orElseGet(() -> factory.getRepository(repositoryType));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see org.springframework.data.repository.cdi.CdiRepositoryBean#getScope()
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Class<? extends Annotation> getScope() {
|
public Class<? extends Annotation> getScope() {
|
||||||
return elasticsearchOperationsBean.getScope();
|
return elasticsearchOperationsBean.getScope();
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2016 the original author or authors.
|
* Copyright 2013-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -15,18 +15,20 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.elasticsearch.repository.cdi;
|
package org.springframework.data.elasticsearch.repository.cdi;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.enterprise.event.Observes;
|
import javax.enterprise.event.Observes;
|
||||||
import javax.enterprise.inject.UnsatisfiedResolutionException;
|
import javax.enterprise.inject.UnsatisfiedResolutionException;
|
||||||
import javax.enterprise.inject.spi.AfterBeanDiscovery;
|
import javax.enterprise.inject.spi.AfterBeanDiscovery;
|
||||||
import javax.enterprise.inject.spi.Bean;
|
import javax.enterprise.inject.spi.Bean;
|
||||||
import javax.enterprise.inject.spi.BeanManager;
|
import javax.enterprise.inject.spi.BeanManager;
|
||||||
import javax.enterprise.inject.spi.ProcessBean;
|
import javax.enterprise.inject.spi.ProcessBean;
|
||||||
import java.lang.annotation.Annotation;
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Map.Entry;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
||||||
import org.springframework.data.repository.cdi.CdiRepositoryBean;
|
import org.springframework.data.repository.cdi.CdiRepositoryBean;
|
||||||
@ -67,7 +69,7 @@ public class ElasticsearchRepositoryExtension extends CdiRepositoryExtensionSupp
|
|||||||
}
|
}
|
||||||
|
|
||||||
private <T> CdiRepositoryBean<T> createRepositoryBean(Class<T> repositoryType, Set<Annotation> qualifiers,
|
private <T> CdiRepositoryBean<T> createRepositoryBean(Class<T> repositoryType, Set<Annotation> qualifiers,
|
||||||
BeanManager beanManager) {
|
BeanManager beanManager) {
|
||||||
|
|
||||||
Bean<ElasticsearchOperations> elasticsearchOperationsBean = this.elasticsearchOperationsMap.get(qualifiers);
|
Bean<ElasticsearchOperations> elasticsearchOperationsBean = this.elasticsearchOperationsMap.get(qualifiers);
|
||||||
|
|
||||||
@ -77,6 +79,6 @@ public class ElasticsearchRepositoryExtension extends CdiRepositoryExtensionSupp
|
|||||||
}
|
}
|
||||||
|
|
||||||
return new ElasticsearchRepositoryBean<T>(elasticsearchOperationsBean, qualifiers, repositoryType, beanManager,
|
return new ElasticsearchRepositoryBean<T>(elasticsearchOperationsBean, qualifiers, repositoryType, beanManager,
|
||||||
getCustomImplementationDetector());
|
Optional.ofNullable(getCustomImplementationDetector()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ import java.lang.reflect.Type;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.elasticsearch.ElasticsearchException;
|
import org.elasticsearch.ElasticsearchException;
|
||||||
import org.elasticsearch.index.query.QueryBuilder;
|
import org.elasticsearch.index.query.QueryBuilder;
|
||||||
@ -45,16 +46,15 @@ import org.springframework.util.Assert;
|
|||||||
* @author Kevin Leturc
|
* @author Kevin Leturc
|
||||||
* @author Mark Paluch
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractElasticsearchRepository<T, ID extends Serializable> implements
|
public abstract class AbstractElasticsearchRepository<T, ID extends Serializable>
|
||||||
ElasticsearchRepository<T, ID> {
|
implements ElasticsearchRepository<T, ID> {
|
||||||
|
|
||||||
static final Logger LOGGER = LoggerFactory.getLogger(AbstractElasticsearchRepository.class);
|
static final Logger LOGGER = LoggerFactory.getLogger(AbstractElasticsearchRepository.class);
|
||||||
protected ElasticsearchOperations elasticsearchOperations;
|
protected ElasticsearchOperations elasticsearchOperations;
|
||||||
protected Class<T> entityClass;
|
protected Class<T> entityClass;
|
||||||
protected ElasticsearchEntityInformation<T, ID> entityInformation;
|
protected ElasticsearchEntityInformation<T, ID> entityInformation;
|
||||||
|
|
||||||
public AbstractElasticsearchRepository() {
|
public AbstractElasticsearchRepository() {}
|
||||||
}
|
|
||||||
|
|
||||||
public AbstractElasticsearchRepository(ElasticsearchOperations elasticsearchOperations) {
|
public AbstractElasticsearchRepository(ElasticsearchOperations elasticsearchOperations) {
|
||||||
|
|
||||||
@ -64,7 +64,7 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
|
|||||||
}
|
}
|
||||||
|
|
||||||
public AbstractElasticsearchRepository(ElasticsearchEntityInformation<T, ID> metadata,
|
public AbstractElasticsearchRepository(ElasticsearchEntityInformation<T, ID> metadata,
|
||||||
ElasticsearchOperations elasticsearchOperations) {
|
ElasticsearchOperations elasticsearchOperations) {
|
||||||
this(elasticsearchOperations);
|
this(elasticsearchOperations);
|
||||||
|
|
||||||
Assert.notNull(metadata, "ElasticsearchEntityInformation must not be null!");
|
Assert.notNull(metadata, "ElasticsearchEntityInformation must not be null!");
|
||||||
@ -94,17 +94,17 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T findOne(ID id) {
|
public Optional<T> findOne(ID id) {
|
||||||
GetQuery query = new GetQuery();
|
GetQuery query = new GetQuery();
|
||||||
query.setId(stringIdRepresentation(id));
|
query.setId(stringIdRepresentation(id));
|
||||||
return elasticsearchOperations.queryForObject(query, getEntityClass());
|
return Optional.ofNullable(elasticsearchOperations.queryForObject(query, getEntityClass()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterable<T> findAll() {
|
public Iterable<T> findAll() {
|
||||||
int itemCount = (int) this.count();
|
int itemCount = (int) this.count();
|
||||||
if (itemCount == 0) {
|
if (itemCount == 0) {
|
||||||
return new PageImpl<T>(Collections.<T>emptyList());
|
return new PageImpl<T>(Collections.<T> emptyList());
|
||||||
}
|
}
|
||||||
return this.findAll(new PageRequest(0, Math.max(1, itemCount)));
|
return this.findAll(new PageRequest(0, Math.max(1, itemCount)));
|
||||||
}
|
}
|
||||||
@ -119,7 +119,7 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
|
|||||||
public Iterable<T> findAll(Sort sort) {
|
public Iterable<T> findAll(Sort sort) {
|
||||||
int itemCount = (int) this.count();
|
int itemCount = (int) this.count();
|
||||||
if (itemCount == 0) {
|
if (itemCount == 0) {
|
||||||
return new PageImpl<T>(Collections.<T>emptyList());
|
return new PageImpl<T>(Collections.<T> emptyList());
|
||||||
}
|
}
|
||||||
SearchQuery query = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
SearchQuery query = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
|
||||||
.withPageable(new PageRequest(0, itemCount, sort)).build();
|
.withPageable(new PageRequest(0, itemCount, sort)).build();
|
||||||
@ -129,9 +129,7 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
|
|||||||
@Override
|
@Override
|
||||||
public Iterable<T> findAll(Iterable<ID> ids) {
|
public Iterable<T> findAll(Iterable<ID> ids) {
|
||||||
Assert.notNull(ids, "ids can't be null.");
|
Assert.notNull(ids, "ids can't be null.");
|
||||||
SearchQuery query = new NativeSearchQueryBuilder()
|
SearchQuery query = new NativeSearchQueryBuilder().withIds(stringIdsRepresentation(ids)).build();
|
||||||
.withIds(stringIdsRepresentation(ids))
|
|
||||||
.build();
|
|
||||||
return elasticsearchOperations.multiGet(query, getEntityClass());
|
return elasticsearchOperations.multiGet(query, getEntityClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -188,7 +186,7 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
|
|||||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(query).build();
|
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(query).build();
|
||||||
int count = (int) elasticsearchOperations.count(searchQuery, getEntityClass());
|
int count = (int) elasticsearchOperations.count(searchQuery, getEntityClass());
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
return new PageImpl<T>(Collections.<T>emptyList());
|
return new PageImpl<T>(Collections.<T> emptyList());
|
||||||
}
|
}
|
||||||
searchQuery.setPageable(new PageRequest(0, count));
|
searchQuery.setPageable(new PageRequest(0, count));
|
||||||
return elasticsearchOperations.queryForPage(searchQuery, getEntityClass());
|
return elasticsearchOperations.queryForPage(searchQuery, getEntityClass());
|
||||||
@ -308,10 +306,7 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected ID extractIdFromBean(T entity) {
|
protected ID extractIdFromBean(T entity) {
|
||||||
if (entityInformation != null) {
|
return entityInformation.getId(entity).orElse(null);
|
||||||
return entityInformation.getId(entity);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<String> stringIdsRepresentation(Iterable<ID> ids) {
|
private List<String> stringIdsRepresentation(Iterable<ID> ids) {
|
||||||
@ -326,16 +321,10 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
|
|||||||
protected abstract String stringIdRepresentation(ID id);
|
protected abstract String stringIdRepresentation(ID id);
|
||||||
|
|
||||||
private Long extractVersionFromBean(T entity) {
|
private Long extractVersionFromBean(T entity) {
|
||||||
if (entityInformation != null) {
|
return entityInformation.getVersion(entity);
|
||||||
return entityInformation.getVersion(entity);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String extractParentIdFromBean(T entity) {
|
private String extractParentIdFromBean(T entity) {
|
||||||
if (entityInformation != null) {
|
return entityInformation.getParentId(entity);
|
||||||
return entityInformation.getParentId(entity);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ public class ElasticsearchEntityInformationCreatorImpl implements ElasticsearchE
|
|||||||
public <T, ID extends Serializable> ElasticsearchEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
|
public <T, ID extends Serializable> ElasticsearchEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
|
||||||
|
|
||||||
ElasticsearchPersistentEntity<T> persistentEntity = (ElasticsearchPersistentEntity<T>) mappingContext
|
ElasticsearchPersistentEntity<T> persistentEntity = (ElasticsearchPersistentEntity<T>) mappingContext
|
||||||
.getPersistentEntity(domainClass);
|
.getRequiredPersistentEntity(domainClass);
|
||||||
|
|
||||||
Assert.notNull(persistentEntity, String.format("Unable to obtain mapping metadata for %s!", domainClass));
|
Assert.notNull(persistentEntity, String.format("Unable to obtain mapping metadata for %s!", domainClass));
|
||||||
Assert.notNull(persistentEntity.getIdProperty(), String.format("No id property found for %s!", domainClass));
|
Assert.notNull(persistentEntity.getIdProperty(), String.format("No id property found for %s!", domainClass));
|
||||||
|
@ -15,10 +15,11 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.elasticsearch.repository.support;
|
package org.springframework.data.elasticsearch.repository.support;
|
||||||
|
|
||||||
import static org.springframework.data.querydsl.QueryDslUtils.*;
|
import static org.springframework.data.querydsl.QuerydslUtils.*;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
||||||
@ -27,12 +28,14 @@ import org.springframework.data.elasticsearch.repository.query.ElasticsearchPart
|
|||||||
import org.springframework.data.elasticsearch.repository.query.ElasticsearchQueryMethod;
|
import org.springframework.data.elasticsearch.repository.query.ElasticsearchQueryMethod;
|
||||||
import org.springframework.data.elasticsearch.repository.query.ElasticsearchStringQuery;
|
import org.springframework.data.elasticsearch.repository.query.ElasticsearchStringQuery;
|
||||||
import org.springframework.data.projection.ProjectionFactory;
|
import org.springframework.data.projection.ProjectionFactory;
|
||||||
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
|
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
|
||||||
import org.springframework.data.repository.core.NamedQueries;
|
import org.springframework.data.repository.core.NamedQueries;
|
||||||
import org.springframework.data.repository.core.RepositoryInformation;
|
import org.springframework.data.repository.core.RepositoryInformation;
|
||||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||||
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
|
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
|
||||||
|
import org.springframework.data.repository.query.EvaluationContextProvider;
|
||||||
import org.springframework.data.repository.query.QueryLookupStrategy;
|
import org.springframework.data.repository.query.QueryLookupStrategy;
|
||||||
|
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
|
||||||
import org.springframework.data.repository.query.RepositoryQuery;
|
import org.springframework.data.repository.query.RepositoryQuery;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
@ -55,8 +58,8 @@ public class ElasticsearchRepositoryFactory extends RepositoryFactorySupport {
|
|||||||
Assert.notNull(elasticsearchOperations, "ElasticsearchOperations must not be null!");
|
Assert.notNull(elasticsearchOperations, "ElasticsearchOperations must not be null!");
|
||||||
|
|
||||||
this.elasticsearchOperations = elasticsearchOperations;
|
this.elasticsearchOperations = elasticsearchOperations;
|
||||||
this.entityInformationCreator = new ElasticsearchEntityInformationCreatorImpl(elasticsearchOperations
|
this.entityInformationCreator = new ElasticsearchEntityInformationCreatorImpl(
|
||||||
.getElasticsearchConverter().getMappingContext());
|
elasticsearchOperations.getElasticsearchConverter().getMappingContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -65,9 +68,10 @@ public class ElasticsearchRepositoryFactory extends RepositoryFactorySupport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||||
protected Object getTargetRepository(RepositoryInformation metadata) {
|
protected Object getTargetRepository(RepositoryInformation metadata) {
|
||||||
return getTargetRepositoryViaReflection(metadata,getEntityInformation(metadata.getDomainType()), elasticsearchOperations);
|
return getTargetRepositoryViaReflection(metadata, getEntityInformation(metadata.getDomainType()),
|
||||||
|
elasticsearchOperations);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -75,8 +79,7 @@ public class ElasticsearchRepositoryFactory extends RepositoryFactorySupport {
|
|||||||
if (isQueryDslRepository(metadata.getRepositoryInterface())) {
|
if (isQueryDslRepository(metadata.getRepositoryInterface())) {
|
||||||
throw new IllegalArgumentException("QueryDsl Support has not been implemented yet.");
|
throw new IllegalArgumentException("QueryDsl Support has not been implemented yet.");
|
||||||
}
|
}
|
||||||
if (Integer.class.isAssignableFrom(metadata.getIdType())
|
if (Integer.class.isAssignableFrom(metadata.getIdType()) || Long.class.isAssignableFrom(metadata.getIdType())
|
||||||
|| Long.class.isAssignableFrom(metadata.getIdType())
|
|
||||||
|| Double.class.isAssignableFrom(metadata.getIdType())) {
|
|| Double.class.isAssignableFrom(metadata.getIdType())) {
|
||||||
return NumberKeyedRepository.class;
|
return NumberKeyedRepository.class;
|
||||||
} else if (metadata.getIdType() == String.class) {
|
} else if (metadata.getIdType() == String.class) {
|
||||||
@ -89,17 +92,18 @@ public class ElasticsearchRepositoryFactory extends RepositoryFactorySupport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isQueryDslRepository(Class<?> repositoryInterface) {
|
private static boolean isQueryDslRepository(Class<?> repositoryInterface) {
|
||||||
return QUERY_DSL_PRESENT && QueryDslPredicateExecutor.class.isAssignableFrom(repositoryInterface);
|
return QUERY_DSL_PRESENT && QuerydslPredicateExecutor.class.isAssignableFrom(repositoryInterface);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected QueryLookupStrategy getQueryLookupStrategy(QueryLookupStrategy.Key key) {
|
protected Optional<QueryLookupStrategy> getQueryLookupStrategy(Key key,
|
||||||
return new ElasticsearchQueryLookupStrategy();
|
EvaluationContextProvider evaluationContextProvider) {
|
||||||
|
return Optional.of(new ElasticsearchQueryLookupStrategy());
|
||||||
}
|
}
|
||||||
|
|
||||||
private class ElasticsearchQueryLookupStrategy implements QueryLookupStrategy {
|
private class ElasticsearchQueryLookupStrategy implements QueryLookupStrategy {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* (non-Javadoc)
|
* (non-Javadoc)
|
||||||
* @see org.springframework.data.repository.query.QueryLookupStrategy#resolveQuery(java.lang.reflect.Method, org.springframework.data.repository.core.RepositoryMetadata, org.springframework.data.projection.ProjectionFactory, org.springframework.data.repository.core.NamedQueries)
|
* @see org.springframework.data.repository.query.QueryLookupStrategy#resolveQuery(java.lang.reflect.Method, org.springframework.data.repository.core.RepositoryMetadata, org.springframework.data.projection.ProjectionFactory, org.springframework.data.repository.core.NamedQueries)
|
||||||
*/
|
*/
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2015 the original author or authors.
|
* Copyright 2013-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -16,13 +16,13 @@
|
|||||||
package org.springframework.data.elasticsearch.repository.support;
|
package org.springframework.data.elasticsearch.repository.support;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
|
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
|
||||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
||||||
import org.springframework.data.repository.core.support.PersistentEntityInformation;
|
import org.springframework.data.repository.core.support.PersistentEntityInformation;
|
||||||
import org.springframework.util.Assert;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Elasticsearch specific implementation of
|
* Elasticsearch specific implementation of
|
||||||
@ -34,9 +34,10 @@ import org.springframework.util.Assert;
|
|||||||
* @author Mohsin Husen
|
* @author Mohsin Husen
|
||||||
* @author Ryan Henszey
|
* @author Ryan Henszey
|
||||||
* @author Oliver Gierke
|
* @author Oliver Gierke
|
||||||
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
public class MappingElasticsearchEntityInformation<T, ID extends Serializable> extends PersistentEntityInformation<T, ID>
|
public class MappingElasticsearchEntityInformation<T, ID extends Serializable>
|
||||||
implements ElasticsearchEntityInformation<T, ID> {
|
extends PersistentEntityInformation<T, ID> implements ElasticsearchEntityInformation<T, ID> {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(MappingElasticsearchEntityInformation.class);
|
private static final Logger logger = LoggerFactory.getLogger(MappingElasticsearchEntityInformation.class);
|
||||||
private final ElasticsearchPersistentEntity<T> entityMetadata;
|
private final ElasticsearchPersistentEntity<T> entityMetadata;
|
||||||
@ -54,13 +55,15 @@ public class MappingElasticsearchEntityInformation<T, ID extends Serializable> e
|
|||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getIdAttribute() {
|
public String getIdAttribute() {
|
||||||
Assert.notNull(entityMetadata.getIdProperty(), "Unable to identify 'id' property in class "
|
|
||||||
+ entityMetadata.getType().getSimpleName()
|
ElasticsearchPersistentProperty property = entityMetadata.getIdProperty()
|
||||||
+ ". Make sure the 'id' property is annotated with @Id or named as 'id' or 'documentId' ");
|
.orElseThrow(() -> new IllegalArgumentException(String.format(
|
||||||
return entityMetadata.getIdProperty().getFieldName();
|
"Unable to identify 'id' property in class %s. Make sure the 'id' property is annotated with @Id or named as 'id' or 'documentId'",
|
||||||
|
entityMetadata.getType().getSimpleName())));
|
||||||
|
|
||||||
|
return property.getFieldName();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -75,27 +78,30 @@ public class MappingElasticsearchEntityInformation<T, ID extends Serializable> e
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Long getVersion(T entity) {
|
public Long getVersion(T entity) {
|
||||||
ElasticsearchPersistentProperty versionProperty = entityMetadata.getVersionProperty();
|
|
||||||
|
Optional<ElasticsearchPersistentProperty> versionProperty = entityMetadata.getVersionProperty();
|
||||||
try {
|
try {
|
||||||
if (versionProperty != null) {
|
|
||||||
return (Long) entityMetadata.getPropertyAccessor(entity).getProperty(versionProperty);
|
return (Long) versionProperty //
|
||||||
}
|
.flatMap(property -> entityMetadata.getPropertyAccessor(entity).getProperty(property)) //
|
||||||
|
.orElse(null);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new IllegalStateException("failed to load version field", e);
|
throw new IllegalStateException("failed to load version field", e);
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getParentId(T entity) {
|
public String getParentId(T entity) {
|
||||||
ElasticsearchPersistentProperty parentProperty = entityMetadata.getParentIdProperty();
|
|
||||||
|
Optional<ElasticsearchPersistentProperty> parentProperty = entityMetadata.getParentIdProperty();
|
||||||
try {
|
try {
|
||||||
if (parentProperty != null) {
|
|
||||||
return (String) entityMetadata.getPropertyAccessor(entity).getProperty(parentProperty);
|
return (String) parentProperty //
|
||||||
}
|
.flatMap(property -> entityMetadata.getPropertyAccessor(entity).getProperty(property)) //
|
||||||
|
.orElse(null);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new IllegalStateException("failed to load parent ID: " + e, e);
|
throw new IllegalStateException("failed to load parent ID: " + e, e);
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2016 the original author or authors.
|
* Copyright 2013-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -41,11 +41,9 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.springframework.data.annotation.AccessType;
|
|
||||||
import org.springframework.data.annotation.Id;
|
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.elasticsearch.annotations.Document;
|
import org.springframework.data.elasticsearch.annotations.Document;
|
||||||
import org.springframework.data.elasticsearch.core.DefaultResultMapperTests.ImmutableEntity;
|
|
||||||
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
|
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
|
||||||
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
|
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
|
||||||
import org.springframework.data.elasticsearch.entities.Car;
|
import org.springframework.data.elasticsearch.entities.Car;
|
||||||
@ -53,6 +51,7 @@ import org.springframework.data.elasticsearch.entities.Car;
|
|||||||
/**
|
/**
|
||||||
* @author Artur Konczak
|
* @author Artur Konczak
|
||||||
* @author Mohsin Husen
|
* @author Mohsin Husen
|
||||||
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
public class DefaultResultMapperTests {
|
public class DefaultResultMapperTests {
|
||||||
|
|
||||||
@ -85,7 +84,7 @@ public class DefaultResultMapperTests {
|
|||||||
when(response.getAggregations()).thenReturn(aggregations);
|
when(response.getAggregations()).thenReturn(aggregations);
|
||||||
|
|
||||||
//When
|
//When
|
||||||
AggregatedPage<Car> page = (AggregatedPage<Car>) resultMapper.mapResults(response, Car.class, null);
|
AggregatedPage<Car> page = (AggregatedPage<Car>) resultMapper.mapResults(response, Car.class, Pageable.NONE);
|
||||||
|
|
||||||
//Then
|
//Then
|
||||||
page.hasFacets();
|
page.hasFacets();
|
||||||
@ -103,7 +102,7 @@ public class DefaultResultMapperTests {
|
|||||||
when(response.getHits()).thenReturn(searchHits);
|
when(response.getHits()).thenReturn(searchHits);
|
||||||
|
|
||||||
//When
|
//When
|
||||||
Page<Car> page = resultMapper.mapResults(response, Car.class, null);
|
Page<Car> page = resultMapper.mapResults(response, Car.class, Pageable.NONE);
|
||||||
|
|
||||||
//Then
|
//Then
|
||||||
assertThat(page.hasContent(), is(true));
|
assertThat(page.hasContent(), is(true));
|
||||||
@ -121,7 +120,7 @@ public class DefaultResultMapperTests {
|
|||||||
when(response.getHits()).thenReturn(searchHits);
|
when(response.getHits()).thenReturn(searchHits);
|
||||||
|
|
||||||
//When
|
//When
|
||||||
Page<Car> page = resultMapper.mapResults(response, Car.class, null);
|
Page<Car> page = resultMapper.mapResults(response, Car.class, Pageable.NONE);
|
||||||
|
|
||||||
//Then
|
//Then
|
||||||
assertThat(page.hasContent(), is(true));
|
assertThat(page.hasContent(), is(true));
|
||||||
@ -143,19 +142,19 @@ public class DefaultResultMapperTests {
|
|||||||
assertThat(result.getModel(), is("Grat"));
|
assertThat(result.getModel(), is("Grat"));
|
||||||
assertThat(result.getName(), is("Ford"));
|
assertThat(result.getName(), is("Ford"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see DATAES-281.
|
* @see DATAES-281.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void setsIdentifierOnImmutableType() {
|
public void setsIdentifierOnImmutableType() {
|
||||||
|
|
||||||
GetResponse response = mock(GetResponse.class);
|
GetResponse response = mock(GetResponse.class);
|
||||||
when(response.getSourceAsString()).thenReturn("{}");
|
when(response.getSourceAsString()).thenReturn("{}");
|
||||||
when(response.getId()).thenReturn("identifier");
|
when(response.getId()).thenReturn("identifier");
|
||||||
|
|
||||||
ImmutableEntity result = resultMapper.mapResult(response, ImmutableEntity.class);
|
ImmutableEntity result = resultMapper.mapResult(response, ImmutableEntity.class);
|
||||||
|
|
||||||
assertThat(result, is(notNullValue()));
|
assertThat(result, is(notNullValue()));
|
||||||
assertThat(result.getId(), is("identifier"));
|
assertThat(result.getId(), is("identifier"));
|
||||||
}
|
}
|
||||||
@ -193,7 +192,7 @@ public class DefaultResultMapperTests {
|
|||||||
result.put("model", new InternalSearchHitField("model", Arrays.<Object>asList(model)));
|
result.put("model", new InternalSearchHitField("model", Arrays.<Object>asList(model)));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Document(indexName = "someIndex")
|
@Document(indexName = "someIndex")
|
||||||
@NoArgsConstructor(force = true)
|
@NoArgsConstructor(force = true)
|
||||||
@Getter
|
@Getter
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013 the original author or authors.
|
* Copyright 2013-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -16,18 +16,20 @@
|
|||||||
package org.springframework.data.elasticsearch.core.mapping;
|
package org.springframework.data.elasticsearch.core.mapping;
|
||||||
|
|
||||||
import java.beans.IntrospectionException;
|
import java.beans.IntrospectionException;
|
||||||
import java.beans.PropertyDescriptor;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.data.annotation.Version;
|
import org.springframework.data.annotation.Version;
|
||||||
import org.springframework.data.mapping.model.MappingException;
|
import org.springframework.data.mapping.model.MappingException;
|
||||||
|
import org.springframework.data.mapping.model.Property;
|
||||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||||
import org.springframework.data.util.ClassTypeInformation;
|
import org.springframework.data.util.ClassTypeInformation;
|
||||||
import org.springframework.data.util.TypeInformation;
|
import org.springframework.data.util.TypeInformation;
|
||||||
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Rizwan Idrees
|
* @author Rizwan Idrees
|
||||||
* @author Mohsin Husen
|
* @author Mohsin Husen
|
||||||
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
public class SimpleElasticsearchPersistentEntityTests {
|
public class SimpleElasticsearchPersistentEntityTests {
|
||||||
|
|
||||||
@ -35,46 +37,43 @@ public class SimpleElasticsearchPersistentEntityTests {
|
|||||||
public void shouldThrowExceptionGivenVersionPropertyIsNotLong() throws NoSuchFieldException, IntrospectionException {
|
public void shouldThrowExceptionGivenVersionPropertyIsNotLong() throws NoSuchFieldException, IntrospectionException {
|
||||||
// given
|
// given
|
||||||
TypeInformation typeInformation = ClassTypeInformation.from(EntityWithWrongVersionType.class);
|
TypeInformation typeInformation = ClassTypeInformation.from(EntityWithWrongVersionType.class);
|
||||||
SimpleElasticsearchPersistentProperty persistentProperty = new SimpleElasticsearchPersistentProperty(
|
SimpleElasticsearchPersistentEntity<EntityWithWrongVersionType> entity = new SimpleElasticsearchPersistentEntity<>(
|
||||||
EntityWithWrongVersionType.class.getDeclaredField("version"), new PropertyDescriptor("version",
|
typeInformation);
|
||||||
EntityWithWrongVersionType.class), new SimpleElasticsearchPersistentEntity<EntityWithWrongVersionType>(
|
|
||||||
typeInformation), new SimpleTypeHolder()
|
SimpleElasticsearchPersistentProperty persistentProperty = createProperty(entity, "version");
|
||||||
);
|
|
||||||
|
|
||||||
// when
|
// when
|
||||||
new SimpleElasticsearchPersistentEntity(typeInformation).addPersistentProperty(persistentProperty);
|
entity.addPersistentProperty(persistentProperty);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = MappingException.class)
|
@Test(expected = MappingException.class)
|
||||||
public void shouldThrowExceptionGivenMultipleVersionPropertiesArePresent() throws NoSuchFieldException,
|
public void shouldThrowExceptionGivenMultipleVersionPropertiesArePresent()
|
||||||
IntrospectionException {
|
throws NoSuchFieldException, IntrospectionException {
|
||||||
// given
|
// given
|
||||||
TypeInformation typeInformation = ClassTypeInformation.from(EntityWithMultipleVersionField.class);
|
TypeInformation typeInformation = ClassTypeInformation.from(EntityWithMultipleVersionField.class);
|
||||||
SimpleElasticsearchPersistentProperty persistentProperty1 = new SimpleElasticsearchPersistentProperty(
|
SimpleElasticsearchPersistentEntity<EntityWithMultipleVersionField> entity = new SimpleElasticsearchPersistentEntity<>(
|
||||||
EntityWithMultipleVersionField.class.getDeclaredField("version1"), new PropertyDescriptor("version1",
|
|
||||||
EntityWithMultipleVersionField.class),
|
|
||||||
new SimpleElasticsearchPersistentEntity<EntityWithMultipleVersionField>(typeInformation),
|
|
||||||
new SimpleTypeHolder()
|
|
||||||
);
|
|
||||||
|
|
||||||
SimpleElasticsearchPersistentProperty persistentProperty2 = new SimpleElasticsearchPersistentProperty(
|
|
||||||
EntityWithMultipleVersionField.class.getDeclaredField("version2"), new PropertyDescriptor("version2",
|
|
||||||
EntityWithMultipleVersionField.class),
|
|
||||||
new SimpleElasticsearchPersistentEntity<EntityWithMultipleVersionField>(typeInformation),
|
|
||||||
new SimpleTypeHolder()
|
|
||||||
);
|
|
||||||
|
|
||||||
SimpleElasticsearchPersistentEntity simpleElasticsearchPersistentEntity = new SimpleElasticsearchPersistentEntity(
|
|
||||||
typeInformation);
|
typeInformation);
|
||||||
simpleElasticsearchPersistentEntity.addPersistentProperty(persistentProperty1);
|
|
||||||
|
SimpleElasticsearchPersistentProperty persistentProperty1 = createProperty(entity, "version1");
|
||||||
|
|
||||||
|
SimpleElasticsearchPersistentProperty persistentProperty2 = createProperty(entity, "version2");
|
||||||
|
|
||||||
|
entity.addPersistentProperty(persistentProperty1);
|
||||||
// when
|
// when
|
||||||
simpleElasticsearchPersistentEntity.addPersistentProperty(persistentProperty2);
|
entity.addPersistentProperty(persistentProperty2);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SimpleElasticsearchPersistentProperty createProperty(SimpleElasticsearchPersistentEntity<?> entity,
|
||||||
|
String field) {
|
||||||
|
|
||||||
|
Property property = Property.of(ReflectionUtils.findField(entity.getTypeInformation().getType(), field));
|
||||||
|
return new SimpleElasticsearchPersistentProperty(property, entity, new SimpleTypeHolder());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class EntityWithWrongVersionType {
|
private class EntityWithWrongVersionType {
|
||||||
|
|
||||||
@Version
|
@Version private String version;
|
||||||
private String version;
|
|
||||||
|
|
||||||
public String getVersion() {
|
public String getVersion() {
|
||||||
return version;
|
return version;
|
||||||
@ -87,10 +86,8 @@ public class SimpleElasticsearchPersistentEntityTests {
|
|||||||
|
|
||||||
private class EntityWithMultipleVersionField {
|
private class EntityWithMultipleVersionField {
|
||||||
|
|
||||||
@Version
|
@Version private Long version1;
|
||||||
private Long version1;
|
@Version private Long version2;
|
||||||
@Version
|
|
||||||
private Long version2;
|
|
||||||
|
|
||||||
public Long getVersion1() {
|
public Long getVersion1() {
|
||||||
return version1;
|
return version1;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2016 the original author or authors.
|
* Copyright 2016-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -18,6 +18,8 @@ package org.springframework.data.elasticsearch.immutable;
|
|||||||
import static org.hamcrest.Matchers.*;
|
import static org.hamcrest.Matchers.*;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -29,6 +31,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||||||
/**
|
/**
|
||||||
* @author Young Gu
|
* @author Young Gu
|
||||||
* @author Oliver Gierke
|
* @author Oliver Gierke
|
||||||
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration("classpath:immutable-repository-test.xml")
|
@ContextConfiguration("classpath:immutable-repository-test.xml")
|
||||||
@ -37,29 +40,34 @@ public class ImmutableElasticsearchRepositoryTests {
|
|||||||
@Autowired ImmutableElasticsearchRepository repository;
|
@Autowired ImmutableElasticsearchRepository repository;
|
||||||
@Autowired ElasticsearchOperations operations;
|
@Autowired ElasticsearchOperations operations;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void before() {
|
public void before() {
|
||||||
|
|
||||||
operations.deleteIndex(ImmutableEntity.class);
|
operations.deleteIndex(ImmutableEntity.class);
|
||||||
operations.createIndex(ImmutableEntity.class);
|
operations.createIndex(ImmutableEntity.class);
|
||||||
operations.refresh(ImmutableEntity.class);
|
operations.refresh(ImmutableEntity.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see DATAES-281
|
* @see DATAES-281
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void shouldSaveAndFindImmutableDocument() {
|
public void shouldSaveAndFindImmutableDocument() {
|
||||||
|
|
||||||
// when
|
// when
|
||||||
ImmutableEntity entity = repository.save(new ImmutableEntity("test name"));
|
ImmutableEntity entity = repository.save(new ImmutableEntity("test name"));
|
||||||
assertThat(entity.getId(), is(notNullValue()));
|
assertThat(entity.getId(), is(notNullValue()));
|
||||||
|
|
||||||
// then
|
// then
|
||||||
ImmutableEntity entityFromElasticSearch = repository.findOne(entity.getId());
|
Optional<ImmutableEntity> entityFromElasticSearch = repository.findOne(entity.getId());
|
||||||
|
|
||||||
assertThat(entityFromElasticSearch.getName(), is("test name"));
|
assertThat(entityFromElasticSearch.isPresent(), is(true));
|
||||||
assertThat(entityFromElasticSearch.getId(), is(entity.getId()));
|
|
||||||
|
entityFromElasticSearch.ifPresent(immutableEntity -> {
|
||||||
|
|
||||||
|
assertThat(immutableEntity.getName(), is("test name"));
|
||||||
|
assertThat(immutableEntity.getId(), is(entity.getId()));
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2014 the original author or authors.
|
* Copyright 2014-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -15,14 +15,17 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.elasticsearch.repositories.cdi;
|
package org.springframework.data.elasticsearch.repositories.cdi;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.springframework.data.elasticsearch.entities.Product;
|
import org.springframework.data.elasticsearch.entities.Product;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Mohsin Husen
|
* @author Mohsin Husen
|
||||||
* @author Oliver Gierke
|
* @author Oliver Gierke
|
||||||
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
public interface CdiProductRepository extends CrudRepository<Product, String> {
|
public interface CdiProductRepository extends CrudRepository<Product, String> {
|
||||||
|
|
||||||
Product findOne(String id);
|
Optional<Product> findOne(String id);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2014-2016 the original author or authors.
|
* Copyright 2014-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -18,6 +18,8 @@ package org.springframework.data.elasticsearch.repositories.cdi;
|
|||||||
import static org.hamcrest.CoreMatchers.*;
|
import static org.hamcrest.CoreMatchers.*;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.apache.webbeans.cditest.CdiTestContainer;
|
import org.apache.webbeans.cditest.CdiTestContainer;
|
||||||
import org.apache.webbeans.cditest.CdiTestContainerLoader;
|
import org.apache.webbeans.cditest.CdiTestContainerLoader;
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
@ -70,10 +72,13 @@ public class CdiRepositoryTests {
|
|||||||
|
|
||||||
assertTrue(repository.exists(bean.getId()));
|
assertTrue(repository.exists(bean.getId()));
|
||||||
|
|
||||||
Product retrieved = repository.findOne(bean.getId());
|
Optional<Product> retrieved = repository.findOne(bean.getId());
|
||||||
assertNotNull(retrieved);
|
|
||||||
assertEquals(bean.getId(), retrieved.getId());
|
assertTrue(retrieved.isPresent());
|
||||||
assertEquals(bean.getName(), retrieved.getName());
|
retrieved.ifPresent(product -> {
|
||||||
|
assertEquals(bean.getId(), product.getId());
|
||||||
|
assertEquals(bean.getName(), product.getName());
|
||||||
|
});
|
||||||
|
|
||||||
assertEquals(1, repository.count());
|
assertEquals(1, repository.count());
|
||||||
|
|
||||||
@ -83,7 +88,7 @@ public class CdiRepositoryTests {
|
|||||||
|
|
||||||
assertEquals(0, repository.count());
|
assertEquals(0, repository.count());
|
||||||
retrieved = repository.findOne(bean.getId());
|
retrieved = repository.findOne(bean.getId());
|
||||||
assertNull(retrieved);
|
assertFalse(retrieved.isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -101,10 +106,13 @@ public class CdiRepositoryTests {
|
|||||||
|
|
||||||
assertTrue(qualifiedProductRepository.exists(bean.getId()));
|
assertTrue(qualifiedProductRepository.exists(bean.getId()));
|
||||||
|
|
||||||
Product retrieved = qualifiedProductRepository.findOne(bean.getId());
|
Optional<Product> retrieved = qualifiedProductRepository.findOne(bean.getId());
|
||||||
assertNotNull(retrieved);
|
|
||||||
assertEquals(bean.getId(), retrieved.getId());
|
assertTrue(retrieved.isPresent());
|
||||||
assertEquals(bean.getName(), retrieved.getName());
|
retrieved.ifPresent(product -> {
|
||||||
|
assertEquals(bean.getId(), product.getId());
|
||||||
|
assertEquals(bean.getName(), product.getName());
|
||||||
|
});
|
||||||
|
|
||||||
assertEquals(1, qualifiedProductRepository.count());
|
assertEquals(1, qualifiedProductRepository.count());
|
||||||
|
|
||||||
@ -114,7 +122,7 @@ public class CdiRepositoryTests {
|
|||||||
|
|
||||||
assertEquals(0, qualifiedProductRepository.count());
|
assertEquals(0, qualifiedProductRepository.count());
|
||||||
retrieved = qualifiedProductRepository.findOne(bean.getId());
|
retrieved = qualifiedProductRepository.findOne(bean.getId());
|
||||||
assertNull(retrieved);
|
assertFalse(retrieved.isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2016 the original author or authors.
|
* Copyright 2016-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -18,6 +18,9 @@ package org.springframework.data.elasticsearch.repositories.geo;
|
|||||||
import static org.hamcrest.Matchers.*;
|
import static org.hamcrest.Matchers.*;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -29,18 +32,16 @@ import org.springframework.data.geo.Point;
|
|||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
import java.util.Locale;
|
/**
|
||||||
|
* @author Mark Paluch
|
||||||
|
*/
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration("classpath:/repository-spring-data-geo-support.xml")
|
@ContextConfiguration("classpath:/repository-spring-data-geo-support.xml")
|
||||||
public class SpringDataGeoRepositoryTests {
|
public class SpringDataGeoRepositoryTests {
|
||||||
|
|
||||||
@Autowired
|
@Autowired ElasticsearchTemplate template;
|
||||||
private ElasticsearchTemplate template;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired SpringDataGeoRepository repository;
|
||||||
private SpringDataGeoRepository repository;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() {
|
public void init() {
|
||||||
@ -52,25 +53,25 @@ public class SpringDataGeoRepositoryTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldSaveAndLoadGeoPoints() {
|
public void shouldSaveAndLoadGeoPoints() {
|
||||||
//given
|
// given
|
||||||
final Point point = new Point(15, 25);
|
final Point point = new Point(15, 25);
|
||||||
GeoEntity entity = GeoEntity.builder()
|
GeoEntity entity = GeoEntity.builder().pointA(point).pointB(new GeoPoint(point.getX(), point.getY()))
|
||||||
.pointA(point)
|
.pointC(toGeoString(point)).pointD(toGeoArray(point)).build();
|
||||||
.pointB(new GeoPoint(point.getX(), point.getY()))
|
// when
|
||||||
.pointC(toGeoString(point))
|
GeoEntity saved = repository.save(entity);
|
||||||
.pointD(toGeoArray(point))
|
Optional<GeoEntity> result = repository.findOne(entity.getId());
|
||||||
.build();
|
// then
|
||||||
//when
|
|
||||||
entity = repository.save(entity);
|
|
||||||
GeoEntity result = repository.findOne(entity.getId());
|
|
||||||
//then
|
|
||||||
|
|
||||||
assertThat(entity.getPointA().getX(), is(result.getPointA().getX()));
|
assertThat(result.isPresent(), is(true));
|
||||||
assertThat(entity.getPointA().getY(), is(result.getPointA().getY()));
|
result.ifPresent(geoEntity -> {
|
||||||
assertThat(entity.getPointB().getLat(), is(result.getPointB().getLat()));
|
|
||||||
assertThat(entity.getPointB().getLon(), is(result.getPointB().getLon()));
|
assertThat(saved.getPointA().getX(), is(geoEntity.getPointA().getX()));
|
||||||
assertThat(entity.getPointC(), is(result.getPointC()));
|
assertThat(saved.getPointA().getY(), is(geoEntity.getPointA().getY()));
|
||||||
assertThat(entity.getPointD(), is(result.getPointD()));
|
assertThat(saved.getPointB().getLat(), is(geoEntity.getPointB().getLat()));
|
||||||
|
assertThat(saved.getPointB().getLon(), is(geoEntity.getPointB().getLon()));
|
||||||
|
assertThat(saved.getPointC(), is(geoEntity.getPointC()));
|
||||||
|
assertThat(saved.getPointD(), is(geoEntity.getPointD()));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private String toGeoString(Point point) {
|
private String toGeoString(Point point) {
|
||||||
@ -78,8 +79,6 @@ public class SpringDataGeoRepositoryTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private double[] toGeoArray(Point point) {
|
private double[] toGeoArray(Point point) {
|
||||||
return new double[]{
|
return new double[] { point.getX(), point.getY() };
|
||||||
point.getX(), point.getY()
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2016 the original author or authors.
|
* Copyright 2013-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -19,6 +19,7 @@ import static org.hamcrest.Matchers.*;
|
|||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.apache.commons.lang.math.RandomUtils;
|
import org.apache.commons.lang.math.RandomUtils;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@ -34,8 +35,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||||||
/**
|
/**
|
||||||
* @author Rizwan Idrees
|
* @author Rizwan Idrees
|
||||||
* @author Mohsin Husen
|
* @author Mohsin Husen
|
||||||
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration("classpath:/double-id-repository-test.xml")
|
@ContextConfiguration("classpath:/double-id-repository-test.xml")
|
||||||
public class DoubleIDRepositoryTests {
|
public class DoubleIDRepositoryTests {
|
||||||
@ -72,11 +73,11 @@ public class DoubleIDRepositoryTests {
|
|||||||
// when
|
// when
|
||||||
repository.save(Arrays.asList(sampleEntity1, sampleEntity2));
|
repository.save(Arrays.asList(sampleEntity1, sampleEntity2));
|
||||||
// then
|
// then
|
||||||
DoubleIDEntity entity1FromElasticSearch = repository.findOne(documentId1);
|
Optional<DoubleIDEntity> entity1FromElasticSearch = repository.findOne(documentId1);
|
||||||
assertThat(entity1FromElasticSearch, is(notNullValue()));
|
assertThat(entity1FromElasticSearch.isPresent(), is(true));
|
||||||
|
|
||||||
DoubleIDEntity entity2FromElasticSearch = repository.findOne(documentId2);
|
Optional<DoubleIDEntity> entity2FromElasticSearch = repository.findOne(documentId2);
|
||||||
assertThat(entity2FromElasticSearch, is(notNullValue()));
|
assertThat(entity2FromElasticSearch.isPresent(), is(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -90,7 +91,7 @@ public class DoubleIDRepositoryTests {
|
|||||||
// when
|
// when
|
||||||
repository.save(sampleEntity);
|
repository.save(sampleEntity);
|
||||||
// then
|
// then
|
||||||
DoubleIDEntity entityFromElasticSearch = repository.findOne(documentId);
|
Optional<DoubleIDEntity> entityFromElasticSearch = repository.findOne(documentId);
|
||||||
assertThat(entityFromElasticSearch, is(notNullValue()));
|
assertThat(entityFromElasticSearch.isPresent(), is(true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2014 the original author or authors.
|
* Copyright 2014-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -15,6 +15,10 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.elasticsearch.repository.support;
|
package org.springframework.data.elasticsearch.repository.support;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -24,19 +28,17 @@ import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersiste
|
|||||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
||||||
import org.springframework.data.mapping.context.MappingContext;
|
import org.springframework.data.mapping.context.MappingContext;
|
||||||
|
|
||||||
import static org.mockito.Mockito.doReturn;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Florian Hopf
|
* @author Florian Hopf
|
||||||
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public class ElasticsearchEntityInformationCreatorImplTests {
|
public class ElasticsearchEntityInformationCreatorImplTests {
|
||||||
|
|
||||||
@Mock
|
@Mock MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
|
||||||
private MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
|
@Mock ElasticsearchPersistentEntity<String> persistentEntity;
|
||||||
@Mock
|
|
||||||
private ElasticsearchPersistentEntity<String> persistentEntity;
|
ElasticsearchEntityInformationCreatorImpl entityInfoCreator;
|
||||||
private ElasticsearchEntityInformationCreatorImpl entityInfoCreator;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void before() {
|
public void before() {
|
||||||
@ -50,9 +52,9 @@ public class ElasticsearchEntityInformationCreatorImplTests {
|
|||||||
|
|
||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void shouldThrowIllegalArgumentExceptionOnMissingIdAnnotation() {
|
public void shouldThrowIllegalArgumentExceptionOnMissingIdAnnotation() {
|
||||||
doReturn(persistentEntity).when(mappingContext).getPersistentEntity(String.class);
|
doReturn(Optional.of(persistentEntity)).when(mappingContext).getPersistentEntity(String.class);
|
||||||
doReturn(String.class).when(persistentEntity).getType();
|
doReturn(String.class).when(persistentEntity).getType();
|
||||||
doReturn(null).when(persistentEntity).getIdProperty();
|
doReturn(Optional.empty()).when(persistentEntity).getIdProperty();
|
||||||
entityInfoCreator.getEntityInformation(String.class);
|
entityInfoCreator.getEntityInformation(String.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013 the original author or authors.
|
* Copyright 2013-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -29,13 +29,14 @@ import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersiste
|
|||||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
||||||
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
|
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
|
||||||
import org.springframework.data.mapping.context.MappingContext;
|
import org.springframework.data.mapping.context.MappingContext;
|
||||||
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
|
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
|
||||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||||
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
|
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Rizwan Idrees
|
* @author Rizwan Idrees
|
||||||
* @author Mohsin Husen
|
* @author Mohsin Husen
|
||||||
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public class ElasticsearchRepositoryFactoryTests {
|
public class ElasticsearchRepositoryFactoryTests {
|
||||||
@ -56,7 +57,7 @@ public class ElasticsearchRepositoryFactoryTests {
|
|||||||
@Test(expected = IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void shouldThrowExceptionGivenQueryDslRepository() {
|
public void shouldThrowExceptionGivenQueryDslRepository() {
|
||||||
// given
|
// given
|
||||||
RepositoryMetadata metadata = new DefaultRepositoryMetadata(QueryDslPredicateExecutor.class);
|
RepositoryMetadata metadata = new DefaultRepositoryMetadata(QuerydslPredicateExecutor.class);
|
||||||
// when
|
// when
|
||||||
factory.getRepositoryBaseClass(metadata);
|
factory.getRepositoryBaseClass(metadata);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2016 the original author or authors.
|
* Copyright 2013-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -19,6 +19,7 @@ import static org.hamcrest.Matchers.*;
|
|||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.apache.commons.lang.math.RandomUtils;
|
import org.apache.commons.lang.math.RandomUtils;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@ -34,8 +35,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||||||
/**
|
/**
|
||||||
* @author Rizwan Idrees
|
* @author Rizwan Idrees
|
||||||
* @author Mohsin Husen
|
* @author Mohsin Husen
|
||||||
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration("classpath:/integer-id-repository-test.xml")
|
@ContextConfiguration("classpath:/integer-id-repository-test.xml")
|
||||||
public class IntegerIDRepositoryTests {
|
public class IntegerIDRepositoryTests {
|
||||||
@ -72,11 +73,11 @@ public class IntegerIDRepositoryTests {
|
|||||||
// when
|
// when
|
||||||
repository.save(Arrays.asList(sampleEntity1, sampleEntity2));
|
repository.save(Arrays.asList(sampleEntity1, sampleEntity2));
|
||||||
// then
|
// then
|
||||||
IntegerIDEntity entity1FromElasticSearch = repository.findOne(documentId1);
|
Optional<IntegerIDEntity> entity1FromElasticSearch = repository.findOne(documentId1);
|
||||||
assertThat(entity1FromElasticSearch, is(notNullValue()));
|
assertThat(entity1FromElasticSearch.isPresent(), is(true));
|
||||||
|
|
||||||
IntegerIDEntity entity2FromElasticSearch = repository.findOne(documentId2);
|
Optional<IntegerIDEntity> entity2FromElasticSearch = repository.findOne(documentId2);
|
||||||
assertThat(entity2FromElasticSearch, is(notNullValue()));
|
assertThat(entity2FromElasticSearch.isPresent(), is(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -90,7 +91,7 @@ public class IntegerIDRepositoryTests {
|
|||||||
// when
|
// when
|
||||||
repository.save(sampleEntity);
|
repository.save(sampleEntity);
|
||||||
// then
|
// then
|
||||||
IntegerIDEntity entityFromElasticSearch = repository.findOne(documentId);
|
Optional<IntegerIDEntity> entityFromElasticSearch = repository.findOne(documentId);
|
||||||
assertThat(entityFromElasticSearch, is(notNullValue()));
|
assertThat(entityFromElasticSearch.isPresent(), is(true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2016 the original author or authors.
|
* Copyright 2013-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -23,6 +23,7 @@ import static org.junit.Assert.*;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@ -43,8 +44,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||||||
/**
|
/**
|
||||||
* @author Rizwan Idrees
|
* @author Rizwan Idrees
|
||||||
* @author Mohsin Husen
|
* @author Mohsin Husen
|
||||||
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration("classpath:/simple-repository-test.xml")
|
@ContextConfiguration("classpath:/simple-repository-test.xml")
|
||||||
public class SimpleElasticsearchRepositoryTests {
|
public class SimpleElasticsearchRepositoryTests {
|
||||||
@ -81,11 +82,11 @@ public class SimpleElasticsearchRepositoryTests {
|
|||||||
// when
|
// when
|
||||||
repository.save(Arrays.asList(sampleEntity1, sampleEntity2));
|
repository.save(Arrays.asList(sampleEntity1, sampleEntity2));
|
||||||
// then
|
// then
|
||||||
SampleEntity entity1FromElasticSearch = repository.findOne(documentId1);
|
Optional<SampleEntity> entity1FromElasticSearch = repository.findOne(documentId1);
|
||||||
assertThat(entity1FromElasticSearch, is(notNullValue()));
|
assertThat(entity1FromElasticSearch.isPresent(), is(true));
|
||||||
|
|
||||||
SampleEntity entity2FromElasticSearch = repository.findOne(documentId2);
|
Optional<SampleEntity> entity2FromElasticSearch = repository.findOne(documentId2);
|
||||||
assertThat(entity2FromElasticSearch, is(notNullValue()));
|
assertThat(entity2FromElasticSearch.isPresent(), is(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -99,8 +100,8 @@ public class SimpleElasticsearchRepositoryTests {
|
|||||||
// when
|
// when
|
||||||
repository.save(sampleEntity);
|
repository.save(sampleEntity);
|
||||||
// then
|
// then
|
||||||
SampleEntity entityFromElasticSearch = repository.findOne(documentId);
|
Optional<SampleEntity> entityFromElasticSearch = repository.findOne(documentId);
|
||||||
assertThat(entityFromElasticSearch, is(notNullValue()));
|
assertThat(entityFromElasticSearch.isPresent(), is(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -125,9 +126,9 @@ public class SimpleElasticsearchRepositoryTests {
|
|||||||
sampleEntity.setVersion(System.currentTimeMillis());
|
sampleEntity.setVersion(System.currentTimeMillis());
|
||||||
repository.save(sampleEntity);
|
repository.save(sampleEntity);
|
||||||
// when
|
// when
|
||||||
SampleEntity entityFromElasticSearch = repository.findOne(documentId);
|
Optional<SampleEntity> entityFromElasticSearch = repository.findOne(documentId);
|
||||||
// then
|
// then
|
||||||
assertThat(entityFromElasticSearch, is(notNullValue()));
|
assertThat(entityFromElasticSearch.isPresent(), is(true));
|
||||||
assertThat(sampleEntity, is((equalTo(sampleEntity))));
|
assertThat(sampleEntity, is((equalTo(sampleEntity))));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -166,8 +167,8 @@ public class SimpleElasticsearchRepositoryTests {
|
|||||||
// when
|
// when
|
||||||
repository.delete(documentId);
|
repository.delete(documentId);
|
||||||
// then
|
// then
|
||||||
SampleEntity entityFromElasticSearch = repository.findOne(documentId);
|
Optional<SampleEntity> entityFromElasticSearch = repository.findOne(documentId);
|
||||||
assertThat(entityFromElasticSearch, is(nullValue()));
|
assertThat(entityFromElasticSearch.isPresent(), is(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -474,8 +475,8 @@ public class SimpleElasticsearchRepositoryTests {
|
|||||||
// when
|
// when
|
||||||
repository.delete(sampleEntities);
|
repository.delete(sampleEntities);
|
||||||
// then
|
// then
|
||||||
assertThat(repository.findOne(documentId1), is(nullValue()));
|
assertThat(repository.findOne(documentId1).isPresent(), is(false));
|
||||||
assertThat(repository.findOne(documentId2), is(nullValue()));
|
assertThat(repository.findOne(documentId2).isPresent(), is(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2014 the original author or authors.
|
* Copyright 2013-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -40,8 +40,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||||||
* @author Gad Akuka
|
* @author Gad Akuka
|
||||||
* @author Rizwan Idrees
|
* @author Rizwan Idrees
|
||||||
* @author Mohsin Husen
|
* @author Mohsin Husen
|
||||||
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration("classpath:/simple-repository-test.xml")
|
@ContextConfiguration("classpath:/simple-repository-test.xml")
|
||||||
public class UUIDElasticsearchRepositoryTests {
|
public class UUIDElasticsearchRepositoryTests {
|
||||||
@ -79,11 +79,11 @@ public class UUIDElasticsearchRepositoryTests {
|
|||||||
// when
|
// when
|
||||||
repository.save(Arrays.asList(sampleEntityUUIDKeyed1, sampleEntityUUIDKeyed2));
|
repository.save(Arrays.asList(sampleEntityUUIDKeyed1, sampleEntityUUIDKeyed2));
|
||||||
// then
|
// then
|
||||||
SampleEntityUUIDKeyed entity1FromElasticSearch = repository.findOne(documentId1);
|
Optional<SampleEntityUUIDKeyed> entity1FromElasticSearch = repository.findOne(documentId1);
|
||||||
assertThat(entity1FromElasticSearch, is(notNullValue()));
|
assertThat(entity1FromElasticSearch.isPresent(), is(true));
|
||||||
|
|
||||||
SampleEntityUUIDKeyed entity2FromElasticSearch = repository.findOne(documentId2);
|
Optional<SampleEntityUUIDKeyed> entity2FromElasticSearch = repository.findOne(documentId2);
|
||||||
assertThat(entity2FromElasticSearch, is(notNullValue()));
|
assertThat(entity2FromElasticSearch.isPresent(), is(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -97,8 +97,8 @@ public class UUIDElasticsearchRepositoryTests {
|
|||||||
// when
|
// when
|
||||||
repository.save(sampleEntityUUIDKeyed);
|
repository.save(sampleEntityUUIDKeyed);
|
||||||
// then
|
// then
|
||||||
SampleEntityUUIDKeyed entityFromElasticSearch = repository.findOne(documentId);
|
Optional<SampleEntityUUIDKeyed> entityFromElasticSearch = repository.findOne(documentId);
|
||||||
assertThat(entityFromElasticSearch, is(notNullValue()));
|
assertThat(entityFromElasticSearch.isPresent(), is(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -111,9 +111,9 @@ public class UUIDElasticsearchRepositoryTests {
|
|||||||
sampleEntityUUIDKeyed.setVersion(System.currentTimeMillis());
|
sampleEntityUUIDKeyed.setVersion(System.currentTimeMillis());
|
||||||
repository.save(sampleEntityUUIDKeyed);
|
repository.save(sampleEntityUUIDKeyed);
|
||||||
// when
|
// when
|
||||||
SampleEntityUUIDKeyed entityFromElasticSearch = repository.findOne(documentId);
|
Optional<SampleEntityUUIDKeyed> entityFromElasticSearch = repository.findOne(documentId);
|
||||||
// then
|
// then
|
||||||
assertThat(entityFromElasticSearch, is(notNullValue()));
|
assertThat(entityFromElasticSearch.isPresent(), is(true));
|
||||||
assertThat(sampleEntityUUIDKeyed, is((equalTo(sampleEntityUUIDKeyed))));
|
assertThat(sampleEntityUUIDKeyed, is((equalTo(sampleEntityUUIDKeyed))));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,8 +152,8 @@ public class UUIDElasticsearchRepositoryTests {
|
|||||||
// when
|
// when
|
||||||
repository.delete(documentId);
|
repository.delete(documentId);
|
||||||
// then
|
// then
|
||||||
SampleEntityUUIDKeyed entityFromElasticSearch = repository.findOne(documentId);
|
Optional<SampleEntityUUIDKeyed> entityFromElasticSearch = repository.findOne(documentId);
|
||||||
assertThat(entityFromElasticSearch, is(nullValue()));
|
assertThat(entityFromElasticSearch.isPresent(), is(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -445,8 +445,8 @@ public class UUIDElasticsearchRepositoryTests {
|
|||||||
// when
|
// when
|
||||||
repository.delete(sampleEntities);
|
repository.delete(sampleEntities);
|
||||||
// then
|
// then
|
||||||
assertThat(repository.findOne(documentId1), is(nullValue()));
|
assertThat(repository.findOne(documentId1).isPresent(), is(false));
|
||||||
assertThat(repository.findOne(documentId2), is(nullValue()));
|
assertThat(repository.findOne(documentId2).isPresent(), is(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user