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.
You're mixing two concepts there. The index definition is for a N1QL query, though it does create a view. Typically, if you create the index through a N1QL query, you'll query with N1QL.
The query you're running is on the view created by it. My suspicion is that you need to publish it or use the full_set parameter against the development view. It may be better to stick with a N1QL query.