mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-22 03:52:10 +00:00
Merge branch 'master' into geo_location
This commit is contained in:
parent
5a499315e8
commit
714bcf87fe
2
pom.xml
2
pom.xml
@ -24,7 +24,7 @@
|
||||
|
||||
<commonscollections>3.2.1</commonscollections>
|
||||
<commonslang>2.6</commonslang>
|
||||
<elasticsearch>0.90.0</elasticsearch>
|
||||
<elasticsearch>0.90.2</elasticsearch>
|
||||
<jackson>1.9.2</jackson>
|
||||
<springdata.commons>1.6.0.BUILD-SNAPSHOT</springdata.commons>
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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")));
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
*
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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){
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -84,11 +84,6 @@
|
||||
<xsd:documentation><![CDATA[Name of the cluster in which this instance of node client will connect to]]> </xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="http-enabled" type="xsd:boolean" default="true">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[to enable or desable http port]]> </xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="client-transport-sniff" type="xsd:boolean" default="true">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[The client allows to sniff the rest of the cluster, and add those into its list of machines to use.]]> </xsd:documentation>
|
||||
|
@ -34,6 +34,7 @@ public class SampleEntity {
|
||||
private String message;
|
||||
private int rate;
|
||||
private boolean available;
|
||||
private String highlightedMessage;
|
||||
@Version
|
||||
private Long version;
|
||||
|
||||
@ -77,6 +78,14 @@ public class SampleEntity {
|
||||
this.available = available;
|
||||
}
|
||||
|
||||
public String getHighlightedMessage() {
|
||||
return highlightedMessage;
|
||||
}
|
||||
|
||||
public void setHighlightedMessage(String highlightedMessage) {
|
||||
this.highlightedMessage = highlightedMessage;
|
||||
}
|
||||
|
||||
public Long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
@ -15,8 +15,11 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core;
|
||||
|
||||
import org.elasticsearch.action.index.IndexRequest;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.index.engine.DocumentMissingException;
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
import org.elasticsearch.search.highlight.HighlightBuilder;
|
||||
import org.elasticsearch.search.sort.FieldSortBuilder;
|
||||
import org.elasticsearch.search.sort.SortOrder;
|
||||
import org.junit.Before;
|
||||
@ -39,12 +42,9 @@ import java.util.List;
|
||||
import static org.apache.commons.lang.RandomStringUtils.randomNumeric;
|
||||
import static org.elasticsearch.index.query.FilterBuilders.boolFilter;
|
||||
import static org.elasticsearch.index.query.FilterBuilders.termFilter;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.fieldQuery;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Rizwan Idrees
|
||||
@ -717,4 +717,164 @@ public class ElasticsearchTemplateTests {
|
||||
// then
|
||||
assertThat(elasticsearchTemplate.indexExists(clazz), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDoPartialUpdateForExistingDocument() {
|
||||
//given
|
||||
String documentId = randomNumeric(5);
|
||||
String messageBeforeUpdate = "some test message";
|
||||
String messageAfterUpdate = "test message";
|
||||
|
||||
SampleEntity sampleEntity = new SampleEntity();
|
||||
sampleEntity.setId(documentId);
|
||||
sampleEntity.setMessage(messageBeforeUpdate);
|
||||
sampleEntity.setVersion(System.currentTimeMillis());
|
||||
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(documentId);
|
||||
indexQuery.setObject(sampleEntity);
|
||||
|
||||
elasticsearchTemplate.index(indexQuery);
|
||||
elasticsearchTemplate.refresh(SampleEntity.class, true);
|
||||
|
||||
IndexRequest indexRequest = new IndexRequest();
|
||||
indexRequest.source("message", messageAfterUpdate);
|
||||
UpdateQuery updateQuery = new UpdateQueryBuilder().withId(documentId)
|
||||
.withClass(SampleEntity.class).withIndexRequest(indexRequest).build();
|
||||
// when
|
||||
elasticsearchTemplate.update(updateQuery);
|
||||
//then
|
||||
GetQuery getQuery = new GetQuery();
|
||||
getQuery.setId(documentId);
|
||||
SampleEntity indexedEntity = elasticsearchTemplate.queryForObject(getQuery, SampleEntity.class);
|
||||
assertThat(indexedEntity.getMessage(), is(messageAfterUpdate));
|
||||
}
|
||||
|
||||
@Test(expected = DocumentMissingException.class)
|
||||
public void shouldThrowExceptionIfDocumentDoesNotExistWhileDoingPartialUpdate(){
|
||||
// when
|
||||
IndexRequest indexRequest = new IndexRequest();
|
||||
UpdateQuery updateQuery = new UpdateQueryBuilder().withId(randomNumeric(5))
|
||||
.withClass(SampleEntity.class).withIndexRequest(indexRequest).build();
|
||||
elasticsearchTemplate.update(updateQuery);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDoUpsertIfDocumentDoesNotExist(){
|
||||
//given
|
||||
String documentId = randomNumeric(5);
|
||||
String message = "test message";
|
||||
IndexRequest indexRequest = new IndexRequest();
|
||||
indexRequest.source("message", message);
|
||||
UpdateQuery updateQuery = new UpdateQueryBuilder().withId(documentId)
|
||||
.withDoUpsert(true).withClass(SampleEntity.class)
|
||||
.withIndexRequest(indexRequest).build();
|
||||
//when
|
||||
elasticsearchTemplate.update(updateQuery);
|
||||
//then
|
||||
GetQuery getQuery = new GetQuery();
|
||||
getQuery.setId(documentId);
|
||||
SampleEntity indexedEntity = elasticsearchTemplate.queryForObject(getQuery, SampleEntity.class);
|
||||
assertThat(indexedEntity.getMessage(), is(message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnHighlightedFieldsForGivenQueryAndFields(){
|
||||
|
||||
//given
|
||||
String documentId = randomNumeric(5);
|
||||
String actualMessage = "some test message";
|
||||
String highlightedMessage = "some <em>test</em> message";
|
||||
|
||||
SampleEntity sampleEntity = new SampleEntity();
|
||||
sampleEntity.setId(documentId);
|
||||
sampleEntity.setMessage(actualMessage);
|
||||
sampleEntity.setVersion(System.currentTimeMillis());
|
||||
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(documentId);
|
||||
indexQuery.setObject(sampleEntity);
|
||||
|
||||
elasticsearchTemplate.index(indexQuery);
|
||||
elasticsearchTemplate.refresh(SampleEntity.class, true);
|
||||
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||
.withQuery(termQuery("message", "test"))
|
||||
.withHighlightFields(new HighlightBuilder.Field("message"))
|
||||
.build();
|
||||
|
||||
Page<SampleEntity> sampleEntities = elasticsearchTemplate.queryForPage(searchQuery, new ResultsMapper<SampleEntity>() {
|
||||
@Override
|
||||
public FacetedPage<SampleEntity> mapResults(SearchResponse response) {
|
||||
List<SampleEntity> chunk = new ArrayList<SampleEntity>();
|
||||
for (SearchHit searchHit : response.getHits()) {
|
||||
if (response.getHits().getHits().length <= 0) {
|
||||
return null;
|
||||
}
|
||||
SampleEntity user = new SampleEntity();
|
||||
user.setId(searchHit.getId());
|
||||
user.setMessage((String) searchHit.getSource().get("message"));
|
||||
user.setHighlightedMessage(searchHit.getHighlightFields().get("message").fragments()[0].toString());
|
||||
chunk.add(user);
|
||||
}
|
||||
if(chunk.size() > 0){
|
||||
return new FacetedPageImpl<SampleEntity>(chunk);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
assertThat(sampleEntities.getContent().get(0).getHighlightedMessage(), is(highlightedMessage));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDeleteSpecifiedTypeFromAnIndex() {
|
||||
// given
|
||||
String documentId = randomNumeric(5);
|
||||
SampleEntity sampleEntity = new SampleEntity();
|
||||
sampleEntity.setId(documentId);
|
||||
sampleEntity.setMessage("some message");
|
||||
sampleEntity.setVersion(System.currentTimeMillis());
|
||||
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(documentId);
|
||||
indexQuery.setObject(sampleEntity);
|
||||
|
||||
elasticsearchTemplate.index(indexQuery);
|
||||
|
||||
// when
|
||||
elasticsearchTemplate.deleteType("test-index","test-type");
|
||||
|
||||
//then
|
||||
boolean typeExists = elasticsearchTemplate.typeExists("test-index", "test-type");
|
||||
assertThat(typeExists, is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDeleteDocumentBySpecifiedTypeUsingDeleteQuery(){
|
||||
// given
|
||||
String documentId = randomNumeric(5);
|
||||
SampleEntity sampleEntity = new SampleEntity();
|
||||
sampleEntity.setId(documentId);
|
||||
sampleEntity.setMessage("some message");
|
||||
sampleEntity.setVersion(System.currentTimeMillis());
|
||||
|
||||
IndexQuery indexQuery = new IndexQuery();
|
||||
indexQuery.setId(documentId);
|
||||
indexQuery.setObject(sampleEntity);
|
||||
|
||||
elasticsearchTemplate.index(indexQuery);
|
||||
// when
|
||||
DeleteQuery deleteQuery = new DeleteQuery();
|
||||
deleteQuery.setQuery(fieldQuery("id", documentId));
|
||||
deleteQuery.setIndex("test-index");
|
||||
deleteQuery.setType("test-type");
|
||||
elasticsearchTemplate.delete(deleteQuery);
|
||||
// then
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(fieldQuery("id", documentId)).build();
|
||||
Page<SampleEntity> sampleEntities = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class);
|
||||
assertThat(sampleEntities.getTotalElements(), equalTo(0L));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ import static org.springframework.data.elasticsearch.annotations.FieldType.Strin
|
||||
/**
|
||||
* Simple type to test facets
|
||||
*/
|
||||
@Document(indexName = "articles", type = "article", shards = 1, replicas = 0, refreshInterval = "-1")
|
||||
@Document(indexName = "articles", type = "article", shards = 1, replicas = 0, refreshInterval = "-1", indexStoreType = "memory")
|
||||
public class ArticleEntity {
|
||||
|
||||
@Id
|
||||
|
@ -77,7 +77,7 @@ public class ElasticsearchTemplateGeoTests {
|
||||
double[] latLonArray = {0.100000, 51.000000};
|
||||
String lonLatString = "51.000000, 0.100000";
|
||||
String geohash = "u1044k2bd6u";
|
||||
indexQueries.add(new AuthorMarkerAnnotatedEntityBuilder("2").name("Mohsin Husen").location(geohash.substring(3)).additionalLocation(latLonArray).buildIndex());
|
||||
indexQueries.add(new AuthorMarkerAnnotatedEntityBuilder("2").name("Mohsin Husen").location(geohash.substring(0,5)).additionalLocation(latLonArray).buildIndex());
|
||||
indexQueries.add(new AuthorMarkerAnnotatedEntityBuilder("1").name("Artur Konczak").location(lonLatString).additionalLocation(latLonArray).buildIndex());
|
||||
indexQueries.add(new AuthorMarkerAnnotatedEntityBuilder("3").name("Rizwan Idrees").location(geohash).additionalLocation(latLonArray).buildIndex());
|
||||
|
||||
@ -132,9 +132,7 @@ public class ElasticsearchTemplateGeoTests {
|
||||
List<AuthorMarkerAnnotatedEntity> geoAuthorsForGeoCriteria = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery, AuthorMarkerAnnotatedEntity.class);
|
||||
|
||||
//then
|
||||
assertThat(geoAuthorsForGeoCriteria.size(), is(1));
|
||||
//TODO: result should be 3 not 1 - geohash points don't work
|
||||
assertEquals("Artur Konczak", geoAuthorsForGeoCriteria.get(0).getName());
|
||||
assertThat(geoAuthorsForGeoCriteria.size(), is(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
x
Reference in New Issue
Block a user