Apply missing request options to the expand phase (#27118)

* Apply missing request options to the expand phase

This change adds some missing options to the expand query that builds the inner hits for field collapsing.
The following options are now applied to the inner_hits query:
 * post_filters
 * preferences
 * routing

Closes #27079
Closes #26649
This commit is contained in:
Jim Ferenczi 2017-10-26 17:01:57 +02:00 committed by GitHub
parent 1460a3feac
commit d1acf449f5
2 changed files with 58 additions and 4 deletions

View File

@ -88,10 +88,9 @@ final class ExpandSearchPhase extends SearchPhase {
} }
for (InnerHitBuilder innerHitBuilder : innerHitBuilders) { for (InnerHitBuilder innerHitBuilder : innerHitBuilders) {
SearchSourceBuilder sourceBuilder = buildExpandSearchSourceBuilder(innerHitBuilder) SearchSourceBuilder sourceBuilder = buildExpandSearchSourceBuilder(innerHitBuilder)
.query(groupQuery); .query(groupQuery)
SearchRequest groupRequest = new SearchRequest(searchRequest.indices()) .postFilter(searchRequest.source().postFilter());
.types(searchRequest.types()) SearchRequest groupRequest = buildExpandSearchRequest(searchRequest, sourceBuilder);
.source(sourceBuilder);
multiRequest.add(groupRequest); multiRequest.add(groupRequest);
} }
} }
@ -120,6 +119,21 @@ final class ExpandSearchPhase extends SearchPhase {
} }
} }
private SearchRequest buildExpandSearchRequest(SearchRequest orig, SearchSourceBuilder sourceBuilder) {
SearchRequest groupRequest = new SearchRequest(orig.indices())
.types(orig.types())
.source(sourceBuilder)
.indicesOptions(orig.indicesOptions())
.requestCache(orig.requestCache())
.preference(orig.preference())
.routing(orig.routing())
.searchType(orig.searchType());
if (orig.isMaxConcurrentShardRequestsSet()) {
groupRequest.setMaxConcurrentShardRequests(orig.getMaxConcurrentShardRequests());
}
return groupRequest;
}
private SearchSourceBuilder buildExpandSearchSourceBuilder(InnerHitBuilder options) { private SearchSourceBuilder buildExpandSearchSourceBuilder(InnerHitBuilder options) {
SearchSourceBuilder groupSource = new SearchSourceBuilder(); SearchSourceBuilder groupSource = new SearchSourceBuilder();
groupSource.from(options.getFrom()); groupSource.from(options.getFrom());

View File

@ -20,6 +20,7 @@
package org.elasticsearch.action.search; package org.elasticsearch.action.search;
import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.common.document.DocumentField; import org.elasticsearch.common.document.DocumentField;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.text.Text; import org.elasticsearch.common.text.Text;
@ -242,4 +243,43 @@ public class ExpandSearchPhaseTests extends ESTestCase {
assertNotNull(reference.get()); assertNotNull(reference.get());
assertEquals(1, mockSearchPhaseContext.phasesExecuted.get()); assertEquals(1, mockSearchPhaseContext.phasesExecuted.get());
} }
public void testExpandRequestOptions() throws IOException {
MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(1);
mockSearchPhaseContext.searchTransport = new SearchTransportService(
Settings.builder().put("search.remote.connect", false).build(), null, null) {
@Override
void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionListener<MultiSearchResponse> listener) {
final QueryBuilder postFilter = QueryBuilders.existsQuery("foo");
assertTrue(request.requests().stream().allMatch((r) -> "foo".equals(r.preference())));
assertTrue(request.requests().stream().allMatch((r) -> "baz".equals(r.routing())));
assertTrue(request.requests().stream().allMatch((r) -> postFilter.equals(r.source().postFilter())));
}
};
mockSearchPhaseContext.getRequest().source(new SearchSourceBuilder()
.collapse(
new CollapseBuilder("someField")
.setInnerHits(new InnerHitBuilder().setName("foobarbaz"))
)
.postFilter(QueryBuilders.existsQuery("foo")))
.preference("foobar")
.routing("baz");
SearchHits hits = new SearchHits(new SearchHit[0], 1, 1.0f);
InternalSearchResponse internalSearchResponse = new InternalSearchResponse(hits, null, null, null, false, null, 1);
AtomicReference<SearchResponse> reference = new AtomicReference<>();
ExpandSearchPhase phase = new ExpandSearchPhase(mockSearchPhaseContext, internalSearchResponse, r ->
new SearchPhase("test") {
@Override
public void run() throws IOException {
reference.set(mockSearchPhaseContext.buildSearchResponse(r, null));
}
}
);
phase.run();
mockSearchPhaseContext.assertNoFailure();
assertNotNull(reference.get());
assertEquals(1, mockSearchPhaseContext.phasesExecuted.get());
}
} }