Query exists view by n1ql

Is it possible to query an exists view by n1ql?
As I know,View is on Data Service side, n1ql is on Query Service side.If I want to query data from view and n1ql, the data service and the query service should both be open to my app.
so if I can query view by n1ql, I only need to open the query service to my app will be better architecture.

You can create indexes that use the Map Reduce Indexer and N1QL will consider those while optimizing queries. However the hand crafted map/reduce views are not accessible at the moment from N1QL.
Is there something you want to do with m/r views that you cannot do through N1QL queries or are you asking because you find this model more convenient for your use case?

do you means that all Map/Reduce views can implement by n1ql? I am not sure how should I convert it.

@cihangirb I am thinking about when should I using m/r, and when should I using n1ql.

  1. I will priority use n1ql query data(create index using view) by m/r, so when I using n1ql, I can scaling query services, and I do not need to open port of couchbase m/r(such as 8092), Am I right?
  2. Can you give me a example on how to create a index using view?

1- N1ql will reach views through 8092 to it needs to be accessible to n1ql nodes but noone else needs access from outside unless you want to query views directly using View REST API.
2- if you’d like to use view as your indexer for N1QL, simply use USING VIEW with the CREATE INDEX statement instead of USING GSI. GSI is the default option so it isn’t required to have USING GSI but for views you do need this additional verb at the end.

BTW, There is a good matrix here that compare views vs global secondary indexes (gsi):
http://developer.couchbase.com/documentation/server/4.1/architecture/gsi-versus-views.html - this may help you make a call on the indexer you’d like to use.


@cihangirb Thank you very much.
from the docs, “N1QL does not support user defined MapReduce definitions and restricts the definitions to a subset of capabilities only available through the CREATE INDEX statement.”.
I want to know that does this means that If I have a complexity m/r, I can not use n1ql to instead of it?

For example,I have a doc like this:


and I want to emit name and level by type and level_members,so have a view like this:

function (doc, meta) {
             if (doc.type == "MTP" && doc.name){
                            for (var i in doc.lev1_members) {
                            for (var i in doc.lev2_members) {

can I create index by using view to use n1ql to instead of view?
If yes, how?
what kinds of view can use n1ql to instead of?

N1QL can only use views when an index is created using the View indexer.
CREATE INDEX … USING VIEW; is the only way to use view index with N1QL.

Stepping back, N1QL can use 2 of the indexers in the system: GSI or Views. The reason for not having any view available to N1QL is we need a specific shape in an index to optimize queries. That is why we tie down the view side but this isn’t a perpetual situation. we do plan to make views more accessible in future from N1QL. Right now, my recommendation is to use N1QL and create the best indexes for your query using N1QL and use Views for cases where you need precalculated aggregations for example interactive reports etc.