DATAES-77 - Search across different indices

applied Spring Data coding/formatting rules
This commit is contained in:
Mohsin Husen 2014-04-21 17:26:08 +01:00
parent d24a92547f
commit 55219c947e
12 changed files with 229 additions and 218 deletions

View File

@ -19,7 +19,7 @@ import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
import static org.apache.commons.lang.StringUtils.*; import static org.apache.commons.lang.StringUtils.*;
import static org.elasticsearch.action.search.SearchType.*; import static org.elasticsearch.action.search.SearchType.*;
import static org.elasticsearch.client.Requests.*; import static org.elasticsearch.client.Requests.*;
import static org.elasticsearch.cluster.metadata.AliasAction.Type.ADD; import static org.elasticsearch.cluster.metadata.AliasAction.Type.*;
import static org.elasticsearch.common.collect.Sets.*; import static org.elasticsearch.common.collect.Sets.*;
import static org.elasticsearch.index.VersionType.*; import static org.elasticsearch.index.VersionType.*;
import static org.springframework.data.elasticsearch.core.MappingBuilder.*; import static org.springframework.data.elasticsearch.core.MappingBuilder.*;
@ -32,7 +32,6 @@ import java.util.*;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest; import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequestBuilder;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder; import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingRequest; import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingRequest;
@ -345,7 +344,6 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
return updateRequestBuilder.execute().actionGet(); return updateRequestBuilder.execute().actionGet();
} }
@Override @Override
public void bulkIndex(List<IndexQuery> queries) { public void bulkIndex(List<IndexQuery> queries) {
BulkRequestBuilder bulkRequest = client.prepareBulk(); BulkRequestBuilder bulkRequest = client.prepareBulk();
@ -563,11 +561,11 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
} }
private <T> boolean createIndexWithSettings(Class<T> clazz) { private <T> boolean createIndexWithSettings(Class<T> clazz) {
if(clazz.isAnnotationPresent(Setting.class)) { if (clazz.isAnnotationPresent(Setting.class)) {
String settingPath = clazz.getAnnotation(Setting.class).settingPath(); String settingPath = clazz.getAnnotation(Setting.class).settingPath();
if(isNotBlank(settingPath)) { if (isNotBlank(settingPath)) {
String settings = readFileFromClasspath(settingPath); String settings = readFileFromClasspath(settingPath);
if(isNotBlank(settings)) { if (isNotBlank(settings)) {
return createIndex(getPersistentEntityFor(clazz).getIndexName(), settings); return createIndex(getPersistentEntityFor(clazz).getIndexName(), settings);
} }
} else { } else {
@ -577,7 +575,6 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
return createIndex(getPersistentEntityFor(clazz).getIndexName(), getDefaultSettings(getPersistentEntityFor(clazz))); return createIndex(getPersistentEntityFor(clazz).getIndexName(), getDefaultSettings(getPersistentEntityFor(clazz)));
} }
@Override @Override
public boolean createIndex(String indexName, Object settings) { public boolean createIndex(String indexName, Object settings) {
CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(indexName); CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(indexName);

View File

@ -68,7 +68,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
this.refreshInterval = typeInformation.getType().getAnnotation(Document.class).refreshInterval(); this.refreshInterval = typeInformation.getType().getAnnotation(Document.class).refreshInterval();
this.indexStoreType = typeInformation.getType().getAnnotation(Document.class).indexStoreType(); this.indexStoreType = typeInformation.getType().getAnnotation(Document.class).indexStoreType();
} }
if(clazz.isAnnotationPresent(Setting.class)) { if (clazz.isAnnotationPresent(Setting.class)) {
this.settingPath = typeInformation.getType().getAnnotation(Setting.class).settingPath(); this.settingPath = typeInformation.getType().getAnnotation(Setting.class).settingPath();
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -33,11 +33,11 @@ import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.highlight.HighlightBuilder; import org.elasticsearch.search.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder; import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.search.sort.SortOrder;
import org.junit.*; import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
@ -80,20 +80,6 @@ public class ElasticsearchTemplateTests {
elasticsearchTemplate.refresh(SampleEntity.class, true); elasticsearchTemplate.refresh(SampleEntity.class, true);
} }
// @After
/** // doing a cleanup to ensure that no indexes are left behind after the test run */
/**
public void after() {
// it is safe to call deleteIndex as it checks for index existance before deleting it
elasticsearchTemplate.deleteIndex(INDEX_NAME);
elasticsearchTemplate.deleteIndex(INDEX_1_NAME);
elasticsearchTemplate.deleteIndex(INDEX_2_NAME);
}**/
@Test @Test
public void shouldReturnCountForGivenSearchQuery() { public void shouldReturnCountForGivenSearchQuery() {
// given // given
@ -1047,7 +1033,6 @@ public class ElasticsearchTemplateTests {
} }
@Test @Test
public void shouldIndexDocumentForSpecifiedSource() { public void shouldIndexDocumentForSpecifiedSource() {
@ -1577,7 +1562,6 @@ public class ElasticsearchTemplateTests {
IndexQuery idxQuery1 = new IndexQueryBuilder().withIndexName(INDEX_1_NAME).withId(entity1.getId()).withObject(entity1).build(); IndexQuery idxQuery1 = new IndexQueryBuilder().withIndexName(INDEX_1_NAME).withId(entity1.getId()).withObject(entity1).build();
IndexQuery idxQuery2 = new IndexQueryBuilder().withIndexName(INDEX_2_NAME).withId(entity2.getId()).withObject(entity2).build(); IndexQuery idxQuery2 = new IndexQueryBuilder().withIndexName(INDEX_2_NAME).withId(entity2.getId()).withObject(entity2).build();
elasticsearchTemplate.bulkIndex(Arrays.asList(idxQuery1, idxQuery2)); elasticsearchTemplate.bulkIndex(Arrays.asList(idxQuery1, idxQuery2));
elasticsearchTemplate.refresh(INDEX_1_NAME, true); elasticsearchTemplate.refresh(INDEX_1_NAME, true);
elasticsearchTemplate.refresh(INDEX_2_NAME, true); elasticsearchTemplate.refresh(INDEX_2_NAME, true);
@ -1591,8 +1575,8 @@ public class ElasticsearchTemplateTests {
List<ResultAggregator> values = new ArrayList<ResultAggregator>(); List<ResultAggregator> values = new ArrayList<ResultAggregator>();
for (SearchHit searchHit : response.getHits()) { for (SearchHit searchHit : response.getHits()) {
String id = String.valueOf(searchHit.getSource().get("id")); String id = String.valueOf(searchHit.getSource().get("id"));
String firstName = StringUtils.isNotEmpty((String)searchHit.getSource().get("firstName"))?(String)searchHit.getSource().get("firstName"):""; String firstName = StringUtils.isNotEmpty((String) searchHit.getSource().get("firstName")) ? (String) searchHit.getSource().get("firstName") : "";
String lastName = StringUtils.isNotEmpty((String)searchHit.getSource().get("lastName"))?(String)searchHit.getSource().get("lastName"):""; String lastName = StringUtils.isNotEmpty((String) searchHit.getSource().get("lastName")) ? (String) searchHit.getSource().get("lastName") : "";
values.add(new ResultAggregator(id, firstName, lastName)); values.add(new ResultAggregator(id, firstName, lastName));
} }
return new FacetedPageImpl<T>((List<T>) values); return new FacetedPageImpl<T>((List<T>) values);
@ -1600,7 +1584,6 @@ public class ElasticsearchTemplateTests {
}); });
assertThat(page.getTotalElements(), is(2l)); assertThat(page.getTotalElements(), is(2l));
} }
@ -1628,10 +1611,5 @@ public class ElasticsearchTemplateTests {
this.firstName = firstName; this.firstName = firstName;
this.lastName = lastName; this.lastName = lastName;
} }
} }
} }

View File

@ -187,7 +187,9 @@ public class ElasticsearchTemplateGeoTests {
CriteriaQuery geoLocationCriteriaQuery3 = new CriteriaQuery( CriteriaQuery geoLocationCriteriaQuery3 = new CriteriaQuery(
new Criteria("location").boundedBy( new Criteria("location").boundedBy(
new GeoBox(new GeoPoint(53.5171d, 0), new GeoBox(new GeoPoint(53.5171d, 0),
new GeoPoint(49.5171d, 0.2062d)))); new GeoPoint(49.5171d, 0.2062d))
)
);
//when //when
List<AuthorMarkerEntity> geoAuthorsForGeoCriteria3 = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery3, AuthorMarkerEntity.class); List<AuthorMarkerEntity> geoAuthorsForGeoCriteria3 = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery3, AuthorMarkerEntity.class);
@ -217,7 +219,8 @@ public class ElasticsearchTemplateGeoTests {
CriteriaQuery geoLocationCriteriaQuery3 = new CriteriaQuery( CriteriaQuery geoLocationCriteriaQuery3 = new CriteriaQuery(
new Criteria("location").boundedBy( new Criteria("location").boundedBy(
new GeoPoint(53.5171d, 0), new GeoPoint(53.5171d, 0),
new GeoPoint(49.5171d, 0.2062d))); new GeoPoint(49.5171d, 0.2062d))
);
//when //when
List<AuthorMarkerEntity> geoAuthorsForGeoCriteria3 = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery3, AuthorMarkerEntity.class); List<AuthorMarkerEntity> geoAuthorsForGeoCriteria3 = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery3, AuthorMarkerEntity.class);

View File

@ -38,7 +38,8 @@ public class SimpleElasticsearchPersistentEntityTests {
SimpleElasticsearchPersistentProperty persistentProperty = new SimpleElasticsearchPersistentProperty( SimpleElasticsearchPersistentProperty persistentProperty = new SimpleElasticsearchPersistentProperty(
EntityWithWrongVersionType.class.getDeclaredField("version"), new PropertyDescriptor("version", EntityWithWrongVersionType.class.getDeclaredField("version"), new PropertyDescriptor("version",
EntityWithWrongVersionType.class), new SimpleElasticsearchPersistentEntity<EntityWithWrongVersionType>( EntityWithWrongVersionType.class), new SimpleElasticsearchPersistentEntity<EntityWithWrongVersionType>(
typeInformation), new SimpleTypeHolder()); typeInformation), new SimpleTypeHolder()
);
// when // when
new SimpleElasticsearchPersistentEntity(typeInformation).addPersistentProperty(persistentProperty); new SimpleElasticsearchPersistentEntity(typeInformation).addPersistentProperty(persistentProperty);
@ -53,13 +54,15 @@ public class SimpleElasticsearchPersistentEntityTests {
EntityWithMultipleVersionField.class.getDeclaredField("version1"), new PropertyDescriptor("version1", EntityWithMultipleVersionField.class.getDeclaredField("version1"), new PropertyDescriptor("version1",
EntityWithMultipleVersionField.class), EntityWithMultipleVersionField.class),
new SimpleElasticsearchPersistentEntity<EntityWithMultipleVersionField>(typeInformation), new SimpleElasticsearchPersistentEntity<EntityWithMultipleVersionField>(typeInformation),
new SimpleTypeHolder()); new SimpleTypeHolder()
);
SimpleElasticsearchPersistentProperty persistentProperty2 = new SimpleElasticsearchPersistentProperty( SimpleElasticsearchPersistentProperty persistentProperty2 = new SimpleElasticsearchPersistentProperty(
EntityWithMultipleVersionField.class.getDeclaredField("version2"), new PropertyDescriptor("version2", EntityWithMultipleVersionField.class.getDeclaredField("version2"), new PropertyDescriptor("version2",
EntityWithMultipleVersionField.class), EntityWithMultipleVersionField.class),
new SimpleElasticsearchPersistentEntity<EntityWithMultipleVersionField>(typeInformation), new SimpleElasticsearchPersistentEntity<EntityWithMultipleVersionField>(typeInformation),
new SimpleTypeHolder()); new SimpleTypeHolder()
);
SimpleElasticsearchPersistentEntity simpleElasticsearchPersistentEntity = new SimpleElasticsearchPersistentEntity( SimpleElasticsearchPersistentEntity simpleElasticsearchPersistentEntity = new SimpleElasticsearchPersistentEntity(
typeInformation); typeInformation);

