use of org.opensearch.search.Scroll in project OpenSearch by opensearch-project.
the class SearchSliceIT method testInvalidFields.
public void testInvalidFields() throws Exception {
setupIndex(0, 1);
SearchPhaseExecutionException exc = expectThrows(SearchPhaseExecutionException.class, () -> client().prepareSearch("test").setQuery(matchAllQuery()).setScroll(new Scroll(TimeValue.timeValueSeconds(10))).slice(new SliceBuilder("invalid_random_int", 0, 10)).get());
Throwable rootCause = findRootCause(exc);
assertThat(rootCause.getClass(), equalTo(IllegalArgumentException.class));
assertThat(rootCause.getMessage(), startsWith("cannot load numeric doc values"));
exc = expectThrows(SearchPhaseExecutionException.class, () -> client().prepareSearch("test").setQuery(matchAllQuery()).setScroll(new Scroll(TimeValue.timeValueSeconds(10))).slice(new SliceBuilder("invalid_random_kw", 0, 10)).get());
rootCause = findRootCause(exc);
assertThat(rootCause.getClass(), equalTo(IllegalArgumentException.class));
assertThat(rootCause.getMessage(), startsWith("cannot load numeric doc values"));
}
use of org.opensearch.search.Scroll in project OpenSearch by opensearch-project.
the class SearchSliceIT method testSearchSort.
public void testSearchSort() throws Exception {
int numShards = randomIntBetween(1, 7);
int numDocs = randomIntBetween(100, 1000);
setupIndex(numDocs, numShards);
int max = randomIntBetween(2, numShards * 3);
for (String field : new String[] { "_id", "random_int", "static_int" }) {
int fetchSize = randomIntBetween(10, 100);
// test _doc sort
SearchRequestBuilder request = client().prepareSearch("test").setQuery(matchAllQuery()).setScroll(new Scroll(TimeValue.timeValueSeconds(10))).setSize(fetchSize).addSort(SortBuilders.fieldSort("_doc"));
assertSearchSlicesWithScroll(request, field, max, numDocs);
// test numeric sort
request = client().prepareSearch("test").setQuery(matchAllQuery()).setScroll(new Scroll(TimeValue.timeValueSeconds(10))).addSort(SortBuilders.fieldSort("random_int")).setSize(fetchSize);
assertSearchSlicesWithScroll(request, field, max, numDocs);
}
}
use of org.opensearch.search.Scroll in project OpenSearch by opensearch-project.
the class SearchSliceIT method testWithPreferenceAndRoutings.
public void testWithPreferenceAndRoutings() throws Exception {
int numShards = 10;
int totalDocs = randomIntBetween(100, 1000);
setupIndex(totalDocs, numShards);
{
SearchResponse sr = client().prepareSearch("test").setQuery(matchAllQuery()).setPreference("_shards:1,4").setSize(0).get();
int numDocs = (int) sr.getHits().getTotalHits().value;
int max = randomIntBetween(2, numShards * 3);
int fetchSize = randomIntBetween(10, 100);
SearchRequestBuilder request = client().prepareSearch("test").setQuery(matchAllQuery()).setScroll(new Scroll(TimeValue.timeValueSeconds(10))).setSize(fetchSize).setPreference("_shards:1,4").addSort(SortBuilders.fieldSort("_doc"));
assertSearchSlicesWithScroll(request, "_id", max, numDocs);
}
{
SearchResponse sr = client().prepareSearch("test").setQuery(matchAllQuery()).setRouting("foo", "bar").setSize(0).get();
int numDocs = (int) sr.getHits().getTotalHits().value;
int max = randomIntBetween(2, numShards * 3);
int fetchSize = randomIntBetween(10, 100);
SearchRequestBuilder request = client().prepareSearch("test").setQuery(matchAllQuery()).setScroll(new Scroll(TimeValue.timeValueSeconds(10))).setSize(fetchSize).setRouting("foo", "bar").addSort(SortBuilders.fieldSort("_doc"));
assertSearchSlicesWithScroll(request, "_id", max, numDocs);
}
{
assertAcked(client().admin().indices().prepareAliases().addAliasAction(IndicesAliasesRequest.AliasActions.add().index("test").alias("alias1").routing("foo")).addAliasAction(IndicesAliasesRequest.AliasActions.add().index("test").alias("alias2").routing("bar")).addAliasAction(IndicesAliasesRequest.AliasActions.add().index("test").alias("alias3").routing("baz")).get());
SearchResponse sr = client().prepareSearch("alias1", "alias3").setQuery(matchAllQuery()).setSize(0).get();
int numDocs = (int) sr.getHits().getTotalHits().value;
int max = randomIntBetween(2, numShards * 3);
int fetchSize = randomIntBetween(10, 100);
SearchRequestBuilder request = client().prepareSearch("alias1", "alias3").setQuery(matchAllQuery()).setScroll(new Scroll(TimeValue.timeValueSeconds(10))).setSize(fetchSize).addSort(SortBuilders.fieldSort("_doc"));
assertSearchSlicesWithScroll(request, "_id", max, numDocs);
}
}
use of org.opensearch.search.Scroll in project OpenSearch by opensearch-project.
the class SearchSliceIT method assertSearchSlicesWithScroll.
private void assertSearchSlicesWithScroll(SearchRequestBuilder request, String field, int numSlice, int numDocs) {
int totalResults = 0;
List<String> keys = new ArrayList<>();
for (int id = 0; id < numSlice; id++) {
SliceBuilder sliceBuilder = new SliceBuilder(field, id, numSlice);
SearchResponse searchResponse = request.slice(sliceBuilder).get();
totalResults += searchResponse.getHits().getHits().length;
int expectedSliceResults = (int) searchResponse.getHits().getTotalHits().value;
int numSliceResults = searchResponse.getHits().getHits().length;
String scrollId = searchResponse.getScrollId();
for (SearchHit hit : searchResponse.getHits().getHits()) {
assertTrue(keys.add(hit.getId()));
}
while (searchResponse.getHits().getHits().length > 0) {
searchResponse = client().prepareSearchScroll("test").setScrollId(scrollId).setScroll(new Scroll(TimeValue.timeValueSeconds(10))).get();
scrollId = searchResponse.getScrollId();
totalResults += searchResponse.getHits().getHits().length;
numSliceResults += searchResponse.getHits().getHits().length;
for (SearchHit hit : searchResponse.getHits().getHits()) {
assertTrue(keys.add(hit.getId()));
}
}
assertThat(numSliceResults, equalTo(expectedSliceResults));
clearScroll(scrollId);
}
assertThat(totalResults, equalTo(numDocs));
assertThat(keys.size(), equalTo(numDocs));
assertThat(new HashSet(keys).size(), equalTo(numDocs));
}
use of org.opensearch.search.Scroll in project OpenSearch by opensearch-project.
the class SearchScrollRequest method fromXContent.
/**
* Parse a search scroll request from a request body provided through the REST layer.
* Values that are already be set and are also found while parsing will be overridden.
*/
public void fromXContent(XContentParser parser) throws IOException {
if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
throw new IllegalArgumentException("Malformed content, must start with an object");
} else {
XContentParser.Token token;
String currentFieldName = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if ("scroll_id".equals(currentFieldName) && token == XContentParser.Token.VALUE_STRING) {
scrollId(parser.text());
} else if ("scroll".equals(currentFieldName) && token == XContentParser.Token.VALUE_STRING) {
scroll(new Scroll(TimeValue.parseTimeValue(parser.text(), null, "scroll")));
} else {
throw new IllegalArgumentException("Unknown parameter [" + currentFieldName + "] in request body or parameter is of the wrong type[" + token + "] ");
}
}
}
}
Aggregations