Search in sources :

Example 1 with SearchResult

use of com.ibm.cloud.cloudant.v1.model.SearchResult 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 SearchResult

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

the class SearchResultTest method testSearchResult.

@Test
public void testSearchResult() throws Throwable {
    SearchResult searchResultModel = new SearchResult();
    assertNull(searchResultModel.getTotalRows());
    assertNull(searchResultModel.getBookmark());
    assertNull(searchResultModel.getBy());
    assertNull(searchResultModel.getCounts());
    assertNull(searchResultModel.getRanges());
    assertNull(searchResultModel.getRows());
    assertNull(searchResultModel.getGroups());
}
Also used : SearchResult(com.ibm.cloud.cloudant.v1.model.SearchResult) Test(org.testng.annotations.Test)

Example 3 with SearchResult

use of com.ibm.cloud.cloudant.v1.model.SearchResult 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 4 with SearchResult

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

the class CloudantTest method testPostPartitionSearchWOptions.

@Test
public void testPostPartitionSearchWOptions() 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 postPartitionSearchPath = "/testString/_partition/testString/_design/testString/_search/testString";
    server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(200).setBody(mockResponseBody));
    constructClientService();
    // Construct an instance of the PostPartitionSearchOptions model
    PostPartitionSearchOptions postPartitionSearchOptionsModel = new PostPartitionSearchOptions.Builder().db("testString").partitionKey("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").build();
    // Invoke operation with valid options model (positive test)
    Response<SearchResult> response = cloudantService.postPartitionSearch(postPartitionSearchOptionsModel).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, postPartitionSearchPath);
}
Also used : PostPartitionSearchOptions(com.ibm.cloud.cloudant.v1.model.PostPartitionSearchOptions) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) SearchResult(com.ibm.cloud.cloudant.v1.model.SearchResult) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with SearchResult

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

the class Cloudant method postPartitionSearch.

/**
 * Query a database partition search index.
 *
 * Partitioned Search indexes, which are defined in design documents, allow partition databases to be queried by using
 * Lucene Query Parser Syntax. Search indexes are defined by an index function, similar to a map function in MapReduce
 * views. The index function decides what data to index and store in the index.
 *
 * @param postPartitionSearchOptions the {@link PostPartitionSearchOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link SearchResult}
 */
public ServiceCall<SearchResult> postPartitionSearch(PostPartitionSearchOptions postPartitionSearchOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(postPartitionSearchOptions, "postPartitionSearchOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("db", postPartitionSearchOptions.db());
    pathParamsMap.put("partition_key", postPartitionSearchOptions.partitionKey());
    pathParamsMap.put("ddoc", postPartitionSearchOptions.ddoc());
    pathParamsMap.put("index", postPartitionSearchOptions.index());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/{db}/_partition/{partition_key}/_design/{ddoc}/_search/{index}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("cloudant", "v1", "postPartitionSearch");
    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", postPartitionSearchOptions.query());
    if (postPartitionSearchOptions.bookmark() != null) {
        contentJson.addProperty("bookmark", postPartitionSearchOptions.bookmark());
    }
    if (postPartitionSearchOptions.highlightFields() != null) {
        contentJson.add("highlight_fields", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postPartitionSearchOptions.highlightFields()));
    }
    if (postPartitionSearchOptions.highlightNumber() != null) {
        contentJson.addProperty("highlight_number", postPartitionSearchOptions.highlightNumber());
    }
    if (postPartitionSearchOptions.highlightPostTag() != null) {
        contentJson.addProperty("highlight_post_tag", postPartitionSearchOptions.highlightPostTag());
    }
    if (postPartitionSearchOptions.highlightPreTag() != null) {
        contentJson.addProperty("highlight_pre_tag", postPartitionSearchOptions.highlightPreTag());
    }
    if (postPartitionSearchOptions.highlightSize() != null) {
        contentJson.addProperty("highlight_size", postPartitionSearchOptions.highlightSize());
    }
    if (postPartitionSearchOptions.includeDocs() != null) {
        contentJson.addProperty("include_docs", postPartitionSearchOptions.includeDocs());
    }
    if (postPartitionSearchOptions.includeFields() != null) {
        contentJson.add("include_fields", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postPartitionSearchOptions.includeFields()));
    }
    if (postPartitionSearchOptions.limit() != null) {
        contentJson.addProperty("limit", postPartitionSearchOptions.limit());
    }
    if (postPartitionSearchOptions.sort() != null) {
        contentJson.add("sort", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(postPartitionSearchOptions.sort()));
    }
    if (postPartitionSearchOptions.stale() != null) {
        contentJson.addProperty("stale", postPartitionSearchOptions.stale());
    }
    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)

Aggregations

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