在 Cloudant 中为 Json 构建搜索索引

Building search index for Json in cloudant

我已经为样本数据库建立了搜索索引,并且 运行 在 cloudant 中建立了一个搜索查询。例如,我有一个数据库为:

{
  "_id": "aardvark",
  "_rev": "3-fe45a3e06244adbe7ba145e74e57aba5",
  "min_weight": 40,
  "max_weight": 65,
  "min_length": 1,
  "max_length": 2.2,
  "latin_name": "Orycteropus afer",
  "wiki_page": "http://en.wikipedia.org/wiki/Aardvark",
  "class": "mammal",
  "diet": "omnivore"
}

为了索引“_id”或 "class" 我可以创建搜索索引:

function(doc){
  index("default", doc._id);
...
}

function(doc){
  index("default", doc.class);
... 
}

但是,我不知道如何在 Json 格式上建立索引。例如,我的 Json 格式为:

  "_id": "08ff683d86484139",
  "_rev": "4-cf6f34c6a2a22780a646b86a3f8d1848",
  "lastUpdated": "2014-01-31 00:00:00",
  "issueId": 62655,
  "isThirdParty": true,
  "dateCreated": "2014-01-29 00:00:00",
  "attributeCollection": {
    "attributeArray": [
      {
        "updateable": false,
        "lookup": "issuetype",
        "issueAttributeDefinitionId": 13,
        "attributeType": 1,
        "name": "Web Type",
        "value": [
          "Improper Neutralization of Input During Web Page Generation"
        ]
      },
 "appReleaseId": 57,
  "hash": "953b33eca52938ab2d21e27eb171998b"
}

我的问题是如何索引 "attributeCollection" 或 Json 格式的属性。特别是,如何为

建立索引
"name": "Web Type",

"value": ["Improper Neutralization of Input During Web Page Generation"] 

我不相信你可以在子文档的数组字段上创建一个 json 索引,但是你可以创建一个搜索索引来查询 namevalue基于您的文档结构的字段。

  1. 在 Cloudant 仪表板 select 您的数据库中,点击设计文档旁边的 +,然后选择 新建搜索索引
  2. 为设计文档指定一个名称(例如 _design/attributes)
  3. 为索引指定一个名称(例如 by_name_value)
  4. 为索引函数输入以下内容:

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

您可以按如下方式对该索引发出查询:

https://<yourcloudanthost>/<databasename>
/_design/attributes
/_search/by_name_value
?limit=10
&q=name:%27Web+Type%27+OR+value:%27Improper%20Neutralization%20of%20Input%20During%20Web%20Page%20Generation%27
&include_docs=true

注:attributes为步骤2中指定的设计文档名称,by_name_value为在步骤 3 中指定的索引。

已解码的查询:

&q=
  name:'Web Type'
    OR 
  value:'Improper Neutralization of Input During Web Page Generation'

以下是此查询的示例响应:

{
   "total_rows":1,
    "bookmark":"g2wAAAABaANkAChkYmNvcmVAZGIyLmJtLWRhbC1zdGFuZGFyZDIuY2xvdWRhbnQubmV0bAAAAAJiQAAAAGJf____amgCRj9_92eAAAAAYQBq",
    "rows":[
      {
         "id":"08ff683d86484139",
         "order":[
            0.0078043024986982346,
            0
         ],
         "fields":{
            "name":"Web Type",
            "value":"Improper Neutralization of Input During Web Page Generation"
         },
         "doc":{
            "_id":"08ff683d86484139",
            "_rev":"1-f4f6b73bbf3420412a5619e74f4cae00",
            "lastUpdated":"2014-01-31 00:00:00",
            "issueId":62655,
            "isThirdParty":true,
            "dateCreated":"2014-01-29 00:00:00",
            "attributeCollection":{
               "attributeArray":[
                  {
                     "updateable":false,
                     "lookup":"issuetype",
                     "issueAttributeDefinitionId":13,
                     "attributeType":1,
                     "name":"Web Type",
                     "value":[
                        "Improper Neutralization of Input During Web Page Generation"
                     ]
                  }
               ]
            },
            "appReleaseId":57,
            "hash":"953b33eca52938ab2d21e27eb171998b"
         }
      }
   ]
}

您可以在此处详细了解如何创建和查询搜索索引:

https://docs.cloudant.com/search.html#