Wildcard Search using Couchbase Views

#1

Hello,

Probably I am asking a question which might have been answered many times in this forum. But I could find a concrete answer when I searched for. Here is my question -

I understand the concept of how Couchbase multi-key index works. But we have a requirement to search for keys of a specific pattern. This is very similar of using the wildcard search with % option. I see that this option is available in N1QL, so I am wondering if this is also available in Coucbase views so that we can specify a pattern to look for the keys in the views.

Thanks,
Kris

#2

Views is ranged based, so you cannot specify a wildcard. What you can do is specify a range. If your wildcard can be expressed as a range, then it will work. Also note that the equivalent of “wildcards” in views will only work via something like fixed% and not %fixed.

To give you an idea, assuming your map() function emits the city of each entry, you can search for all cities starting with ne like so:

startkey=["ne"]&endkey=["ne\uefff"] – all this really does is say “give me all keys with the upper range of ‘ne’ and the lower range of ‘ne’ followed by anything else”.

The above would be the equivalent of ne% or ne*.

#3

Thanks Mark. This helps.

So, how does the fixed% option work in terms of multi-key? I believe I can use it only on the last key. Is that right?

Another question along the same lines. How do I do a “Not equal to” query on views? I mean the normal != or <> operator that we have in any other database languages. Is there any option for that?

Thanks,
K

#4

You can apply this pattern at any level – you just need to make sure the previous keys contain the ranges for their minimum and maximum value, so for example, if your view emits [state,city] you can do something like:

startkey=["", "ne"]&endkey=["\uefff", "ne\uefff"]

Regarding the next question, you can’t do a not equal, because views does range-based searches you need to express your query in terms of things you can include, not exclude :slight_smile:

#5

Thank you very much. Appreciate your quick replies!

#6

I am facing an issue. I have the below Emit statement.

emit([doc.customerName, doc.age, doc.sex, doc.clientId, doc.state, doc.city], [doc]);

I am querying like this
startkey=["", 1, “”, 0, “”, “”]&endkey=[“A\uefff”,15,“Female”,0,"",""]

Now I expect that I get only the records that Start with customerName starting with ‘A’, age between 0-15 and sex is ‘Female’.

But it returns me all the records where customerName starting with ‘A’. It just ignores my rest of the range specifications.

Please let me know if I am doing something wrong.

1 Like