4

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

koalabruder
  • 2,794
  • 9
  • 33
  • 40

1 Answers1

4

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.

Dominic Barnes
  • 28,083
  • 8
  • 65
  • 90
  • You could also try Cloudant's recently released Cloudant Search - http://blog.cloudant.com/announcing-cloudant-search/ – Matt Passell Jul 16 '11 at 16:26
  • What do you mean with "recomputed every single time"? Are simple SQL-Statements recomputed every time too? Perhaps I need this complex view only one single time. – koalabruder Jul 18 '11 at 12:06
  • Most of the time, SQL commands are sped greatly up by indexes that are in place. This can be a Primary Key, or an Indexed field. A CouchDB map/reduce index has a bit more involved compared to a relational database's index, which gives it much more flexibility. – Dominic Barnes Jul 18 '11 at 13:15
  • Is the SQL statement "WHERE firstName LIKE '%jim%' AND lastName LIKE '%knopf%'" sped up by indexes? – koalabruder Jul 19 '11 at 11:57