Reputation: 545
I am using couchbase3 with spring-data-couchbase, and want to query data using spring data repository with multiple columns.
public interface UserAccountRepository extends CrudRepository<UserAccount, Long> {
public UserAccount findByEmail(Query eMail);
public UserAccount findByEmailAndStatus(Query query); // fn with multiple column, but not getting the result
}
How should I write Map function and Reduce function for the same?
For the function findByEmail(Query eMail); to work, I have added the view with Map fn()
function (doc, meta) {
emit(doc.email,doc);
}
This view have email as key, and value is the document. But if i need to query using email and status? How should the view look like ?
I have seen this link, but not very clear. https://stackoverflow.com/questions/28938755
Upvotes: 2
Views: 895
Reputation: 545
I was able make springdata function to invoke a compound Key view. My Document name is : Data Compound Key View
function (doc, meta) {
if(doc && doc._class == "com.couchbase.entity.Data"){
emit([doc.key1, doc.key2], doc);
}
}
SpringData Repository Inferface shown below :
package com.couchbase.repository;
import java.util.List;
import org.springframework.data.couchbase.core.view.View;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import com.couchbase.client.protocol.views.Query;
import com.couchbase.entity.Data;
public interface DataRepository extends CrudRepository<Data, String> {
@View(designDocument="Data",viewName="findByKey1AndKey2")
public List<Data> findByKey1AndKey2(Query query);
}
Test Class shown below :
import com.couchbase.client.protocol.views.ComplexKey;
import com.couchbase.client.protocol.views.Query;
public class DataTest extends WebAppConfigurationAware{
@Autowired
private DataRepository dataRepository;
@Test
public void testStringDataCompoundQuery(){
Object[] objArr = new Object[2];
objArr[0] = "aaa";
objArr[1] = 1;
Query query = new Query();
query.setKey(ComplexKey.of(objArr));
System.out.println(dataRepository.findByKey1AndKey2(query));
}
}
If this was useful for you, please up vote
Upvotes: 1
Reputation: 528
You could use compound key like describe in the documentation here: http://docs.couchbase.com/developer/dev-guide-3.0/compound-keys.html
Upvotes: 0