How to check Contains in the view key in couch base

1.9k views Asked by At

I have a view like

    function (doc, meta)
    {
    if(doc.Tenant)
    {
    emit([doc.Tenant.Id,doc.Tenant.User.Name],doc);
    }
    }

In this view I want all the value belongs to Tenant.Id == 1 and User.Name where Contains "a"

I can search this in my C# by collecting all the Tenant data belongs to particular Tenant Id. But I have million of data for each Tenant. So need to check this in the server side itself. Is this possible to search.

1

There are 1 answers

0
scalabilitysolved On

I'm guessing that you want to be able to change which letter you are searching for in the string, unfortunately couchbase isn't going to be the best thing for this type of query.

If it will always be the letter 'a' that you want to search for then you could do a map like this and then query on the id.

function (doc, meta) {

  if(doc.Tenant) {
    var name = doc.Tenant.User.Name.toLowerCase();
    if(name.indexOf("a") > -1) {    
       emit(doc.Tenant.Id,null); 
    }
  } 
}

If however you want to be able to dynamically change which letter or even substring you want to search for in the name then you want to consider something like elasticsearch (great for text searching). Couchbase has an elasticsearch transport plugin that will automatically replicate to your elasticsearch node(s).

Here is a presentation on ES and Couchbase

http://www.slideshare.net/Couchbase/using-elasticsearch-and-couchbase-together-to-build-large-scale-applications

The documentation for installation and getting started with the ES plugin

http://docs.couchbase.com/couchbase-elastic-search/

And a cool tutorial detailing how to add a GUI on top of your ES data for easy filtering.

http://blog.jeroenreijn.com/2013/07/visitor-analysis-with-couchbase-elasticsearch.html