ElasticSearch Relationships -


hi friends please me ...

i have doubt regarding relationships in elastic search.

i explain doubt following example

step 1: have created index named "books" in elastic search

example.com:9200/books/

step 2: created type named 'author' , data inserted it

post example.com:9200/books/author/1 { "fname" : "david","lname":"thomas" }

post example.com:9200/books/author/2 { "fname" : "hamton","lname":"vergo" }

step 3: created mapping type 'authorbook' child of 'author'

post example.com:9200/books/authorbook/_mapping

{"authorbook":{"_parent":{"type":"author"}}}

step 4 : inserted data authorbook

post example.com:9200/books/authorbook/100?parent=1 { "bookname" : "bookname1" }

post example.com:9200/books/authorbook/200?parent=1 { "bookname" : "bookname2" }

post example.com:9200/books/authorbook/300?parent=2 { "bookname" : "bookname3" }

step :5 created mapping type named 'publisher' , child of authorbook

post example.com:9200/books/publisher/_mapping

{"publisher":{"_parent":{"type":"authorbook"}}}

**step 6: inserted data publisher

post example.com:9200/books/publisher/50?parent=200 { "publname" : "publisher1" }

post example.com:9200/books/publisher/51?parent=200 { "publname" : "publisher2" }

post example.com:9200/books/publisher/52?parent=100 { "publname" : "publisher3" }

step 7 have achieved in getting 'authorbook' details using post request following body

{ "query": {     "has_parent": {         "type": "author",         "query": {             "filtered": {                 "query": {                     "match_all": {}                 },                 "filter": {                     "or": [                         {                             "term": {                                 "fname": "hamton"                             }                         },                         {                             "term": {                                 "fname": "david"                                  }                         }                     ]                 }             }         }     } }  }    

my requirement publisher details also..how can achieve that? pls help..

try this:

{ "query": {     "constant_score": {          "filter": {              "or": [                   {                       "has_parent": {                           "type": "author",                            "query": <your query above>                   },                   {                       "has_parent": {                           "type": "author"                           "query": {                               "constant_score": {                                    "filter": {                                        "has_parent": {                                            "type": "authorbook",                                            "query": <some query need publishers>                                        }                                    }                               }                           }                       }                   }               ]           }      } }    

this should return authorbooks , publishers in same query.

first tip: if use "query": {"match_all": {}} in filtered query can use constant_score query instead. have provide filter.

second tip: when indexing grandchild publisher better provide routing query parameter (which should point id of author). makes sure children , grandchildren stored in same shard makes lookup faster.

in general advice against complicated relationship though. can see query hard read , hard debug later on. maybe keeping simple better solution. idea have authorbook (or book parent , publisher , author normal (first level) children of book. reason author , publisher not related, don't need deep nesting.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -