2017-05-11 3 views
2

Angenommen, es gibt einen einfachen Blog-Index, der zwei Typen enthält: Blog und Kommentar. Ein Blog kann mehrere Kommentare enthalten. Der Index wird wie dieser Elasticsearch: Eltern-Kind-Beziehung nach Rollover

erstellt
curl -X PUT \ 
    'http://localhost:9200/%3Cblog-%7Bnow%2Fd%7D-000001%3E?pretty=' \ 
    -H 'content-type: application/json' \ 
    -d '{ 
    "mappings": { 
     "comment": { 
      "_parent": { "type": "blog" }, 
      "properties": { 
       "name": { "type": "keyword" }, 
       "comment": { "type": "text" } 
      } 
     }, 
     "blog": { 
      "properties": { 
       "author": { "type": "keyword" }, 
       "subject": { "type": "text" }, 
       "content": { "type": "text" } 
      } 
     } 
    } 
}' 

Der Index ist %3Cblog-%7Bnow%2Fd%7D-000001%3E gleich <blog-{now/d}-000001> (here für mehr über Datum Mathematik sehen). Wir werden diesem Index einen 'blog-aktiven' Alias ​​hinzufügen. Dieser Alias ​​wird zum Speichern von Daten verwendet.

curl -X POST 'http://localhost:9200/_aliases?pretty=' \ 
    -H 'content-type: application/json' \ 
    -d '{ "actions" : [ { "add" : { "index" : "blog-*", "alias" : "blog-active" } } ] }' 

Nun, wenn wir tun, um die folgenden Aktionen aus:

1.Add ein Blog mit blog-active alias

curl -X POST http://localhost:9200/blog-active/blog/1 \ 
    -H 'content-type: application/json' \ 
    -d '{ 
     "author": "author1", 
     "subject": "subject1", 
     "content": "content1" 
    }' 

2.Add einen Kommentar zu dem Blog

curl -X POST \ 
    'http://localhost:9200/blog-active/comment/1?parent=1' \ 
    -H 'content-type: application/json' \ 
    -d '{ 
    "name": "commenter1", 
    "comment": "new comment1" 
}' 

3 . Führen Sie einen Rollover mit max_docs = 2

curl -X POST \ 
    http://localhost:9200/blog-active/_rollover \ 
    -H 'content-type: application/json' \ 
    -d '{ 
    "conditions": { 
    "max_docs": 2 
    }, 
    "mappings": { 
    "comment": { 
     "_parent": { "type": "blog" }, 
     "properties": { 
     "name": { "type": "keyword" }, 
     "comment": { "type": "text" } 
     } 
    }, 
    "blog": { 
     "properties": { 
     "author": { "type": "keyword" }, 
     "subject": { "type": "text" }, 
     "content": { "type": "text" } 
     } 
    } 
    } 
}' 

4.Und fügen Sie eine weitere Kommentar zum Blog

curl -X POST \ 
    'http://localhost:9200/blog-active/comment/1?parent=1' \ 
    -H 'content-type: application/json' \ 
    -d '{ 
    "name": "commenter2", 
    "comment": "new comment2" 
}' 

Wenn wir nun alle Blog-Indizes für alle Kommentare zu 'author1 Blogs mit Suche (blog-%2A ist blog-*)

curl -X POST \ 
    http://localhost:9200/blog-%2A/comment/_search \ 
    -H 'content-type: application/json' \ 
    -d '{ 
    "query": { 
     "has_parent" : { 
     "query" : { 
      "match" : { "author" : { "query" : "author1" } } 
     }, 
     "parent_type" : "blog" 
     } 
    } 
}' 

die Ergebnis enthält nur den ersten Kommentar.

Dies ist aufgrund der Tatsache, dass der zweite Kommentar im zweiten Index ist, die nicht über Eltern-Blog-Dokument in sich hat. So weiß es nicht über den Autor des Blogs.

blog indices

Also, meine Frage ist, wie gehe ich Eltern-Kind-Beziehungen, wenn Rollover verwendet wird?

Ist die Beziehung in diesem Fall sogar möglich?

ähnliche Frage: ElasticSearch parent/child on different indexes

Antwort

0

Alle Dokumente, den Teil einer Eltern-Kind-Beziehung bilden müssen im gleichen Index leben, mehr kostbar gleiches Shard. Daher ist es nicht möglich, Eltern-Kind-Beziehung zu haben, wenn Rollover verwendet wird, da es neue Indizes erstellt.

Eine Lösung für das obige Problem könnte sein, Daten zu denormalisieren, indem die Felder blog_author und blog_id in den Typ comment hinzugefügt werden. Die Abbildung in diesem Fall wird wie folgt aussehen (beachten Sie, dass die Eltern-Kind-Beziehung entfernt wurde):

"mappings": { 
    "comment": { 
    "properties": { 
     "blog_id": { "type": "keyword" }, 
     "blog_author": { "type": "keyword" }, 
     "name": { "type": "keyword" }, 
     "comment": { "type": "text" } 
    } 
    }, 
    "blog": { 
    "properties": { 
     "author": { "type": "keyword" }, 
     "subject": { "type": "text" }, 
     "content": { "type": "text" } 
    } 
    } 
} 

und die Abfrage Kommentare von Blog-Autor zu holen ist:

curl -X POST \ 
    http://localhost:9200/blog-%2A/comment/_search \ 
    -H 'cache-control: no-cache' \ 
    -H 'content-type: application/json' \ 
    -d '{ 
    "query": { 
    "match": { 
     "blog_author": "user1" 
    } 
    } 
}'