Search in sources :

Example 1 with PostSearchOptions

use of com.ibm.cloud.cloudant.v1.model.PostSearchOptions in project cloudant-java-sdk by IBM.

the class Cloudant method postSearch.

/**
 * Query a search index.
 *
 * Search indexes, which are defined in design documents, allow databases to be queried by using Lucene Query Parser
 * Syntax. An index function defines a search index, similar to a map function in MapReduce views. The index function
 * decides what data to index and what data to store in the index. The advantage of using the HTTP `POST` method is
 * that the query is submitted as a JSON object in the request body. This avoids the limitations of passing query
 * options as URL query parameters of a `GET` request.
 *
 * @param postSearchOptions the {@link PostSearchOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link SearchResult}
 */
public ServiceCall<SearchResult> postSearch(PostSearchOptions postSearchOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(postSearchOptions, "postSearchOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("db", postSearchOptions.db());
    pathParamsMap.put("ddoc", postSearchOptions.ddoc());
    pathParamsMap.put("index", postSearchOptions.index());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}/_design/{ddoc}/_search/{index}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "postSearch");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    final JsonObject contentJson = new JsonObject();
    contentJson.addProperty("query", postSearchOptions.query());
    if (postSearchOptions.bookmark() != null) {
        contentJson.addProperty("bookmark", postSearchOptions.bookmark());
    }
    if (postSearchOptions.highlightFields() != null) {
        contentJson.add("highlight_fields", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postSearchOptions.highlightFields()));
    }
    if (postSearchOptions.highlightNumber() != null) {
        contentJson.addProperty("highlight_number", postSearchOptions.highlightNumber());
    }
    if (postSearchOptions.highlightPostTag() != null) {
        contentJson.addProperty("highlight_post_tag", postSearchOptions.highlightPostTag());
    }
    if (postSearchOptions.highlightPreTag() != null) {
        contentJson.addProperty("highlight_pre_tag", postSearchOptions.highlightPreTag());
    }
    if (postSearchOptions.highlightSize() != null) {
        contentJson.addProperty("highlight_size", postSearchOptions.highlightSize());
    }
    if (postSearchOptions.includeDocs() != null) {
        contentJson.addProperty("include_docs", postSearchOptions.includeDocs());
    }
    if (postSearchOptions.includeFields() != null) {
        contentJson.add("include_fields", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postSearchOptions.includeFields()));
    }
    if (postSearchOptions.limit() != null) {
        contentJson.addProperty("limit", postSearchOptions.limit());
    }
    if (postSearchOptions.sort() != null) {
        contentJson.add("sort", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postSearchOptions.sort()));
    }
    if (postSearchOptions.stale() != null) {
        contentJson.addProperty("stale", postSearchOptions.stale());
    }
    if (postSearchOptions.counts() != null) {
        contentJson.add("counts", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postSearchOptions.counts()));
    }
    if (postSearchOptions.drilldown() != null) {
        contentJson.add("drilldown", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postSearchOptions.drilldown()));
    }
    if (postSearchOptions.groupField() != null) {
        contentJson.addProperty("group_field", postSearchOptions.groupField());
    }
    if (postSearchOptions.groupLimit() != null) {
        contentJson.addProperty("group_limit", postSearchOptions.groupLimit());
    }
    if (postSearchOptions.groupSort() != null) {
        contentJson.add("group_sort", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postSearchOptions.groupSort()));
    }
    if (postSearchOptions.ranges() != null) {
        contentJson.add("ranges", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postSearchOptions.ranges()));
    }
    builder.bodyJson(contentJson);
    ResponseConverter<SearchResult> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<SearchResult>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) SearchResult(com.ibm.cloud.cloudant.v1.model.SearchResult)

Example 2 with PostSearchOptions

use of com.ibm.cloud.cloudant.v1.model.PostSearchOptions in project cloudant-java-sdk by IBM.

the class CloudantTest method testPostSearchWOptions.

