1

I've been developing using Couchbase Server 4.0 and upgrading to 5.0 is down the road but not at the moment.

But for now I do need to search views (map/reduce) using text search, on Couchbase Lite .Net there is PostFilter that serve the purpose.

But I could not find the same settings on Couchnode, checking the Couchbase Lite .Net told me that a query option might help.

I tried couple of things like this:

  query.options.filter = r => {
    console.log('******', r)
    return true
  }
  query.options.post_filter = r => {
    console.log('******', r)
    return true
  }
  query.options.postFilter = r => {
    console.log('******', r)
    return true
  }

but nothing seems to work. Anyone experienced this before please help!!

borrrden
  • 33,256
  • 8
  • 74
  • 109
Chinh Nguyen
  • 583
  • 1
  • 7
  • 14

1 Answers1

1

On Couchbase server, map/reduce queries are created on the Server cluster itself, not created in the SDK like with Couchbase Lite. An example:

function(doc, meta)
{
  emit(doc.name, [doc.city, doc.salary]);
}

When you create a view, you give it a name. You can call these view from the Node SDK (couchnode) by name like so:

var couchbase = require('couchbase');
var ViewQuery = couchbase.ViewQuery;

var query = ViewQuery.from('beer', 'by_name');

See the documentation: https://docs.couchbase.com/server/4.0/developer-guide/views-writing.html and https://docs.couchbase.com/nodejs-sdk/2.6/view-queries-with-sdk.html

Matthew Groves
  • 25,181
  • 9
  • 71
  • 121
  • Your map function is a really good example for my case. I want to query rows with name containing some value which equal to SQL `LIKE %searchstring%`. I'm well aware of the N1QL but in my case it is NOT (yet) an option. For more detail, I could do this using Couchbase Lite .NET PostFilter, but I could not find the same equivalent on Couchnode. If this is not an option, then I think I have to do the Filtering in memory page by page. – Chinh Nguyen Dec 18 '18 at 08:31
  • You can write a map reduce function that accomplishes that (use `indexOf`, for instance). You can call the map reduce view with couchnode, but the view must be created on the cluster (through the Couchbase UI for instance). – Matthew Groves Dec 18 '18 at 13:22
  • 1
    That's a good choice except writing inside Map/Reduce function will fix the search term and that is not what I'm looking for. Thanks! – Chinh Nguyen Dec 18 '18 at 14:54
  • 1
    Ah, you're right. You can do some matching based on keys, but it would be a workaround at best. Server 4.0 does have N1QL though, so you're aren't completely out of luck. There's also an Elasticsearch connector for 4.0, so that would be another option. – Matthew Groves Dec 18 '18 at 18:26
  • 1
    Yay! Looking forward to upgrading to 4.0, 5.0 and even 6.0 ... for now, I must stick with in-memory filtering. Thanks for your comment! – Chinh Nguyen Dec 19 '18 at 07:38