2016-04-11 7 views
1

サンプルdbの検索インデックスを構築して、雲の中で厄介な検索クエリを実行しました。例えば、私のようにデータベースを持っていた: "_id" または "クラス" をインデックス化するために雲の中のJsonの検索インデックスを構築する

{ 
    "_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" 
} 

私は、検索インデックスを作成することができますように:

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

または

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

しかし、I 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を」のインデックスプロパティを、以下のとおりです。たとえば、私はJSON形式を持っています。特に、どのように私はあなたがサブ文書の配列フィールドにJSONのインデックスを作成することができると信じていませんが、照会のための検索インデックスを作成することができます

"name": "Web Type", 

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

Nguyen、あなたが解決しようとしていた使用例は、名前や値のフィルタリングに関連していたと仮定します。私は下でそれを行うためのソリューションを提供しましたが、 "json"インデックスは使用しません。以下の答えがあなたのユースケースを解決しない場合は、不足している箇所を教えてください。ありがとう! – markwatsonatx

答えて

2

のインデックスへnamevalueのフィールドがドキュメント構造に基づいています。 Cloudantダッシュボードで

  1. データベースを選択し、ドキュメントをデザインし、新しい検索インデックスを選択する+次へ]をタップします。
  2. インデックス機能については、以下を入力します(例をby_name_value。)インデックスの名前を指定し
  3. (EX _design /属性。)
  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 

注:ここでは

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

がある:属性は、ステップ2と by_name_value で指定された設計文書の名前が3

クエリは、復号ステップで指定したインデックスの名前ですこのクエリのサンプル応答:

{ 
    "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#

関連する問題