【问题标题】:Building search index for Json in cloudant在 cloudant 中为 Json 建立搜索索引
【发布时间】:2016-04-11 18:14:43
【问题描述】:

我已经为示例数据库建立了搜索索引,并在 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”或“类”,我可以创建搜索索引:

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"] 

【问题讨论】:

  • Nguyen,我认为您尝试解决的用例与过滤名称和/或值有关。我在下面提供了一个解决方案,但它不使用“json”索引。如果下面的答案不能解决您的用例,请告诉我它缺少的地方。谢谢!

标签: json search indexing lucene cloudant


【解决方案1】:

我不相信您可以在子文档的数组字段上创建 json 索引,但您可以根据您的文档结构创建用于查询 namevalue 字段的搜索索引。

  1. 在 Cloudant 仪表板中选择您的数据库,点击 Design Documents 旁边的 +,然后选择 New Search Index
  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#

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    相关资源
    最近更新 更多