Search in sources :

Example 1 with Lookup

use of com.couchbase.client.core.message.kv.subdoc.multi.Lookup in project try-cb-java by couchbaselabs.

the class Hotel method extractResultOrThrow.

/**
 * Extract a FTS result or throw if there is an issue.
 */
private List<Map<String, Object>> extractResultOrThrow(SearchQueryResult result) {
    if (!result.status().isSuccess()) {
        LOGGER.warn("Query returned with errors: " + result.errors());
        throw new DataRetrievalFailureException("Query error: " + result.errors());
    }
    List<Map<String, Object>> content = new ArrayList<Map<String, Object>>();
    for (SearchQueryRow row : result) {
        DocumentFragment<Lookup> fragment = bucket.lookupIn(row.id()).get("country").get("city").get("state").get("address").get("name").get("description").execute();
        Map<String, Object> map = new HashMap<String, Object>();
        String country = (String) fragment.content("country");
        String city = (String) fragment.content("city");
        String state = (String) fragment.content("state");
        String address = (String) fragment.content("address");
        StringBuilder fullAddr = new StringBuilder();
        if (address != null)
            fullAddr.append(address).append(", ");
        if (city != null)
            fullAddr.append(city).append(", ");
        if (state != null)
            fullAddr.append(state).append(", ");
        if (country != null)
            fullAddr.append(country);
        if (fullAddr.length() > 2 && fullAddr.charAt(fullAddr.length() - 2) == ',')
            fullAddr.delete(fullAddr.length() - 2, fullAddr.length() - 1);
        map.put("name", fragment.content("name"));
        map.put("description", fragment.content("description"));
        map.put("address", fullAddr.toString());
        content.add(map);
    }
    return content;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Lookup(com.couchbase.client.core.message.kv.subdoc.multi.Lookup) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) HashMap(java.util.HashMap) Map(java.util.Map) SearchQueryRow(com.couchbase.client.java.search.result.SearchQueryRow)

Example 2 with Lookup

use of com.couchbase.client.core.message.kv.subdoc.multi.Lookup in project jans by JanssenProject.

the class ManualCouchbaseEntryManagerTest method sampleSessionId.

// manual
@Test(enabled = false)
public void sampleSessionId() throws IOException, SearchException {
    CouchbaseEntryManager manager = createCouchbaseEntryManager();
    try {
        SessionId sessionId = createSessionId();
        manager.persist(sessionId);
        final String key = "sessions_" + sessionId.getId();
        System.out.println("Key: " + key + ", ttl:" + sessionId.getTtl());
        Bucket sessionBucket = manager.getOperationService().getConnectionProvider().getBucketMapping("sessions").getBucket();
        final JsonDocument lookup = sessionBucket.get(key);
        System.out.println("expiry: " + lookup.expiry());
        DocumentFragment<Lookup> ttl = sessionBucket.lookupIn(key).get("$document.exptime", new SubdocOptionsBuilder().xattr(true)).execute();
        System.out.println("ttl: " + ttl.content("$document.exptime"));
        updateSession(sessionId);
        manager.merge(sessionId);
        final JsonDocument lookup2 = manager.getOperationService().getConnectionProvider().getBucketMapping("sessions").getBucket().get(key);
        System.out.println("expiry after update: " + lookup2.expiry());
    } finally {
        manager.destroy();
    }
}
Also used : SubdocOptionsBuilder(com.couchbase.client.java.subdoc.SubdocOptionsBuilder) Bucket(com.couchbase.client.java.Bucket) CouchbaseEntryManager(io.jans.orm.couchbase.impl.CouchbaseEntryManager) Lookup(com.couchbase.client.core.message.kv.subdoc.multi.Lookup) JsonDocument(com.couchbase.client.java.document.JsonDocument) Test(org.testng.annotations.Test)

Aggregations

Lookup (com.couchbase.client.core.message.kv.subdoc.multi.Lookup)2 Bucket (com.couchbase.client.java.Bucket)1 JsonDocument (com.couchbase.client.java.document.JsonDocument)1 SearchQueryRow (com.couchbase.client.java.search.result.SearchQueryRow)1 SubdocOptionsBuilder (com.couchbase.client.java.subdoc.SubdocOptionsBuilder)1 CouchbaseEntryManager (io.jans.orm.couchbase.impl.CouchbaseEntryManager)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DataRetrievalFailureException (org.springframework.dao.DataRetrievalFailureException)1 Test (org.testng.annotations.Test)1