Skip to content

Commit 4e0cbb8

Browse files
committed
DATAES-211
changes in AliasRequest fix tests due to changes in mapping, geo point, alias etc
1 parent 9519496 commit 4e0cbb8

File tree

8 files changed

+100
-105
lines changed

8 files changed

+100
-105
lines changed

src/main/java/org/springframework/data/elasticsearch/core/CriteriaFilterProcessor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,9 @@
3737
* CriteriaFilterProcessor
3838
*
3939
* @author Franck Marchand
40+
* @author Mohsin Husen
41+
* @author Artur Konczak
42+
*
4043
*/
4144
class CriteriaFilterProcessor {
4245

@@ -105,6 +108,7 @@ private QueryBuilder processCriteriaEntry(OperationKey key, Object value, String
105108
}
106109
QueryBuilder filter = null;
107110

111+
//todo : expose more option for GeoPoint i.e GeoDistance.PLANE or GeoDistance.ARC
108112
switch (key) {
109113
case WITHIN: {
110114
GeoDistanceQueryBuilder geoDistanceQueryBuilder = QueryBuilders.geoDistanceQuery(fieldName);

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchOperations.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616
package org.springframework.data.elasticsearch.core;
1717

1818
import org.elasticsearch.action.update.UpdateResponse;
19+
import org.elasticsearch.cluster.metadata.AliasMetaData;
1920
import org.springframework.data.domain.Page;
2021
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
2122
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
@@ -25,7 +26,6 @@
2526
import java.util.LinkedList;
2627
import java.util.List;
2728
import java.util.Map;
28-
import java.util.Set;
2929

3030
/**
3131
* ElasticsearchOperations
@@ -588,7 +588,7 @@ public interface ElasticsearchOperations {
588588
* @param indexName
589589
* @return
590590
*/
591-
Set<String> queryForAlias(String indexName);
591+
List<AliasMetaData> queryForAlias(String indexName);
592592

593593

594594
<T> T query(SearchQuery query, ResultsExtractor<T> resultsExtractor);

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

Lines changed: 43 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,7 +32,7 @@
3232

3333
import org.apache.commons.collections.CollectionUtils;
3434
import org.elasticsearch.action.ListenableActionFuture;
35-
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
35+
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
3636
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
3737
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
3838
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
@@ -57,13 +57,13 @@
5757
import org.elasticsearch.client.Client;
5858
import org.elasticsearch.client.Requests;
5959
import org.elasticsearch.cluster.metadata.AliasAction;
60+
import org.elasticsearch.cluster.metadata.AliasMetaData;
6061
import org.elasticsearch.common.collect.MapBuilder;
6162
import org.elasticsearch.common.unit.TimeValue;
6263
import org.elasticsearch.common.xcontent.XContentBuilder;
6364
import org.elasticsearch.index.query.MoreLikeThisQueryBuilder;
6465
import org.elasticsearch.index.query.QueryBuilder;
6566
import org.elasticsearch.index.query.QueryBuilders;
66-
import org.elasticsearch.script.Script;
6767
import org.elasticsearch.search.SearchHit;
6868
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
6969
import org.elasticsearch.search.highlight.HighlightBuilder;
@@ -642,6 +642,7 @@ public <T> String delete(Class<T> clazz, String id) {
642642
@Override
643643
public <T> void delete(DeleteQuery deleteQuery, Class<T> clazz) {
644644

645+
//TODO : clean up expose parameter for scan and scroll
645646
String iName = deleteQuery.getIndex();
646647
String tName = deleteQuery.getType();
647648
if(clazz!=null){
@@ -656,37 +657,46 @@ public <T> void delete(DeleteQuery deleteQuery, Class<T> clazz) {
656657
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(deleteQuery.getQuery())
657658
.withIndices(indexName)
658659
.withTypes(typeName)
659-
//TOD: ako - check that id is all the time avaialable
660-
//.withFields("_id")
661660
.withPageable(new PageRequest(0, 1000))
662661
.build();
663662

664663
final String scrollId = scan(searchQuery, 10000, true);
665664

666665
final BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
667-
boolean hasMoreRecords = true;
668-
while (hasMoreRecords) {
669-
final Page<Object> scroll = scroll(scrollId, 10000L, new SearchResultMapper() {
666+
List<String> ids = new ArrayList<String>();
667+
boolean hasRecords = true;
668+
while (hasRecords) {
669+
Page<String> page = scroll(scrollId, 5000, new SearchResultMapper() {
670670
@Override
671671
public <T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
672-
boolean hasItems = false;
672+
List<String> result = new ArrayList<String>();
673673
for (SearchHit searchHit : response.getHits()) {
674-
hasItems = true;
675-
bulkRequestBuilder.add(client.prepareDelete(indexName, typeName, searchHit.getId()));
674+
String id = searchHit.getId();
675+
result.add(id);
676676
}
677-
if(hasItems){
678-
return new FacetedPageImpl<T>((List<T>)Arrays.asList(new Object()));
677+
678+
if (result.size() > 0) {
679+
return new FacetedPageImpl<T>((List<T>) result);
679680
}
680681
return null;
681682
}
682683
});
683-
if (scroll == null) {
684-
hasMoreRecords = false;
684+
if (page != null && page.getContent().size() > 0) {
685+
ids.addAll(page.getContent());
686+
} else {
687+
hasRecords = false;
685688
}
686689
}
687-
if(bulkRequestBuilder.numberOfActions()>0) {
690+
691+
for(String id : ids) {
692+
bulkRequestBuilder.add(client.prepareDelete(indexName, typeName, id));
693+
}
694+
695+
if(bulkRequestBuilder.numberOfActions() > 0) {
688696
bulkRequestBuilder.execute().actionGet();
689697
}
698+
699+
refresh(indexName, false);
690700
}
691701

692702
@Override
@@ -795,7 +805,7 @@ public <T> Page<T> scroll(String scrollId, long scrollTimeInMillis, SearchResult
795805

796806
@Override
797807
public <T> Page<T> moreLikeThis(MoreLikeThisQuery query, Class<T> clazz) {
798-
int startRecord = 0;
808+
//todo : clean up
799809
ElasticsearchPersistentEntity persistentEntity = getPersistentEntityFor(clazz);
800810
String indexName = isNotBlank(query.getIndexName()) ? query.getIndexName() : persistentEntity.getIndexName();
801811
String type = isNotBlank(query.getType()) ? query.getType() : persistentEntity.getIndexType();
@@ -805,60 +815,35 @@ public <T> Page<T> moreLikeThis(MoreLikeThisQuery query, Class<T> clazz) {
805815
Assert.notNull(query.getId(), "No document id defined for MoreLikeThisQuery");
806816

807817
final MoreLikeThisQueryBuilder.Item item = new MoreLikeThisQueryBuilder.Item(indexName, type, query.getId());
808-
final NativeSearchQuery build = new NativeSearchQueryBuilder().withQuery(moreLikeThisQuery().addLikeItem(item)).build();
809-
return queryForPage(build,clazz);
810-
811-
//TODO: Mohins - set all other params for moreLikeThis
812-
/* MoreLikeThisRequestBuilder requestBuilder = client.prepareMoreLikeThis(indexName, type, query.getId());
813-
814-
if (query.getPageable() != null) {
815-
startRecord = query.getPageable().getPageNumber() * query.getPageable().getPageSize();
816-
requestBuilder.setSearchSize(query.getPageable().getPageSize());
817-
}
818-
requestBuilder.setSearchFrom(startRecord);
818+
MoreLikeThisQueryBuilder moreLikeThisQueryBuilder = moreLikeThisQuery().addLikeItem(item);
819819

820-
if (isNotEmpty(query.getSearchIndices())) {
821-
requestBuilder.setSearchIndices(toArray(query.getSearchIndices()));
822-
}
823-
if (isNotEmpty(query.getSearchTypes())) {
824-
requestBuilder.setSearchTypes(toArray(query.getSearchTypes()));
825-
}
826-
if (isNotEmpty(query.getFields())) {
827-
requestBuilder.setField(toArray(query.getFields()));
828-
}
829-
if (isNotBlank(query.getRouting())) {
830-
requestBuilder.setRouting(query.getRouting());
831-
}
832-
if (query.getPercentTermsToMatch() != null) {
833-
requestBuilder.setPercentTermsToMatch(query.getPercentTermsToMatch());
834-
}
835820
if (query.getMinTermFreq() != null) {
836-
requestBuilder.setMinTermFreq(query.getMinTermFreq());
821+
moreLikeThisQueryBuilder.minTermFreq(query.getMinTermFreq());
837822
}
838823
if (query.getMaxQueryTerms() != null) {
839-
requestBuilder.maxQueryTerms(query.getMaxQueryTerms());
824+
moreLikeThisQueryBuilder.maxQueryTerms(query.getMaxQueryTerms());
840825
}
841826
if (isNotEmpty(query.getStopWords())) {
842-
requestBuilder.setStopWords(toArray(query.getStopWords()));
827+
moreLikeThisQueryBuilder.stopWords(toArray(query.getStopWords()));
843828
}
844829
if (query.getMinDocFreq() != null) {
845-
requestBuilder.setMinDocFreq(query.getMinDocFreq());
830+
moreLikeThisQueryBuilder.minDocFreq(query.getMinDocFreq());
846831
}
847832
if (query.getMaxDocFreq() != null) {
848-
requestBuilder.setMaxDocFreq(query.getMaxDocFreq());
833+
moreLikeThisQueryBuilder.maxDocFreq(query.getMaxDocFreq());
849834
}
850835
if (query.getMinWordLen() != null) {
851-
requestBuilder.setMinWordLen(query.getMinWordLen());
836+
moreLikeThisQueryBuilder.minWordLength(query.getMinWordLen());
852837
}
853838
if (query.getMaxWordLen() != null) {
854-
requestBuilder.setMaxWordLen(query.getMaxWordLen());
839+
moreLikeThisQueryBuilder.maxWordLength(query.getMaxWordLen());
855840
}
856841
if (query.getBoostTerms() != null) {
857-
requestBuilder.setBoostTerms(query.getBoostTerms());
842+
moreLikeThisQueryBuilder.boostTerms(query.getBoostTerms());
858843
}
859844

860-
SearchResponse response = getSearchResponse(requestBuilder.execute());
861-
return resultsMapper.mapResults(response, clazz, query.getPageable());*/
845+
final NativeSearchQuery build = new NativeSearchQueryBuilder().withQuery(moreLikeThisQueryBuilder).build();
846+
return queryForPage(build,clazz);
862847
}
863848

864849
private SearchResponse doSearch(SearchRequestBuilder searchRequest, SearchQuery searchQuery) {
@@ -1052,11 +1037,13 @@ private IndexRequestBuilder prepareIndex(IndexQuery query) {
10521037
}
10531038
}
10541039

1040+
//TODO : remove or waitForOperation
10551041
@Override
10561042
public void refresh(String indexName, boolean waitForOperation) {
10571043
client.admin().indices().refresh(refreshRequest(indexName)).actionGet();
10581044
}
10591045

1046+
//TODO : remove or waitForOperation
10601047
@Override
10611048
public <T> void refresh(Class<T> clazz, boolean waitForOperation) {
10621049
ElasticsearchPersistentEntity persistentEntity = getPersistentEntityFor(clazz);
@@ -1092,13 +1079,9 @@ public Boolean removeAlias(AliasQuery query) {
10921079
}
10931080

10941081
@Override
1095-
public Set<String> queryForAlias(String indexName) {
1096-
ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest()
1097-
.routingTable(true).nodes(true).indices(indexName);
1098-
//TODO: ako check how to find aliases for index
1099-
/* Iterator<String> iterator = client.admin().cluster().state(clusterStateRequest).actionGet().getState().getMetaData().aliases().keysIt();
1100-
return newHashSet(iterator);*/
1101-
return null;
1082+
public List<AliasMetaData> queryForAlias(String indexName) {
1083+
return client.admin().indices().getAliases(new GetAliasesRequest().indices(indexName))
1084+
.actionGet().getAliases().get(indexName);
11021085
}
11031086

11041087
@Override

0 commit comments

Comments
 (0)