|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.search; |
| 11 | + |
| 12 | +import org.elasticsearch.action.admin.indices.readonly.AddIndexBlockRequest; |
| 13 | +import org.elasticsearch.action.admin.indices.readonly.TransportAddIndexBlockAction; |
| 14 | +import org.elasticsearch.action.search.ClosePointInTimeRequest; |
| 15 | +import org.elasticsearch.action.search.OpenPointInTimeRequest; |
| 16 | +import org.elasticsearch.action.search.SearchRequest; |
| 17 | +import org.elasticsearch.action.search.SearchResponse; |
| 18 | +import org.elasticsearch.action.search.TransportClosePointInTimeAction; |
| 19 | +import org.elasticsearch.action.search.TransportOpenPointInTimeAction; |
| 20 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 21 | +import org.elasticsearch.common.bytes.BytesReference; |
| 22 | +import org.elasticsearch.core.TimeValue; |
| 23 | +import org.elasticsearch.index.query.QueryBuilders; |
| 24 | +import org.elasticsearch.search.builder.PointInTimeBuilder; |
| 25 | +import org.elasticsearch.search.builder.SearchSourceBuilder; |
| 26 | +import org.elasticsearch.test.ESIntegTestCase; |
| 27 | + |
| 28 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; |
| 29 | + |
| 30 | +public class SearchWithIndexBlocksIT extends ESIntegTestCase { |
| 31 | + |
| 32 | + public void testSearchIndexWithIndexRefreshBlock() { |
| 33 | + createIndex("test"); |
| 34 | + |
| 35 | + var addIndexBlockRequest = new AddIndexBlockRequest(IndexMetadata.APIBlock.REFRESH, "test"); |
| 36 | + client().execute(TransportAddIndexBlockAction.TYPE, addIndexBlockRequest).actionGet(); |
| 37 | + |
| 38 | + indexRandom( |
| 39 | + true, |
| 40 | + prepareIndex("test").setId("1").setSource("field", "value"), |
| 41 | + prepareIndex("test").setId("2").setSource("field", "value"), |
| 42 | + prepareIndex("test").setId("3").setSource("field", "value"), |
| 43 | + prepareIndex("test").setId("4").setSource("field", "value"), |
| 44 | + prepareIndex("test").setId("5").setSource("field", "value"), |
| 45 | + prepareIndex("test").setId("6").setSource("field", "value") |
| 46 | + ); |
| 47 | + |
| 48 | + assertHitCount(prepareSearch().setQuery(QueryBuilders.matchAllQuery()), 0); |
| 49 | + } |
| 50 | + |
| 51 | + public void testSearchMultipleIndicesEachWithAnIndexRefreshBlock() { |
| 52 | + createIndex("test"); |
| 53 | + createIndex("test2"); |
| 54 | + |
| 55 | + var addIndexBlockRequest = new AddIndexBlockRequest(IndexMetadata.APIBlock.REFRESH, "test", "test2"); |
| 56 | + client().execute(TransportAddIndexBlockAction.TYPE, addIndexBlockRequest).actionGet(); |
| 57 | + |
| 58 | + indexRandom( |
| 59 | + true, |
| 60 | + prepareIndex("test").setId("1").setSource("field", "value"), |
| 61 | + prepareIndex("test").setId("2").setSource("field", "value"), |
| 62 | + prepareIndex("test").setId("3").setSource("field", "value"), |
| 63 | + prepareIndex("test").setId("4").setSource("field", "value"), |
| 64 | + prepareIndex("test").setId("5").setSource("field", "value"), |
| 65 | + prepareIndex("test").setId("6").setSource("field", "value"), |
| 66 | + prepareIndex("test2").setId("1").setSource("field", "value"), |
| 67 | + prepareIndex("test2").setId("2").setSource("field", "value"), |
| 68 | + prepareIndex("test2").setId("3").setSource("field", "value"), |
| 69 | + prepareIndex("test2").setId("4").setSource("field", "value"), |
| 70 | + prepareIndex("test2").setId("5").setSource("field", "value"), |
| 71 | + prepareIndex("test2").setId("6").setSource("field", "value") |
| 72 | + ); |
| 73 | + |
| 74 | + assertHitCount(prepareSearch().setQuery(QueryBuilders.matchAllQuery()), 0); |
| 75 | + } |
| 76 | + |
| 77 | + public void testSearchMultipleIndicesWithOneIndexRefreshBlock() { |
| 78 | + createIndex("test"); |
| 79 | + createIndex("test2"); |
| 80 | + |
| 81 | + // Only block test |
| 82 | + var addIndexBlockRequest = new AddIndexBlockRequest(IndexMetadata.APIBlock.REFRESH, "test"); |
| 83 | + client().execute(TransportAddIndexBlockAction.TYPE, addIndexBlockRequest).actionGet(); |
| 84 | + |
| 85 | + indexRandom( |
| 86 | + true, |
| 87 | + prepareIndex("test").setId("1").setSource("field", "value"), |
| 88 | + prepareIndex("test").setId("2").setSource("field", "value"), |
| 89 | + prepareIndex("test").setId("3").setSource("field", "value"), |
| 90 | + prepareIndex("test").setId("4").setSource("field", "value"), |
| 91 | + prepareIndex("test").setId("5").setSource("field", "value"), |
| 92 | + prepareIndex("test").setId("6").setSource("field", "value"), |
| 93 | + prepareIndex("test2").setId("1").setSource("field", "value"), |
| 94 | + prepareIndex("test2").setId("2").setSource("field", "value"), |
| 95 | + prepareIndex("test2").setId("3").setSource("field", "value"), |
| 96 | + prepareIndex("test2").setId("4").setSource("field", "value"), |
| 97 | + prepareIndex("test2").setId("5").setSource("field", "value"), |
| 98 | + prepareIndex("test2").setId("6").setSource("field", "value") |
| 99 | + ); |
| 100 | + |
| 101 | + // We should get test2 results (not blocked) |
| 102 | + assertHitCount(prepareSearch().setQuery(QueryBuilders.matchAllQuery()), 6); |
| 103 | + } |
| 104 | + |
| 105 | + public void testOpenPITWithIndexRefreshBlock() { |
| 106 | + createIndex("test"); |
| 107 | + |
| 108 | + var addIndexBlockRequest = new AddIndexBlockRequest(IndexMetadata.APIBlock.REFRESH, "test"); |
| 109 | + client().execute(TransportAddIndexBlockAction.TYPE, addIndexBlockRequest).actionGet(); |
| 110 | + |
| 111 | + indexRandom( |
| 112 | + true, |
| 113 | + prepareIndex("test").setId("1").setSource("field", "value"), |
| 114 | + prepareIndex("test").setId("2").setSource("field", "value"), |
| 115 | + prepareIndex("test").setId("3").setSource("field", "value"), |
| 116 | + prepareIndex("test").setId("4").setSource("field", "value"), |
| 117 | + prepareIndex("test").setId("5").setSource("field", "value"), |
| 118 | + prepareIndex("test").setId("6").setSource("field", "value") |
| 119 | + ); |
| 120 | + |
| 121 | + BytesReference pitId = null; |
| 122 | + try { |
| 123 | + OpenPointInTimeRequest openPITRequest = new OpenPointInTimeRequest("test").keepAlive(TimeValue.timeValueSeconds(10)) |
| 124 | + .allowPartialSearchResults(true); |
| 125 | + pitId = client().execute(TransportOpenPointInTimeAction.TYPE, openPITRequest).actionGet().getPointInTimeId(); |
| 126 | + SearchRequest searchRequest = new SearchRequest().source( |
| 127 | + new SearchSourceBuilder().pointInTimeBuilder(new PointInTimeBuilder(pitId).setKeepAlive(TimeValue.timeValueSeconds(10))) |
| 128 | + ); |
| 129 | + SearchResponse searchResponse = client().search(searchRequest).actionGet(); |
| 130 | + assertHitCount(searchResponse, 0); |
| 131 | + } finally { |
| 132 | + if (pitId != null) { |
| 133 | + client().execute(TransportClosePointInTimeAction.TYPE, new ClosePointInTimeRequest(pitId)).actionGet(); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments