Search in sources :

Example 46 with CouchbaseException

use of com.couchbase.client.core.error.CouchbaseException in project couchbase-jvm-clients by couchbase.

the class AsyncSearchIndexManager method analyzeDocument.

/**
 * Allows to see how a document is analyzed against a specific index.
 *
 * @param name the name of the search index.
 * @param document the document to analyze.
 * @return a {@link CompletableFuture} with analyzed document parts once complete.
 */
public CompletableFuture<List<JsonObject>> analyzeDocument(final String name, final JsonObject document, final AnalyzeDocumentOptions options) {
    notNullOrEmpty(name, "Search Index Name");
    notNull(document, "Document");
    return searchHttpClient.post(path(analyzeDocumentPath(name)), options.build()).trace(TracingIdentifiers.SPAN_REQUEST_MS_ANALYZE_DOCUMENT).json(Mapper.encodeAsBytes(document.toMap())).exec(core).exceptionally(throwable -> {
        if (throwable.getMessage().contains("Page not found")) {
            throw new FeatureNotAvailableException("Document analysis is not available on this server version!");
        } else if (throwable instanceof RuntimeException) {
            throw (RuntimeException) throwable;
        } else {
            throw new CouchbaseException("Failed to analyze search document", throwable);
        }
    }).thenApply(response -> {
        JsonNode rootNode = Mapper.decodeIntoTree(response.content());
        List<Map<String, Object>> analyzed = Mapper.convertValue(rootNode.get("analyzed"), new TypeReference<List<Map<String, Object>>>() {
        });
        return analyzed.stream().filter(Objects::nonNull).map(JsonObject::from).collect(Collectors.toList());
    });
}
Also used : AnalyzeDocumentOptions.analyzeDocumentOptions(com.couchbase.client.java.manager.search.AnalyzeDocumentOptions.analyzeDocumentOptions) HttpHeaderNames(com.couchbase.client.core.deps.io.netty.handler.codec.http.HttpHeaderNames) GetSearchIndexOptions.getSearchIndexOptions(com.couchbase.client.java.manager.search.GetSearchIndexOptions.getSearchIndexOptions) Validators.notNull(com.couchbase.client.core.util.Validators.notNull) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) RequestTarget(com.couchbase.client.core.msg.RequestTarget) TypeReference(com.couchbase.client.core.deps.com.fasterxml.jackson.core.type.TypeReference) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) JsonNode(com.couchbase.client.core.deps.com.fasterxml.jackson.databind.JsonNode) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) Map(java.util.Map) CoreHttpPath.path(com.couchbase.client.core.endpoint.http.CoreHttpPath.path) DropSearchIndexOptions.dropSearchIndexOptions(com.couchbase.client.java.manager.search.DropSearchIndexOptions.dropSearchIndexOptions) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) GetAllSearchIndexesOptions.getAllSearchIndexesOptions(com.couchbase.client.java.manager.search.GetAllSearchIndexesOptions.getAllSearchIndexesOptions) CbTracing(com.couchbase.client.core.cnc.CbTracing) AllowQueryingSearchIndexOptions.allowQueryingSearchIndexOptions(com.couchbase.client.java.manager.search.AllowQueryingSearchIndexOptions.allowQueryingSearchIndexOptions) UTF_8(java.nio.charset.StandardCharsets.UTF_8) IndexNotFoundException(com.couchbase.client.core.error.IndexNotFoundException) Mapper(com.couchbase.client.core.json.Mapper) CoreHttpClient(com.couchbase.client.core.endpoint.http.CoreHttpClient) UrlQueryStringBuilder.urlEncode(com.couchbase.client.core.util.UrlQueryStringBuilder.urlEncode) FeatureNotAvailableException(com.couchbase.client.core.error.FeatureNotAvailableException) UpsertSearchIndexOptions.upsertSearchIndexOptions(com.couchbase.client.java.manager.search.UpsertSearchIndexOptions.upsertSearchIndexOptions) Collectors(java.util.stream.Collectors) Validators.notNullOrEmpty(com.couchbase.client.core.util.Validators.notNullOrEmpty) PauseIngestSearchIndexOptions.pauseIngestSearchIndexOptions(com.couchbase.client.java.manager.search.PauseIngestSearchIndexOptions.pauseIngestSearchIndexOptions) Objects(java.util.Objects) List(java.util.List) GetIndexedSearchIndexOptions.getIndexedSearchIndexOptions(com.couchbase.client.java.manager.search.GetIndexedSearchIndexOptions.getIndexedSearchIndexOptions) UnfreezePlanSearchIndexOptions.unfreezePlanSearchIndexOptions(com.couchbase.client.java.manager.search.UnfreezePlanSearchIndexOptions.unfreezePlanSearchIndexOptions) ResumeIngestSearchIndexOptions.resumeIngestSearchIndexOptions(com.couchbase.client.java.manager.search.ResumeIngestSearchIndexOptions.resumeIngestSearchIndexOptions) JsonObject(com.couchbase.client.java.json.JsonObject) FreezePlanSearchIndexOptions.freezePlanSearchIndexOptions(com.couchbase.client.java.manager.search.FreezePlanSearchIndexOptions.freezePlanSearchIndexOptions) Core(com.couchbase.client.core.Core) DisallowQueryingSearchIndexOptions.disallowQueryingSearchIndexOptions(com.couchbase.client.java.manager.search.DisallowQueryingSearchIndexOptions.disallowQueryingSearchIndexOptions) FeatureNotAvailableException(com.couchbase.client.core.error.FeatureNotAvailableException) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) Objects(java.util.Objects) JsonNode(com.couchbase.client.core.deps.com.fasterxml.jackson.databind.JsonNode) ArrayList(java.util.ArrayList) List(java.util.List) JsonObject(com.couchbase.client.java.json.JsonObject) Map(java.util.Map)

Aggregations

CouchbaseException (com.couchbase.client.core.error.CouchbaseException)46 DocumentNotFoundException (com.couchbase.client.core.error.DocumentNotFoundException)8 Test (org.junit.jupiter.api.Test)8 ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)7 CasMismatchException (com.couchbase.client.core.error.CasMismatchException)7 RetryExhaustedException (com.couchbase.client.core.retry.reactor.RetryExhaustedException)7 LookupInResult (com.couchbase.client.java.kv.LookupInResult)7 JavaIntegrationTest (com.couchbase.client.java.util.JavaIntegrationTest)7 Duration (java.time.Duration)7 IgnoreWhen (com.couchbase.client.test.IgnoreWhen)6 ArrayList (java.util.ArrayList)6 AuthenticationFailureException (com.couchbase.client.core.error.AuthenticationFailureException)5 RateLimitedException (com.couchbase.client.core.error.RateLimitedException)5 IndexNotFoundException (com.couchbase.client.core.error.IndexNotFoundException)4 UnambiguousTimeoutException (com.couchbase.client.core.error.UnambiguousTimeoutException)4 PathNotFoundException (com.couchbase.client.core.error.subdoc.PathNotFoundException)4 JsonObject (com.couchbase.client.java.json.JsonObject)4 SearchIndex (com.couchbase.client.java.manager.search.SearchIndex)4 List (java.util.List)4 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)3