Ido Shilon
Ido Shilon

Reputation: 405

Sorting and filtering multiple attributes in Couchbase

I have a document with several different attributes. In my app I want to do filtering on one of the doc attributes and possibility sorting by any of the other attribute.

i.e

{
"accountId": "qa32176148",
"id": "122230310111",
"rtSessionId": "qa321761484294973207",
"sessionStartTime": 1387705538426,
"visitorLastUpdateTime": 1387705717094,
"countryCode": "US",
"state": "NY",
"city": "New York",
"country": "United States",
"organization": "Google",
"ipAddress": "216.239.39.99",
"agentNickName": "john"
}

  1. I want to filter by each one of these fields, Should I create a views for each one of the fields ?
  2. (as an example) Can I filer on state and sort by city?

Upvotes: 1

Views: 390

Answers (1)

m03geek
m03geek

Reputation: 2538

  1. Basucally, yes, you need to create separate view for each attribute (in case, if you want filter only by one attribute). If you also need ability to filter results by two or more attributes in one query you will also need a view per each attribute combination. But I don't understand about what did you mean composite index (may be if you saw some example, it would be greate if you show it). As I understand composite index it will not suite this situation at all.

  2. Yes, in most cases. But you can do that only if you expect exact match on attribute X, i.e. X == 1 (not range: X == 1 ... 6). To sort by Y attribute, just append it to your emit function after X attribute: emit([X,Y], null) and use startKey and endKey params. I.e. startKey=[1], endkey=[1,{}].

Now I return to your first question. I think that case is used in some kind of search operation. So there is good couchbase extension for elasticsearch (also see this page), that will allow you much more flexebility in filtering and ordering without creating many views.

Upvotes: 1

Related Questions