DATAES-211

changes in AliasRequest
 fix tests due to changes in mapping, geo point, alias etc
This commit is contained in:
Mohsin Husen 2016-02-16 16:07:36 +00:00
parent 95194961dc
commit 4e0cbb8966
8 changed files with 100 additions and 105 deletions

View File

@ -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.
@ -37,6 +37,9 @@ import org.springframework.util.Assert;
* CriteriaFilterProcessor
*
* @author Franck Marchand
* @author Mohsin Husen
* @author Artur Konczak
*
*/
class CriteriaFilterProcessor {
@ -105,6 +108,7 @@ class CriteriaFilterProcessor {
}
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);

View File

@ -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.
@ -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;
@ -25,7 +26,6 @@ import org.springframework.data.util.CloseableIterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* ElasticsearchOperations
@ -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);

View File

@ -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.
@ -32,7 +32,7 @@ import java.util.*;
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;
@ -57,13 +57,13 @@ import org.elasticsearch.action.update.UpdateResponse;
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;
@ -642,6 +642,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
@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){
@ -656,37 +657,46 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
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
@ -795,7 +805,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
@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();
@ -805,60 +815,35 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
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);
MoreLikeThisQueryBuilder moreLikeThisQueryBuilder = moreLikeThisQuery().addLikeItem(item);
//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);
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) {
@ -1052,11 +1037,13 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
}
}
//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);
@ -1092,13 +1079,9 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
}
@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

View File

