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());
});
}
Aggregations