|
20 | 20 | import org.opensearch.search.aggregations.AggregatorFactories;
|
21 | 21 | import org.opensearch.search.aggregations.SearchContextAggregations;
|
22 | 22 | import org.opensearch.search.builder.SearchSourceBuilder;
|
| 23 | +import org.opensearch.search.internal.SearchContext; |
23 | 24 | import org.opensearch.search.internal.ShardSearchRequest;
|
24 | 25 | import org.opensearch.search.sort.FieldSortBuilder;
|
25 | 26 | import org.opensearch.search.sort.SortOrder;
|
@@ -105,4 +106,60 @@ public ShardSearchRequest request() {
|
105 | 106 | assertThrows(IllegalStateException.class, () -> approximateMatchAllQuery.rewrite(null));
|
106 | 107 | }
|
107 | 108 |
|
| 109 | + public void testCannotApproximateWithTrackTotalHits() { |
| 110 | + ApproximateMatchAllQuery approximateMatchAllQuery = new ApproximateMatchAllQuery(); |
| 111 | + |
| 112 | + ShardSearchRequest[] shardSearchRequest = new ShardSearchRequest[1]; |
| 113 | + |
| 114 | + MapperService mockMapper = mock(MapperService.class); |
| 115 | + String sortfield = "myfield"; |
| 116 | + MappedFieldType myFieldType = new NumberFieldMapper.NumberFieldType(sortfield, NumberFieldMapper.NumberType.LONG); |
| 117 | + when(mockMapper.fieldType(sortfield)).thenReturn(myFieldType); |
| 118 | + |
| 119 | + Settings settings = Settings.builder() |
| 120 | + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) |
| 121 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) |
| 122 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) |
| 123 | + .build(); |
| 124 | + IndexMetadata indexMetadata = new IndexMetadata.Builder("index").settings(settings).build(); |
| 125 | + QueryShardContext queryShardContext = new QueryShardContext( |
| 126 | + 0, |
| 127 | + new IndexSettings(indexMetadata, settings), |
| 128 | + BigArrays.NON_RECYCLING_INSTANCE, |
| 129 | + null, |
| 130 | + null, |
| 131 | + mockMapper, |
| 132 | + null, |
| 133 | + null, |
| 134 | + null, |
| 135 | + null, |
| 136 | + null, |
| 137 | + null, |
| 138 | + null, |
| 139 | + null, |
| 140 | + null, |
| 141 | + null, |
| 142 | + null |
| 143 | + ); |
| 144 | + TestSearchContext searchContext = new TestSearchContext(queryShardContext) { |
| 145 | + @Override |
| 146 | + public ShardSearchRequest request() { |
| 147 | + return shardSearchRequest[0]; |
| 148 | + } |
| 149 | + }; |
| 150 | + |
| 151 | + SearchSourceBuilder source = new SearchSourceBuilder(); |
| 152 | + shardSearchRequest[0] = new ShardSearchRequest(null, System.currentTimeMillis(), null); |
| 153 | + shardSearchRequest[0].source(source); |
| 154 | + source.sort(sortfield, SortOrder.ASC); |
| 155 | + |
| 156 | + assertTrue(approximateMatchAllQuery.canApproximate(searchContext)); |
| 157 | + |
| 158 | + searchContext.trackTotalHitsUpTo(SearchContext.TRACK_TOTAL_HITS_ACCURATE); |
| 159 | + assertFalse("Should not approximate when track_total_hits is accurate", approximateMatchAllQuery.canApproximate(searchContext)); |
| 160 | + |
| 161 | + searchContext.trackTotalHitsUpTo(SearchContext.DEFAULT_TRACK_TOTAL_HITS_UP_TO); |
| 162 | + assertTrue("Should approximate when track_total_hits is not accurate", approximateMatchAllQuery.canApproximate(searchContext)); |
| 163 | + } |
| 164 | + |
108 | 165 | }
|
0 commit comments