How to realize complex search filters in couchdb?

2019-06-25 04:40发布

I want to administrate my User-entities in a grid. I want to sort them and I want to have a search filter for each column.

My dynamic generated temporary view works fine:

function(doc){
  if(doc.type === 'User' && 
    // Dynamic filters: WHERE firstName LIKE '%jim%' AND lastName LIKE '%knopf%'
    (doc.firstName.match(/.*?jim.*?/i) && 
    doc.lastName.match(/.*?knopf.*?/i)) ) {

    // Dynamic sort
    emit(doc.lastName, doc);
  }
}

BUT everywhere is written you have to AVOID temporary views. IS there a better way? Should I save these searches on demand at runtime?

Thanks

1条回答
smile是对你的礼貌
2楼-- · 2019-06-25 05:18

You should definitely not use temporary views, as they have to be recomputed every single time they are queried. (which is a very "expensive" process) A stored view is perfect when you know the fields you are searching ahead of time. (it builds the index one time, only making incremental changes after that)

However, you won't be able to get "contains" searches. (you can get exact matches and "starts with" matches, but that's not what your example shows) If you need ad-hoc querying, you should seriously consider couchdb-lucene.

查看更多
登录 后发表回答