Skip to content

Commit

Permalink
DATAES-211
Browse files Browse the repository at this point in the history
 changes in AliasRequest
 fix tests due to changes in mapping, geo point, alias etc
  • Loading branch information
mohsinh committed Feb 24, 2016
1 parent 9519496 commit 4e0cbb8
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2016 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.
Expand Down Expand Up @@ -37,6 +37,9 @@
* CriteriaFilterProcessor
*
* @author Franck Marchand
* @author Mohsin Husen
* @author Artur Konczak
*
*/
class CriteriaFilterProcessor {

Expand Down Expand Up @@ -105,6 +108,7 @@ private QueryBuilder processCriteriaEntry(OperationKey key, Object value, String
}
QueryBuilder filter = null;

//todo : expose more option for GeoPoint i.e GeoDistance.PLANE or GeoDistance.ARC
switch (key) {
case WITHIN: {
GeoDistanceQueryBuilder geoDistanceQueryBuilder = QueryBuilders.geoDistanceQuery(fieldName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2016 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.
Expand All @@ -16,6 +16,7 @@
package org.springframework.data.elasticsearch.core;

import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
Expand All @@ -25,7 +26,6 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* ElasticsearchOperations
Expand Down Expand Up @@ -588,7 +588,7 @@ public interface ElasticsearchOperations {
* @param indexName
* @return
*/
Set<String> queryForAlias(String indexName);
List<AliasMetaData> queryForAlias(String indexName);


<T> T query(SearchQuery query, ResultsExtractor<T> resultsExtractor);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2016 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.
Expand Down Expand Up @@ -32,7 +32,7 @@

import org.apache.commons.collections.CollectionUtils;
import org.elasticsearch.action.ListenableActionFuture;
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
Expand All @@ -57,13 +57,13 @@
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.cluster.metadata.AliasAction;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.MoreLikeThisQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.script.Script;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.highlight.HighlightBuilder;
Expand Down Expand Up @@ -642,6 +642,7 @@ public <T> String delete(Class<T> clazz, String id) {
@Override
public <T> void delete(DeleteQuery deleteQuery, Class<T> clazz) {

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

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

final BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
boolean hasMoreRecords = true;
while (hasMoreRecords) {
final Page<Object> scroll = scroll(scrollId, 10000L, new SearchResultMapper() {
List<String> ids = new ArrayList<String>();
boolean hasRecords = true;
while (hasRecords) {
Page<String> page = scroll(scrollId, 5000, new SearchResultMapper() {
@Override
public <T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
boolean hasItems = false;
List<String> result = new ArrayList<String>();
for (SearchHit searchHit : response.getHits()) {
hasItems = true;
bulkRequestBuilder.add(client.prepareDelete(indexName, typeName, searchHit.getId()));
String id = searchHit.getId();
result.add(id);
}
if(hasItems){
return new FacetedPageImpl<T>((List<T>)Arrays.asList(new Object()));

if (result.size() > 0) {
return new FacetedPageImpl<T>((List<T>) result);
}
return null;
}
});
if (scroll == null) {
hasMoreRecords = false;
if (page != null && page.getContent().size() > 0) {
ids.addAll(page.getContent());
} else {
hasRecords = false;
}
}
if(bulkRequestBuilder.numberOfActions()>0) {

for(String id : ids) {
bulkRequestBuilder.add(client.prepareDelete(indexName, typeName, id));
}

if(bulkRequestBuilder.numberOfActions() > 0) {
bulkRequestBuilder.execute().actionGet();
}

refresh(indexName, false);
}

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

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

final MoreLikeThisQueryBuilder.Item item = new MoreLikeThisQueryBuilder.Item(indexName, type, query.getId());
final NativeSearchQuery build = new NativeSearchQueryBuilder().withQuery(moreLikeThisQuery().addLikeItem(item)).build();
return queryForPage(build,clazz);

//TODO: Mohins - set all other params for moreLikeThis
/* MoreLikeThisRequestBuilder requestBuilder = client.prepareMoreLikeThis(indexName, type, query.getId());
if (query.getPageable() != null) {
startRecord = query.getPageable().getPageNumber() * query.getPageable().getPageSize();
requestBuilder.setSearchSize(query.getPageable().getPageSize());
}
requestBuilder.setSearchFrom(startRecord);
MoreLikeThisQueryBuilder moreLikeThisQueryBuilder = moreLikeThisQuery().addLikeItem(item);

if (isNotEmpty(query.getSearchIndices())) {
requestBuilder.setSearchIndices(toArray(query.getSearchIndices()));
}
if (isNotEmpty(query.getSearchTypes())) {
requestBuilder.setSearchTypes(toArray(query.getSearchTypes()));
}
if (isNotEmpty(query.getFields())) {
requestBuilder.setField(toArray(query.getFields()));
}
if (isNotBlank(query.getRouting())) {
requestBuilder.setRouting(query.getRouting());
}
if (query.getPercentTermsToMatch() != null) {
requestBuilder.setPercentTermsToMatch(query.getPercentTermsToMatch());
}
if (query.getMinTermFreq() != null) {
requestBuilder.setMinTermFreq(query.getMinTermFreq());
moreLikeThisQueryBuilder.minTermFreq(query.getMinTermFreq());
}
if (query.getMaxQueryTerms() != null) {
requestBuilder.maxQueryTerms(query.getMaxQueryTerms());
moreLikeThisQueryBuilder.maxQueryTerms(query.getMaxQueryTerms());
}
if (isNotEmpty(query.getStopWords())) {
requestBuilder.setStopWords(toArray(query.getStopWords()));
moreLikeThisQueryBuilder.stopWords(toArray(query.getStopWords()));
}
if (query.getMinDocFreq() != null) {
requestBuilder.setMinDocFreq(query.getMinDocFreq());
moreLikeThisQueryBuilder.minDocFreq(query.getMinDocFreq());
}
if (query.getMaxDocFreq() != null) {
requestBuilder.setMaxDocFreq(query.getMaxDocFreq());
moreLikeThisQueryBuilder.maxDocFreq(query.getMaxDocFreq());
}
if (query.getMinWordLen() != null) {
requestBuilder.setMinWordLen(query.getMinWordLen());
moreLikeThisQueryBuilder.minWordLength(query.getMinWordLen());
}
if (query.getMaxWordLen() != null) {
requestBuilder.setMaxWordLen(query.getMaxWordLen());
moreLikeThisQueryBuilder.maxWordLength(query.getMaxWordLen());
}
if (query.getBoostTerms() != null) {
requestBuilder.setBoostTerms(query.getBoostTerms());
moreLikeThisQueryBuilder.boostTerms(query.getBoostTerms());
}

SearchResponse response = getSearchResponse(requestBuilder.execute());
return resultsMapper.mapResults(response, clazz, query.getPageable());*/
final NativeSearchQuery build = new NativeSearchQueryBuilder().withQuery(moreLikeThisQueryBuilder).build();
return queryForPage(build,clazz);
}

private SearchResponse doSearch(SearchRequestBuilder searchRequest, SearchQuery searchQuery) {
Expand Down Expand Up @@ -1052,11 +1037,13 @@ private IndexRequestBuilder prepareIndex(IndexQuery query) {
}
}

//TODO : remove or waitForOperation
@Override
public void refresh(String indexName, boolean waitForOperation) {
client.admin().indices().refresh(refreshRequest(indexName)).actionGet();
}

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

@Override
public Set<String> queryForAlias(String indexName) {
ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest()
.routingTable(true).nodes(true).indices(indexName);
//TODO: ako check how to find aliases for index
/* Iterator<String> iterator = client.admin().cluster().state(clusterStateRequest).actionGet().getState().getMetaData().aliases().keysIt();
return newHashSet(iterator);*/
return null;
public List<AliasMetaData> queryForAlias(String indexName) {
return client.admin().indices().getAliases(new GetAliasesRequest().indices(indexName))
.actionGet().getAliases().get(indexName);
}

@Override
Expand Down
Loading

0 comments on commit 4e0cbb8

Please sign in to comment.