mirror of
				https://github.com/spring-projects/spring-data-elasticsearch.git
				synced 2025-10-31 06:38:44 +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> |     	<commonscollections>3.2.1</commonscollections> | ||||||
|     	<commonslang>2.6</commonslang> |     	<commonslang>2.6</commonslang> | ||||||
|     	<elasticsearch>0.20.5</elasticsearch> |     	<elasticsearch>0.90.0</elasticsearch> | ||||||
|     	<jackson>1.9.2</jackson> |     	<jackson>1.9.2</jackson> | ||||||
|     	<springdata.commons>1.6.0.BUILD-SNAPSHOT</springdata.commons> |     	<springdata.commons>1.6.0.BUILD-SNAPSHOT</springdata.commons> | ||||||
|     	 |     	 | ||||||
|  | |||||||
| @ -107,7 +107,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations { | |||||||
| 
 | 
 | ||||||
|         try { |         try { | ||||||
|             XContentBuilder xContentBuilder = buildMapping(clazz, persistentEntity.getIndexType(), persistentEntity.getIdProperty().getFieldName()); |             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) { |         } catch (Exception e) { | ||||||
|             throw new ElasticsearchException("Failed to build mapping for " + clazz.getSimpleName() , e); |             throw new ElasticsearchException("Failed to build mapping for " + clazz.getSimpleName() , e); | ||||||
|         } |         } | ||||||
| @ -199,7 +199,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations { | |||||||
|         if(query.getQuery() != null){ |         if(query.getQuery() != null){ | ||||||
|             countRequestBuilder.setQuery(query.getQuery()); |             countRequestBuilder.setQuery(query.getQuery()); | ||||||
|         } |         } | ||||||
|         return countRequestBuilder.execute().actionGet().count(); |         return countRequestBuilder.execute().actionGet().getCount(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
| @ -218,9 +218,9 @@ public class ElasticsearchTemplate implements ElasticsearchOperations { | |||||||
|         BulkResponse bulkResponse = bulkRequest.execute().actionGet(); |         BulkResponse bulkResponse = bulkRequest.execute().actionGet(); | ||||||
|         if (bulkResponse.hasFailures()) { |         if (bulkResponse.hasFailures()) { | ||||||
|             Map<String, String> failedDocuments = new HashMap<String, String>(); |             Map<String, String> failedDocuments = new HashMap<String, String>(); | ||||||
|             for (BulkItemResponse item : bulkResponse.items()) { |             for (BulkItemResponse item : bulkResponse.getItems()) { | ||||||
|                 if (item.failed()) |                 if (item.isFailed()) | ||||||
|                     failedDocuments.put(item.getId(), item.failureMessage()); |                     failedDocuments.put(item.getId(), item.getFailureMessage()); | ||||||
|             } |             } | ||||||
|             throw new ElasticsearchException("Bulk indexing has failures. Use ElasticsearchException.getFailedDocuments() for detailed messages [" + failedDocuments+"]", failedDocuments); |             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){ |     public <T> boolean deleteIndex(Class<T> clazz){ | ||||||
|         String indexName = getPersistentEntityFor(clazz).getIndexName(); |         String indexName = getPersistentEntityFor(clazz).getIndexName(); | ||||||
|         if(indexExists(indexName)){ |         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; |         return false; | ||||||
|     } |     } | ||||||
| @ -376,13 +376,13 @@ public class ElasticsearchTemplate implements ElasticsearchOperations { | |||||||
|     private boolean indexExists(String indexName) { |     private boolean indexExists(String indexName) { | ||||||
|         return client.admin() |         return client.admin() | ||||||
|                 .indices() |                 .indices() | ||||||
|                 .exists(indicesExistsRequest(indexName)).actionGet().exists(); |                 .exists(indicesExistsRequest(indexName)).actionGet().isExists(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private <T> boolean createIndexWithSettings(Class<T> clazz) { |     private <T> boolean createIndexWithSettings(Class<T> clazz) { | ||||||
|         ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz); |         ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz); | ||||||
|         return client.admin().indices().create(Requests.createIndexRequest(persistentEntity.getIndexName()). |         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) { |     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> search(SearchQuery searchQuery); | ||||||
| 
 | 
 | ||||||
|     Page<T> searchSimilar(T entity); |     Page<T> searchSimilar(T entity, SearchQuery searchQuery); | ||||||
| 
 |  | ||||||
|     Page<T> searchSimilar(T entity, Pageable pageable); |  | ||||||
| } | } | ||||||
|  | |||||||
| @ -200,17 +200,19 @@ public abstract class AbstractElasticsearchRepository<T,ID extends Serializable> | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public Page<T> searchSimilar(T entity) { |     public Page<T> searchSimilar(T entity, SearchQuery searchQuery) { | ||||||
|         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, "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(); |         MoreLikeThisQuery query = new MoreLikeThisQuery(); | ||||||
|         query.setId(stringIdRepresentation(extractIdFromBean(entity))); |         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()); |         return elasticsearchOperations.moreLikeThis(query, getEntityClass()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -296,14 +298,14 @@ public abstract class AbstractElasticsearchRepository<T,ID extends Serializable> | |||||||
|         this.elasticsearchOperations = elasticsearchOperations; |         this.elasticsearchOperations = elasticsearchOperations; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|      | 
 | ||||||
|     protected ID extractIdFromBean(T entity) { |     protected ID extractIdFromBean(T entity) { | ||||||
|       if (entityInformation != null) { |       if (entityInformation != null) { | ||||||
|           return entityInformation.getId(entity); |           return entityInformation.getId(entity); | ||||||
|       } |       } | ||||||
|       return null; |       return null; | ||||||
|     } |     } | ||||||
|      | 
 | ||||||
|     protected abstract String stringIdRepresentation(ID id); |     protected abstract String stringIdRepresentation(ID id); | ||||||
| 
 | 
 | ||||||
|     private Long extractVersionFromBean(T entity){ |     private Long extractVersionFromBean(T entity){ | ||||||
|  | |||||||
| @ -387,7 +387,6 @@ public class SimpleElasticsearchRepositoryTests { | |||||||
|         assertThat(sampleEntities,is(notNullValue())); |         assertThat(sampleEntities,is(notNullValue())); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|     @Test |     @Test | ||||||
|     public void shouldReturnSimilarEntities(){ |     public void shouldReturnSimilarEntities(){ | ||||||
|         //given |         //given | ||||||
| @ -403,7 +402,11 @@ public class SimpleElasticsearchRepositoryTests { | |||||||
|         repository.save(sampleEntities); |         repository.save(sampleEntities); | ||||||
| 
 | 
 | ||||||
|         //when |         //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 |         //then | ||||||
|         assertThat(results.getTotalElements(), is(greaterThanOrEqualTo(1L))); |         assertThat(results.getTotalElements(), is(greaterThanOrEqualTo(1L))); | ||||||
| @ -416,6 +419,7 @@ public class SimpleElasticsearchRepositoryTests { | |||||||
|             SampleEntity sampleEntity = new SampleEntity(); |             SampleEntity sampleEntity = new SampleEntity(); | ||||||
|             sampleEntity.setId(documentId); |             sampleEntity.setId(documentId); | ||||||
|             sampleEntity.setMessage(message); |             sampleEntity.setMessage(message); | ||||||
|  |             sampleEntity.setRate(2); | ||||||
|             sampleEntity.setVersion(System.currentTimeMillis()); |             sampleEntity.setVersion(System.currentTimeMillis()); | ||||||
|             sampleEntities.add(sampleEntity); |             sampleEntities.add(sampleEntity); | ||||||
|         } |         } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user