Merge branch 'master' into geo_location

This commit is contained in:
Artur Konczak
2013-08-14 22:38:50 +01:00
parent 5a499315e8
commit 714bcf87fe
18 changed files with 462 additions and 81 deletions

View File

@@ -3,5 +3,5 @@ package org.springframework.data.elasticsearch.annotations;
/**
*/
public enum FieldType {
String, Integer, Long, Date, Object, Auto
String, Integer, Long, Date, Float, Double, Boolean, Object, Auto
}

View File

@@ -42,7 +42,6 @@ public class TransportClientFactoryBean implements FactoryBean<TransportClient>,
private static final Logger logger = LoggerFactory.getLogger(TransportClientFactoryBean.class);
private String clusterNodes;
private String clusterName;
private Boolean enableHttp;
private Boolean clientTransportSniff;
private TransportClient client;
private Properties properties;
@@ -102,7 +101,6 @@ public class TransportClientFactoryBean implements FactoryBean<TransportClient>,
return settingsBuilder()
.put("cluster.name", clusterName)
.put("client.transport.sniff", clientTransportSniff)
.put("http.enabled", enableHttp)
.build();
}
@@ -114,10 +112,6 @@ public class TransportClientFactoryBean implements FactoryBean<TransportClient>,
this.clusterName = clusterName;
}
public void setEnableHttp(Boolean enableHttp) {
this.enableHttp = enableHttp;
}
public void setClientTransportSniff(Boolean clientTransportSniff) {
this.clientTransportSniff = clientTransportSniff;
}

View File