View File

@ -1,3 +1,18 @@
/*
* Copyright 2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.elasticsearch.entities; package org.springframework.data.elasticsearch.entities;
import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.EqualsBuilder;
@ -7,7 +22,8 @@ import org.springframework.data.annotation.Version;
import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Document;
/** /**
* @author abdul. * @author Abdul Waheed
* @author Mohsin Husen
*/ */
@Document(indexName = "test-index-1", type = "hetro", replicas = 0, shards = 1) @Document(indexName = "test-index-1", type = "hetro", replicas = 0, shards = 1)
public class HetroEntity1 { public class HetroEntity1 {
@ -64,5 +80,4 @@ public class HetroEntity1 {
public int hashCode() { public int hashCode() {
return new HashCodeBuilder().append(id).append(firstName).append(version).toHashCode(); return new HashCodeBuilder().append(id).append(firstName).append(version).toHashCode();
} }
} }

View File

@ -1,3 +1,18 @@
/*
* Copyright 2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.elasticsearch.entities; package org.springframework.data.elasticsearch.entities;
import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.EqualsBuilder;
@ -7,7 +22,8 @@ import org.springframework.data.annotation.Version;
import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Document;
/** /**
* @author abdul. * @author Abdul Waheed
* @author Mohsin Husen
*/ */
@Document(indexName = "test-index-2", type = "hetro", replicas = 0, shards = 1) @Document(indexName = "test-index-2", type = "hetro", replicas = 0, shards = 1)
public class HetroEntity2 { public class HetroEntity2 {
@ -24,30 +40,30 @@ public class HetroEntity2 {
this.version = System.currentTimeMillis(); this.version = System.currentTimeMillis();
} }
public void setId(String id) {
this.id = id;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setVersion(Long version) {
this.version = version;
}
public String getId() { public String getId() {
return id; return id;
} }
public void setId(String id) {
this.id = id;
}
public String getLastName() { public String getLastName() {
return lastName; return lastName;
} }
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Long getVersion() { public Long getVersion() {
return version; return version;
} }
public void setVersion(Long version) {
this.version = version;
}
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (!(obj instanceof SampleEntity)) { if (!(obj instanceof SampleEntity)) {
@ -64,5 +80,4 @@ public class HetroEntity2 {
public int hashCode() { public int hashCode() {
return new HashCodeBuilder().append(id).append(lastName).append(version).toHashCode(); return new HashCodeBuilder().append(id).append(lastName).append(version).toHashCode();
} }
} }

View File

@ -23,6 +23,6 @@ import org.springframework.data.elasticsearch.repository.ElasticsearchCrudReposi
* *
* @author Mohsin Husen * @author Mohsin Husen
*/ */
public interface SettingEntityRepository extends ElasticsearchCrudRepository<SettingEntity, String>{ public interface SettingEntityRepository extends ElasticsearchCrudRepository<SettingEntity, String> {
} }

View File

@ -5,7 +5,7 @@
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<import resource="infrastructure.xml"/> <import resource="infrastructure.xml"/>
<bean name="elasticsearchTemplate" <bean name="elasticsearchTemplate"
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate"> class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">

View File

@ -7,6 +7,6 @@
<elasticsearch:node-client id="client" local="true" cluster-name="testCluster" http-enabled="false"/> <elasticsearch:node-client id="client" local="true" cluster-name="testCluster" http-enabled="false"/>
<!--<elasticsearch:transport-client id="client" cluster-name="elasticsearch" cluster-nodes="127.0.1.1:9300" />--> <!--<elasticsearch:transport-client id="client" cluster-name="elasticsearch" cluster-nodes="127.0.0.1:9300" />-->
</beans> </beans>

View File

@ -1,12 +1,12 @@
{ {
"index": { "index": {
"number_of_shards" : "1", "number_of_shards": "1",
"number_of_replicas" : "0", "number_of_replicas": "0",
"analysis" :{ "analysis": {
"analyzer": { "analyzer": {
"emailAnalyzer": { "emailAnalyzer": {
"type" : "custom", "type": "custom",
"tokenizer" : "uax_url_email" "tokenizer": "uax_url_email"
} }
} }
} }