use of org.opensearch.action.admin.indices.validate.query.ValidateQueryResponse in project OpenSearch by opensearch-project.
the class IndicesClientIT method testInvalidValidateQuery.
public void testInvalidValidateQuery() throws IOException {
String index = "shakespeare";
createIndex(index, Settings.EMPTY);
Request postDoc = new Request(HttpPost.METHOD_NAME, "/" + index + "/_doc");
postDoc.setJsonEntity("{" + " \"type\": \"act\"," + " \"line_id\": 1," + " \"play_name\": \"Henry IV\"," + " \"speech_number\": \"\"," + " \"line_number\": \"\"," + " \"speaker\": \"\"," + " \"text_entry\": \"ACT I\"" + "}");
assertOK(client().performRequest(postDoc));
QueryBuilder builder = QueryBuilders.queryStringQuery("line_id:foo").lenient(false);
ValidateQueryRequest request = new ValidateQueryRequest(index).query(builder);
request.explain(true);
ValidateQueryResponse response = execute(request, highLevelClient().indices()::validateQuery, highLevelClient().indices()::validateQueryAsync);
assertFalse(response.isValid());
}
use of org.opensearch.action.admin.indices.validate.query.ValidateQueryResponse in project OpenSearch by opensearch-project.
the class IndicesClientIT method testValidateQuery.
public void testValidateQuery() throws IOException {
String index = "some_index";
createIndex(index, Settings.EMPTY);
QueryBuilder builder = QueryBuilders.boolQuery().must(QueryBuilders.queryStringQuery("*:*")).filter(QueryBuilders.termQuery("user", "foobar"));
ValidateQueryRequest request = new ValidateQueryRequest(index).query(builder);
request.explain(randomBoolean());
ValidateQueryResponse response = execute(request, highLevelClient().indices()::validateQuery, highLevelClient().indices()::validateQueryAsync);
assertTrue(response.isValid());
}
use of org.opensearch.action.admin.indices.validate.query.ValidateQueryResponse in project OpenSearch by opensearch-project.
the class IndicesClientDocumentationIT method testValidateQuery.
@SuppressWarnings("unused")
public void testValidateQuery() throws IOException, InterruptedException {
RestHighLevelClient client = highLevelClient();
String index = "some_index";
createIndex(index, Settings.EMPTY);
// tag::indices-validate-query-request
// <1>
ValidateQueryRequest request = new ValidateQueryRequest(index);
// end::indices-validate-query-request
// tag::indices-validate-query-request-query
QueryBuilder builder = QueryBuilders.boolQuery().must(QueryBuilders.queryStringQuery("*:*")).filter(QueryBuilders.termQuery("user", "foobar"));
// <2>
request.query(builder);
// end::indices-validate-query-request-query
// tag::indices-validate-query-request-explain
// <1>
request.explain(true);
// end::indices-validate-query-request-explain
// tag::indices-validate-query-request-allShards
// <1>
request.allShards(true);
// end::indices-validate-query-request-allShards
// tag::indices-validate-query-request-rewrite
// <1>
request.rewrite(true);
// end::indices-validate-query-request-rewrite
// tag::indices-validate-query-execute
// <1>
ValidateQueryResponse response = client.indices().validateQuery(request, RequestOptions.DEFAULT);
// end::indices-validate-query-execute
// tag::indices-validate-query-response
// <1>
boolean isValid = response.isValid();
// <2>
int totalShards = response.getTotalShards();
// <3>
int successfulShards = response.getSuccessfulShards();
// <4>
int failedShards = response.getFailedShards();
if (failedShards > 0) {
for (DefaultShardOperationFailedException failure : response.getShardFailures()) {
// <5>
// <6>
String failedIndex = failure.index();
// <7>
int shardId = failure.shardId();
// <8>
String reason = failure.reason();
}
}
for (QueryExplanation explanation : response.getQueryExplanation()) {
// <9>
// <10>
String explanationIndex = explanation.getIndex();
// <11>
int shardId = explanation.getShard();
// <12>
String explanationString = explanation.getExplanation();
}
// end::indices-validate-query-response
// tag::indices-validate-query-execute-listener
ActionListener<ValidateQueryResponse> listener = new ActionListener<ValidateQueryResponse>() {
@Override
public void onResponse(ValidateQueryResponse validateQueryResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::indices-validate-query-execute-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::indices-validate-query-execute-async
// <1>
client.indices().validateQueryAsync(request, RequestOptions.DEFAULT, listener);
// end::indices-validate-query-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
use of org.opensearch.action.admin.indices.validate.query.ValidateQueryResponse in project OpenSearch by opensearch-project.
the class SimpleValidateQueryIT method testExplainFilteredAlias.
public void testExplainFilteredAlias() {
assertAcked(prepareCreate("test").addMapping("test", "field", "type=text").addAlias(new Alias("alias").filter(QueryBuilders.termQuery("field", "value1"))));
ensureGreen();
ValidateQueryResponse validateQueryResponse = client().admin().indices().prepareValidateQuery("alias").setQuery(QueryBuilders.matchAllQuery()).setExplain(true).get();
assertThat(validateQueryResponse.isValid(), equalTo(true));
assertThat(validateQueryResponse.getQueryExplanation().size(), equalTo(1));
assertThat(validateQueryResponse.getQueryExplanation().get(0).getIndex(), equalTo("test"));
assertThat(validateQueryResponse.getQueryExplanation().get(0).getExplanation(), containsString("field:value1"));
}
use of org.opensearch.action.admin.indices.validate.query.ValidateQueryResponse in project OpenSearch by opensearch-project.
the class SimpleValidateQueryIT method testExplainDateRangeInQueryString.
// Issue #3629
public void testExplainDateRangeInQueryString() {
assertAcked(prepareCreate("test").setSettings(Settings.builder().put(indexSettings()).put("index.number_of_shards", 1)));
ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
String aMonthAgo = DateTimeFormatter.ISO_LOCAL_DATE.format(now.plus(1, ChronoUnit.MONTHS));
String aMonthFromNow = DateTimeFormatter.ISO_LOCAL_DATE.format(now.minus(1, ChronoUnit.MONTHS));
client().prepareIndex("test").setId("1").setSource("past", aMonthAgo, "future", aMonthFromNow).get();
refresh();
ValidateQueryResponse response = client().admin().indices().prepareValidateQuery().setQuery(queryStringQuery("past:[now-2M/d TO now/d]")).setRewrite(true).get();
assertNoFailures(response);
assertThat(response.getQueryExplanation().size(), equalTo(1));
assertThat(response.getQueryExplanation().get(0).getError(), nullValue());
long twoMonthsAgo = now.minus(2, ChronoUnit.MONTHS).truncatedTo(ChronoUnit.DAYS).toEpochSecond() * 1000;
long rangeEnd = (now.plus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.DAYS).toEpochSecond() * 1000) - 1;
assertThat(response.getQueryExplanation().get(0).getExplanation(), equalTo("past:[" + twoMonthsAgo + " TO " + rangeEnd + "]"));
assertThat(response.isValid(), equalTo(true));
}
Aggregations