use of com.couchbase.client.java.search.result.SearchResult in project couchbase-jvm-clients by couchbase.
the class SearchIntegrationTest method searchMatchOperatorAndHit.
@Test
void searchMatchOperatorAndHit() throws Throwable {
String missId = UUID.randomUUID().toString();
String hitId = UUID.randomUUID().toString();
MutationResult insertResult1 = collection.insert(missId, mapOf("fields", mapOf("name", "billy")));
MutationResult insertResult2 = collection.insert(hitId, mapOf("fields", mapOf("name", "billy", "surname", "kid")));
try {
runWithRetry(Duration.ofSeconds(30), () -> {
SearchResult result = cluster.searchQuery(indexName, match("billy kid").operator(MatchOperator.AND), searchOptions().consistentWith(MutationState.from(insertResult1.mutationToken().get(), insertResult2.mutationToken().get())));
List<String> actualDocIds = result.rows().stream().map(SearchRow::id).collect(toList());
assertEquals(listOf(hitId), actualDocIds);
});
} finally {
collection.remove(missId);
collection.remove(hitId);
}
}
use of com.couchbase.client.java.search.result.SearchResult in project couchbase-jvm-clients by couchbase.
the class SearchIntegrationTest method searchMatchOperatorAndMiss.
@Test
void searchMatchOperatorAndMiss() throws Throwable {
String docId1 = UUID.randomUUID().toString();
String docId2 = UUID.randomUUID().toString();
MutationResult insertResult1 = collection.insert(docId1, mapOf("name", "billy"));
MutationResult insertResult2 = collection.insert(docId2, mapOf("name", "silly"));
try {
runWithRetry(Duration.ofSeconds(10), () -> {
SearchResult result = cluster.searchQuery(indexName, match("silly billy").operator(MatchOperator.AND), searchOptions().consistentWith(MutationState.from(insertResult1.mutationToken().get(), insertResult2.mutationToken().get())));
List<String> actualDocIds = result.rows().stream().map(SearchRow::id).collect(toList());
assertTrue(actualDocIds.isEmpty());
});
} finally {
collection.remove(docId1);
collection.remove(docId2);
}
}
use of com.couchbase.client.java.search.result.SearchResult in project couchbase-jvm-clients by couchbase.
the class SearchIntegrationTest method searchIncludeLocations.
@Test
void searchIncludeLocations() throws Throwable {
String docId = UUID.randomUUID().toString();
MutationResult insertResult = collection.insert(docId, mapOf("name", "billy"));
try {
runWithRetry(Duration.ofSeconds(30), () -> {
SearchResult result = cluster.searchQuery(indexName, queryString("billy"), searchOptions().consistentWith(MutationState.from(insertResult.mutationToken().get())).includeLocations(true));
List<SearchRowLocations> locationsList = result.rows().stream().map(SearchRow::locations).filter(opt -> opt.isPresent()).map(Optional::get).collect(toList());
assertTrue(!locationsList.isEmpty());
});
} finally {
collection.remove(docId);
}
}
use of com.couchbase.client.java.search.result.SearchResult in project couchbase-jvm-clients by couchbase.
the class SearchMock method loadSearchTestCase.
/**
* Given JSON in the form expected, e.g. those from https://github.com/chvck/sdk-testcases which contains the
* returned JSON from the search service in a field "data", returns the completed SearchResult that the API
* would return.
*/
public static SearchResult loadSearchTestCase(InputStream json) throws ExecutionException, InterruptedException, IOException {
// The idea is to fake packets that have come from the search service.
// Start by preparing the packets.
JsonObject jo = JsonObject.fromJson(toByteArray(json));
JsonObject data = jo.getObject("data");
byte[] b = data.toString().getBytes(StandardCharsets.UTF_8);
ByteBuf bytes = Unpooled.wrappedBuffer(b);
HttpContent content = new DefaultLastHttpContent(bytes);
HttpResponse resp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
// Fake some core stuff
Core mockedCore = mock(Core.class);
CoreEnvironment env = CoreEnvironment.create();
CoreContext ctx = new CoreContext(mockedCore, 0, env, PasswordAuthenticator.create("Administrator", "password"));
// Our ChunkedSearchMessageHandler needs to be initialised by pretending we've sent an outbound SearchRequest
// through it
SearchRequest req = new SearchRequest(Duration.ofSeconds(10), ctx, BestEffortRetryStrategy.INSTANCE, null, null, null, null);
// ChunkedSearchMessageHandler will try to encode() the SearchRequest. Rather than mocking everything required
// to get that working, just mock the encode method.
SearchRequest spiedReq = spy(req);
doReturn(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "localhost")).when(spiedReq).encode();
doAnswer(v -> {
EndpointContext endpointContext = new EndpointContext(ctx, new HostAndPort(null, 0), null, null, null, Optional.of("bucket"), null);
BaseEndpoint endpoint = mock(BaseEndpoint.class);
when(endpoint.context()).thenReturn(endpointContext);
when(endpoint.pipelined()).thenReturn(false);
// ChunkedSearchMessageHandler does most of the work in handling responses from the service
ChunkedSearchMessageHandler handler = new ChunkedSearchMessageHandler(endpoint, endpointContext);
// Netty's EmbeddedChannel lets us test ChannelHandlers like ChunkedSearchMessageHandler. It's a Netty Channel
// that doesn't touch the network at all.
final EmbeddedChannel channel = new EmbeddedChannel(handler);
// Writing the request is necessary to estabish some initial state inChunkedSearchMessageHandler
channel.writeAndFlush(spiedReq);
// Finally we can do the interesting bit of passing our fake FTS service response into
// ChunkedSearchMessageHandler
channel.writeInbound(resp);
channel.writeInbound(content);
return null;
}).when(mockedCore).send(any());
CompletableFuture<SearchResult> future = SearchAccessor.searchQueryAsync(mockedCore, req, DefaultJsonSerializer.create());
SearchResult result = future.get();
return result;
}
Aggregations