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));
}
use of org.opensearch.action.fieldcaps.FieldCapabilitiesRequest in project OpenSearch by opensearch-project.
the class SearchIT method testFieldCaps.
public void testFieldCaps() throws IOException {
FieldCapabilitiesRequest request = new FieldCapabilitiesRequest().indices("index1", "index2").fields("rating", "field");
FieldCapabilitiesResponse response = execute(request, highLevelClient()::fieldCaps, highLevelClient()::fieldCapsAsync);
assertThat(response.getIndices(), arrayContaining("index1", "index2"));
// Check the capabilities for the 'rating' field.
assertTrue(response.get().containsKey("rating"));
Map<String, FieldCapabilities> ratingResponse = response.getField("rating");
assertEquals(2, ratingResponse.size());
FieldCapabilities expectedKeywordCapabilities = new FieldCapabilities("rating", "keyword", true, true, new String[] { "index2" }, null, null, Collections.emptyMap());
assertEquals(expectedKeywordCapabilities, ratingResponse.get("keyword"));
FieldCapabilities expectedLongCapabilities = new FieldCapabilities("rating", "long", true, true, new String[] { "index1" }, null, null, Collections.emptyMap());
assertEquals(expectedLongCapabilities, ratingResponse.get("long"));
// Check the capabilities for the 'field' field.
assertTrue(response.get().containsKey("field"));
Map<String, FieldCapabilities> fieldResponse = response.getField("field");
assertEquals(1, fieldResponse.size());
FieldCapabilities expectedTextCapabilities = new FieldCapabilities("field", "text", true, false, null, null, null, Collections.emptyMap());
assertEquals(expectedTextCapabilities, fieldResponse.get("text"));
}
use of org.opensearch.action.fieldcaps.FieldCapabilitiesRequest in project OpenSearch by opensearch-project.
the class RequestConvertersTests method testFieldCapsWithIndexFilter.
public void testFieldCapsWithIndexFilter() 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).indexFilter(QueryBuilders.matchAllQuery());
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()));
}
assertNotNull(request.getEntity());
assertToXContentBody(fieldCapabilitiesRequest, request.getEntity());
}
use of org.opensearch.action.fieldcaps.FieldCapabilitiesRequest in project OpenSearch by opensearch-project.
the class IndicesRequestIT method testFieldCapabilities.
public void testFieldCapabilities() {
String fieldCapabilitiesShardAction = FieldCapabilitiesAction.NAME + "[index][s]";
interceptTransportActions(fieldCapabilitiesShardAction);
FieldCapabilitiesRequest fieldCapabilitiesRequest = new FieldCapabilitiesRequest();
fieldCapabilitiesRequest.indices(randomIndicesOrAliases());
fieldCapabilitiesRequest.fields(randomAlphaOfLength(8));
internalCluster().coordOnlyNodeClient().fieldCaps(fieldCapabilitiesRequest).actionGet();
clearInterceptedActions();
assertSameIndices(fieldCapabilitiesRequest, fieldCapabilitiesShardAction);
}
Aggregations