use of org.opensearch.action.fieldcaps.FieldCapabilitiesRequest in project OpenSearch by opensearch-project.
the class RequestConverters method fieldCaps.
static Request fieldCaps(FieldCapabilitiesRequest fieldCapabilitiesRequest) throws IOException {
String methodName = fieldCapabilitiesRequest.indexFilter() != null ? HttpPost.METHOD_NAME : HttpGet.METHOD_NAME;
Request request = new Request(methodName, endpoint(fieldCapabilitiesRequest.indices(), "_field_caps"));
Params params = new Params();
params.withFields(fieldCapabilitiesRequest.fields());
params.withIndicesOptions(fieldCapabilitiesRequest.indicesOptions());
request.addParameters(params.asMap());
if (fieldCapabilitiesRequest.indexFilter() != null) {
request.setEntity(createEntity(fieldCapabilitiesRequest, REQUEST_BODY_CONTENT_TYPE));
}
return request;
}
use of org.opensearch.action.fieldcaps.FieldCapabilitiesRequest in project OpenSearch by opensearch-project.
the class RequestConvertersTests method testFieldCaps.
public void testFieldCaps() throws IOException {
// Create a random request.
String[] indices = randomIndicesNames(0, 5);
String[] fields = generateRandomStringArray(5, 10, false, false);
FieldCapabilitiesRequest fieldCapabilitiesRequest = new FieldCapabilitiesRequest().indices(indices).fields(fields);
Map<String, String> indicesOptionsParams = new HashMap<>();
setRandomIndicesOptions(fieldCapabilitiesRequest::indicesOptions, fieldCapabilitiesRequest::indicesOptions, indicesOptionsParams);
Request request = RequestConverters.fieldCaps(fieldCapabilitiesRequest);
// Verify that the resulting REST request looks as expected.
StringJoiner endpoint = new StringJoiner("/", "/", "");
String joinedIndices = String.join(",", indices);
if (!joinedIndices.isEmpty()) {
endpoint.add(joinedIndices);
}
endpoint.add("_field_caps");
assertEquals(endpoint.toString(), request.getEndpoint());
assertEquals(5, request.getParameters().size());
// Note that we don't check the field param value explicitly, as field names are
// passed through
// a hash set before being added to the request, and can appear in a
// non-deterministic order.
assertThat(request.getParameters(), hasKey("fields"));
String[] requestFields = Strings.splitStringByCommaToArray(request.getParameters().get("fields"));
assertEquals(new HashSet<>(Arrays.asList(fields)), new HashSet<>(Arrays.asList(requestFields)));
for (Map.Entry<String, String> param : indicesOptionsParams.entrySet()) {
assertThat(request.getParameters(), hasEntry(param.getKey(), param.getValue()));
}
assertNull(request.getEntity());
}
use of org.opensearch.action.fieldcaps.FieldCapabilitiesRequest in project OpenSearch by opensearch-project.
the class SearchIT method testFieldCapsWithNonExistentFields.
public void testFieldCapsWithNonExistentFields() throws IOException {
FieldCapabilitiesRequest request = new FieldCapabilitiesRequest().indices("index2").fields("nonexistent");
FieldCapabilitiesResponse response = execute(request, highLevelClient()::fieldCaps, highLevelClient()::fieldCapsAsync);
assertTrue(response.get().isEmpty());
}
use of org.opensearch.action.fieldcaps.FieldCapabilitiesRequest in project OpenSearch by opensearch-project.
the class SearchIT method testFieldCapsWithNonExistentIndices.
public void testFieldCapsWithNonExistentIndices() {
FieldCapabilitiesRequest request = new FieldCapabilitiesRequest().indices("non-existent").fields("rating");
OpenSearchException exception = expectThrows(OpenSearchException.class, () -> execute(request, highLevelClient()::fieldCaps, highLevelClient()::fieldCapsAsync));
assertEquals(RestStatus.NOT_FOUND, exception.status());
}
use of org.opensearch.action.fieldcaps.FieldCapabilitiesRequest in project OpenSearch by opensearch-project.
the class SearchDocumentationIT method testFieldCaps.
public void testFieldCaps() throws Exception {
indexSearchTestData();
RestHighLevelClient client = highLevelClient();
// tag::field-caps-request
FieldCapabilitiesRequest request = new FieldCapabilitiesRequest().fields("user").indices("posts", "authors", "contributors");
// end::field-caps-request
// tag::field-caps-request-indicesOptions
// <1>
request.indicesOptions(IndicesOptions.lenientExpandOpen());
// end::field-caps-request-indicesOptions
// tag::field-caps-execute
FieldCapabilitiesResponse response = client.fieldCaps(request, RequestOptions.DEFAULT);
// end::field-caps-execute
// tag::field-caps-response
// <1>
Map<String, FieldCapabilities> userResponse = response.getField("user");
FieldCapabilities textCapabilities = userResponse.get("keyword");
boolean isSearchable = textCapabilities.isSearchable();
boolean isAggregatable = textCapabilities.isAggregatable();
// <2>
String[] indices = textCapabilities.indices();
// <3>
String[] nonSearchableIndices = textCapabilities.nonSearchableIndices();
// <4>
String[] nonAggregatableIndices = textCapabilities.nonAggregatableIndices();
// end::field-caps-response
assertThat(userResponse.keySet(), containsInAnyOrder("keyword", "text"));
assertTrue(isSearchable);
assertFalse(isAggregatable);
assertArrayEquals(indices, new String[] { "authors", "contributors" });
assertNull(nonSearchableIndices);
assertArrayEquals(nonAggregatableIndices, new String[] { "authors" });
// tag::field-caps-execute-listener
ActionListener<FieldCapabilitiesResponse> listener = new ActionListener<FieldCapabilitiesResponse>() {
@Override
public void onResponse(FieldCapabilitiesResponse response) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::field-caps-execute-listener
// Replace the empty listener by a blocking listener for tests.
CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::field-caps-execute-async
// <1>
client.fieldCapsAsync(request, RequestOptions.DEFAULT, listener);
// end::field-caps-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Aggregations