Added support for varying ID types (int/long) for repositories.
This commit is contained in:
@@ -0,0 +1,316 @@
|
||||
/*
|
||||
* Copyright 2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.repository.support;
|
||||
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.data.domain.*;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
||||
import org.springframework.data.elasticsearch.core.query.*;
|
||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.elasticsearch.index.query.QueryBuilders.inQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||
import static org.springframework.data.elasticsearch.core.query.Query.DEFAULT_PAGE;
|
||||
|
||||
/**
|
||||
* Elasticsearch specific repository implementation. Likely to be used as target within {@link ElasticsearchRepositoryFactory}
|
||||
*
|
||||
*
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
* @author Ryan Henszey
|
||||
*/
|
||||
public abstract class AbstractElasticsearchRepository<T,ID extends Serializable> implements ElasticsearchRepository<T, ID> {
|
||||
|
||||
|
||||
protected ElasticsearchOperations elasticsearchOperations;
|
||||
protected Class<T> entityClass;
|
||||
protected ElasticsearchEntityInformation<T, ID> entityInformation;
|
||||
|
||||
public AbstractElasticsearchRepository() {
|
||||
}
|
||||
|
||||
public AbstractElasticsearchRepository(ElasticsearchOperations elasticsearchOperations) {
|
||||
Assert.notNull(elasticsearchOperations);
|
||||
this.setElasticsearchOperations(elasticsearchOperations);
|
||||
}
|
||||
|
||||
public AbstractElasticsearchRepository(ElasticsearchEntityInformation<T, ID> metadata, ElasticsearchOperations elasticsearchOperations) {
|
||||
this(elasticsearchOperations);
|
||||
Assert.notNull(metadata);
|
||||
this.entityInformation = metadata;
|
||||
setEntityClass(this.entityInformation.getJavaType());
|
||||
createIndex();
|
||||
putMapping();
|
||||
}
|
||||
|
||||
private void createIndex(){
|
||||
elasticsearchOperations.createIndex(getEntityClass());
|
||||
}
|
||||
|
||||
private void putMapping(){
|
||||
elasticsearchOperations.putMapping(getEntityClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public T findOne(ID id) {
|
||||
GetQuery query = new GetQuery();
|
||||
query.setId(stringIdRepresentation(id));
|
||||
return elasticsearchOperations.queryForObject(query, getEntityClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<T> findAll() {
|
||||
int itemCount = (int) this.count();
|
||||
if (itemCount == 0) {
|
||||
return new PageImpl<T>(Collections.<T> emptyList());
|
||||
}
|
||||
return this.findAll(new PageRequest(0, Math.max(1, itemCount)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<T> findAll(Pageable pageable) {
|
||||
SearchQuery query = new NativeSearchQueryBuilder()
|
||||
.withQuery(matchAllQuery())
|
||||
.withPageable(pageable)
|
||||
.build();
|
||||
return elasticsearchOperations.queryForPage(query, getEntityClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<T> findAll(Sort sort) {
|
||||
int itemCount = (int) this.count();
|
||||
if (itemCount == 0) {
|
||||
return new PageImpl<T>(Collections.<T> emptyList());
|
||||
}
|
||||
SearchQuery query = new NativeSearchQueryBuilder()
|
||||
.withQuery(matchAllQuery())
|
||||
.withPageable(new PageRequest(0,itemCount, sort))
|
||||
.build();
|
||||
return elasticsearchOperations.queryForPage(query, getEntityClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<T> findAll(Iterable<ID> ids) {
|
||||
SearchQuery query = new NativeSearchQueryBuilder()
|
||||
.withQuery(inQuery(entityInformation.getIdAttribute(), ids))
|
||||
.build();
|
||||
return elasticsearchOperations.queryForPage(query, getEntityClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public long count() {
|
||||
SearchQuery query = new NativeSearchQueryBuilder()
|
||||
.withQuery(matchAllQuery()).build();
|
||||
return elasticsearchOperations.count(query,getEntityClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S extends T> S save(S entity) {
|
||||
Assert.notNull(entity, "Cannot save 'null' entity.");
|
||||
elasticsearchOperations.index(createIndexQuery(entity));
|
||||
elasticsearchOperations.refresh(entityInformation.getIndexName(), true);
|
||||
return entity;
|
||||
}
|
||||
|
||||
public <S extends T> List<S> save(List<S> entities) {
|
||||
Assert.notNull(entities, "Cannot insert 'null' as a List.");
|
||||
Assert.notEmpty(entities,"Cannot insert empty List.");
|
||||
List<IndexQuery> queries = new ArrayList<IndexQuery>();
|
||||
for(S s:entities){
|
||||
queries.add(createIndexQuery(s));
|
||||
}
|
||||
elasticsearchOperations.bulkIndex(queries);
|
||||
elasticsearchOperations.refresh(entityInformation.getIndexName(), true);
|
||||
return entities;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S extends T> S index(S entity) {
|
||||
return save(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S extends T> Iterable<S> save(Iterable<S> entities) {
|
||||
Assert.notNull(entities, "Cannot insert 'null' as a List.");
|
||||
if (!(entities instanceof Collection<?>)) {
|
||||
throw new InvalidDataAccessApiUsageException("Entities have to be inside a collection");
|
||||
}
|
||||
List<IndexQuery> queries = new ArrayList<IndexQuery>();
|
||||
for(S s: entities){
|
||||
queries.add(createIndexQuery(s));
|
||||
}
|
||||
elasticsearchOperations.bulkIndex(queries);
|
||||
elasticsearchOperations.refresh(entityInformation.getIndexName(), true);
|
||||
return entities;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists(ID id) {
|
||||
return findOne(id) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<T> search(QueryBuilder query) {
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||
.withQuery(query).build();
|
||||
int count = (int) elasticsearchOperations.count(searchQuery, getEntityClass());
|
||||
if(count == 0){
|
||||
return new PageImpl<T>(Collections.<T>emptyList());
|
||||
}
|
||||
searchQuery.setPageable(new PageRequest(0, count));
|
||||
return elasticsearchOperations.queryForPage(searchQuery, getEntityClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<T> search(QueryBuilder query, Pageable pageable) {
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||
.withQuery(query)
|
||||
.withPageable(pageable)
|
||||
.build();
|
||||
return elasticsearchOperations.queryForPage(searchQuery, getEntityClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<T> search(SearchQuery query){
|
||||
return elasticsearchOperations.queryForPage(query, getEntityClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<T> searchSimilar(T entity) {
|
||||
return searchSimilar(entity, DEFAULT_PAGE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<T> searchSimilar(T entity, Pageable pageable) {
|
||||
Assert.notNull(entity, "Cannot search similar records for 'null'.");
|
||||
Assert.notNull(entity, "Pageable cannot be 'null'");
|
||||
MoreLikeThisQuery query = new MoreLikeThisQuery();
|
||||
query.setId(stringIdRepresentation(extractIdFromBean(entity)));
|
||||
query.setPageable(pageable);
|
||||
return elasticsearchOperations.moreLikeThis(query, getEntityClass());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void delete(ID id) {
|
||||
Assert.notNull(id, "Cannot delete entity with id 'null'.");
|
||||
elasticsearchOperations.delete(entityInformation.getIndexName(), entityInformation.getType(),stringIdRepresentation(id));
|
||||
elasticsearchOperations.refresh(entityInformation.getIndexName(),true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(T entity) {
|
||||
Assert.notNull(entity, "Cannot delete 'null' entity.");
|
||||
delete(extractIdFromBean(entity));
|
||||
elasticsearchOperations.refresh(entityInformation.getIndexName(), true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Iterable<? extends T> entities) {
|
||||
Assert.notNull(entities, "Cannot delete 'null' list.");
|
||||
for (T entity : entities) {
|
||||
delete(entity);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAll() {
|
||||
DeleteQuery deleteQuery = new DeleteQuery();
|
||||
deleteQuery.setQuery(matchAllQuery());
|
||||
elasticsearchOperations.delete(deleteQuery, getEntityClass());
|
||||
elasticsearchOperations.refresh(entityInformation.getIndexName(),true);
|
||||
}
|
||||
|
||||
private IndexQuery createIndexQuery(T entity){
|
||||
IndexQuery query = new IndexQuery();
|
||||
query.setObject(entity);
|
||||
query.setId(stringIdRepresentation(extractIdFromBean(entity)));
|
||||
query.setVersion(extractVersionFromBean(entity));
|
||||
return query;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Class<T> resolveReturnedClassFromGenericType() {
|
||||
ParameterizedType parameterizedType = resolveReturnedClassFromGenericType(getClass());
|
||||
return (Class<T>) parameterizedType.getActualTypeArguments()[0];
|
||||
}
|
||||
|
||||
private ParameterizedType resolveReturnedClassFromGenericType(Class<?> clazz) {
|
||||
Object genericSuperclass = clazz.getGenericSuperclass();
|
||||
if (genericSuperclass instanceof ParameterizedType) {
|
||||
ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
|
||||
Type rawtype = parameterizedType.getRawType();
|
||||
if (SimpleElasticsearchRepository.class.equals(rawtype)) {
|
||||
return parameterizedType;
|
||||
}
|
||||
}
|
||||
return resolveReturnedClassFromGenericType(clazz.getSuperclass());
|
||||
}
|
||||
|
||||
public Class<T> getEntityClass() {
|
||||
if (!isEntityClassSet()) {
|
||||
try {
|
||||
this.entityClass = resolveReturnedClassFromGenericType();
|
||||
} catch (Exception e) {
|
||||
throw new InvalidDataAccessApiUsageException("Unable to resolve EntityClass. Please use according setter!", e);
|
||||
}
|
||||
}
|
||||
return entityClass;
|
||||
}
|
||||
|
||||
private boolean isEntityClassSet() {
|
||||
return entityClass != null;
|
||||
}
|
||||
|
||||
public final void setEntityClass(Class<T> entityClass) {
|
||||
Assert.notNull(entityClass, "EntityClass must not be null.");
|
||||
this.entityClass = entityClass;
|
||||
}
|
||||
|
||||
public final void setElasticsearchOperations(ElasticsearchOperations elasticsearchOperations) {
|
||||
Assert.notNull(elasticsearchOperations, "ElasticsearchOperations must not be null.");
|
||||
this.elasticsearchOperations = elasticsearchOperations;
|
||||
}
|
||||
|
||||
|
||||
protected ID extractIdFromBean(T entity) {
|
||||
if (entityInformation != null) {
|
||||
return entityInformation.getId(entity);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected abstract String stringIdRepresentation(ID id);
|
||||
|
||||
private Long extractVersionFromBean(T entity){
|
||||
if (entityInformation != null) {
|
||||
return entityInformation.getVersion(entity);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,6 +15,11 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.repository.support;
|
||||
|
||||
import static org.springframework.data.querydsl.QueryDslUtils.QUERY_DSL_PRESENT;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||
import org.springframework.data.elasticsearch.repository.query.ElasticsearchPartQuery;
|
||||
@@ -28,18 +33,15 @@ import org.springframework.data.repository.query.QueryLookupStrategy;
|
||||
import org.springframework.data.repository.query.RepositoryQuery;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import static org.springframework.data.querydsl.QueryDslUtils.QUERY_DSL_PRESENT;
|
||||
|
||||
/**
|
||||
* Factory to create {@link ElasticsearchRepository}
|
||||
*
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
* @author Ryan Henszey
|
||||
*/
|
||||
public class ElasticsearchRepositoryFactory extends RepositoryFactorySupport {
|
||||
|
||||
|
||||
private final ElasticsearchOperations elasticsearchOperations;
|
||||
private final ElasticsearchEntityInformationCreator entityInformationCreator;
|
||||
@@ -59,8 +61,27 @@ public class ElasticsearchRepositoryFactory extends RepositoryFactorySupport {
|
||||
@Override
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
protected Object getTargetRepository(RepositoryMetadata metadata) {
|
||||
SimpleElasticsearchRepository repository = new SimpleElasticsearchRepository(getEntityInformation(metadata.getDomainType()), elasticsearchOperations);
|
||||
|
||||
ElasticsearchEntityInformation<?, ?> entityInformation = getEntityInformation(metadata.getDomainType());
|
||||
|
||||
AbstractElasticsearchRepository repository;
|
||||
|
||||
//Probably a better way to store and look these up.
|
||||
if(Integer.class.isAssignableFrom(entityInformation.getIdType()) ||
|
||||
Long.class.isAssignableFrom(entityInformation.getIdType()) ||
|
||||
Double.class.isAssignableFrom(entityInformation.getIdType())){
|
||||
//logger.debug("Using NumberKeyedRepository for " + metadata.getRepositoryInterface());
|
||||
repository = new NumberKeyedRepository(getEntityInformation(metadata.getDomainType()), elasticsearchOperations);
|
||||
}
|
||||
else if (entityInformation.getIdType() == String.class){
|
||||
//logger.debug("Using SimpleElasticsearchRepository for " + metadata.getRepositoryInterface());
|
||||
repository = new SimpleElasticsearchRepository(getEntityInformation(metadata.getDomainType()), elasticsearchOperations);
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("Unsuppored ID type " + entityInformation.getIdType());
|
||||
}
|
||||
repository.setEntityClass(metadata.getDomainType());
|
||||
|
||||
return repository;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.repository.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
|
||||
@@ -23,8 +25,6 @@ import org.springframework.data.mapping.model.BeanWrapper;
|
||||
import org.springframework.data.repository.core.support.AbstractEntityInformation;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Elasticsearch specific implementation of {@link org.springframework.data.repository.core.support.AbstractEntityInformation}
|
||||
*
|
||||
@@ -33,6 +33,7 @@ import java.io.Serializable;
|
||||
*
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
* @author Ryan Henszey
|
||||
*/
|
||||
public class MappingElasticsearchEntityInformation<T, ID extends Serializable> extends AbstractEntityInformation<T, ID>
|
||||
implements ElasticsearchEntityInformation<T, ID> {
|
||||
@@ -41,6 +42,7 @@ public class MappingElasticsearchEntityInformation<T, ID extends Serializable> e
|
||||
private final ElasticsearchPersistentEntity<T> entityMetadata;
|
||||
private final String indexName;
|
||||
private final String type;
|
||||
private Class<?> idClass;
|
||||
|
||||
public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity<T> entity) {
|
||||
this(entity, null, null);
|
||||
@@ -51,6 +53,7 @@ public class MappingElasticsearchEntityInformation<T, ID extends Serializable> e
|
||||
this.entityMetadata = entity;
|
||||
this.indexName = indexName;
|
||||
this.type = type;
|
||||
this.idClass = entity.getIdProperty().getType();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -67,7 +70,7 @@ public class MappingElasticsearchEntityInformation<T, ID extends Serializable> e
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Class<ID> getIdType() {
|
||||
return (Class<ID>) String.class;
|
||||
return (Class<ID>)idClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.repository.support;
|
||||
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
||||
|
||||
/**
|
||||
* Elasticsearch specific repository implementation. Likely to be used as target within {@link ElasticsearchRepositoryFactory}
|
||||
*
|
||||
*
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
* @author Ryan Henszey
|
||||
*/
|
||||
public class NumberKeyedRepository<T,ID extends Number> extends AbstractElasticsearchRepository<T,ID> {
|
||||
|
||||
|
||||
public NumberKeyedRepository() {
|
||||
super();
|
||||
}
|
||||
|
||||
public NumberKeyedRepository(ElasticsearchEntityInformation<T, ID> metadata,ElasticsearchOperations elasticsearchOperations) {
|
||||
super(metadata, elasticsearchOperations);
|
||||
}
|
||||
|
||||
public NumberKeyedRepository(ElasticsearchOperations elasticsearchOperations) {
|
||||
super(elasticsearchOperations);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected String stringIdRepresentation(ID id) {
|
||||
return String.valueOf(id);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -15,299 +15,33 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.repository.support;
|
||||
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.data.domain.*;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
||||
import org.springframework.data.elasticsearch.core.query.*;
|
||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.elasticsearch.index.query.QueryBuilders.inQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||
import static org.springframework.data.elasticsearch.core.query.Query.DEFAULT_PAGE;
|
||||
|
||||
/**
|
||||
* Elasticsearch specific repository implementation. Likely to be used as target within {@link ElasticsearchRepositoryFactory}
|
||||
*
|
||||
* @param <T>
|
||||
*
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
* @author Ryan Henszey
|
||||
*/
|
||||
public class SimpleElasticsearchRepository<T> implements ElasticsearchRepository<T, String> {
|
||||
public class SimpleElasticsearchRepository<T> extends AbstractElasticsearchRepository<T,String> {
|
||||
|
||||
public SimpleElasticsearchRepository() {
|
||||
super();
|
||||
}
|
||||
|
||||
public SimpleElasticsearchRepository(ElasticsearchEntityInformation<T, String> metadata,ElasticsearchOperations elasticsearchOperations) {
|
||||
super(metadata, elasticsearchOperations);
|
||||
}
|
||||
|
||||
private ElasticsearchOperations elasticsearchOperations;
|
||||
private Class<T> entityClass;
|
||||
private ElasticsearchEntityInformation<T, String> entityInformation;
|
||||
public SimpleElasticsearchRepository(ElasticsearchOperations elasticsearchOperations) {
|
||||
super(elasticsearchOperations);
|
||||
}
|
||||
|
||||
public SimpleElasticsearchRepository() {
|
||||
}
|
||||
|
||||
public SimpleElasticsearchRepository(ElasticsearchOperations elasticsearchOperations) {
|
||||
Assert.notNull(elasticsearchOperations);
|
||||
this.setElasticsearchOperations(elasticsearchOperations);
|
||||
}
|
||||
|
||||
public SimpleElasticsearchRepository(ElasticsearchEntityInformation<T, String> metadata, ElasticsearchOperations elasticsearchOperations) {
|
||||
this(elasticsearchOperations);
|
||||
Assert.notNull(metadata);
|
||||
this.entityInformation = metadata;
|
||||
setEntityClass(this.entityInformation.getJavaType());
|
||||
createIndex();
|
||||
putMapping();
|
||||
}
|
||||
|
||||
private void createIndex(){
|
||||
elasticsearchOperations.createIndex(getEntityClass());
|
||||
}
|
||||
|
||||
private void putMapping(){
|
||||
elasticsearchOperations.putMapping(getEntityClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public T findOne(String id) {
|
||||
GetQuery query = new GetQuery();
|
||||
query.setId(id);
|
||||
return elasticsearchOperations.queryForObject(query, getEntityClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<T> findAll() {
|
||||
int itemCount = (int) this.count();
|
||||
if (itemCount == 0) {
|
||||
return new PageImpl<T>(Collections.<T> emptyList());
|
||||
}
|
||||
return this.findAll(new PageRequest(0, Math.max(1, itemCount)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<T> findAll(Pageable pageable) {
|
||||
SearchQuery query = new NativeSearchQueryBuilder()
|
||||
.withQuery(matchAllQuery())
|
||||
.withPageable(pageable)
|
||||
.build();
|
||||
return elasticsearchOperations.queryForPage(query, getEntityClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<T> findAll(Sort sort) {
|
||||
int itemCount = (int) this.count();
|
||||
if (itemCount == 0) {
|
||||
return new PageImpl<T>(Collections.<T> emptyList());
|
||||
}
|
||||
SearchQuery query = new NativeSearchQueryBuilder()
|
||||
.withQuery(matchAllQuery())
|
||||
.withPageable(new PageRequest(0,itemCount, sort))
|
||||
.build();
|
||||
return elasticsearchOperations.queryForPage(query, getEntityClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<T> findAll(Iterable<String> ids) {
|
||||
SearchQuery query = new NativeSearchQueryBuilder()
|
||||
.withQuery(inQuery(entityInformation.getIdAttribute(), ids))
|
||||
.build();
|
||||
return elasticsearchOperations.queryForPage(query, getEntityClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public long count() {
|
||||
SearchQuery query = new NativeSearchQueryBuilder()
|
||||
.withQuery(matchAllQuery()).build();
|
||||
return elasticsearchOperations.count(query,getEntityClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S extends T> S save(S entity) {
|
||||
Assert.notNull(entity, "Cannot save 'null' entity.");
|
||||
elasticsearchOperations.index(createIndexQuery(entity));
|
||||
elasticsearchOperations.refresh(entityInformation.getIndexName(), true);
|
||||
return entity;
|
||||
}
|
||||
|
||||
public <S extends T> List<S> save(List<S> entities) {
|
||||
Assert.notNull(entities, "Cannot insert 'null' as a List.");
|
||||
Assert.notEmpty(entities,"Cannot insert empty List.");
|
||||
List<IndexQuery> queries = new ArrayList<IndexQuery>();
|
||||
for(S s:entities){
|
||||
queries.add(createIndexQuery(s));
|
||||
}
|
||||
elasticsearchOperations.bulkIndex(queries);
|
||||
elasticsearchOperations.refresh(entityInformation.getIndexName(), true);
|
||||
return entities;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S extends T> S index(S entity) {
|
||||
return save(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S extends T> Iterable<S> save(Iterable<S> entities) {
|
||||
Assert.notNull(entities, "Cannot insert 'null' as a List.");
|
||||
if (!(entities instanceof Collection<?>)) {
|
||||
throw new InvalidDataAccessApiUsageException("Entities have to be inside a collection");
|
||||
}
|
||||
List<IndexQuery> queries = new ArrayList<IndexQuery>();
|
||||
for(S s: entities){
|
||||
queries.add(createIndexQuery(s));
|
||||
}
|
||||
elasticsearchOperations.bulkIndex(queries);
|
||||
elasticsearchOperations.refresh(entityInformation.getIndexName(), true);
|
||||
return entities;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists(String id) {
|
||||
return findOne(id) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<T> search(QueryBuilder query) {
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||
.withQuery(query).build();
|
||||
int count = (int) elasticsearchOperations.count(searchQuery, getEntityClass());
|
||||
if(count == 0){
|
||||
return new PageImpl<T>(Collections.<T>emptyList());
|
||||
}
|
||||
searchQuery.setPageable(new PageRequest(0, count));
|
||||
return elasticsearchOperations.queryForPage(searchQuery, getEntityClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<T> search(QueryBuilder query, Pageable pageable) {
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||
.withQuery(query)
|
||||
.withPageable(pageable)
|
||||
.build();
|
||||
return elasticsearchOperations.queryForPage(searchQuery, getEntityClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<T> search(SearchQuery query){
|
||||
return elasticsearchOperations.queryForPage(query, getEntityClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<T> searchSimilar(T entity) {
|
||||
return searchSimilar(entity, DEFAULT_PAGE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<T> searchSimilar(T entity, Pageable pageable) {
|
||||
Assert.notNull(entity, "Cannot search similar records for 'null'.");
|
||||
Assert.notNull(entity, "Pageable cannot be 'null'");
|
||||
MoreLikeThisQuery query = new MoreLikeThisQuery();
|
||||
query.setId(extractIdFromBean(entity));
|
||||
query.setPageable(pageable);
|
||||
return elasticsearchOperations.moreLikeThis(query, getEntityClass());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void delete(String id) {
|
||||
Assert.notNull(id, "Cannot delete entity with id 'null'.");
|
||||
elasticsearchOperations.delete(entityInformation.getIndexName(), entityInformation.getType(),id);
|
||||
elasticsearchOperations.refresh(entityInformation.getIndexName(),true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(T entity) {
|
||||
Assert.notNull(entity, "Cannot delete 'null' entity.");
|
||||
delete(extractIdFromBean(entity));
|
||||
elasticsearchOperations.refresh(entityInformation.getIndexName(), true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Iterable<? extends T> entities) {
|
||||
Assert.notNull(entities, "Cannot delete 'null' list.");
|
||||
for (T entity : entities) {
|
||||
delete(entity);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAll() {
|
||||
DeleteQuery deleteQuery = new DeleteQuery();
|
||||
deleteQuery.setQuery(matchAllQuery());
|
||||
elasticsearchOperations.delete(deleteQuery, getEntityClass());
|
||||
elasticsearchOperations.refresh(entityInformation.getIndexName(),true);
|
||||
}
|
||||
|
||||
private IndexQuery createIndexQuery(T entity){
|
||||
IndexQuery query = new IndexQuery();
|
||||
query.setObject(entity);
|
||||
query.setId(extractIdFromBean(entity));
|
||||
query.setVersion(extractVersionFromBean(entity));
|
||||
return query;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Class<T> resolveReturnedClassFromGenericType() {
|
||||
ParameterizedType parameterizedType = resolveReturnedClassFromGenericType(getClass());
|
||||
return (Class<T>) parameterizedType.getActualTypeArguments()[0];
|
||||
}
|
||||
|
||||
private ParameterizedType resolveReturnedClassFromGenericType(Class<?> clazz) {
|
||||
Object genericSuperclass = clazz.getGenericSuperclass();
|
||||
if (genericSuperclass instanceof ParameterizedType) {
|
||||
ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
|
||||
Type rawtype = parameterizedType.getRawType();
|
||||
if (SimpleElasticsearchRepository.class.equals(rawtype)) {
|
||||
return parameterizedType;
|
||||
}
|
||||
}
|
||||
return resolveReturnedClassFromGenericType(clazz.getSuperclass());
|
||||
}
|
||||
|
||||
public Class<T> getEntityClass() {
|
||||
if (!isEntityClassSet()) {
|
||||
try {
|
||||
this.entityClass = resolveReturnedClassFromGenericType();
|
||||
} catch (Exception e) {
|
||||
throw new InvalidDataAccessApiUsageException("Unable to resolve EntityClass. Please use according setter!", e);
|
||||
}
|
||||
}
|
||||
return entityClass;
|
||||
}
|
||||
|
||||
private boolean isEntityClassSet() {
|
||||
return entityClass != null;
|
||||
}
|
||||
|
||||
public final void setEntityClass(Class<T> entityClass) {
|
||||
Assert.notNull(entityClass, "EntityClass must not be null.");
|
||||
this.entityClass = entityClass;
|
||||
}
|
||||
|
||||
public final void setElasticsearchOperations(ElasticsearchOperations elasticsearchOperations) {
|
||||
Assert.notNull(elasticsearchOperations, "ElasticsearchOperations must not be null.");
|
||||
this.elasticsearchOperations = elasticsearchOperations;
|
||||
}
|
||||
|
||||
|
||||
private String extractIdFromBean(T entity) {
|
||||
if (entityInformation != null) {
|
||||
return entityInformation.getId(entity);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Long extractVersionFromBean(T entity){
|
||||
if (entityInformation != null) {
|
||||
return entityInformation.getVersion(entity);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
protected String stringIdRepresentation(String id) {
|
||||
return id;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user