@@ -43,7 +43,6 @@ public class TransportClientBeanDefinitionParser extends AbstractBeanDefinitionP
private void setClusterNodes(Element element, BeanDefinitionBuilder builder) {
builder.addPropertyValue("clusterNodes", element.getAttribute("cluster-nodes"));
builder.addPropertyValue("clusterName", element.getAttribute("cluster-name"));
builder.addPropertyValue("enableHttp", Boolean.valueOf(element.getAttribute("http-enabled")));
builder.addPropertyValue("clientTransportSniff", Boolean.valueOf(element.getAttribute("client-transport-sniff")));
}

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.elasticsearch.core;
import org.elasticsearch.action.update.UpdateResponse;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.query.*;
@@ -168,6 +169,14 @@ public interface ElasticsearchOperations {
*/
String index(IndexQuery query);
/**
* Partial update of the document
*
* @param updateQuery
* @return
*/
UpdateResponse update(UpdateQuery updateQuery);
/**
* Bulk index all objects. Will do save or update
*
@@ -202,15 +211,30 @@ public interface ElasticsearchOperations {
*/
<T> void delete(DeleteQuery query, Class<T> clazz);
/**
* Delete all records matching the query
*
* @param query
*/
void delete(DeleteQuery query);
/**
* Deletes an index for given entity
*
*
* @param clazz
* @param <T>
* @return
*/
<T> boolean deleteIndex(Class<T> clazz);
/**
* Deletes a type in an index
*
* @param index
* @param type
*/
void deleteType(String index, String type);
/**
* check if index is exists
*
@@ -220,6 +244,15 @@ public interface ElasticsearchOperations {
*/
<T> boolean indexExists(Class<T> clazz);
/**
* check if type is exists in an index
*
* @param index
* @param type
* @return
*/
boolean typeExists(String index, String type);
/**
* refresh the index
*

View File

@@ -19,6 +19,7 @@ import org.apache.commons.collections.CollectionUtils;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequestBuilder;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
@@ -29,6 +30,8 @@ import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.mlt.MoreLikeThisRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.collect.MapBuilder;
@@ -40,6 +43,7 @@ import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.facet.Facet;
import org.elasticsearch.search.facet.FacetBuilder;
import org.elasticsearch.search.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
@@ -101,7 +105,6 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
@Override
public <T> boolean createIndex(Class<T> clazz) {
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
return createIndexIfNotCreated(clazz);
}
@@ -227,6 +230,25 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
return prepareIndex(query).execute().actionGet().getId();
}
@Override
public UpdateResponse update(UpdateQuery query) {
String indexName = isNotBlank(query.getIndexName()) ? query.getIndexName() : getPersistentEntityFor(query.getClazz()).getIndexName();
String type = isNotBlank(query.getType()) ? query.getType() : getPersistentEntityFor(query.getClazz()).getIndexType();
Assert.notNull(indexName, "No index defined for Query");
Assert.notNull(type, "No type define for Query");
Assert.notNull(query.getId(), "No Id define for Query");
Assert.notNull(query.getIndexRequest(), "No IndexRequest define for Query");
UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate(indexName, type, query.getId());
if(query.DoUpsert()){
updateRequestBuilder.setDocAsUpsert(true)
.setUpsertRequest(query.getIndexRequest()).setDoc(query.getIndexRequest());
} else {
updateRequestBuilder.setDoc(query.getIndexRequest());
}
return updateRequestBuilder.execute().actionGet();
}
@Override
public void bulkIndex(List<IndexQuery> queries) {
BulkRequestBuilder bulkRequest = client.prepareBulk();
@@ -251,6 +273,12 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
return indexExists(getPersistentEntityFor(clazz).getIndexName());
}
@Override
public boolean typeExists(String index, String type) {
return client.admin().cluster().prepareState().execute().actionGet()
.getState().metaData().index(index).mappings().containsKey(type);
}
@Override
public <T> boolean deleteIndex(Class<T> clazz) {
String indexName = getPersistentEntityFor(clazz).getIndexName();
@@ -260,6 +288,15 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
return false;
}
@Override
public void deleteType(String index, String type){
Map mappings = client.admin().cluster().prepareState().execute().actionGet()
.getState().metaData().index(index).mappings();
if (mappings.containsKey(type)) {
client.admin().indices().deleteMapping(new DeleteMappingRequest(index).type(type)).actionGet();
}
}
@Override
public String delete(String indexName, String type, String id) {
return client.prepareDelete(indexName, type, id).execute().actionGet().getId();
@@ -278,6 +315,14 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
.setQuery(deleteQuery.getQuery()).execute().actionGet();
}
@Override
public void delete(DeleteQuery deleteQuery) {
Assert.notNull(deleteQuery.getIndex(), "No index defined for Query");
Assert.notNull(deleteQuery.getType(), "No type define for Query");
client.prepareDeleteByQuery(deleteQuery.getIndex()).setTypes(deleteQuery.getType())
.setQuery(deleteQuery.getQuery()).execute().actionGet();
}
@Override
public String scan(SearchQuery searchQuery, long scrollTimeInMillis, boolean noFields) {
Assert.notNull(searchQuery.getIndices(), "No index defined for Query");
@@ -388,6 +433,12 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
}
}
if(searchQuery.getHighlightFields() != null) {
for(HighlightBuilder.Field highlightField : searchQuery.getHighlightFields()){
searchRequest.addHighlightedField(highlightField);
}
}
return searchRequest.setQuery(searchQuery.getQuery()).execute().actionGet();
}

View File

@@ -1,52 +0,0 @@
package org.springframework.data.elasticsearch.core;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.facet.FacetResult;
import java.util.List;
import java.util.Map;
/**
* @author Rizwan Idrees
* @author Mohsin Husen
* @author Artur Konczak
* @author Jonathan Yan
*/
public class FactedPageImpl<T> extends PageImpl<T> implements FacetedPage<T> {
private List<FacetResult> facets;
private Map<String, FacetResult> mapOfFacets;
public FactedPageImpl(List<T> content) {
super(content);
}
public FactedPageImpl(List<T> content, Pageable pageable, long total) {
super(content, pageable, total);
}
public FactedPageImpl(List<T> content, Pageable pageable, long total, List<FacetResult> facets) {
super(content, pageable, total);
this.facets = facets;
for (FacetResult facet : facets) {
mapOfFacets.put(facet.getName(), facet);
}
}
@Override
public boolean hasFacets() {
return CollectionUtils.isNotEmpty(facets);
}
@Override
public List<FacetResult> getFacets() {
return facets;
}
@Override
public FacetResult getFacet(String name) {
return mapOfFacets.get(name);
}
}

View File

@@ -19,13 +19,15 @@ import org.elasticsearch.index.query.QueryBuilder;
/**
* DeleteQuery
*
*
* @author Rizwan Idrees
* @author Mohsin Husen
*/
public class DeleteQuery {
private QueryBuilder query;
private String index;
private String type;
public QueryBuilder getQuery() {
return query;
@@ -33,5 +35,21 @@ public class DeleteQuery {
public void setQuery(QueryBuilder query) {
this.query = query;
}
public String getIndex() {
return index;
}
public void setIndex(String index) {
this.index = index;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}

View File

@@ -17,7 +17,7 @@ package org.springframework.data.elasticsearch.core.query;
import org.elasticsearch.index.query.FilterBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.facet.FacetBuilder;
import org.elasticsearch.search.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.springframework.data.elasticsearch.core.facet.FacetRequest;
@@ -37,6 +37,7 @@ public class NativeSearchQuery extends AbstractQuery implements SearchQuery {
private FilterBuilder filter;
private SortBuilder sort;
private List<FacetRequest> facets;
private HighlightBuilder.Field[] highlightFields;
public NativeSearchQuery(QueryBuilder query) {
@@ -54,6 +55,13 @@ public class NativeSearchQuery extends AbstractQuery implements SearchQuery {
this.sort = sort;
}
public NativeSearchQuery(QueryBuilder query, FilterBuilder filter, SortBuilder sort, HighlightBuilder.Field[] highlightFields) {
this.query = query;
this.filter = filter;
this.sort = sort;
this.highlightFields = highlightFields;
}
public QueryBuilder getQuery() {
return query;
}
@@ -64,6 +72,11 @@ public class NativeSearchQuery extends AbstractQuery implements SearchQuery {
public SortBuilder getElasticsearchSort() {
return sort;
}
@Override
public HighlightBuilder.Field[] getHighlightFields() {
return highlightFields;
}
public void addFacet(FacetRequest facetRequest){

View File

@@ -18,6 +18,7 @@ package org.springframework.data.elasticsearch.core.query;
import org.apache.commons.collections.CollectionUtils;
import org.elasticsearch.index.query.FilterBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.facet.FacetRequest;
@@ -39,6 +40,7 @@ public class NativeSearchQueryBuilder {
private FilterBuilder filterBuilder;
private SortBuilder sortBuilder;
private List<FacetRequest> facetRequests = new ArrayList<FacetRequest>();
private HighlightBuilder.Field[] highlightFields;
private Pageable pageable;
private String[] indices;
private String[] types;
@@ -64,6 +66,11 @@ public class NativeSearchQueryBuilder {
return this;
}
public NativeSearchQueryBuilder withHighlightFields(HighlightBuilder.Field... highlightFields){
this.highlightFields = highlightFields;
return this;
}
public NativeSearchQueryBuilder withPageable(Pageable pageable) {
this.pageable = pageable;
return this;
@@ -85,7 +92,7 @@ public class NativeSearchQueryBuilder {
}
public NativeSearchQuery build() {
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder, filterBuilder, sortBuilder);
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder, filterBuilder, sortBuilder, highlightFields);
if (pageable != null) {
nativeSearchQuery.setPageable(pageable);
}

View File

@@ -17,6 +17,7 @@ package org.springframework.data.elasticsearch.core.query;
import org.elasticsearch.index.query.FilterBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.springframework.data.elasticsearch.core.facet.FacetRequest;
@@ -37,4 +38,6 @@ public interface SearchQuery extends Query {
SortBuilder getElasticsearchSort();
List<FacetRequest> getFacets();
HighlightBuilder.Field[] getHighlightFields();
}

View File

@@ -0,0 +1,80 @@
/*
* Copyright 2013 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.core.query;
import org.elasticsearch.action.index.IndexRequest;
/**
* @author Rizwan Idrees
* @author Mohsin Husen
*/
public class UpdateQuery {
private String id;
private IndexRequest indexRequest;
private String indexName;
private String type;
private Class clazz;
private boolean doUpsert;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public IndexRequest getIndexRequest() {
return indexRequest;
}
public void setIndexRequest(IndexRequest indexRequest) {
this.indexRequest = indexRequest;
}
public String getIndexName() {
return indexName;
}
public void setIndexName(String indexName) {
this.indexName = indexName;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Class getClazz() {
return clazz;
}
public void setClazz(Class clazz) {
this.clazz = clazz;
}
public boolean DoUpsert() {
return doUpsert;
}
public void setDoUpsert(boolean doUpsert) {
this.doUpsert = doUpsert;
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright 2013 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.core.query;
import org.elasticsearch.action.index.IndexRequest;
/**
* @author Rizwan Idrees
* @author Mohsin Husen
*/
public class UpdateQueryBuilder {
private String id;
private IndexRequest indexRequest;
private String indexName;
private String type;
private Class clazz;
private boolean doUpsert;
public UpdateQueryBuilder withId(String id){
this.id = id;
return this;
}
public UpdateQueryBuilder withIndexRequest(IndexRequest indexRequest){
this.indexRequest = indexRequest;
return this;
}
public UpdateQueryBuilder withIndexName(String indexName){
this.indexName = indexName;
return this;
}
public UpdateQueryBuilder withType(String type){
this.type = type;
return this;
}
public UpdateQueryBuilder withClass(Class clazz){
this.clazz = clazz;
return this;
}
public UpdateQueryBuilder withDoUpsert(boolean doUpsert){
this.doUpsert = doUpsert;
return this;
}
public UpdateQuery build(){
UpdateQuery updateQuery = new UpdateQuery();
updateQuery.setId(id);
updateQuery.setIndexName(indexName);
updateQuery.setType(type);
updateQuery.setClazz(clazz);
updateQuery.setIndexRequest(indexRequest);
updateQuery.setDoUpsert(doUpsert);
return updateQuery;
}
}