Search in sources :

Example 1 with FieldCapabilities

use of org.opensearch.action.fieldcaps.FieldCapabilities 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));
}
Also used : FieldCapabilities(org.opensearch.action.fieldcaps.FieldCapabilities) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) FieldCapabilitiesResponse(org.opensearch.action.fieldcaps.FieldCapabilitiesResponse) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest)

Example 2 with FieldCapabilities

use of org.opensearch.action.fieldcaps.FieldCapabilities in project OpenSearch by opensearch-project.

the class FieldCapabilitiesIT method testFieldAlias.

public void testFieldAlias() {
    FieldCapabilitiesResponse response = client().prepareFieldCaps().setFields("distance", "route_length_miles").get();
    assertIndices(response, "old_index", "new_index");
    // Ensure the response has entries for both requested fields.
    assertTrue(response.get().containsKey("distance"));
    assertTrue(response.get().containsKey("route_length_miles"));
    // Check the capabilities for the 'distance' field.
    Map<String, FieldCapabilities> distance = response.getField("distance");
    assertEquals(2, distance.size());
    assertTrue(distance.containsKey("double"));
    assertEquals(new FieldCapabilities("distance", "double", true, true, new String[] { "old_index" }, null, null, Collections.emptyMap()), distance.get("double"));
    assertTrue(distance.containsKey("text"));
    assertEquals(new FieldCapabilities("distance", "text", true, false, new String[] { "new_index" }, null, null, Collections.emptyMap()), distance.get("text"));
    // Check the capabilities for the 'route_length_miles' alias.
    Map<String, FieldCapabilities> routeLength = response.getField("route_length_miles");
    assertEquals(1, routeLength.size());
    assertTrue(routeLength.containsKey("double"));
    assertEquals(new FieldCapabilities("route_length_miles", "double", true, true, null, null, null, Collections.emptyMap()), routeLength.get("double"));
}
Also used : FieldCapabilities(org.opensearch.action.fieldcaps.FieldCapabilities) FieldCapabilitiesResponse(org.opensearch.action.fieldcaps.FieldCapabilitiesResponse)

Example 3 with FieldCapabilities

use of org.opensearch.action.fieldcaps.FieldCapabilities 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"));
}
Also used : FieldCapabilities(org.opensearch.action.fieldcaps.FieldCapabilities) FieldCapabilitiesResponse(org.opensearch.action.fieldcaps.FieldCapabilitiesResponse) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 4 with FieldCapabilities

use of org.opensearch.action.fieldcaps.FieldCapabilities in project OpenSearch by opensearch-project.

the class FieldCapabilitiesIT method testWithIndexFilter.

public void testWithIndexFilter() throws InterruptedException {
    assertAcked(prepareCreate("index-1").addMapping("_doc", "timestamp", "type=date", "field1", "type=keyword"));
    assertAcked(prepareCreate("index-2").addMapping("_doc", "timestamp", "type=date", "field1", "type=long"));
    List<IndexRequestBuilder> reqs = new ArrayList<>();
    reqs.add(client().prepareIndex("index-1").setSource("timestamp", "2015-07-08"));
    reqs.add(client().prepareIndex("index-1").setSource("timestamp", "2018-07-08"));
    reqs.add(client().prepareIndex("index-2").setSource("timestamp", "2019-10-12"));
    reqs.add(client().prepareIndex("index-2").setSource("timestamp", "2020-07-08"));
    indexRandom(true, reqs);
    FieldCapabilitiesResponse response = client().prepareFieldCaps("index-*").setFields("*").get();
    assertIndices(response, "index-1", "index-2");
    Map<String, FieldCapabilities> newField = response.getField("field1");
    assertEquals(2, newField.size());
    assertTrue(newField.containsKey("long"));
    assertTrue(newField.containsKey("keyword"));
    response = client().prepareFieldCaps("index-*").setFields("*").setIndexFilter(QueryBuilders.rangeQuery("timestamp").gte("2019-11-01")).get();
    assertIndices(response, "index-2");
    newField = response.getField("field1");
    assertEquals(1, newField.size());
    assertTrue(newField.containsKey("long"));
    response = client().prepareFieldCaps("index-*").setFields("*").setIndexFilter(QueryBuilders.rangeQuery("timestamp").lte("2017-01-01")).get();
    assertIndices(response, "index-1");
    newField = response.getField("field1");
    assertEquals(1, newField.size());
    assertTrue(newField.containsKey("keyword"));
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) FieldCapabilities(org.opensearch.action.fieldcaps.FieldCapabilities) ArrayList(java.util.ArrayList) FieldCapabilitiesResponse(org.opensearch.action.fieldcaps.FieldCapabilitiesResponse)

Example 5 with FieldCapabilities

use of org.opensearch.action.fieldcaps.FieldCapabilities in project OpenSearch by opensearch-project.

the class FieldCapabilitiesIT method testWithUnmapped.

public void testWithUnmapped() {
    FieldCapabilitiesResponse response = client().prepareFieldCaps().setFields("new_field", "old_field").setIncludeUnmapped(true).get();
    assertIndices(response, "old_index", "new_index");
    assertEquals(2, response.get().size());
    assertTrue(response.get().containsKey("old_field"));
    Map<String, FieldCapabilities> oldField = response.getField("old_field");
    assertEquals(2, oldField.size());
    assertTrue(oldField.containsKey("long"));
    assertEquals(new FieldCapabilities("old_field", "long", true, true, new String[] { "old_index" }, null, null, Collections.emptyMap()), oldField.get("long"));
    assertTrue(oldField.containsKey("unmapped"));
    assertEquals(new FieldCapabilities("old_field", "unmapped", false, false, new String[] { "new_index" }, null, null, Collections.emptyMap()), oldField.get("unmapped"));
    Map<String, FieldCapabilities> newField = response.getField("new_field");
    assertEquals(1, newField.size());
    assertTrue(newField.containsKey("long"));
    assertEquals(new FieldCapabilities("new_field", "long", true, true, null, null, null, Collections.emptyMap()), newField.get("long"));
}
Also used : FieldCapabilities(org.opensearch.action.fieldcaps.FieldCapabilities) FieldCapabilitiesResponse(org.opensearch.action.fieldcaps.FieldCapabilitiesResponse)

Aggregations

FieldCapabilities (org.opensearch.action.fieldcaps.FieldCapabilities)5 FieldCapabilitiesResponse (org.opensearch.action.fieldcaps.FieldCapabilitiesResponse)5 Matchers.containsString (org.hamcrest.Matchers.containsString)2 FieldCapabilitiesRequest (org.opensearch.action.fieldcaps.FieldCapabilitiesRequest)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ActionListener (org.opensearch.action.ActionListener)1 LatchedActionListener (org.opensearch.action.LatchedActionListener)1 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)1 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)1