I am using scala to connect with bucket and insert data in bucket.
case class User(
firstName: String,
lastName: String,
userName: String,
email: String)
bucket.upsert(SerializableDocument.create("usr::" + user.email,user))
I am able to insert and retrieve data from bucket. Now I want to create view/secondary index on firstName field of user.
val ensureIndex = Query.simple("CREATE INDEX firstName ON `user_account`(firstName)");
val queryResult = bucket.query(ensureIndex)
val queryResult = bucket.query(ViewQuery.from("dev_ddl_firstName", "firstName"))
But I am getting 0 as result of queryResult.totalRows().
Can anyone help me a correct way for creating view/secondary index on field in couchbase?
Thanks in advance.
additionally to what Matt Ingenthron said,
SerializableDocument
in the Java SDK is storing data with a binary flag. Views can only index content of JSON document, not binary ones...You can either use the marshaller of your choice to transform your instance to a JSON String and use the
RawJsonDocument
with it, or let the SDK marshall it with Jackson by doing a conversion of your document to aJsonObject
(a SDK simple JSON manipulation class, but more meant for Java) and use theJsonDocument
.