mirror of
				https://github.com/spring-projects/spring-data-elasticsearch.git
				synced 2025-10-30 22:28:47 +00:00 
			
		
		
		
	DATAES-7 : Migrate to the latest version of Elasticsearch 0.90.0
This commit is contained in:
		
							parent
							
								
									b943dc2515
								
							
						
					
					
						commit
						7f11f23718
					
				
							
								
								
									
										2
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								pom.xml
									
									
									
									
									
								
							| @ -23,7 +23,7 @@ | ||||
|     	 | ||||
|     	<commonscollections>3.2.1</commonscollections> | ||||
|     	<commonslang>2.6</commonslang> | ||||
|     	<elasticsearch>0.20.5</elasticsearch> | ||||
|     	<elasticsearch>0.90.0</elasticsearch> | ||||
|     	<jackson>1.9.2</jackson> | ||||
|     	<springdata.commons>1.6.0.BUILD-SNAPSHOT</springdata.commons> | ||||
|     	 | ||||
|  | ||||
| @ -107,7 +107,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations { | ||||
| 
 | ||||
|         try { | ||||
|             XContentBuilder xContentBuilder = buildMapping(clazz, persistentEntity.getIndexType(), persistentEntity.getIdProperty().getFieldName()); | ||||
|             return requestBuilder.setSource(xContentBuilder).execute().actionGet().acknowledged(); | ||||
|             return requestBuilder.setSource(xContentBuilder).execute().actionGet().isAcknowledged(); | ||||
|         } catch (Exception e) { | ||||
|             throw new ElasticsearchException("Failed to build mapping for " + clazz.getSimpleName() , e); | ||||
|         } | ||||
| @ -199,7 +199,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations { | ||||
|         if(query.getQuery() != null){ | ||||
|             countRequestBuilder.setQuery(query.getQuery()); | ||||
|         } | ||||
|         return countRequestBuilder.execute().actionGet().count(); | ||||
|         return countRequestBuilder.execute().actionGet().getCount(); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
| @ -218,9 +218,9 @@ public class ElasticsearchTemplate implements ElasticsearchOperations { | ||||
|         BulkResponse bulkResponse = bulkRequest.execute().actionGet(); | ||||
|         if (bulkResponse.hasFailures()) { | ||||
|             Map<String, String> failedDocuments = new HashMap<String, String>(); | ||||
|             for (BulkItemResponse item : bulkResponse.items()) { | ||||
|                 if (item.failed()) | ||||
|                     failedDocuments.put(item.getId(), item.failureMessage()); | ||||
|             for (BulkItemResponse item : bulkResponse.getItems()) { | ||||
|                 if (item.isFailed()) | ||||
|                     failedDocuments.put(item.getId(), item.getFailureMessage()); | ||||
|             } | ||||
|             throw new ElasticsearchException("Bulk indexing has failures. Use ElasticsearchException.getFailedDocuments() for detailed messages [" + failedDocuments+"]", failedDocuments); | ||||
|         } | ||||
| @ -235,7 +235,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations { | ||||
|     public <T> boolean deleteIndex(Class<T> clazz){ | ||||
|         String indexName = getPersistentEntityFor(clazz).getIndexName(); | ||||
|         if(indexExists(indexName)){ | ||||
|             return client.admin().indices().delete(new DeleteIndexRequest(indexName)).actionGet().acknowledged(); | ||||
|             return client.admin().indices().delete(new DeleteIndexRequest(indexName)).actionGet().isAcknowledged(); | ||||
|         } | ||||
|         return false; | ||||
|     } | ||||
| @ -376,13 +376,13 @@ public class ElasticsearchTemplate implements ElasticsearchOperations { | ||||
|     private boolean indexExists(String indexName) { | ||||
|         return client.admin() | ||||
|                 .indices() | ||||
|                 .exists(indicesExistsRequest(indexName)).actionGet().exists(); | ||||
|                 .exists(indicesExistsRequest(indexName)).actionGet().isExists(); | ||||
|     } | ||||
| 
 | ||||
|     private <T> boolean createIndexWithSettings(Class<T> clazz) { | ||||
|         ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz); | ||||
|         return client.admin().indices().create(Requests.createIndexRequest(persistentEntity.getIndexName()). | ||||
|                 settings(getSettings(persistentEntity))).actionGet().acknowledged(); | ||||
|                 settings(getSettings(persistentEntity))).actionGet().isAcknowledged(); | ||||
|     } | ||||
| 
 | ||||
|     private <T> Map getSettings(ElasticsearchPersistentEntity<T> persistentEntity) { | ||||
|  | ||||
| @ -41,7 +41,5 @@ public interface ElasticsearchRepository<T, ID extends Serializable> extends Ela | ||||
| 
 | ||||
|     Page<T> search(SearchQuery searchQuery); | ||||
| 
 | ||||
|     Page<T> searchSimilar(T entity); | ||||
| 
 | ||||
|     Page<T> searchSimilar(T entity, Pageable pageable); | ||||
|     Page<T> searchSimilar(T entity, SearchQuery searchQuery); | ||||
| } | ||||
|  | ||||
| @ -200,17 +200,19 @@ public abstract class AbstractElasticsearchRepository<T,ID extends Serializable> | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public Page<T> searchSimilar(T entity) { | ||||
|         return searchSimilar(entity, DEFAULT_PAGE); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public Page<T> searchSimilar(T entity, Pageable pageable) { | ||||
|     public Page<T> searchSimilar(T entity, SearchQuery searchQuery) { | ||||
|         Assert.notNull(entity, "Cannot search similar records for 'null'."); | ||||
|         Assert.notNull(entity, "Pageable cannot be 'null'"); | ||||
|         Assert.notNull(searchQuery.getFields(), "Fields cannot be 'null'"); | ||||
|         MoreLikeThisQuery query = new MoreLikeThisQuery(); | ||||
|         query.setId(stringIdRepresentation(extractIdFromBean(entity))); | ||||
|         query.setPageable(pageable); | ||||
|         query.setPageable(searchQuery.getPageable() != null ? searchQuery.getPageable() : DEFAULT_PAGE); | ||||
|         query.addFields(searchQuery.getFields().toArray(new String[searchQuery.getFields().size()])); | ||||
|         if(!searchQuery.getIndices().isEmpty()) { | ||||
|             query.addSearchIndices(searchQuery.getIndices().toArray(new String[searchQuery.getIndices().size()])); | ||||
|         } | ||||
|         if(!searchQuery.getTypes().isEmpty()){ | ||||
|             query.addSearchTypes(searchQuery.getTypes().toArray(new String[searchQuery.getTypes().size()])); | ||||
|         } | ||||
|         return elasticsearchOperations.moreLikeThis(query, getEntityClass()); | ||||
|     } | ||||
| 
 | ||||
| @ -296,14 +298,14 @@ public abstract class AbstractElasticsearchRepository<T,ID extends Serializable> | ||||
|         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){ | ||||
|  | ||||
| @ -387,7 +387,6 @@ public class SimpleElasticsearchRepositoryTests { | ||||
|         assertThat(sampleEntities,is(notNullValue())); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     @Test | ||||
|     public void shouldReturnSimilarEntities(){ | ||||
|         //given | ||||
| @ -403,7 +402,11 @@ public class SimpleElasticsearchRepositoryTests { | ||||
|         repository.save(sampleEntities); | ||||
| 
 | ||||
|         //when | ||||
|         Page<SampleEntity> results = repository.searchSimilar(sampleEntities.get(0)); | ||||
|         SearchQuery searchQuery = new NativeSearchQueryBuilder() | ||||
|                 .withPageable(new PageRequest(0, 5)) | ||||
|                 .withFields("message") | ||||
|                 .build(); | ||||
|         Page<SampleEntity> results = repository.searchSimilar(sampleEntities.get(0), searchQuery); | ||||
| 
 | ||||
|         //then | ||||
|         assertThat(results.getTotalElements(), is(greaterThanOrEqualTo(1L))); | ||||
| @ -416,6 +419,7 @@ public class SimpleElasticsearchRepositoryTests { | ||||
|             SampleEntity sampleEntity = new SampleEntity(); | ||||
|             sampleEntity.setId(documentId); | ||||
|             sampleEntity.setMessage(message); | ||||
|             sampleEntity.setRate(2); | ||||
|             sampleEntity.setVersion(System.currentTimeMillis()); | ||||
|             sampleEntities.add(sampleEntity); | ||||
|         } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user