在cloudant中访问搜索索引的Json的指定属性

时间:2016-05-02 08:13:58

标签: json search indexing cloudant

我有一个Json文档:

{
  "_id": "3de00db35e6549604c711e7295a1982a",
  "_rev": "1-ecba71644d341dfe5cb9abf6dd13b23a",
  "dateCreated": "2014-01-29 00:00:00",
  "attributeCollection": {
    "attributeArray": [
      {
        "updateable": false,
        "lookup": "issuetype",
        "issueAttributeDefinitionId": 13,
        "attributeType": 1,
        "name": "Web Type",
        "value": [
          "Improper Limitation of Authentication"
        ]
      },
      {
        "updateable": true,
        "lookup": "status",
        "issueAttributeDefinitionId": 1,
        "attributeType": 4,
        "name": "Status",
        "value": [
          "Access with Right Permission"
        ]
      }
    ]
  },
  "hash": "287030d6efa085b5b92b7106c0edb6d7"
}

我想使用" name"为文档创建搜索索引。和"价值" ("名称"或"值"仅)。我通过以下代码访问了这些属性:

for (var i=0; i<doc.attributeCollection.attributeArray.length; i++) {
    if (doc.attributeCollection.attributeArray[i].name) {
        name = doc.attributeCollection.attributeArray[i].name;
    }
    if (doc.attributeCollection.attributeArray[i].value) {
        value = doc.attributeCollection.attributeArray[i].value;
    }
}

当我使用contentindex = name + " "+ value时,它有效;内容显示"Web Type Improper Limitation of Authentication"。但是,如果我仅使用值contentindex = value,则它不起作用,它显示为空。

我知道&#34;值&#34;的结构喜欢array(包含1个元素的数组)并且它没有任何属性名称。

如何正确访问该值?

更新

当我将某些案例索引为:

1。它有效

var content=name + " " + value; index("default", content);

2。它有效

index("default", name);

第3。它不起作用

index("default", value);

4.我通过修改代码来修复以获得&#34;值&#34;为:

     if (doc.attributeCollection.attributeArray[i].value) {

     for (var j=0; j<doc.attributeCollection.attributeArray[i].value.length; j++){
       value = doc.attributeCollection.attributeArray[i].value[j];
       } 
     }

或者

 if (doc.attributeCollection.attributeArray[i].value) {
      value = doc.attributeCollection.attributeArray[i].value[0];
 }

适用于index("default", value);

然而,当我使用this post

中讨论的排列函数时

5。它有效

var content= permuteword(name);
    for(var k=0; k<content.length; k++){
         index("default", content[k], { store : true });
    }

6。它不起作用

var content= permuteword(value);
    for(var k=0; k<content.length; k++){
         index("default", content[k], { store : true });
    }

7。它不起作用

var content=name + " " +value;
var content1= permuteword(content);
    for(var k=0; k<content1.length; k++){
         index("default", content1[k], { store : true });
    }

1 个答案:

答案 0 :(得分:1)

我不确定我完全理解你的问题。听起来你正在创建一个像这样的索引:

for (var i=0; i<doc.attributeCollection.attributeArray.length; i++) {
    if (doc.attributeCollection.attributeArray[i].name) {
        name = doc.attributeCollection.attributeArray[i].name;
    }
    if (doc.attributeCollection.attributeArray[i].value) {
        value = doc.attributeCollection.attributeArray[i].value;
    }
    index("contentindex", name + " " + value);
}    

如果是这种情况,您可以尝试单独索引名称和值(更新:已更改以访问值数组中的每个元素):

for (var i=0; i<doc.attributeCollection.attributeArray.length; i++) {
    if (doc.attributeCollection.attributeArray[i].name) {
        name = doc.attributeCollection.attributeArray[i].name;
        index("contentindex", name);
    }
    if (doc.attributeCollection.attributeArray[i].value) {
        for (var j=0; j<doc.attributeCollection.attributeArray[i].value.length; j++) {
            value = doc.attributeCollection.attributeArray[i].value[j];
            index("contentindex", value);
        }
    }
} 

然后通过名称或值查询应该返回正确的结果。

如果我误解你的问题,请告诉我。

相关问题