在cloudant中构建Json的搜索索引

时间:2016-04-11 18:14:44

标签: json search indexing lucene 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"
}

我的问题是如何索引Json格式的“attributeCollection”中的属性。特别是,如何索引

"name": "Web Type",

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

1 个答案:

答案 0 :(得分:2)

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

  1. 在Cloudant信息中心中选择您的数据库,点击“设计文档”旁边的+,然后选择新搜索索引
  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 });
                }
             }
          }
       }
    }
    
  5. 您可以按如下方式对此索引发出查询:

    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
    

    注意:属性是步骤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#