Merge pull request #29 from henszey/multipleprimitiverepo

Added support for varying ID types (int/long) for repositories.
This commit is contained in:
Mohsin Husen 2013-04-25 10:19:09 -07:00
commit 9856a04056
11 changed files with 1461 additions and 291 deletions

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,68 @@
/*
* 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;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "double-keyed-entity", type = "double-keyed-entity")
public class DoubleIDEntity {
@Id
private Double id;
private String type;
private String message;
@Version
private Long version;
public Double getId() {
return id;
}
public void setId(Double id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
}

View File

@ -0,0 +1,70 @@
/*
* 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;
import java.math.BigInteger;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "integer-keyed-entity", type = "integer-keyed-entity")
public class IntegerIDEntity {
@Id
private Integer id;
private String type;
private String message;
@Version
private Long version;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
}

View File

@ -0,0 +1,25 @@
/*
* 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.repositories;
import org.springframework.data.elasticsearch.DoubleIDEntity;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface DoubleIDRepository extends ElasticsearchRepository<DoubleIDEntity,Double> {
}

View File

@ -0,0 +1,24 @@
/*
* 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.repositories;
import org.springframework.data.elasticsearch.IntegerIDEntity;
import org.springframework.data.elasticsearch.SampleEntity;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface IntegerIDRepository extends ElasticsearchRepository<IntegerIDEntity,Integer> {
}

View File

@ -0,0 +1,429 @@
/*
* 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 static org.elasticsearch.index.query.QueryBuilders.fieldQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang.math.RandomUtils;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.DoubleIDEntity;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.data.elasticsearch.repositories.DoubleIDRepository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/simple-repository-test.xml")
public class DoubleIDRepositoryTest {
@Resource
private DoubleIDRepository repository;
@Before
public void before(){
repository.deleteAll();
}
@Test
public void shouldDoBulkIndexDocument(){
//given
Double documentId1 = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity1 = new DoubleIDEntity();
sampleEntity1.setId(documentId1);
sampleEntity1.setMessage("some message");
sampleEntity1.setVersion(System.currentTimeMillis());
Double documentId2 = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity2 = new DoubleIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("some message");
sampleEntity2.setVersion(System.currentTimeMillis());
//when
repository.save(Arrays.asList(sampleEntity1, sampleEntity2));
//then
DoubleIDEntity entity1FromElasticSearch = repository.findOne(documentId1);
assertThat(entity1FromElasticSearch, is(notNullValue()));
DoubleIDEntity entity2FromElasticSearch = repository.findOne(documentId2);
assertThat(entity2FromElasticSearch, is(notNullValue()));
}
@Test
public void shouldSaveDocument(){
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("some message");
sampleEntity.setVersion(System.currentTimeMillis());
//when
repository.save(sampleEntity);
//then
DoubleIDEntity entityFromElasticSearch = repository.findOne(documentId);
assertThat(entityFromElasticSearch, is(notNullValue()));
}
@Test
public void shouldFindDocumentById(){
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("some message");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
DoubleIDEntity entityFromElasticSearch = repository.findOne(documentId);
//then
assertThat(entityFromElasticSearch, is(notNullValue()));
assertThat(sampleEntity, is((equalTo(sampleEntity))));
}
@Test
public void shouldReturnCountOfDocuments(){
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("some message");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
Long count = repository.count();
//then
assertThat(count, is(greaterThanOrEqualTo(1L)));
}
@Test
public void shouldFindAllDocuments(){
//when
Iterable<DoubleIDEntity> results = repository.findAll();
//then
assertThat(results, is(notNullValue()));
}
@Test
public void shouldDeleteDocument(){
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("some message");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
repository.delete(documentId);
//then
DoubleIDEntity entityFromElasticSearch = repository.findOne(documentId);
assertThat(entityFromElasticSearch, is(nullValue()));
}
@Test
public void shouldSearchDocumentsGivenSearchQuery(){
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("some test message");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
SearchQuery query = new NativeSearchQueryBuilder()
.withQuery(termQuery("message", "test"))
.build();
//when
Page<DoubleIDEntity> page = repository.search(query);
//then
assertThat(page, is(notNullValue()));
assertThat(page.getNumberOfElements(), is(greaterThanOrEqualTo(1)));
}
@Test
public void shouldSearchDocumentsGivenElasticsearchQuery(){
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
Page<DoubleIDEntity> page = repository.search(termQuery("message", "world"), new PageRequest(0,50));
//then
assertThat(page, is(notNullValue()));
assertThat(page.getNumberOfElements(), is(greaterThanOrEqualTo(1)));
}
@Test
@Ignore
public void shouldFindAllByIdQuery(){
//todo : find solution for findAll(Iterable<Ids> ids)
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
Double documentId2 = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity2 = new DoubleIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("hello world.");
sampleEntity2.setVersion(System.currentTimeMillis());
repository.save(sampleEntity2);
//when
Iterable<DoubleIDEntity> sampleEntities=repository.findAll(Arrays.asList(documentId,documentId2));
//then
assertNotNull("sample entities cant be null..", sampleEntities);
}
@Test
public void shouldSaveIterableEntities(){
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity1 = new DoubleIDEntity();
sampleEntity1.setId(documentId);
sampleEntity1.setMessage("hello world.");
sampleEntity1.setVersion(System.currentTimeMillis());
Double documentId2 = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity2 = new DoubleIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("hello world.");
sampleEntity2.setVersion(System.currentTimeMillis());
Iterable<DoubleIDEntity> sampleEntities = Arrays.asList(sampleEntity1,sampleEntity2);
//when
repository.save(sampleEntities);
//then
Page<DoubleIDEntity> entities = repository.search(fieldQuery("id", documentId), new PageRequest(0, 50));
assertNotNull(entities);
}
@Test
public void shouldReturnTrueGivenDocumentWithIdExists(){
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
boolean exist = repository.exists(documentId);
//then
assertEquals(exist, true);
}
@Test
public void shouldReturnResultsForGivenSearchQuery(){
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(fieldQuery("id",documentId))
.build();
Page<DoubleIDEntity> sampleEntities= repository.search(searchQuery);
//then
assertThat(sampleEntities.getTotalElements(), equalTo(1L));
}
@Test
public void shouldDeleteAll(){
//when
repository.deleteAll();
//then
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery())
.build();
Page<DoubleIDEntity> sampleEntities= repository.search(searchQuery);
assertThat(sampleEntities.getTotalElements(), equalTo(0L));
}
@Test
public void shouldDeleteEntity(){
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
repository.delete(sampleEntity);
//then
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(fieldQuery("id", documentId))
.build();
Page<DoubleIDEntity> sampleEntities= repository.search(searchQuery);
assertThat(sampleEntities.getTotalElements(),equalTo(0L));
}
@Test
public void shouldReturnIterableEntities(){
//given
Double documentId1 = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity1 = new DoubleIDEntity();
sampleEntity1.setId(documentId1);
sampleEntity1.setMessage("hello world.");
sampleEntity1.setVersion(System.currentTimeMillis());
repository.save(sampleEntity1);
Double documentId2 = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity2 = new DoubleIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("hello world.");
sampleEntity2.setVersion(System.currentTimeMillis());
repository.save(sampleEntity2);
//when
Iterable<DoubleIDEntity> sampleEntities = repository.search(fieldQuery("id",documentId1));
//then
assertNotNull("sample entities cant be null..", sampleEntities);
}
@Test
public void shouldDeleteIterableEntities(){
//given
Double documentId1 = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity1 = new DoubleIDEntity();
sampleEntity1.setId(documentId1);
sampleEntity1.setMessage("hello world.");
sampleEntity1.setVersion(System.currentTimeMillis());
Double documentId2 = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity2 = new DoubleIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("hello world.");
sampleEntity2.setVersion(System.currentTimeMillis());
repository.save(sampleEntity2);
Iterable<DoubleIDEntity> sampleEntities = Arrays.asList(sampleEntity2,sampleEntity2);
//when
repository.delete(sampleEntities);
//then
assertThat(repository.findOne(documentId1),is(nullValue()));
assertThat(repository.findOne(documentId2),is(nullValue()));
}
@Test
public void shouldIndexEntity(){
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setVersion(System.currentTimeMillis());
sampleEntity.setMessage("some message");
//when
repository.index(sampleEntity);
//then
Page<DoubleIDEntity> entities = repository.search(fieldQuery("id", documentId), new PageRequest(0,50));
assertThat(entities.getTotalElements(),equalTo(1L));
}
@Test
public void shouldSortByGivenField(){
//todo
//given
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("A. hello world.");
repository.save(sampleEntity);
Double documentId2 = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity2 = new DoubleIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("B.hello world.");
repository.save(sampleEntity2);
//when
Iterable<DoubleIDEntity> sampleEntities=repository.findAll(new Sort(new Sort.Order(Sort.Direction.ASC,"message")));
//then
assertThat(sampleEntities,is(notNullValue()));
}
@Test
public void shouldReturnSimilarEntities(){
//given
String sampleMessage = "So we build a web site or an application and want to add search to it, " +
"and then it hits us: getting search working is hard. We want our search solution to be fast," +
" we want a painless setup and a completely free search schema, we want to be able to index data simply using JSON over HTTP, " +
"we want our search server to be always available, we want to be able to start with one machine and scale to hundreds, " +
"we want real-time search, we want simple multi-tenancy, and we want a solution that is built for the cloud.";
List<DoubleIDEntity> sampleEntities = createSampleEntitiesWithMessage(sampleMessage, 30);
repository.save(sampleEntities);
//when
Page<DoubleIDEntity> results = repository.searchSimilar(sampleEntities.get(0));
//then
assertThat(results.getTotalElements(), is(greaterThanOrEqualTo(1L)));
}
private static List<DoubleIDEntity> createSampleEntitiesWithMessage(String message, int numberOfEntities){
List<DoubleIDEntity> sampleEntities = new ArrayList<DoubleIDEntity>();
for(int i = 0; i < numberOfEntities; i++){
Double documentId = RandomUtils.nextDouble();
DoubleIDEntity sampleEntity = new DoubleIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage(message);
sampleEntity.setVersion(System.currentTimeMillis());
sampleEntities.add(sampleEntity);
}
return sampleEntities;
}
}

View File

@ -0,0 +1,429 @@
/*
* 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 static org.elasticsearch.index.query.QueryBuilders.fieldQuery;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang.math.RandomUtils;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.IntegerIDEntity;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.data.elasticsearch.repositories.IntegerIDRepository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/simple-repository-test.xml")
public class IntegerIDRepositoryTest {
@Resource
private IntegerIDRepository repository;
@Before
public void before(){
repository.deleteAll();
}
@Test
public void shouldDoBulkIndexDocument(){
//given
Integer documentId1 = RandomUtils.nextInt();
IntegerIDEntity sampleEntity1 = new IntegerIDEntity();
sampleEntity1.setId(documentId1);
sampleEntity1.setMessage("some message");
sampleEntity1.setVersion(System.currentTimeMillis());
Integer documentId2 = RandomUtils.nextInt();
IntegerIDEntity sampleEntity2 = new IntegerIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("some message");
sampleEntity2.setVersion(System.currentTimeMillis());
//when
repository.save(Arrays.asList(sampleEntity1, sampleEntity2));
//then
IntegerIDEntity entity1FromElasticSearch = repository.findOne(documentId1);
assertThat(entity1FromElasticSearch, is(notNullValue()));
IntegerIDEntity entity2FromElasticSearch = repository.findOne(documentId2);
assertThat(entity2FromElasticSearch, is(notNullValue()));
}
@Test
public void shouldSaveDocument(){
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("some message");
sampleEntity.setVersion(System.currentTimeMillis());
//when
repository.save(sampleEntity);
//then
IntegerIDEntity entityFromElasticSearch = repository.findOne(documentId);
assertThat(entityFromElasticSearch, is(notNullValue()));
}
@Test
public void shouldFindDocumentById(){
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("some message");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
IntegerIDEntity entityFromElasticSearch = repository.findOne(documentId);
//then
assertThat(entityFromElasticSearch, is(notNullValue()));
assertThat(sampleEntity, is((equalTo(sampleEntity))));
}
@Test
public void shouldReturnCountOfDocuments(){
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("some message");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
Long count = repository.count();
//then
assertThat(count, is(greaterThanOrEqualTo(1L)));
}
@Test
public void shouldFindAllDocuments(){
//when
Iterable<IntegerIDEntity> results = repository.findAll();
//then
assertThat(results, is(notNullValue()));
}
@Test
public void shouldDeleteDocument(){
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("some message");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
repository.delete(documentId);
//then
IntegerIDEntity entityFromElasticSearch = repository.findOne(documentId);
assertThat(entityFromElasticSearch, is(nullValue()));
}
@Test
public void shouldSearchDocumentsGivenSearchQuery(){
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("some test message");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
SearchQuery query = new NativeSearchQueryBuilder()
.withQuery(termQuery("message", "test"))
.build();
//when
Page<IntegerIDEntity> page = repository.search(query);
//then
assertThat(page, is(notNullValue()));
assertThat(page.getNumberOfElements(), is(greaterThanOrEqualTo(1)));
}
@Test
public void shouldSearchDocumentsGivenElasticsearchQuery(){
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
Page<IntegerIDEntity> page = repository.search(termQuery("message", "world"), new PageRequest(0,50));
//then
assertThat(page, is(notNullValue()));
assertThat(page.getNumberOfElements(), is(greaterThanOrEqualTo(1)));
}
@Test
@Ignore
public void shouldFindAllByIdQuery(){
//todo : find solution for findAll(Iterable<Ids> ids)
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
Integer documentId2 = RandomUtils.nextInt();
IntegerIDEntity sampleEntity2 = new IntegerIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("hello world.");
sampleEntity2.setVersion(System.currentTimeMillis());
repository.save(sampleEntity2);
//when
Iterable<IntegerIDEntity> sampleEntities=repository.findAll(Arrays.asList(documentId,documentId2));
//then
assertNotNull("sample entities cant be null..", sampleEntities);
}
@Test
public void shouldSaveIterableEntities(){
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity1 = new IntegerIDEntity();
sampleEntity1.setId(documentId);
sampleEntity1.setMessage("hello world.");
sampleEntity1.setVersion(System.currentTimeMillis());
Integer documentId2 = RandomUtils.nextInt();
IntegerIDEntity sampleEntity2 = new IntegerIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("hello world.");
sampleEntity2.setVersion(System.currentTimeMillis());
Iterable<IntegerIDEntity> sampleEntities = Arrays.asList(sampleEntity1,sampleEntity2);
//when
repository.save(sampleEntities);
//then
Page<IntegerIDEntity> entities = repository.search(fieldQuery("id", documentId), new PageRequest(0, 50));
assertNotNull(entities);
}
@Test
public void shouldReturnTrueGivenDocumentWithIdExists(){
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
boolean exist = repository.exists(documentId);
//then
assertEquals(exist, true);
}
@Test
public void shouldReturnResultsForGivenSearchQuery(){
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(fieldQuery("id",documentId))
.build();
Page<IntegerIDEntity> sampleEntities= repository.search(searchQuery);
//then
assertThat(sampleEntities.getTotalElements(), equalTo(1L));
}
@Test
public void shouldDeleteAll(){
//when
repository.deleteAll();
//then
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery())
.build();
Page<IntegerIDEntity> sampleEntities= repository.search(searchQuery);
assertThat(sampleEntities.getTotalElements(), equalTo(0L));
}
@Test
public void shouldDeleteEntity(){
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("hello world.");
sampleEntity.setVersion(System.currentTimeMillis());
repository.save(sampleEntity);
//when
repository.delete(sampleEntity);
//then
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(fieldQuery("id", documentId))
.build();
Page<IntegerIDEntity> sampleEntities= repository.search(searchQuery);
assertThat(sampleEntities.getTotalElements(),equalTo(0L));
}
@Test
public void shouldReturnIterableEntities(){
//given
Integer documentId1 = RandomUtils.nextInt();
IntegerIDEntity sampleEntity1 = new IntegerIDEntity();
sampleEntity1.setId(documentId1);
sampleEntity1.setMessage("hello world.");
sampleEntity1.setVersion(System.currentTimeMillis());
repository.save(sampleEntity1);
Integer documentId2 = RandomUtils.nextInt();
IntegerIDEntity sampleEntity2 = new IntegerIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("hello world.");
sampleEntity2.setVersion(System.currentTimeMillis());
repository.save(sampleEntity2);
//when
Iterable<IntegerIDEntity> sampleEntities = repository.search(fieldQuery("id",documentId1));
//then
assertNotNull("sample entities cant be null..", sampleEntities);
}
@Test
public void shouldDeleteIterableEntities(){
//given
Integer documentId1 = RandomUtils.nextInt();
IntegerIDEntity sampleEntity1 = new IntegerIDEntity();
sampleEntity1.setId(documentId1);
sampleEntity1.setMessage("hello world.");
sampleEntity1.setVersion(System.currentTimeMillis());
Integer documentId2 = RandomUtils.nextInt();
IntegerIDEntity sampleEntity2 = new IntegerIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("hello world.");
sampleEntity2.setVersion(System.currentTimeMillis());
repository.save(sampleEntity2);
Iterable<IntegerIDEntity> sampleEntities = Arrays.asList(sampleEntity2,sampleEntity2);
//when
repository.delete(sampleEntities);
//then
assertThat(repository.findOne(documentId1),is(nullValue()));
assertThat(repository.findOne(documentId2),is(nullValue()));
}
@Test
public void shouldIndexEntity(){
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setVersion(System.currentTimeMillis());
sampleEntity.setMessage("some message");
//when
repository.index(sampleEntity);
//then
Page<IntegerIDEntity> entities = repository.search(fieldQuery("id", documentId), new PageRequest(0,50));
assertThat(entities.getTotalElements(),equalTo(1L));
}
@Test
public void shouldSortByGivenField(){
//todo
//given
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage("A. hello world.");
repository.save(sampleEntity);
Integer documentId2 = RandomUtils.nextInt();
IntegerIDEntity sampleEntity2 = new IntegerIDEntity();
sampleEntity2.setId(documentId2);
sampleEntity2.setMessage("B.hello world.");
repository.save(sampleEntity2);
//when
Iterable<IntegerIDEntity> sampleEntities=repository.findAll(new Sort(new Sort.Order(Sort.Direction.ASC,"message")));
//then
assertThat(sampleEntities,is(notNullValue()));
}
@Test
public void shouldReturnSimilarEntities(){
//given
String sampleMessage = "So we build a web site or an application and want to add search to it, " +
"and then it hits us: getting search working is hard. We want our search solution to be fast," +
" we want a painless setup and a completely free search schema, we want to be able to index data simply using JSON over HTTP, " +
"we want our search server to be always available, we want to be able to start with one machine and scale to hundreds, " +
"we want real-time search, we want simple multi-tenancy, and we want a solution that is built for the cloud.";
List<IntegerIDEntity> sampleEntities = createSampleEntitiesWithMessage(sampleMessage, 30);
repository.save(sampleEntities);
//when
Page<IntegerIDEntity> results = repository.searchSimilar(sampleEntities.get(0));
//then
assertThat(results.getTotalElements(), is(greaterThanOrEqualTo(1L)));
}
private static List<IntegerIDEntity> createSampleEntitiesWithMessage(String message, int numberOfEntities){
List<IntegerIDEntity> sampleEntities = new ArrayList<IntegerIDEntity>();
for(int i = 0; i < numberOfEntities; i++){
Integer documentId = RandomUtils.nextInt();
IntegerIDEntity sampleEntity = new IntegerIDEntity();
sampleEntity.setId(documentId);
sampleEntity.setMessage(message);
sampleEntity.setVersion(System.currentTimeMillis());
sampleEntities.add(sampleEntity);
}
return sampleEntities;
}
}