DATAES-54 - upgraded to elasticsearch 1.0

This commit is contained in:
Artur Konczak
2014-02-16 21:44:36 +00:00
parent ea50046f45
commit 1352c9f45b
12 changed files with 54 additions and 64 deletions

View File

@@ -23,6 +23,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import org.apache.lucene.queryparser.flexible.core.util.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.BoostableQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
@@ -117,18 +118,20 @@ class CriteriaQueryProcessor {
}
QueryBuilder query = null;
String searchText = StringUtils.toString(value);
switch (key) {
case EQUALS:
query = fieldQuery(fieldName, value);
query = queryString(searchText).field(fieldName);
break;
case CONTAINS:
query = fieldQuery(fieldName, "*" + value + "*").analyzeWildcard(true);
query = queryString("*" + searchText + "*").field(fieldName).analyzeWildcard(true);
break;
case STARTS_WITH:
query = fieldQuery(fieldName, value + "*").analyzeWildcard(true);
query = queryString(searchText + "*").field(fieldName).analyzeWildcard(true);
break;
case ENDS_WITH:
query = fieldQuery(fieldName, "*" + value).analyzeWildcard(true);
query = queryString("*" + searchText).field(fieldName).analyzeWildcard(true);
break;
case EXPRESSION:
query = queryString((String) value).field(fieldName);
@@ -144,7 +147,7 @@ class CriteriaQueryProcessor {
query = boolQuery();
Iterable<Object> collection = (Iterable<Object>) value;
for (Object item : collection) {
((BoolQueryBuilder) query).should(fieldQuery(fieldName, item));
((BoolQueryBuilder) query).should(queryString((String) item).field(fieldName));
}
break;
}
@@ -152,14 +155,6 @@ class CriteriaQueryProcessor {
return query;
}
private QueryBuilder buildNegationQuery(String fieldName, Iterator<Criteria.CriteriaEntry> it) {
BoolQueryBuilder notQuery = boolQuery();
while (it.hasNext()) {
notQuery.mustNot(fieldQuery(fieldName, it.next().getValue()));
}
return notQuery;
}
private void addBoost(QueryBuilder query, float boost) {
if (Float.isNaN(boost)) {
return;

View File

@@ -193,7 +193,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
public <T> List<String> queryForIds(SearchQuery query) {
SearchRequestBuilder request = prepareSearch(query).setQuery(query.getQuery()).setNoFields();
if (query.getFilter() != null) {
request.setFilter(query.getFilter());
request.setPostFilter(query.getFilter());
}
SearchResponse response = request.execute().actionGet();
return extractIds(response);
@@ -216,7 +216,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
}
if (elasticsearchFilter != null)
searchRequestBuilder.setFilter(elasticsearchFilter);
searchRequestBuilder.setPostFilter(elasticsearchFilter);
SearchResponse response = searchRequestBuilder
.execute().actionGet();
@@ -318,7 +318,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
ImmutableOpenMap<String, MappingMetaData> 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();
client.admin().indices().deleteMapping(new DeleteMappingRequest(index).types(type)).actionGet();
}
}
@@ -360,7 +360,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
.setSize(searchQuery.getPageable().getPageSize());
if (searchQuery.getFilter() != null) {
requestBuilder.setFilter(searchQuery.getFilter());
requestBuilder.setPostFilter(searchQuery.getFilter());
}
if (noFields) {
@@ -448,7 +448,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
private SearchResponse doSearch(SearchRequestBuilder searchRequest, SearchQuery searchQuery) {
if (searchQuery.getFilter() != null) {
searchRequest.setFilter(searchQuery.getFilter());
searchRequest.setPostFilter(searchQuery.getFilter());
}
if (searchQuery.getElasticsearchSort() != null) {
@@ -616,9 +616,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
@Override
public Set<String> queryForAlias(String indexName) {
ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest()
.filterRoutingTable(true)
.filterNodes(true)
.filteredIndices(indexName);
.routingTable(true).nodes(true).indices(indexName);
Iterator<String> iterator = client.admin().cluster().state(clusterStateRequest).actionGet().getState().getMetaData().aliases().keysIt();
return newHashSet(iterator);
}

View File

@@ -53,7 +53,7 @@ public class Criteria {
}
/**
* Creates a new CriterSimpleFieldia for the Filed with provided name
* Creates a new Criteria with provided field name
*
* @param fieldname
*/
@@ -394,14 +394,14 @@ public class Criteria {
/**
* Creates new CriteriaEntry for bounding box created from points
*
* @param topLeftPoint left top corner of bounding box
* @param bottomRightPoint right bottom corner of bounding box
* @param topLeftGeohash left top corner of bounding box as geohash
* @param bottomRightGeohash right bottom corner of bounding box as geohash
* @return Criteria the chaind criteria with the new 'boundedBy' criteria included.
*/
public Criteria boundedBy(String topLeftPoint, String bottomRightPoint) {
Assert.isTrue(StringUtils.isNotBlank(topLeftPoint), "topLeftPoint must not be empty");
Assert.isTrue(StringUtils.isNotBlank(bottomRightPoint), "bottomRightPoint must not be empty");
filterCriteria.add(new CriteriaEntry(OperationKey.BBOX, new Object[]{topLeftPoint, bottomRightPoint}));
public Criteria boundedBy(String topLeftGeohash, String bottomRightGeohash) {
Assert.isTrue(StringUtils.isNotBlank(topLeftGeohash), "topLeftGeohash must not be empty");
Assert.isTrue(StringUtils.isNotBlank(bottomRightGeohash), "bottomRightGeohash must not be empty");
filterCriteria.add(new CriteriaEntry(OperationKey.BBOX, new Object[]{topLeftGeohash, bottomRightGeohash}));
return this;
}

View File

@@ -25,7 +25,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.index.query.QueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -48,7 +48,7 @@ import org.springframework.util.Assert;
public abstract class AbstractElasticsearchRepository<T, ID extends Serializable> implements
ElasticsearchRepository<T, ID> {
static final Logger LOGGER = LoggerFactory.getLogger(AbstractElasticsearchRepository.class);
static final Logger LOGGER = LoggerFactory.getLogger(AbstractElasticsearchRepository.class);
protected ElasticsearchOperations elasticsearchOperations;
protected Class<T> entityClass;
protected ElasticsearchEntityInformation<T, ID> entityInformation;
@@ -67,12 +67,12 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
Assert.notNull(metadata);
this.entityInformation = metadata;
setEntityClass(this.entityInformation.getJavaType());
try {
try {
createIndex();
putMapping();
} catch (ElasticSearchException exception) {
LOGGER.error("failed to load elasticsearch nodes : " + exception.getDetailedMessage());
}
} catch (ElasticsearchException exception) {
LOGGER.error("failed to load elasticsearch nodes : " + exception.getDetailedMessage());
}
}
private void createIndex() {