From ba1fb9fc6f8235532f57998601eb6b8cb9d41f67 Mon Sep 17 00:00:00 2001 From: Thiago Locatelli Date: Thu, 26 Nov 2015 11:09:33 -0500 Subject: [PATCH] DATAES-216 - Adding support to 'indices_boost' when searching against multiple indices Applying spring-data code format Merge branch 'master' of https://github.com/thiagolocatelli/spring-data-elasticsearch into thiagolocatelli-master --- .../core/ElasticsearchTemplate.java | 6 +++ .../elasticsearch/core/query/IndexBoost.java | 42 +++++++++++++++++++ .../core/query/NativeSearchQuery.java | 13 +++++- .../core/query/NativeSearchQueryBuilder.java | 13 +++++- .../elasticsearch/core/query/SearchQuery.java | 4 +- 5 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/springframework/data/elasticsearch/core/query/IndexBoost.java diff --git a/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java b/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java index 21b8fb028..c4d7dd03b 100755 --- a/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java @@ -863,6 +863,12 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati searchRequest.addHighlightedField(highlightField); } } + + if (searchQuery.getIndicesBoost() != null) { + for (IndexBoost indexBoost : searchQuery.getIndicesBoost()) { + searchRequest.addIndexBoost(indexBoost.getIndexName(), indexBoost.getBoost()); + } + } if (CollectionUtils.isNotEmpty(searchQuery.getAggregations())) { for (AbstractAggregationBuilder aggregationBuilder : searchQuery.getAggregations()) { diff --git a/src/main/java/org/springframework/data/elasticsearch/core/query/IndexBoost.java b/src/main/java/org/springframework/data/elasticsearch/core/query/IndexBoost.java new file mode 100644 index 000000000..fd2e0c3a4 --- /dev/null +++ b/src/main/java/org/springframework/data/elasticsearch/core/query/IndexBoost.java @@ -0,0 +1,42 @@ +/* + * Copyright 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. + * 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; + +/** + * Defines a IndexBoost to be applied on the "indices_boost" query clause + * + * @author Thiago Locatelli + */ +public class IndexBoost { + + private String indexName; + private float boost; + + public IndexBoost(String indexName, float boost) { + this.indexName = indexName; + this.boost = boost; + } + + public String getIndexName() { + return indexName; + } + + public float getBoost() { + return boost; + } + +} diff --git a/src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQuery.java b/src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQuery.java index ed16f734c..e2e9bed68 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQuery.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQuery.java @@ -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. @@ -45,6 +45,7 @@ public class NativeSearchQuery extends AbstractQuery implements SearchQuery { private List facets; private List aggregations; private HighlightBuilder.Field[] highlightFields; + private IndexBoost[] indicesBoost; public NativeSearchQuery(QueryBuilder query) { @@ -129,4 +130,14 @@ public class NativeSearchQuery extends AbstractQuery implements SearchQuery { public void setAggregations(List aggregations) { this.aggregations = aggregations; } + + @Override + public IndexBoost[] getIndicesBoost() { + return indicesBoost; + } + + public void setIndicesBoost(IndexBoost... indicesBoost) { + this.indicesBoost = indicesBoost; + } + } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQueryBuilder.java b/src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQueryBuilder.java index 2b4884c8f..8be64c562 100755 --- a/src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQueryBuilder.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQueryBuilder.java @@ -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. @@ -50,6 +50,7 @@ public class NativeSearchQueryBuilder { private String[] indices; private String[] types; private String[] fields; + private IndexBoost[] indicesBoost; private float minScore; private Collection ids; private String route; @@ -90,6 +91,11 @@ public class NativeSearchQueryBuilder { return this; } + public NativeSearchQueryBuilder withIndicesBoost(IndexBoost... indicesBoost) { + this.indicesBoost = indicesBoost; + return this; + } + public NativeSearchQueryBuilder withPageable(Pageable pageable) { this.pageable = pageable; return this; @@ -147,6 +153,11 @@ public class NativeSearchQueryBuilder { if (fields != null) { nativeSearchQuery.addFields(fields); } + + if(indicesBoost != null) { + nativeSearchQuery.setIndicesBoost(indicesBoost); + } + if (CollectionUtils.isNotEmpty(scriptFields)) { nativeSearchQuery.setScriptFields(scriptFields); } diff --git a/src/main/java/org/springframework/data/elasticsearch/core/query/SearchQuery.java b/src/main/java/org/springframework/data/elasticsearch/core/query/SearchQuery.java index 283d02897..83057a898 100644 --- a/src/main/java/org/springframework/data/elasticsearch/core/query/SearchQuery.java +++ b/src/main/java/org/springframework/data/elasticsearch/core/query/SearchQuery.java @@ -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. @@ -45,6 +45,8 @@ public interface SearchQuery extends Query { HighlightBuilder.Field[] getHighlightFields(); + IndexBoost[] getIndicesBoost(); + List getScriptFields(); }