Explain the detail of create index using view

n1ql
#1

when I create a index using:

CREATE INDEX **idx4view2name** ON sample(name) USING VIEW;

I found that after creating the index, a view named idx4view2name in Production Views under ddl_idx4view2name was created,and when the view looks like this:

function (doc, meta) {
  if (meta.type != "json") return;
 
  var stringToUtf8Bytes = function (str) {
    var utf8 = unescape(encodeURIComponent(str));
    var bytes = [];
    for (var i = 0; i < str.length; ++i) {
        bytes.push(str.charCodeAt(i));
    }
    return bytes;
  };
 
  var indexFormattedValue = function (val) {
    if (val === null) {
      return [64];
    } else if (typeof val == "boolean") {
      return [96, val];
    } else if (typeof val == "number") {
      return [128, val];
    } else if (typeof val == "string") {
      return [160, stringToUtf8Bytes(val)];
    } else if (typeof val == "object") {
      if (val instanceof Array) {
        return [192, val];
      } else {
        var innerKeys = [];
        for (var k in val) {
          innerKeys.push(k);
        }
        innerKeys.sort()
        var innerVals = [];
        for (var i in innerKeys) {
          if (typeof val[innerKeys[i]] == "object" && 
            (val[innerKeys[i]] === null || Object.keys(val[innerKeys[i]]).length === 0)) {
            if (val[innerKeys[i]] === null) {
                innerVals.push([64])
            } else if (Object.keys(val[innerKeys[i]]).length === 0) {
              innerVals.push([224, [[], []]]);
            }
          } else {
            innerVals.push(indexFormattedValue(val[innerKeys[i]]));
          }
        }
        return [224, [innerKeys, innerVals]];
      }
    } else {
        return undefined;
    }
  };
 
  var key1 = indexFormattedValue(doc.name);
  var key = [key1];
  var pos = key.indexOf(undefined);
  if (pos == 0) {
    return;
  } else if (pos > 0) {
    key.splice(pos)
  }
 
  emit(key, null);
}
// salt: 565289788

and when I query the view, the result shows as following:

{"total_rows":1457,"rows":[
{"id":"doc:1db9cf92-4de1-41b0-a0a2-a883617a481d","key":**[[160,[66,85,83,58,48,53,68,52,48,56,53,56,45,54,48,51,69,45,52,67,68,65,45,56,48,48,53,45,50,54,65,56,70,68,69,67,53,65,53,70]]]**,"value":null},
..........

can anyone explain what this view mean? what the key means?

#2

Hi,

View indexes created using N1QL should only be used for N1QL queries. The view index should not queried directly as a view. If you want to query a view directly, please create it using JavaScript and provide your own logic as desired.

Thanks.