【问题标题】:Access specified attributes of Json for search index in cloudant访问 Cloudant 中搜索索引的 Json 的指定属性
【发布时间】:2016-05-02 08:13:58
【问题描述】:

我有一个 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"
}

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

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,它不起作用,它显示为空。

我知道“值”的结构喜欢数组(1个元素的数组),它没有任何属性名称。

如何正确访问该值?

更新:

当我将某些情况索引为:

1.它有效

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

2。它有效

index("default", name);

3.它不起作用

index("default", value);

4.我通过修改代码来获得“价值”为:

     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 });
    }

【问题讨论】:

    标签: json search indexing cloudant


    【解决方案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);
            }
        }
    } 
    

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

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

    【讨论】:

    • “值”以字符串形式返回,但为什么它在情况 6 和 7 中不起作用。
    • “值”总是一个字符串吗?当它是一个数组时它可能不起作用 - 如果你将它设置为 doc.attributeCollection.attributeArray[i].value 就会是这样。当您使用数组中的值而不是数组本身时,您是否遇到问题?
    • 我更新了我的响应以索引值数组中的每个元素。我不确定这是否是您的问题。
    • 这不是我的问题。你的更新看起来像我的情况。 4. 实际上,我想在字符串中获取“值”并调用 permuteword 函数。 permuateword 可以与“name”一起使用,但不能与“value”或“name + value”一起使用。所以我认为问题在于“价值”的回归。请您看一下案例 5、6 和 7。
    • 如何将“值”转换为字符串?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    相关资源
    最近更新 更多