@Test
public void testPostSearchWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"total_rows\": 0, \"bookmark\": \"bookmark\", \"by\": \"by\", \"counts\": {\"mapKey\": {\"mapKey\": 0}}, \"ranges\": {\"mapKey\": {\"mapKey\": 0}}, \"rows\": [{\"doc\": {\"_attachments\": {\"mapKey\": {\"content_type\": \"contentType\", \"data\": \"VGhpcyBpcyBhbiBlbmNvZGVkIGJ5dGUgYXJyYXku\", \"digest\": \"digest\", \"encoded_length\": 0, \"encoding\": \"encoding\", \"follows\": false, \"length\": 0, \"revpos\": 1, \"stub\": true}}, \"_conflicts\": [\"conflicts\"], \"_deleted\": false, \"_deleted_conflicts\": [\"deletedConflicts\"], \"_id\": \"id\", \"_local_seq\": \"localSeq\", \"_rev\": \"rev\", \"_revisions\": {\"ids\": [\"ids\"], \"start\": 1}, \"_revs_info\": [{\"rev\": \"rev\", \"status\": \"available\"}]}, \"fields\": {\"mapKey\": \"anyValue\"}, \"highlights\": {\"mapKey\": [\"inner\"]}, \"id\": \"id\"}], \"groups\": [{\"total_rows\": 0, \"bookmark\": \"bookmark\", \"by\": \"by\", \"counts\": {\"mapKey\": {\"mapKey\": 0}}, \"ranges\": {\"mapKey\": {\"mapKey\": 0}}, \"rows\": [{\"doc\": {\"_attachments\": {\"mapKey\": {\"content_type\": \"contentType\", \"data\": \"VGhpcyBpcyBhbiBlbmNvZGVkIGJ5dGUgYXJyYXku\", \"digest\": \"digest\", \"encoded_length\": 0, \"encoding\": \"encoding\", \"follows\": false, \"length\": 0, \"revpos\": 1, \"stub\": true}}, \"_conflicts\": [\"conflicts\"], \"_deleted\": false, \"_deleted_conflicts\": [\"deletedConflicts\"], \"_id\": \"id\", \"_local_seq\": \"localSeq\", \"_rev\": \"rev\", \"_revisions\": {\"ids\": [\"ids\"], \"start\": 1}, \"_revs_info\": [{\"rev\": \"rev\", \"status\": \"available\"}]}, \"fields\": {\"mapKey\": \"anyValue\"}, \"highlights\": {\"mapKey\": [\"inner\"]}, \"id\": \"id\"}]}]}";
    String postSearchPath = "/testString/_design/testString/_search/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the PostSearchOptions model
    PostSearchOptions postSearchOptionsModel = new PostSearchOptions.Builder().db("testString").ddoc("testString").index("testString").query("testString").bookmark("testString").highlightFields(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).highlightNumber(Long.valueOf("1")).highlightPostTag("</em>").highlightPreTag("<em>").highlightSize(Long.valueOf("1")).includeDocs(false).includeFields(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).limit(Long.valueOf("3")).sort(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).stale("ok").counts(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).drilldown(new java.util.ArrayList<List<String>>(java.util.Arrays.asList(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))))).groupField("testString").groupLimit(Long.valueOf("1")).groupSort(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).ranges(new java.util.HashMap<String, Map<String, Map<String, String>>>() {

        {
            put("foo", new java.util.HashMap<String, Map<String, String>>() {

                {
                    put("foo", new java.util.HashMap<String, String>() {

                        {
                            put("foo", "testString");
                        }
                    });
                }
            });
        }
    }).build();
    // Invoke operation with valid options model (positive test)
    Response<SearchResult> response = cloudantService.postSearch(postSearchOptionsModel).execute();
    assertNotNull(response);
    SearchResult responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "POST");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNull(query);
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, postSearchPath);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SearchResult(com.ibm.cloud.cloudant.v1.model.SearchResult) PostSearchOptions(com.ibm.cloud.cloudant.v1.model.PostSearchOptions) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with PostSearchOptions

use of com.ibm.cloud.cloudant.v1.model.PostSearchOptions in project cloudant-java-sdk by IBM.

the class PostSearchOptionsTest method testPostSearchOptions.

