Skip to content

Commit ba1fb9f

Browse files
thiagolocatellimohsinh
authored andcommitted
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
1 parent 9c91723 commit ba1fb9f

File tree

5 files changed

+75
-3
lines changed

5 files changed

+75
-3
lines changed

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,12 @@ private SearchResponse doSearch(SearchRequestBuilder searchRequest, SearchQuery
863863
searchRequest.addHighlightedField(highlightField);
864864
}
865865
}
866+
867+
if (searchQuery.getIndicesBoost() != null) {
868+
for (IndexBoost indexBoost : searchQuery.getIndicesBoost()) {
869+
searchRequest.addIndexBoost(indexBoost.getIndexName(), indexBoost.getBoost());
870+
}
871+
}
866872

867873
if (CollectionUtils.isNotEmpty(searchQuery.getAggregations())) {
868874
for (AbstractAggregationBuilder aggregationBuilder : searchQuery.getAggregations()) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.elasticsearch.core.query;
18+
19+
/**
20+
* Defines a IndexBoost to be applied on the "indices_boost" query clause
21+
*
22+
* @author Thiago Locatelli
23+
*/
24+
public class IndexBoost {
25+
26+
private String indexName;
27+
private float boost;
28+
29+
public IndexBoost(String indexName, float boost) {
30+
this.indexName = indexName;
31+
this.boost = boost;
32+
}
33+
34+
public String getIndexName() {
35+
return indexName;
36+
}
37+
38+
public float getBoost() {
39+
return boost;
40+
}
41+
42+
}

src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQuery.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,6 +45,7 @@ public class NativeSearchQuery extends AbstractQuery implements SearchQuery {
4545
private List<FacetRequest> facets;
4646
private List<AbstractAggregationBuilder> aggregations;
4747
private HighlightBuilder.Field[] highlightFields;
48+
private IndexBoost[] indicesBoost;
4849

4950

5051
public NativeSearchQuery(QueryBuilder query) {
@@ -129,4 +130,14 @@ public void addAggregation(AbstractAggregationBuilder aggregationBuilder) {
129130
public void setAggregations(List<AbstractAggregationBuilder> aggregations) {
130131
this.aggregations = aggregations;
131132
}
133+
134+
@Override
135+
public IndexBoost[] getIndicesBoost() {
136+
return indicesBoost;
137+
}
138+
139+
public void setIndicesBoost(IndexBoost... indicesBoost) {
140+
this.indicesBoost = indicesBoost;
141+
}
142+
132143
}

src/main/java/org/springframework/data/elasticsearch/core/query/NativeSearchQueryBuilder.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,6 +50,7 @@ public class NativeSearchQueryBuilder {
5050
private String[] indices;
5151
private String[] types;
5252
private String[] fields;
53+
private IndexBoost[] indicesBoost;
5354
private float minScore;
5455
private Collection<String> ids;
5556
private String route;
@@ -90,6 +91,11 @@ public NativeSearchQueryBuilder withHighlightFields(HighlightBuilder.Field... hi
9091
return this;
9192
}
9293

94+
public NativeSearchQueryBuilder withIndicesBoost(IndexBoost... indicesBoost) {
95+
this.indicesBoost = indicesBoost;
96+
return this;
97+
}
98+
9399
public NativeSearchQueryBuilder withPageable(Pageable pageable) {
94100
this.pageable = pageable;
95101
return this;
@@ -147,6 +153,11 @@ public NativeSearchQuery build() {
147153
if (fields != null) {
148154
nativeSearchQuery.addFields(fields);
149155
}
156+
157+
if(indicesBoost != null) {
158+
nativeSearchQuery.setIndicesBoost(indicesBoost);
159+
}
160+
150161
if (CollectionUtils.isNotEmpty(scriptFields)) {
151162
nativeSearchQuery.setScriptFields(scriptFields);
152163
}

src/main/java/org/springframework/data/elasticsearch/core/query/SearchQuery.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,6 +45,8 @@ public interface SearchQuery extends Query {
4545

4646
HighlightBuilder.Field[] getHighlightFields();
4747

48+
IndexBoost[] getIndicesBoost();
49+
4850
List<ScriptField> getScriptFields();
4951

5052
}

0 commit comments

Comments
 (0)