|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.search.query; |
| 21 | + |
| 22 | +import org.apache.lucene.search.ScoreDoc; |
| 23 | +import org.apache.lucene.search.TopDocs; |
| 24 | +import org.apache.lucene.search.TotalHits; |
| 25 | +import org.elasticsearch.Version; |
| 26 | +import org.elasticsearch.action.OriginalIndices; |
| 27 | +import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput; |
| 28 | +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; |
| 29 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 30 | +import org.elasticsearch.common.lucene.search.TopDocsAndMaxScore; |
| 31 | +import org.elasticsearch.common.settings.Settings; |
| 32 | +import org.elasticsearch.index.shard.ShardId; |
| 33 | +import org.elasticsearch.search.DocValueFormat; |
| 34 | +import org.elasticsearch.search.SearchModule; |
| 35 | +import org.elasticsearch.search.SearchShardTarget; |
| 36 | +import org.elasticsearch.search.aggregations.Aggregations; |
| 37 | +import org.elasticsearch.search.aggregations.InternalAggregations; |
| 38 | +import org.elasticsearch.search.aggregations.InternalAggregationsTests; |
| 39 | +import org.elasticsearch.search.aggregations.pipeline.SiblingPipelineAggregator; |
| 40 | +import org.elasticsearch.search.suggest.SuggestTests; |
| 41 | +import org.elasticsearch.test.ESTestCase; |
| 42 | +import org.elasticsearch.test.VersionUtils; |
| 43 | + |
| 44 | +import java.io.IOException; |
| 45 | +import java.util.Base64; |
| 46 | +import java.util.List; |
| 47 | + |
| 48 | +import static java.util.Collections.emptyList; |
| 49 | + |
| 50 | +public class QuerySearchResultTests extends ESTestCase { |
| 51 | + |
| 52 | + private final NamedWriteableRegistry namedWriteableRegistry; |
| 53 | + |
| 54 | + public QuerySearchResultTests() { |
| 55 | + SearchModule searchModule = new SearchModule(Settings.EMPTY, false, emptyList()); |
| 56 | + this.namedWriteableRegistry = new NamedWriteableRegistry(searchModule.getNamedWriteables()); |
| 57 | + } |
| 58 | + |
| 59 | + private static QuerySearchResult createTestInstance() throws Exception { |
| 60 | + ShardId shardId = new ShardId("index", "uuid", randomInt()); |
| 61 | + QuerySearchResult result = new QuerySearchResult(randomLong(), new SearchShardTarget("node", shardId, null, OriginalIndices.NONE)); |
| 62 | + if (randomBoolean()) { |
| 63 | + result.terminatedEarly(randomBoolean()); |
| 64 | + } |
| 65 | + TopDocs topDocs = new TopDocs(new TotalHits(randomLongBetween(0, Long.MAX_VALUE), TotalHits.Relation.EQUAL_TO), new ScoreDoc[0]); |
| 66 | + result.topDocs(new TopDocsAndMaxScore(topDocs, randomBoolean() ? Float.NaN : randomFloat()), new DocValueFormat[0]); |
| 67 | + result.size(randomInt()); |
| 68 | + result.from(randomInt()); |
| 69 | + if (randomBoolean()) { |
| 70 | + result.suggest(SuggestTests.createTestItem()); |
| 71 | + } |
| 72 | + if (randomBoolean()) { |
| 73 | + result.aggregations(InternalAggregationsTests.createTestInstance()); |
| 74 | + } |
| 75 | + return result; |
| 76 | + } |
| 77 | + |
| 78 | + public void testSerialization() throws Exception { |
| 79 | + QuerySearchResult querySearchResult = createTestInstance(); |
| 80 | + Version version = VersionUtils.randomVersion(random()); |
| 81 | + QuerySearchResult deserialized = copyStreamable(querySearchResult, namedWriteableRegistry, QuerySearchResult::new, version); |
| 82 | + assertEquals(querySearchResult.getRequestId(), deserialized.getRequestId()); |
| 83 | + assertNull(deserialized.getSearchShardTarget()); |
| 84 | + assertEquals(querySearchResult.topDocs().maxScore, deserialized.topDocs().maxScore, 0f); |
| 85 | + assertEquals(querySearchResult.topDocs().topDocs.totalHits, deserialized.topDocs().topDocs.totalHits); |
| 86 | + assertEquals(querySearchResult.from(), deserialized.from()); |
| 87 | + assertEquals(querySearchResult.size(), deserialized.size()); |
| 88 | + assertEquals(querySearchResult.hasAggs(), deserialized.hasAggs()); |
| 89 | + if (deserialized.hasAggs()) { |
| 90 | + Aggregations aggs = querySearchResult.consumeAggs(); |
| 91 | + Aggregations deserializedAggs = deserialized.consumeAggs(); |
| 92 | + assertEquals(aggs.asList(), deserializedAggs.asList()); |
| 93 | + List<SiblingPipelineAggregator> pipelineAggs = ((InternalAggregations) aggs).getTopLevelPipelineAggregators(); |
| 94 | + List<SiblingPipelineAggregator> deserializedPipelineAggs = |
| 95 | + ((InternalAggregations) deserializedAggs).getTopLevelPipelineAggregators(); |
| 96 | + assertEquals(pipelineAggs.size(), deserializedPipelineAggs.size()); |
| 97 | + for (int i = 0; i < pipelineAggs.size(); i++) { |
| 98 | + SiblingPipelineAggregator pipelineAgg = pipelineAggs.get(i); |
| 99 | + SiblingPipelineAggregator deserializedPipelineAgg = deserializedPipelineAggs.get(i); |
| 100 | + assertArrayEquals(pipelineAgg.bucketsPaths(), deserializedPipelineAgg.bucketsPaths()); |
| 101 | + assertEquals(pipelineAgg.name(), deserializedPipelineAgg.name()); |
| 102 | + } |
| 103 | + } |
| 104 | + assertEquals(querySearchResult.terminatedEarly(), deserialized.terminatedEarly()); |
| 105 | + } |
| 106 | + |
| 107 | + public void testReadFromPre_7_1_0() throws IOException { |
| 108 | + String message = "AAAAAAAAAGQAAAEAAAB/wAAAAAEBBnN0ZXJtcwVJblhNRgoDBVNhdWpvAAVrS3l3cwVHSVVZaAAFZXRUbEUFZGN0WVoABXhzYnVrAAEDAfoN" + |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+ |
| 117 | + "d3BwQlpBZEhpMQAAAAAAAAN9AAAKREZ3UVpTSXhDSE4AAAAAAAADfQAAClVMZW1YZGtkSHUUAAAAAAAAA30AAApBUVdKVk1kTlF1BnN0ZXJtcwVxbkJGVgoAA" + |
| 118 | + "AEDAfoNA3JhdwUBAAYJAAAAAAAAA30AAApBS2NLU1ZVS25EIQAAAAAAAAN9AAAKWGpCbXZBZmduRhsAAAAAAAADfQAACk54TkJEV3pLRmI7AAAAAAAAA30AAA" + |
| 119 | + "pydkdaZnJycXhWSAAAAAAAAAN9AAAKSURVZ3JhQlFHSy4AAAAAAAADfQAACmJmZ0x5YlFlVksACm5rdExLUHp3cGgBCm1heF9idWNrZXQFbmFtZTEBB2J1Y2t" + |
| 120 | + "ldDH/A3JhdwEBCm1heF9idWNrZXQFbmFtZTEBB2J1Y2tldDH/A3JhdwEAAAIAAf////8AAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; |
| 121 | + byte[] bytes = Base64.getDecoder().decode(message); |
| 122 | + try (NamedWriteableAwareStreamInput in = new NamedWriteableAwareStreamInput(StreamInput.wrap(bytes), namedWriteableRegistry)) { |
| 123 | + in.setVersion(Version.V_7_0_0); |
| 124 | + QuerySearchResult querySearchResult = new QuerySearchResult(); |
| 125 | + querySearchResult.readFrom(in); |
| 126 | + assertEquals(100, querySearchResult.getRequestId()); |
| 127 | + assertTrue(querySearchResult.hasAggs()); |
| 128 | + InternalAggregations aggs = (InternalAggregations)querySearchResult.consumeAggs(); |
| 129 | + assertEquals(1, aggs.asList().size()); |
| 130 | + //top-level pipeline aggs are retrieved as part of InternalAggregations although they were serialized separately |
| 131 | + assertEquals(1, aggs.getTopLevelPipelineAggregators().size()); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments