Skip to content

Provide support for GetFieldMapping request #1641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import io.netty.handler.ssl.JdkSslContext;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
import org.elasticsearch.client.indices.GetFieldMappingsResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;
Expand Down Expand Up @@ -139,6 +141,7 @@
* @author Russell Parry
* @author Thomas Geese
* @author Brian Clozel
* @author Farid Faoudi
* @since 3.2
* @see ClientConfiguration
* @see ReactiveRestClients
Expand Down Expand Up @@ -674,6 +677,11 @@ public Mono<GetMappingsResponse> getMapping(HttpHeaders headers, GetMappingsRequ
return sendRequest(getMappingsRequest, requestCreator.getMapping(), GetMappingsResponse.class, headers).next();
}

@Override
public Mono<GetFieldMappingsResponse> getFieldMapping(HttpHeaders headers, GetFieldMappingsRequest getFieldMappingsRequest) {
return sendRequest(getFieldMappingsRequest, requestCreator.getFieldMapping(), GetFieldMappingsResponse.class, headers).next();
}

@Override
public Mono<GetSettingsResponse> getSettings(HttpHeaders headers, GetSettingsRequest getSettingsRequest) {
return sendRequest(getSettingsRequest, requestCreator.getSettings(), GetSettingsResponse.class, headers).next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.springframework.data.elasticsearch.client.reactive;

import org.elasticsearch.client.indices.GetFieldMappingsRequest;
import org.elasticsearch.client.indices.GetFieldMappingsResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand Down Expand Up @@ -77,6 +79,7 @@
* @author Peter-Josef Meisch
* @author Henrique Amaral
* @author Thomas Geese
* @author Farid Faoudi
* @since 3.2
* @see ClientConfiguration
* @see ReactiveRestClients
Expand Down Expand Up @@ -1162,6 +1165,50 @@ default Mono<GetMappingsResponse> getMapping(GetMappingsRequest getMappingsReque
*/
Mono<GetMappingsResponse> getMapping(HttpHeaders headers, GetMappingsRequest getMappingsRequest);

/**
* Execute the given {@link GetFieldMappingsRequest} against the {@literal indices} API.
*
* @param consumer never {@literal null}.
* @return a {@link Mono} signalling operation completion or an {@link Mono#error(Throwable) error} if eg. the index
* does not exist.
* @see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-field-mapping.html"> Indices
* Flush API on elastic.co</a>
* @since 4.2
*/
default Mono<GetFieldMappingsResponse> getFieldMapping(Consumer<GetFieldMappingsRequest> consumer) {

GetFieldMappingsRequest request = new GetFieldMappingsRequest();
consumer.accept(request);
return getFieldMapping(request);
}

/**
* Execute the given {@link GetFieldMappingsRequest} against the {@literal indices} API.
*
* @param getFieldMappingsRequest must not be {@literal null}.
* @return a {@link Mono} signalling operation completion or an {@link Mono#error(Throwable) error} if eg. the index
* does not exist.
* @see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-field-mapping.html"> Indices
* Flush API on elastic.co</a>
* @since 4.2
*/
default Mono<GetFieldMappingsResponse> getFieldMapping(GetFieldMappingsRequest getFieldMappingsRequest) {
return getFieldMapping(HttpHeaders.EMPTY, getFieldMappingsRequest);
}

/**
* Execute the given {@link GetFieldMappingsRequest} against the {@literal indices} API.
*
* @param headers Use {@link HttpHeaders} to provide eg. authentication data. Must not be {@literal null}.
* @param getFieldMappingsRequest must not be {@literal null}.
* @return a {@link Mono} signalling operation completion or an {@link Mono#error(Throwable) error} if eg. the index
* does not exist.
* @see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-field-mapping.html"> Indices
* Flush API on elastic.co</a>
* @since 4.2
*/
Mono<GetFieldMappingsResponse> getFieldMapping(HttpHeaders headers, GetFieldMappingsRequest getFieldMappingsRequest);

/**
* Execute the given {@link IndicesAliasesRequest} against the {@literal indices} API.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
Expand All @@ -37,6 +38,7 @@

/**
* @author Roman Puchkovskiy
* @author Farid Faoudi
* @since 4.0
*/
public interface RequestCreator {
Expand Down Expand Up @@ -194,4 +196,11 @@ default Function<IndexTemplatesExistRequest, Request> templatesExist() {
default Function<DeleteIndexTemplateRequest, Request> deleteTemplate() {
return RequestConverters::deleteTemplate;
}

/**
* @since 4.2
*/
default Function<GetFieldMappingsRequest, Request> getFieldMapping() {
return RequestConverters::getFieldMapping;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import org.elasticsearch.client.RethrottleRequest;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.indices.AnalyzeRequest;
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
Expand Down Expand Up @@ -115,6 +116,7 @@
*
* @author Christoph Strobl
* @author Peter-Josef Meisch
* @author Farid Faoudi
* @since 3.2
*/
@SuppressWarnings("JavadocReference")
Expand Down Expand Up @@ -891,6 +893,24 @@ public static Request deleteTemplate(DeleteIndexTemplateRequest deleteIndexTempl
return request;
}

public static Request getFieldMapping(GetFieldMappingsRequest getFieldMappingsRequest) {
String[] indices = getFieldMappingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getFieldMappingsRequest.indices();
String[] fields = getFieldMappingsRequest.fields() == null ? Strings.EMPTY_ARRAY : getFieldMappingsRequest.fields();

final String endpoint = new EndpointBuilder().addCommaSeparatedPathParts(indices)
.addPathPartAsIs("_mapping").addPathPartAsIs("field")
.addCommaSeparatedPathParts(fields)
.build();

Request request = new Request(HttpMethod.GET.name(), endpoint);

RequestConverters.Params parameters = new Params(request);
parameters.withIndicesOptions(getFieldMappingsRequest.indicesOptions());
parameters.withIncludeDefaults(getFieldMappingsRequest.includeDefaults());
parameters.withIncludeTypeName(false);
return request;
}

static HttpEntity createEntity(ToXContent toXContent, XContentType xContentType) {

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
import static org.assertj.core.api.Assertions.*;

import lombok.SneakyThrows;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
Expand Down Expand Up @@ -73,6 +75,7 @@
* @author Henrique Amaral
* @author Russell Parry
* @author Thomas Geese
* @author Farid Faoudi
*/
@SpringIntegrationTest
@ContextConfiguration(classes = { ReactiveElasticsearchClientIntegrationTests.Config.class })
Expand Down Expand Up @@ -721,6 +724,61 @@ public void suggestReturnsSuggestionResults() {
.verifyComplete();
}

@Test // #1640
void getFieldMapping() {

operations.indexOps(IndexCoordinates.of(INDEX_I)).create().block();

Map<String, Object> properties = new HashMap<>();
properties.put("message1", Collections.singletonMap("type", "text"));
properties.put("message2", Collections.singletonMap("type", "keyword"));

Map<String, Object> jsonMap = Collections.singletonMap("properties", properties);

final PutMappingRequest putMappingRequest = new PutMappingRequest(INDEX_I)
.source(jsonMap);

client.indices().putMapping(putMappingRequest).block();

client.indices().getFieldMapping(request -> request.indices(INDEX_I).fields("message1", "message2"))
.as(StepVerifier::create)
.consumeNextWith(it -> {
assertThat(it.mappings().get(INDEX_I).keySet().size()).isEqualTo(2);
assertThat(it.mappings().get(INDEX_I).get("message1").sourceAsMap()).isEqualTo(Collections.singletonMap("message1", Collections.singletonMap("type", "text")));
assertThat(it.mappings().get(INDEX_I).get("message2").sourceAsMap()).isEqualTo(Collections.singletonMap("message2", Collections.singletonMap("type", "keyword")));
})
.verifyComplete();
}

@Test // #1640
void getFieldMappingNonExistingField() {

operations.indexOps(IndexCoordinates.of(INDEX_I)).create().block();

Map<String, Object> jsonMap = Collections.singletonMap("properties",
Collections.singletonMap("message", Collections.singletonMap("type", "text")));

final PutMappingRequest putMappingRequest = new PutMappingRequest(INDEX_I)
.source(jsonMap);

client.indices().putMapping(putMappingRequest).block();

client.indices().getFieldMapping(request -> request.indices(INDEX_I).fields("message1"))
.as(StepVerifier::create)
.consumeNextWith(it -> {
assertThat(it.mappings().get(INDEX_I).keySet().size()).isZero();
})
.verifyComplete();
}

@Test // #1640
void getFieldMappingNonExistingIndex() {

client.indices().getFieldMapping(request -> request.indices(INDEX_I).fields("message1"))
.as(StepVerifier::create)
.verifyError(ElasticsearchStatusException.class);
}

@Test // DATAES-796
@DisplayName("should return the whole SearchResponse")
void shouldReturnTheWholeSearchResponse() {
Expand Down