@Test
public void testPostSearchOptions() throws Throwable {
    PostSearchOptions postSearchOptionsModel = new PostSearchOptions.Builder().db("testString").ddoc("testString").index("testString").query("testString").bookmark("testString").highlightFields(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).highlightNumber(Long.valueOf("1")).highlightPostTag("</em>").highlightPreTag("<em>").highlightSize(Long.valueOf("1")).includeDocs(false).includeFields(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).limit(Long.valueOf("0")).sort(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).stale("ok").counts(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).drilldown(new java.util.ArrayList<List<String>>(java.util.Arrays.asList(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))))).groupField("testString").groupLimit(Long.valueOf("1")).groupSort(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).ranges(new java.util.HashMap<String, Map<String, Map<String, String>>>() {

        {
            put("foo", new java.util.HashMap<String, Map<String, String>>() {

                {
                    put("foo", new java.util.HashMap<String, String>() {

                        {
                            put("foo", "testString");
                        }
                    });
                }
            });
        }
    }).build();
    assertEquals(postSearchOptionsModel.db(), "testString");
    assertEquals(postSearchOptionsModel.ddoc(), "testString");
    assertEquals(postSearchOptionsModel.index(), "testString");
    assertEquals(postSearchOptionsModel.query(), "testString");
    assertEquals(postSearchOptionsModel.bookmark(), "testString");
    assertEquals(postSearchOptionsModel.highlightFields(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    assertEquals(postSearchOptionsModel.highlightNumber(), Long.valueOf("1"));
    assertEquals(postSearchOptionsModel.highlightPostTag(), "</em>");
    assertEquals(postSearchOptionsModel.highlightPreTag(), "<em>");
    assertEquals(postSearchOptionsModel.highlightSize(), Long.valueOf("1"));
    assertEquals(postSearchOptionsModel.includeDocs(), Boolean.valueOf(false));
    assertEquals(postSearchOptionsModel.includeFields(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    assertEquals(postSearchOptionsModel.limit(), Long.valueOf("0"));
    assertEquals(postSearchOptionsModel.sort(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    assertEquals(postSearchOptionsModel.stale(), "ok");
    assertEquals(postSearchOptionsModel.counts(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    assertEquals(postSearchOptionsModel.drilldown(), new java.util.ArrayList<List<String>>(java.util.Arrays.asList(new java.util.ArrayList<String>(java.util.Arrays.asList("testString")))));
    assertEquals(postSearchOptionsModel.groupField(), "testString");
    assertEquals(postSearchOptionsModel.groupLimit(), Long.valueOf("1"));
    assertEquals(postSearchOptionsModel.groupSort(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    assertEquals(postSearchOptionsModel.ranges(), new java.util.HashMap<String, Map<String, Map<String, String>>>() {

        {
            put("foo", new java.util.HashMap<String, Map<String, String>>() {

                {
                    put("foo", new java.util.HashMap<String, String>() {

                        {
                            put("foo", "testString");
                        }
                    });
                }
            });
        }
    });
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PostSearchOptions(com.ibm.cloud.cloudant.v1.model.PostSearchOptions) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test)

Example 4 with PostSearchOptions

use of com.ibm.cloud.cloudant.v1.model.PostSearchOptions in project cloudant-java-sdk by IBM.

the class CloudantTest method testPostSearchAsStreamWOptions.

@Test
public void testPostSearchAsStreamWOptions() throws Throwable {
    // Schedule some responses.
    String mockResponseBody = "{\"foo\": \"this is a mock response for JSON streaming\"}";
    String postSearchAsStreamPath = "/testString/_design/testString/_search/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the PostSearchOptions model
    PostSearchOptions postSearchOptionsModel = new PostSearchOptions.Builder().db("testString").ddoc("testString").index("testString").query("testString").bookmark("testString").highlightFields(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).highlightNumber(Long.valueOf("1")).highlightPostTag("</em>").highlightPreTag("<em>").highlightSize(Long.valueOf("1")).includeDocs(false).includeFields(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).limit(Long.valueOf("3")).sort(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).stale("ok").counts(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).drilldown(new java.util.ArrayList<List<String>>(java.util.Arrays.asList(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))))).groupField("testString").groupLimit(Long.valueOf("1")).groupSort(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).ranges(new java.util.HashMap<String, Map<String, Map<String, String>>>() {

        {
            put("foo", new java.util.HashMap<String, Map<String, String>>() {

                {
                    put("foo", new java.util.HashMap<String, String>() {

                        {
                            put("foo", "testString");
                        }
                    });
                }
            });
        }
    }).build();
    // Invoke operation with valid options model (positive test)
    Response<InputStream> response = cloudantService.postSearchAsStream(postSearchOptionsModel).execute();
    assertNotNull(response);
    InputStream responseObj = response.getResult();
    assertNotNull(responseObj);
    // Verify the contents of the request
    RecordedRequest request = server.takeRequest();
    assertNotNull(request);
    assertEquals(request.getMethod(), "POST");
    // Check query
    Map<String, String> query = TestUtilities.parseQueryString(request);
    assertNull(query);
    // Check request path
    String parsedPath = TestUtilities.parseReqPath(request);
    assertEquals(parsedPath, postSearchAsStreamPath);
    // Verify streamed JSON response
    java.util.Scanner s = new java.util.Scanner(responseObj).useDelimiter("\\A");
    String streamedResponseBody = s.hasNext() ? s.next() : "";
    assertEquals(streamedResponseBody, mockResponseBody);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) HashMap(java.util.HashMap) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) PostSearchOptions(com.ibm.cloud.cloudant.v1.model.PostSearchOptions) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

HashMap (java.util.HashMap)4 PostSearchOptions (com.ibm.cloud.cloudant.v1.model.PostSearchOptions)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 Test (org.testng.annotations.Test)3 SearchResult (com.ibm.cloud.cloudant.v1.model.SearchResult)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 JsonObject (com.google.gson.JsonObject)1 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 InputStream (java.io.InputStream)1 List (java.util.List)1