Skip to content

Commit de1afe8

Browse files
alturkovicxhaggi
authored andcommitted
DATAES-422 - Add support for IndicesOptions in search queries.
See also: DATAES-191..
1 parent 92c3bbe commit de1afe8

File tree

5 files changed

+71
-5
lines changed

5 files changed

+71
-5
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2017 the original author or authors.
2+
* Copyright 2013-2018 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.
@@ -108,6 +108,7 @@
108108
* @author Mark Janssen
109109
* @author Mark Paluch
110110
* @author Ilkang Na
111+
* @author Alen Turkovic
111112
*/
112113
public class ElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware {
113114

@@ -995,6 +996,10 @@ private SearchRequestBuilder prepareSearch(Query query) {
995996
searchRequestBuilder.setFetchSource(toArray(query.getFields()), null);
996997
}
997998

999+
if (query.getIndicesOptions() != null) {
1000+
searchRequestBuilder.setIndicesOptions(query.getIndicesOptions());
1001+
}
1002+
9981003
if (query.getSort() != null) {
9991004
for (Sort.Order order : query.getSort()) {
10001005
searchRequestBuilder.addSort(order.getProperty(),

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2017 the original author or authors.
2+
* Copyright 2013-2018 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.
@@ -20,6 +20,7 @@
2020
import java.util.Collection;
2121
import java.util.List;
2222
import org.elasticsearch.action.search.SearchType;
23+
import org.elasticsearch.action.support.IndicesOptions;
2324
import org.springframework.data.domain.Pageable;
2425
import org.springframework.data.domain.Sort;
2526
import org.springframework.util.Assert;
@@ -30,6 +31,7 @@
3031
* @author Rizwan Idrees
3132
* @author Mohsin Husen
3233
* @author Mark Paluch
34+
* @author Alen Turkovic
3335
*/
3436
abstract class AbstractQuery implements Query {
3537

@@ -43,6 +45,7 @@ abstract class AbstractQuery implements Query {
4345
protected Collection<String> ids;
4446
protected String route;
4547
protected SearchType searchType = SearchType.DFS_QUERY_THEN_FETCH;
48+
protected IndicesOptions indicesOptions;
4649

4750
@Override
4851
public Sort getSort() {
@@ -149,4 +152,12 @@ public void setSearchType(SearchType searchType) {
149152
public SearchType getSearchType() {
150153
return searchType;
151154
}
155+
156+
public IndicesOptions getIndicesOptions() {
157+
return indicesOptions;
158+
}
159+
160+
public void setIndicesOptions(IndicesOptions indicesOptions) {
161+
this.indicesOptions = indicesOptions;
162+
}
152163
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2017 the original author or authors.
2+
* Copyright 2013-2018 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.
@@ -20,6 +20,7 @@
2020
import java.util.Collection;
2121
import java.util.List;
2222
import org.elasticsearch.action.search.SearchType;
23+
import org.elasticsearch.action.support.IndicesOptions;
2324
import org.elasticsearch.index.query.QueryBuilder;
2425
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
2526
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
@@ -34,6 +35,7 @@
3435
* @author Mohsin Husen
3536
* @author Artur Konczak
3637
* @author Mark Paluch
38+
* @author Alen Turkovic
3739
*/
3840
public class NativeSearchQueryBuilder {
3941

@@ -54,6 +56,7 @@ public class NativeSearchQueryBuilder {
5456
private Collection<String> ids;
5557
private String route;
5658
private SearchType searchType;
59+
private IndicesOptions indicesOptions;
5760

5861
public NativeSearchQueryBuilder withQuery(QueryBuilder queryBuilder) {
5962
this.queryBuilder = queryBuilder;
@@ -140,6 +143,11 @@ public NativeSearchQueryBuilder withSearchType(SearchType searchType) {
140143
return this;
141144
}
142145

146+
public NativeSearchQueryBuilder withIndicesOptions(IndicesOptions indicesOptions) {
147+
this.indicesOptions = indicesOptions;
148+
return this;
149+
}
150+
143151
public NativeSearchQuery build() {
144152
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder, filterBuilder, sortBuilders, highlightFields);
145153
nativeSearchQuery.setPageable(pageable);
@@ -192,6 +200,10 @@ public NativeSearchQuery build() {
192200
nativeSearchQuery.setSearchType(searchType);
193201
}
194202

203+
if (indicesOptions != null) {
204+
nativeSearchQuery.setIndicesOptions(indicesOptions);
205+
}
206+
195207
return nativeSearchQuery;
196208
}
197209
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2017 the original author or authors.
2+
* Copyright 2013-2018 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.
@@ -18,6 +18,7 @@
1818
import java.util.Collection;
1919
import java.util.List;
2020
import org.elasticsearch.action.search.SearchType;
21+
import org.elasticsearch.action.support.IndicesOptions;
2122
import org.springframework.data.domain.PageRequest;
2223
import org.springframework.data.domain.Pageable;
2324
import org.springframework.data.domain.Sort;
@@ -28,6 +29,7 @@
2829
* @author Rizwan Idrees
2930
* @author Mohsin Husen
3031
* @author Mark Paluch
32+
* @author Alen Turkovic
3133
*/
3234
public interface Query {
3335

@@ -147,4 +149,11 @@ public interface Query {
147149
* @return
148150
*/
149151
SearchType getSearchType();
152+
153+
/**
154+
* Get indices options
155+
*
156+
* @return null if not set
157+
*/
158+
IndicesOptions getIndicesOptions();
150159
}

src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2017 the original author or authors.
2+
* Copyright 2014-2018 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.
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.action.get.MultiGetResponse;
2929
import org.elasticsearch.action.index.IndexRequest;
3030
import org.elasticsearch.action.search.SearchResponse;
31+
import org.elasticsearch.action.support.IndicesOptions;
3132
import org.elasticsearch.index.engine.DocumentMissingException;
3233
import org.elasticsearch.script.Script;
3334
import org.elasticsearch.script.ScriptType;
@@ -71,6 +72,7 @@
7172
* @author Kevin Leturc
7273
* @author Mason Chan
7374
* @author Ilkang Na
75+
* @author Alen Turkovic
7476
*/
7577
@RunWith(SpringJUnit4ClassRunner.class)
7678
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
@@ -243,6 +245,33 @@ public void shouldReturnPageForGivenSearchQuery() {
243245
assertThat(sampleEntities.getTotalElements(), greaterThanOrEqualTo(1L));
244246
}
245247

248+
// DATAES-422 - Add support for IndicesOptions in search queries
249+
@Test
250+
public void shouldPassIndicesOptionsForGivenSearchQuery() {
251+
// given
252+
String documentId = randomNumeric(5);
253+
SampleEntity sampleEntity = SampleEntity.builder().id(documentId).message("some message")
254+
.version(System.currentTimeMillis()).build();
255+
256+
IndexQuery idxQuery = new IndexQueryBuilder().withIndexName(INDEX_1_NAME)
257+
.withId(sampleEntity.getId())
258+
.withObject(sampleEntity).build();
259+
260+
elasticsearchTemplate.index(idxQuery);
261+
elasticsearchTemplate.refresh(INDEX_1_NAME);
262+
263+
// when
264+
SearchQuery searchQuery = new NativeSearchQueryBuilder()
265+
.withQuery(matchAllQuery())
266+
.withIndices(INDEX_1_NAME, INDEX_2_NAME)
267+
.withIndicesOptions(IndicesOptions.lenientExpandOpen())
268+
.build();
269+
Page<SampleEntity> entities = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class);
270+
// then
271+
assertThat(entities, is(notNullValue()));
272+
assertThat(entities.getTotalElements(), greaterThanOrEqualTo(1L));
273+
}
274+
246275
@Test
247276
public void shouldDoBulkIndex() {
248277
// given

0 commit comments

Comments
 (0)