mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-22 03:52:10 +00:00
DATAES-422 - Add support for IndicesOptions in search queries.
See also: DATAES-191..
This commit is contained in:
parent
92c3bbebed
commit
de1afe8bb0
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2017 the original author or authors.
|
||||
* Copyright 2013-2018 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.
|
||||
@ -108,6 +108,7 @@ import org.springframework.util.Assert;
|
||||
* @author Mark Janssen
|
||||
* @author Mark Paluch
|
||||
* @author Ilkang Na
|
||||
* @author Alen Turkovic
|
||||
*/
|
||||
public class ElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware {
|
||||
|
||||
@ -995,6 +996,10 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
||||
searchRequestBuilder.setFetchSource(toArray(query.getFields()), null);
|
||||
}
|
||||
|
||||
if (query.getIndicesOptions() != null) {
|
||||
searchRequestBuilder.setIndicesOptions(query.getIndicesOptions());
|
||||
}
|
||||
|
||||
if (query.getSort() != null) {
|
||||
for (Sort.Order order : query.getSort()) {
|
||||
searchRequestBuilder.addSort(order.getProperty(),
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2017 the original author or authors.
|
||||
* Copyright 2013-2018 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.
|
||||
@ -20,6 +20,7 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import org.elasticsearch.action.search.SearchType;
|
||||
import org.elasticsearch.action.support.IndicesOptions;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.util.Assert;
|
||||
@ -30,6 +31,7 @@ import org.springframework.util.Assert;
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
* @author Mark Paluch
|
||||
* @author Alen Turkovic
|
||||
*/
|
||||
abstract class AbstractQuery implements Query {
|
||||
|
||||
@ -43,6 +45,7 @@ abstract class AbstractQuery implements Query {
|
||||
protected Collection<String> ids;
|
||||
protected String route;
|
||||
protected SearchType searchType = SearchType.DFS_QUERY_THEN_FETCH;
|
||||
protected IndicesOptions indicesOptions;
|
||||
|
||||
@Override
|
||||
public Sort getSort() {
|
||||
@ -149,4 +152,12 @@ abstract class AbstractQuery implements Query {
|
||||
public SearchType getSearchType() {
|
||||
return searchType;
|
||||
}
|
||||
|
||||
public IndicesOptions getIndicesOptions() {
|
||||
return indicesOptions;
|
||||
}
|
||||
|
||||
public void setIndicesOptions(IndicesOptions indicesOptions) {
|
||||
this.indicesOptions = indicesOptions;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2017 the original author or authors.
|
||||
* Copyright 2013-2018 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.
|
||||
@ -20,6 +20,7 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import org.elasticsearch.action.search.SearchType;
|
||||
import org.elasticsearch.action.support.IndicesOptions;
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
|
||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
||||
@ -34,6 +35,7 @@ import org.springframework.data.elasticsearch.core.facet.FacetRequest;
|
||||
* @author Mohsin Husen
|
||||
* @author Artur Konczak
|
||||
* @author Mark Paluch
|
||||
* @author Alen Turkovic
|
||||
*/
|
||||
public class NativeSearchQueryBuilder {
|
||||
|
||||
@ -54,6 +56,7 @@ public class NativeSearchQueryBuilder {
|
||||
private Collection<String> ids;
|
||||
private String route;
|
||||
private SearchType searchType;
|
||||
private IndicesOptions indicesOptions;
|
||||
|
||||
public NativeSearchQueryBuilder withQuery(QueryBuilder queryBuilder) {
|
||||
this.queryBuilder = queryBuilder;
|
||||
@ -140,6 +143,11 @@ public class NativeSearchQueryBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
public NativeSearchQueryBuilder withIndicesOptions(IndicesOptions indicesOptions) {
|
||||
this.indicesOptions = indicesOptions;
|
||||
return this;
|
||||
}
|
||||
|
||||
public NativeSearchQuery build() {
|
||||
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(queryBuilder, filterBuilder, sortBuilders, highlightFields);
|
||||
nativeSearchQuery.setPageable(pageable);
|
||||
@ -192,6 +200,10 @@ public class NativeSearchQueryBuilder {
|
||||
nativeSearchQuery.setSearchType(searchType);
|
||||
}
|
||||
|
||||
if (indicesOptions != null) {
|
||||
nativeSearchQuery.setIndicesOptions(indicesOptions);
|
||||
}
|
||||
|
||||
return nativeSearchQuery;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2017 the original author or authors.
|
||||
* Copyright 2013-2018 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.
|
||||
@ -18,6 +18,7 @@ package org.springframework.data.elasticsearch.core.query;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import org.elasticsearch.action.search.SearchType;
|
||||
import org.elasticsearch.action.support.IndicesOptions;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
@ -28,6 +29,7 @@ import org.springframework.data.domain.Sort;
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
* @author Mark Paluch
|
||||
* @author Alen Turkovic
|
||||
*/
|
||||
public interface Query {
|
||||
|
||||
@ -147,4 +149,11 @@ public interface Query {
|
||||
* @return
|
||||
*/
|
||||
SearchType getSearchType();
|
||||
|
||||
/**
|
||||
* Get indices options
|
||||
*
|
||||
* @return null if not set
|
||||
*/
|
||||
IndicesOptions getIndicesOptions();
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2017 the original author or authors.
|
||||
* Copyright 2014-2018 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.
|
||||
@ -28,6 +28,7 @@ import org.elasticsearch.action.get.MultiGetItemResponse;
|
||||
import org.elasticsearch.action.get.MultiGetResponse;
|
||||
import org.elasticsearch.action.index.IndexRequest;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.action.support.IndicesOptions;
|
||||
import org.elasticsearch.index.engine.DocumentMissingException;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptType;
|
||||
@ -71,6 +72,7 @@ import static org.springframework.data.elasticsearch.utils.IndexBuilder.*;
|
||||
* @author Kevin Leturc
|
||||
* @author Mason Chan
|
||||
* @author Ilkang Na
|
||||
* @author Alen Turkovic
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
|
||||
@ -243,6 +245,33 @@ public class ElasticsearchTemplateTests {
|
||||
assertThat(sampleEntities.getTotalElements(), greaterThanOrEqualTo(1L));
|
||||
}
|
||||
|
||||
// DATAES-422 - Add support for IndicesOptions in search queries
|
||||
@Test
|
||||
public void shouldPassIndicesOptionsForGivenSearchQuery() {
|
||||
// given
|
||||
String documentId = randomNumeric(5);
|
||||
SampleEntity sampleEntity = SampleEntity.builder().id(documentId).message("some message")
|
||||
.version(System.currentTimeMillis()).build();
|
||||
|
||||
IndexQuery idxQuery = new IndexQueryBuilder().withIndexName(INDEX_1_NAME)
|
||||
.withId(sampleEntity.getId())
|
||||
.withObject(sampleEntity).build();
|
||||
|
||||
elasticsearchTemplate.index(idxQuery);
|
||||
elasticsearchTemplate.refresh(INDEX_1_NAME);
|
||||
|
||||
// when
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||
.withQuery(matchAllQuery())
|
||||
.withIndices(INDEX_1_NAME, INDEX_2_NAME)
|
||||
.withIndicesOptions(IndicesOptions.lenientExpandOpen())
|
||||
.build();
|
||||
Page<SampleEntity> entities = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class);
|
||||
// then
|
||||
assertThat(entities, is(notNullValue()));
|
||||
assertThat(entities.getTotalElements(), greaterThanOrEqualTo(1L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDoBulkIndex() {
|
||||
// given
|
||||
|
Loading…
x
Reference in New Issue
Block a user