1

I need to search the CouchDB based on several criteria entered in a form. Name, an array of Tags and so on. I would then need various views to index on these fields. Ultimately, all the results will be collated in data.js and provided to mustache.html. Say there are 3 views - docsByName, docsByTags, docsById.

What I don't know is, how to query all these views in query.js. Can this be done and how ?

Or should the approach be of that to write one view that makes multiple emits for each search somehow ?

Thank you.

soumya
  • 278
  • 3
  • 8

1 Answers1

1

From what you say I assume you are using Evently, so I will quote from Evently primer:

The async function is the main star, which in this case makes an Ajax request (but it can do anything it wants). Another important thing to note is that the first argument to the async function is a callback which you use to tell Evently when you are done with your asynchronous action. [...] Whatever you pass to the callback function then becomes the first item passed to the data function.

In short: put your Ajax requests in async.js.

As a side note: Evently is only one of the possible choices to write a couchapp and it is not clear if it is maintained. However it works and it is easy to rearrange the code to not use it.

EDIT: here is a sample async function (cut&paste from an old program):

function(cb, e) {
  var app = $$(this).app
    ;
  app.db.openDoc('SOMEDOCID', {
    error: function(code, error, reason) {
      alert("Error("+code+" "+error+"): "+reason);
    }
  , success: function(doc) {
      app.view('SOMEVIEWNAME', {
        include_docs: true
      , error: function(code, error, reason) {
          alert("Error("+code+" "+error+"): "+reason);
        }
      , success: function(resp) {
          resp.doc = doc;
          cb(resp);
        }
      });
    }
  });
}
Marcello Nuccio
  • 3,901
  • 2
  • 28
  • 28
  • Thank you for that useful hint. I missed mentioning, yes I am using evently and a total newbie at this whole thing. Since async.js bogged me down a bit with starting trouble, I tried a different way and found something that works for me. – soumya May 23 '11 at 14:13
  • What I am doing now is - do multiple emits with partial strings for all text fields on my 'form', emit all tags individually. The keys now in the query is an array of all the field values (strings) and selected tags (my app allows selection of tags only). I get the results as required. E.g. All documents that have city = 'rado' (partial text), tags =['a', 'b'], docId = 'myDocId'. The documents fetched now include those having Colorado or whatever that matches the pattern. I have no idea what this means for performance though, for large volume of data :( – soumya May 23 '11 at 14:20
  • Having said that, could you please throw a little more light on the async.js way of doing it ? I couldn't figure out how to specify the url in $.ajax(). Should I call this multiple times for all the views that I have ? Any idea would get me a step ahead. Thank you. – soumya May 23 '11 at 14:23
  • Thanks a lot, that was a great help! – soumya May 24 '11 at 14:36
  • you are welcome. Please remember to vote up if you really found my answer useful. – Marcello Nuccio May 24 '11 at 17:06
  • I am sorry, i have not enough reputation to vote up, I can only tick it as useful for the time being. – soumya May 25 '11 at 12:48
  • Does this actually answer the question? I thought the question was about how to allows users to query on multiple keys at the same time? – Nick Perkins Aug 03 '11 at 20:21
  • In the sample code, I do two queries: one for a document by id (using `app.db.openDoc()`), and one for a view (using `app.view()`). Obviously you can repeat the same pattern, to query as many views or documents as you like. – Marcello Nuccio Aug 08 '11 at 12:37