2016-09-22 10 views
0

私は、次のような構造にマップしたいと思います: を - 私は、ブログの記事 を持っている - ブログの投稿は、コメント を持つことができます - コメントは(もコメントしている)の返信を持つことができるので、それはする必要があります再帰的なデータ構造Elasticsearchネストされた親子マッピング

POST ----- * - > COMMENT

COMMENT ----- * ---> COMMENT

は、ここに私が試したものです:

mappings: { 
    "comment": { 
     "properties": { 
      "content": { "type": "string" }, 
      "replies": { "type": "comment" } 
     } 
    }, 
    "post": { 
     "properties": { 
      "comments": { 
        "type": "comment" 
      } 
     } 
    } 
} 

もちろん動作しません。どうすればこれを達成できますか?

答えて

1

OOプログラミングで行うように型を宣言しようとしていますが、これはESでの動作とは異なります。次のようにparent-child relationshipsを使用する必要があります。つまり、postにはcommentsというフィールドはありませんが、commentマッピングタイプにはpost親タイプを参照する_parentメタフィールドがあります。

返信をモデル化するには、返信が関係するコメントのIDを含むin_reply_toという別のフィールドを単に持つことをお勧めします。そんなに簡単!

PUT blogs 
{ 
    "mappings": { 
    "post": { 
     "properties": { 
     "title": { "type": "string"} 
     } 
    }, 
    "comment": { 
     "_parent": { 
     "type": "post" 
     }, 
     "properties": { 
     "id": { 
      "type": "long" 
     } 
     "content": { 
      "type": "string" 
     }, 
     "in_reply_to": { 
      "type": "long" 
     } 
     } 
    } 
    } 
} 
+0

合法的であるように思われます。コメントに対する返信は、投稿への返信と考えることもできます。 'in_reply_to'フィールドは、コメントへの返答を結合します。ありがとう! – maestro

+0

素晴らしい、嬉しい助け! – Val

0
mappings: { 
    "post": { 
     "properties": { 
      "content": { "type": "string" }, 
      "comment": { 
       "properties" : { 
        "content": { "type": "string" }, 
        "replies": { 
         "properties" : { 
          "content": { "type": "string" } 
         } 
        } 
     } 
    } 
} 
+0

返信することもできます – maestro

+0

この場合、コメントと同じ階層構造をもう1つ追加するか、より多くのレベルがある場合は親マッピング手法を使用できます(https://www.elastic.co/guide/en/elasticsearch/reference/current /mapping-parent-field.html) –

+0

リンクされたドキュメントから: ''親子型は異なっていなければなりません - 同じ型のドキュメント間で親子関係を確立することはできません。 – maestro

関連する問題