From f5e0932c8ddc2cff808b60f04fca46de601f6260 Mon Sep 17 00:00:00 2001 From: kel Date: Sat, 16 Dec 2017 01:00:40 +0800 Subject: [PATCH] Add version support for inner hits in field collapsing (#27822) (#27833) Add version support for inner hits in field collapsing --- .../action/search/ExpandSearchPhase.java | 1 + .../action/search/ExpandSearchPhaseTests.java | 6 +- .../test/search/110_field_collapsing.yml | 65 +++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/action/search/ExpandSearchPhase.java b/core/src/main/java/org/elasticsearch/action/search/ExpandSearchPhase.java index 53ce4299c54..35df6212833 100644 --- a/core/src/main/java/org/elasticsearch/action/search/ExpandSearchPhase.java +++ b/core/src/main/java/org/elasticsearch/action/search/ExpandSearchPhase.java @@ -165,6 +165,7 @@ final class ExpandSearchPhase extends SearchPhase { } groupSource.explain(options.isExplain()); groupSource.trackScores(options.isTrackScores()); + groupSource.version(options.isVersion()); return groupSource; } } diff --git a/core/src/test/java/org/elasticsearch/action/search/ExpandSearchPhaseTests.java b/core/src/test/java/org/elasticsearch/action/search/ExpandSearchPhaseTests.java index 0951380fcf4..b580d48c11a 100644 --- a/core/src/test/java/org/elasticsearch/action/search/ExpandSearchPhaseTests.java +++ b/core/src/test/java/org/elasticsearch/action/search/ExpandSearchPhaseTests.java @@ -20,7 +20,6 @@ package org.elasticsearch.action.search; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.common.document.DocumentField; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.text.Text; @@ -248,6 +247,8 @@ public class ExpandSearchPhaseTests extends ESTestCase { public void testExpandRequestOptions() throws IOException { MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(1); + boolean version = randomBoolean(); + mockSearchPhaseContext.searchTransport = new SearchTransportService( Settings.builder().put("search.remote.connect", false).build(), null, null) { @@ -256,13 +257,14 @@ public class ExpandSearchPhaseTests extends ESTestCase { 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) -> version == r.source().version())); 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")) + .setInnerHits(new InnerHitBuilder().setName("foobarbaz").setVersion(version)) ) .postFilter(QueryBuilders.existsQuery("foo"))) .preference("foobar") diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/search/110_field_collapsing.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/search/110_field_collapsing.yml index fa012ff3acb..48ca92c1ee9 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/search/110_field_collapsing.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/search/110_field_collapsing.yml @@ -7,36 +7,48 @@ setup: index: test type: test id: 1 + version_type: external + version: 11 body: { numeric_group: 1, sort: 10 } - do: index: index: test type: test id: 2 + version_type: external + version: 22 body: { numeric_group: 1, sort: 6 } - do: index: index: test type: test id: 3 + version_type: external + version: 33 body: { numeric_group: 1, sort: 24 } - do: index: index: test type: test id: 4 + version_type: external + version: 44 body: { numeric_group: 25, sort: 10 } - do: index: index: test type: test id: 5 + version_type: external + version: 55 body: { numeric_group: 25, sort: 5 } - do: index: index: test type: test id: 6 + version_type: external + version: 66 body: { numeric_group: 3, sort: 36 } - do: indices.refresh: @@ -322,3 +334,56 @@ setup: - match: { hits.hits.2.inner_hits.sub_hits_desc.hits.total: 2 } - length: { hits.hits.2.inner_hits.sub_hits_desc.hits.hits: 1 } - match: { hits.hits.2.inner_hits.sub_hits_desc.hits.hits.0._id: "4" } + +--- +"field collapsing, inner_hits and version": + + - skip: + version: " - 6.99.99" + reason: "bug fixed in 7.0.0" + + - do: + search: + index: test + type: test + body: + collapse: { field: numeric_group, inner_hits: { name: sub_hits, version: true, size: 2, sort: [{ sort: asc }] } } + sort: [{ sort: desc }] + version: true + + - match: { hits.total: 6 } + - length: { hits.hits: 3 } + - match: { hits.hits.0._index: test } + - match: { hits.hits.0._type: test } + - match: { hits.hits.0.fields.numeric_group: [3] } + - match: { hits.hits.0.sort: [36] } + - match: { hits.hits.0._id: "6" } + - match: { hits.hits.0._version: 66 } + - match: { hits.hits.0.inner_hits.sub_hits.hits.total: 1 } + - length: { hits.hits.0.inner_hits.sub_hits.hits.hits: 1 } + - match: { hits.hits.0.inner_hits.sub_hits.hits.hits.0._id: "6" } + - match: { hits.hits.0.inner_hits.sub_hits.hits.hits.0._version: 66 } + - match: { hits.hits.1._index: test } + - match: { hits.hits.1._type: test } + - match: { hits.hits.1.fields.numeric_group: [1] } + - match: { hits.hits.1.sort: [24] } + - match: { hits.hits.1._id: "3" } + - match: { hits.hits.1._version: 33 } + - match: { hits.hits.1.inner_hits.sub_hits.hits.total: 3 } + - length: { hits.hits.1.inner_hits.sub_hits.hits.hits: 2 } + - match: { hits.hits.1.inner_hits.sub_hits.hits.hits.0._id: "2" } + - match: { hits.hits.1.inner_hits.sub_hits.hits.hits.0._version: 22 } + - match: { hits.hits.1.inner_hits.sub_hits.hits.hits.1._id: "1" } + - match: { hits.hits.1.inner_hits.sub_hits.hits.hits.1._version: 11 } + - match: { hits.hits.2._index: test } + - match: { hits.hits.2._type: test } + - match: { hits.hits.2.fields.numeric_group: [25] } + - match: { hits.hits.2.sort: [10] } + - match: { hits.hits.2._id: "4" } + - match: { hits.hits.2._version: 44 } + - match: { hits.hits.2.inner_hits.sub_hits.hits.total: 2 } + - length: { hits.hits.2.inner_hits.sub_hits.hits.hits: 2 } + - match: { hits.hits.2.inner_hits.sub_hits.hits.hits.0._id: "5" } + - match: { hits.hits.2.inner_hits.sub_hits.hits.hits.0._version: 55 } + - match: { hits.hits.2.inner_hits.sub_hits.hits.hits.1._id: "4" } + - match: { hits.hits.2.inner_hits.sub_hits.hits.hits.1._version: 44 }