@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 the original author or authors.
* Copyright 2014-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.
@ -19,11 +19,10 @@ import static org.apache.commons.lang.RandomStringUtils.*;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -40,7 +39,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
public class AliasTests {
private static final String INDEX_NAME = "test-alias-index";
private static final String INDEX_NAME_1 = "test-alias-index-1";
private static final String INDEX_NAME_2 = "test-alias-index-2";
private static final String TYPE_NAME = "test-alias-type";
@Autowired
@ -54,9 +54,13 @@ public class AliasTests {
settings.put("index.number_of_shards", "2");
settings.put("index.store.type", "fs");
elasticsearchTemplate.deleteIndex(INDEX_NAME);
elasticsearchTemplate.createIndex(INDEX_NAME, settings);
elasticsearchTemplate.refresh(INDEX_NAME, true);
elasticsearchTemplate.deleteIndex(INDEX_NAME_1);
elasticsearchTemplate.createIndex(INDEX_NAME_1, settings);
elasticsearchTemplate.refresh(INDEX_NAME_1, true);
elasticsearchTemplate.deleteIndex(INDEX_NAME_2);
elasticsearchTemplate.createIndex(INDEX_NAME_2, settings);
elasticsearchTemplate.refresh(INDEX_NAME_2, true);
}
@Test
@ -64,34 +68,33 @@ public class AliasTests {
// given
String aliasName = "test-alias";
AliasQuery aliasQuery = new AliasBuilder()
.withIndexName(INDEX_NAME)
.withIndexName(INDEX_NAME_1)
.withAliasName(aliasName).build();
// when
elasticsearchTemplate.addAlias(aliasQuery);
// then
Set<String> aliases = elasticsearchTemplate.queryForAlias(INDEX_NAME);
List<AliasMetaData> aliases = elasticsearchTemplate.queryForAlias(INDEX_NAME_1);
assertThat(aliases, is(notNullValue()));
assertThat(aliases.contains(aliasName), is(true));
assertThat(aliases.get(0).alias(), is(aliasName));
}
@Test
public void shouldRemoveAlias() {
// given
String indexName = INDEX_NAME;
String indexName = INDEX_NAME_1;
String aliasName = "test-alias";
AliasQuery aliasQuery = new AliasBuilder()
.withIndexName(indexName)
.withAliasName(aliasName).build();
// when
elasticsearchTemplate.addAlias(aliasQuery);
Set<String> aliases = elasticsearchTemplate.queryForAlias(indexName);
List<AliasMetaData> aliases = elasticsearchTemplate.queryForAlias(indexName);
assertThat(aliases, is(notNullValue()));
assertThat(aliases.contains(aliasName), is(true));
assertThat(aliases.get(0).alias(), is(aliasName));
// then
elasticsearchTemplate.removeAlias(aliasQuery);
aliases = elasticsearchTemplate.queryForAlias(indexName);
assertThat(aliases, is(notNullValue()));
assertThat(aliases.size(), is(0));
assertThat(aliases, is(nullValue()));
}
/*
@ -100,7 +103,7 @@ public class AliasTests {
@Test
public void shouldAddAliasWithGivenRoutingValue() {
//given
String indexName = INDEX_NAME;
String indexName = INDEX_NAME_1;
String alias = "test-alias";
AliasQuery aliasQuery = new AliasBuilder()
@ -124,15 +127,17 @@ public class AliasTests {
.build();
elasticsearchTemplate.index(indexQuery);
elasticsearchTemplate.refresh(INDEX_NAME, true);
elasticsearchTemplate.refresh(INDEX_NAME_1, true);
SearchQuery query = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
.withIndices(alias).withTypes(TYPE_NAME).build();
long count = elasticsearchTemplate.count(query);
//then
Set<String> aliases = elasticsearchTemplate.queryForAlias(INDEX_NAME);
List<AliasMetaData> aliases = elasticsearchTemplate.queryForAlias(INDEX_NAME_1);
assertThat(aliases, is(notNullValue()));
assertThat(aliases.contains(alias), is(true));
assertThat(aliases.get(0).alias(), is(alias));
assertThat(aliases.get(0).searchRouting(), is("0"));
assertThat(aliases.get(0).indexRouting(), is("0"));
assertThat(count, is(1L));
//cleanup
@ -153,12 +158,12 @@ public class AliasTests {
String alias2 = "test-alias-2";
AliasQuery aliasQuery1 = new AliasBuilder()
.withIndexName(INDEX_NAME)
.withIndexName(INDEX_NAME_1)
.withAliasName(alias1)
.withIndexRouting("0").build();
AliasQuery aliasQuery2 = new AliasBuilder()
.withIndexName(INDEX_NAME)
.withIndexName(INDEX_NAME_2)
.withAliasName(alias2)
.withSearchRouting("1").build();
@ -179,15 +184,17 @@ public class AliasTests {
elasticsearchTemplate.index(indexQuery);
SearchQuery query = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
.withIndices(alias2).withTypes(TYPE_NAME).build();
long count = elasticsearchTemplate.count(query, SampleEntity.class);
// then
Set<String> aliases = elasticsearchTemplate.queryForAlias(INDEX_NAME);
assertThat(aliases, is(notNullValue()));
assertThat(aliases.contains(alias1), is(true));
assertThat(aliases.contains(alias2), is(true));
assertThat(count, is(0L));
List<AliasMetaData> responseAlias1 = elasticsearchTemplate.queryForAlias(INDEX_NAME_1);
assertThat(responseAlias1, is(notNullValue()));
assertThat(responseAlias1.get(0).alias(), is(alias1));
assertThat(responseAlias1.get(0).indexRouting(), is("0"));
List<AliasMetaData> responseAlias2 = elasticsearchTemplate.queryForAlias(INDEX_NAME_2);
assertThat(responseAlias2, is(notNullValue()));
assertThat(responseAlias2.get(0).alias(), is(alias2));
assertThat(responseAlias2.get(0).searchRouting(), is("1"));
//cleanup
elasticsearchTemplate.removeAlias(aliasQuery1);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 the original author or authors.
* Copyright 2014-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.
@ -338,6 +338,8 @@ public class ElasticsearchTemplateTests {
IndexQuery indexQuery = getIndexQuery(sampleEntity);
elasticsearchTemplate.index(indexQuery);
elasticsearchTemplate.refresh(SampleEntity.class, true);
// when
DeleteQuery deleteQuery = new DeleteQuery();
deleteQuery.setQuery(termQuery("id", documentId));

View File

@ -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.
@ -60,8 +60,8 @@ public class MappingBuilderTests {
@Test
public void testInfiniteLoopAvoidance() throws IOException {
final String expected = "{\"mapping\":{\"properties\":{\"message\":{\"store\":true,\"" +
"type\":\"string\",\"index\":\"not_analyzed\",\"search_analyzer\":\"standard\"," +
"\"index_analyzer\":\"standard\"}}}}";
"type\":\"string\",\"index\":\"not_analyzed\"," +
"\"analyzer\":\"standard\"}}}}";
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleTransientEntity.class, "mapping", "id", null);
assertThat(xContentBuilder.string(), is(expected));
@ -115,8 +115,8 @@ public class MappingBuilderTests {
@Test
public void shouldBuildMappingWithSuperclass() throws IOException {
final String expected = "{\"mapping\":{\"properties\":{\"message\":{\"store\":true,\"" +
"type\":\"string\",\"index\":\"not_analyzed\",\"search_analyzer\":\"standard\"," +
"\"index_analyzer\":\"standard\"},\"createdDate\":{\"store\":false," +
"type\":\"string\",\"index\":\"not_analyzed\",\"analyzer\":\"standard\"}" +
",\"createdDate\":{\"store\":false," +
"\"type\":\"date\",\"index\":\"not_analyzed\"}}}}";
XContentBuilder xContentBuilder = MappingBuilder.buildMapping(SampleInheritedEntity.class, "mapping", "id", null);

View File

@ -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.
@ -146,7 +146,7 @@ public class ElasticsearchTemplateGeoTests {
List<LocationMarkerEntity> geoAuthorsForGeoCriteria = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery, LocationMarkerEntity.class);
//then
assertThat(geoAuthorsForGeoCriteria.size(), is(3));
assertThat(geoAuthorsForGeoCriteria.size(), is(2));
}
@Test
@ -180,7 +180,7 @@ public class ElasticsearchTemplateGeoTests {
//given
loadAnnotationBaseEntities();
CriteriaQuery geoLocationCriteriaQuery = new CriteriaQuery(
new Criteria("locationAsArray").within("u1044", "1km"));
new Criteria("locationAsArray").within("u1044", "3km"));
//when
List<LocationMarkerEntity> geoAuthorsForGeoCriteria = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery, LocationMarkerEntity.class);

View File

@ -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.
@ -25,7 +25,6 @@ import java.util.Arrays;
import java.util.List;
import com.google.common.collect.Lists;
import org.apache.lucene.util.CollectionUtil;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;