|
| 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.NamedWriteableRegistry; |
| 28 | +import org.elasticsearch.common.lucene.search.TopDocsAndMaxScore; |
| 29 | +import org.elasticsearch.common.settings.Settings; |
| 30 | +import org.elasticsearch.index.shard.ShardId; |
| 31 | +import org.elasticsearch.search.DocValueFormat; |
| 32 | +import org.elasticsearch.search.SearchModule; |
| 33 | +import org.elasticsearch.search.SearchShardTarget; |
| 34 | +import org.elasticsearch.search.aggregations.Aggregations; |
| 35 | +import org.elasticsearch.search.aggregations.InternalAggregations; |
| 36 | +import org.elasticsearch.search.aggregations.InternalAggregationsTests; |
| 37 | +import org.elasticsearch.search.aggregations.pipeline.SiblingPipelineAggregator; |
| 38 | +import org.elasticsearch.search.suggest.SuggestTests; |
| 39 | +import org.elasticsearch.test.ESTestCase; |
| 40 | +import org.elasticsearch.test.VersionUtils; |
| 41 | + |
| 42 | +import java.util.List; |
| 43 | + |
| 44 | +import static java.util.Collections.emptyList; |
| 45 | + |
| 46 | +public class QuerySearchResultTests extends ESTestCase { |
| 47 | + |
| 48 | + private final NamedWriteableRegistry namedWriteableRegistry; |
| 49 | + |
| 50 | + public QuerySearchResultTests() { |
| 51 | + SearchModule searchModule = new SearchModule(Settings.EMPTY, false, emptyList()); |
| 52 | + this.namedWriteableRegistry = new NamedWriteableRegistry(searchModule.getNamedWriteables()); |
| 53 | + } |
| 54 | + |
| 55 | + private static QuerySearchResult createTestInstance() throws Exception { |
| 56 | + ShardId shardId = new ShardId("index", "uuid", randomInt()); |
| 57 | + QuerySearchResult result = new QuerySearchResult(randomLong(), new SearchShardTarget("node", shardId, null, OriginalIndices.NONE)); |
| 58 | + if (randomBoolean()) { |
| 59 | + result.terminatedEarly(randomBoolean()); |
| 60 | + } |
| 61 | + TopDocs topDocs = new TopDocs(new TotalHits(randomLongBetween(0, Long.MAX_VALUE), TotalHits.Relation.EQUAL_TO), new ScoreDoc[0]); |
| 62 | + result.topDocs(new TopDocsAndMaxScore(topDocs, randomBoolean() ? Float.NaN : randomFloat()), new DocValueFormat[0]); |
| 63 | + result.size(randomInt()); |
| 64 | + result.from(randomInt()); |
| 65 | + if (randomBoolean()) { |
| 66 | + result.suggest(SuggestTests.createTestItem()); |
| 67 | + } |
| 68 | + if (randomBoolean()) { |
| 69 | + result.aggregations(InternalAggregationsTests.createTestInstance()); |
| 70 | + } |
| 71 | + return result; |
| 72 | + } |
| 73 | + |
| 74 | + public void testSerialization() throws Exception { |
| 75 | + QuerySearchResult querySearchResult = createTestInstance(); |
| 76 | + Version version = VersionUtils.randomVersion(random()); |
| 77 | + QuerySearchResult deserialized = copyStreamable(querySearchResult, namedWriteableRegistry, QuerySearchResult::new, version); |
| 78 | + assertEquals(querySearchResult.getRequestId(), deserialized.getRequestId()); |
| 79 | + assertNull(deserialized.getSearchShardTarget()); |
| 80 | + assertEquals(querySearchResult.topDocs().maxScore, deserialized.topDocs().maxScore, 0f); |
| 81 | + assertEquals(querySearchResult.topDocs().topDocs.totalHits, deserialized.topDocs().topDocs.totalHits); |
| 82 | + assertEquals(querySearchResult.from(), deserialized.from()); |
| 83 | + assertEquals(querySearchResult.size(), deserialized.size()); |
| 84 | + assertEquals(querySearchResult.hasAggs(), deserialized.hasAggs()); |
| 85 | + if (deserialized.hasAggs()) { |
| 86 | + Aggregations aggs = querySearchResult.consumeAggs(); |
| 87 | + Aggregations deserializedAggs = deserialized.consumeAggs(); |
| 88 | + assertEquals(aggs.asList(), deserializedAggs.asList()); |
| 89 | + List<SiblingPipelineAggregator> pipelineAggs = ((InternalAggregations) aggs).getTopLevelPipelineAggregators(); |
| 90 | + List<SiblingPipelineAggregator> deserializedPipelineAggs = |
| 91 | + ((InternalAggregations) deserializedAggs).getTopLevelPipelineAggregators(); |
| 92 | + assertEquals(pipelineAggs.size(), deserializedPipelineAggs.size()); |
| 93 | + for (int i = 0; i < pipelineAggs.size(); i++) { |
| 94 | + SiblingPipelineAggregator pipelineAgg = pipelineAggs.get(i); |
| 95 | + SiblingPipelineAggregator deserializedPipelineAgg = deserializedPipelineAggs.get(i); |
| 96 | + assertArrayEquals(pipelineAgg.bucketsPaths(), deserializedPipelineAgg.bucketsPaths()); |
| 97 | + assertEquals(pipelineAgg.name(), deserializedPipelineAgg.name()); |
| 98 | + } |
| 99 | + } |
| 100 | + assertEquals(querySearchResult.terminatedEarly(), deserialized.terminatedEarly()); |
| 101 | + } |
| 102 | +} |
0 commit comments