Search in sources :

Example 1 with IndexField

use of com.cloudant.client.api.model.IndexField in project java-cloudant by cloudant.

the class Database method getFindByIndexBody.

// private helper methods
/**
 * @param selector sanitized selector JSON object (excluding "selector" key)
 * @param options find by index options
 * @return query object to POST
 */
private JsonObject getFindByIndexBody(JsonObject selector, FindByIndexOptions options) {
    JsonArray fieldsArray = new JsonArray();
    if (options.getFields().size() > 0) {
        for (String field : options.getFields()) {
            JsonPrimitive jsonField = client.getGson().fromJson(field, JsonPrimitive.class);
            fieldsArray.add(jsonField);
        }
    }
    JsonArray sortArray = new JsonArray();
    if (options.getSort().size() > 0) {
        for (IndexField sort : options.getSort()) {
            JsonObject sortObject = new JsonObject();
            sortObject.addProperty(sort.getName(), sort.getOrder().toString());
            sortArray.add(sortObject);
        }
    }
    JsonObject indexObject = new JsonObject();
    indexObject.add("selector", selector);
    if (fieldsArray.size() > 0) {
        indexObject.add("fields", fieldsArray);
    }
    if (sortArray.size() > 0) {
        indexObject.add("sort", sortArray);
    }
    if (options.getLimit() != null) {
        indexObject.addProperty("limit", options.getLimit());
    }
    if (options.getSkip() != null) {
        indexObject.addProperty("skip", options.getSkip());
    }
    if (options.getReadQuorum() != null) {
        indexObject.addProperty("r", options.getReadQuorum());
    }
    if (options.getUseIndex() != null) {
        indexObject.add("use_index", getGson().fromJson(options.getUseIndex(), JsonElement.class));
    }
    return indexObject;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) CouchDbUtil.getAsString(com.cloudant.client.org.lightcouch.internal.CouchDbUtil.getAsString) IndexField(com.cloudant.client.api.model.IndexField)

Example 2 with IndexField

use of com.cloudant.client.api.model.IndexField in project java-cloudant by cloudant.

the class Database method createIndex.

/**
 * Create a new JSON index
 * <P>
 * Example usage creating an index that sorts ascending on name, then by year:
 * </P>
 * <pre>
 * {@code
 * db.createIndex("Person_name", "Person_name", null, new IndexField[]{
 *       new IndexField("Person_name",SortOrder.asc),
 *       new IndexField("Movie_year",SortOrder.asc)});
 * }
 * </pre>
 * <P>
 * Example usage creating an index that sorts ascending by year:
 * </P>
 * <pre>
 * {@code
 * db.createIndex("Movie_year", "Movie_year", null, new IndexField[]{
 *      new IndexField("Movie_year",SortOrder.asc)});
 * }
 * </pre>
 *
 * @param indexName     optional name of the index (if not provided one will be generated)
 * @param designDocName optional name of the design doc in which the index will be created
 * @param indexType     optional, type of index (only "json" for this method)
 * @param fields        array of fields in the index
 * @see Database#createIndex(String)
 */
@Deprecated
public void createIndex(String indexName, String designDocName, String indexType, IndexField[] fields) {
    if (indexType == null || "json".equalsIgnoreCase(indexType)) {
        JsonIndex.Builder b = JsonIndex.builder().name(indexName).designDocument(designDocName);
        for (IndexField f : fields) {
            switch(f.getOrder()) {
                case desc:
                    b.desc(f.getName());
                    break;
                case asc:
                default:
                    b.asc(f.getName());
                    break;
            }
        }
        createIndex(b.definition());
    } else {
        throw new CouchDbException("Unsupported index type " + indexType);
    }
}
Also used : CouchDbException(com.cloudant.client.org.lightcouch.CouchDbException) JsonIndex(com.cloudant.client.api.query.JsonIndex) IndexField(com.cloudant.client.api.model.IndexField)

Aggregations

IndexField (com.cloudant.client.api.model.IndexField)2 JsonIndex (com.cloudant.client.api.query.JsonIndex)1 CouchDbException (com.cloudant.client.org.lightcouch.CouchDbException)1 CouchDbUtil.getAsString (com.cloudant.client.org.lightcouch.internal.CouchDbUtil.getAsString)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1