Note that there are some explanatory texts on larger screens.

plurals
  1. POCouchbase Views length - searching on multiple factors
    text
    copied!<p>Straight up, I have read: <a href="http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-views-writing-bestpractice.html" rel="nofollow">http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-views-writing-bestpractice.html</a> and various other pages on Couchbase site, but the following question just bothers me still so I want to double check before rolling this out.</p> <p>If I have a document about a product, say:</p> <pre><code>"DocumentType": "productDoc", "Name": "product", "Price": 150, "SellerID": 10, "DeliverableAUWide": true, "Colour": "red", "Size": "large" </code></pre> <p>say I want a product that is between 100 and 200 then:</p> <pre><code>if(doc.DocumentType == "productDoc" &amp;&amp; doc.Price) { emit([1, doc.Price], null) } </code></pre> <p>would get me what I want with a start and end key</p> <p>say I also want to search by size, then </p> <pre><code>if(doc.DocumentType == "productDoc" &amp;&amp; doc.Size) { emit([2, doc.Size], null) } </code></pre> <p>would get that again with correct key for search.</p> <p>say I want to search for both at the same time, then:</p> <pre><code>if(doc.DocumentType == "productDoc" &amp;&amp; doc.Size &amp;&amp; doc.Price) { emit([3, doc.Size, doc.Price], null) } </code></pre> <p>would get that. </p> <p>Now say I want to search by: Price, SellerID, AU deliverable or not, Colour and size....</p> <pre><code>if(doc.DocumentType == "productDoc" &amp;&amp; doc.Price &amp;&amp; doc.SellerID &amp;&amp; doc.DeliverableAUWide &amp;&amp; doc.Colour &amp;&amp; doc.Size ) { emit([4, doc.Price, doc.SellerID, doc.DeliverableAUWide, doc.Colour, doc.Size], null) } </code></pre> <p>would get that.</p> <p>But say I also want to be able to search by all of those expect price, I can't use the above, because Price will be null and hence the rest of that emit will be 'useless', because everything will be matched...</p> <p>so I would need a new view query?? e.g.</p> <p>if(doc.DocumentType == "productDoc" </p> <pre><code>&amp;&amp; doc.SellerID &amp;&amp; doc.DeliverableAUWide &amp;&amp; doc.Colour &amp;&amp; doc.Size ) { emit([5, doc.SellerID, doc.DeliverableAUWide, doc.Colour, doc.Size], null) } </code></pre> <p><strong>The Question</strong></p> <p>Is this approach correct because it seems that I would need a 'new' emit call for each type of search. So in the .net code I would check what I have re search input from the user and then call the right 'emit' (Note: that is why I have the number in the front of the emits so I can tell them apart later -- for sanity...). </p> <p>Not only am I concerned about the length of the view that I would have to write, but say I later add a field to the documents, like 'Discount Amount', then I change the view, the re index of that would be massive or? Is this a concern???</p> <p><strong>Possible alternative to above structure???</strong></p> <p>Or is it better to have say only,</p> <pre><code>if(doc.DocumentType == "productDoc" &amp;&amp; doc.Price) { emit([1, doc.Price], null) } if(doc.DocumentType == "productDoc" &amp;&amp; doc.Size) { emit([2, doc.Size], null) } </code></pre> <p>and then when I want a product by price and size call both and get effectively 'lists' of document ids and then 'intersect' these lists and see which id's are in both and then make calls to get the documents. This approach but has significantly more calls to the CB sever and also I would not be able to use the built in skip, limit and startkey_docid for paging. This also seem more performance intensive in the code side. I am presuming this is the 'wrong' mindset for Couchbase, but coming from an environment where the mentality is "less calls to DB = better" I might not be embracing the CB philosophy correctly.... </p> <p>If someone can please:</p> <ol> <li>Confirm the first approach is correct...</li> <li>the second is wrong </li> </ol> <p>that would be great. </p> <p>Please let me know if something does not make sense...</p> <p>Thanks in advance,</p> <p>Cheers</p> <p>Robin</p> <p>Other note: this document is the only document structure that would be in the bucket. I would only have 1 view. 10k documents ~ish. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload