elasticSearch get data from two indices in one object












0














I have index users and posts.
In post index I have user_id parameter, and when I search post in posts index using user_id, I should get this post and user data full in one object.
How I can send search query in two indices ?










share|improve this question






















  • Don't think it is possible. You would require that to be managed at your application/service layer or denormalize the data in such a way that you'd have single index, querying which would display all the required information. Other alternatives would be to make use of nested datatype. Refer to this link for more info: elastic.co/guide/en/elasticsearch/reference/current/…
    – Kamal
    Nov 10 at 20:09












  • Kamal, thanks for answer. And can you give one example in my case of this nested datatype ? I have very little time for this
    – Marat Tynarbekov
    Nov 10 at 20:14












  • I've posted an answer below, Marat. Hope it would help!
    – Kamal
    Nov 10 at 22:35
















0














I have index users and posts.
In post index I have user_id parameter, and when I search post in posts index using user_id, I should get this post and user data full in one object.
How I can send search query in two indices ?










share|improve this question






















  • Don't think it is possible. You would require that to be managed at your application/service layer or denormalize the data in such a way that you'd have single index, querying which would display all the required information. Other alternatives would be to make use of nested datatype. Refer to this link for more info: elastic.co/guide/en/elasticsearch/reference/current/…
    – Kamal
    Nov 10 at 20:09












  • Kamal, thanks for answer. And can you give one example in my case of this nested datatype ? I have very little time for this
    – Marat Tynarbekov
    Nov 10 at 20:14












  • I've posted an answer below, Marat. Hope it would help!
    – Kamal
    Nov 10 at 22:35














0












0








0







I have index users and posts.
In post index I have user_id parameter, and when I search post in posts index using user_id, I should get this post and user data full in one object.
How I can send search query in two indices ?










share|improve this question













I have index users and posts.
In post index I have user_id parameter, and when I search post in posts index using user_id, I should get this post and user data full in one object.
How I can send search query in two indices ?







elasticsearch elasticsearch-5






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 19:55









Marat Tynarbekov

498




498












  • Don't think it is possible. You would require that to be managed at your application/service layer or denormalize the data in such a way that you'd have single index, querying which would display all the required information. Other alternatives would be to make use of nested datatype. Refer to this link for more info: elastic.co/guide/en/elasticsearch/reference/current/…
    – Kamal
    Nov 10 at 20:09












  • Kamal, thanks for answer. And can you give one example in my case of this nested datatype ? I have very little time for this
    – Marat Tynarbekov
    Nov 10 at 20:14












  • I've posted an answer below, Marat. Hope it would help!
    – Kamal
    Nov 10 at 22:35


















  • Don't think it is possible. You would require that to be managed at your application/service layer or denormalize the data in such a way that you'd have single index, querying which would display all the required information. Other alternatives would be to make use of nested datatype. Refer to this link for more info: elastic.co/guide/en/elasticsearch/reference/current/…
    – Kamal
    Nov 10 at 20:09












  • Kamal, thanks for answer. And can you give one example in my case of this nested datatype ? I have very little time for this
    – Marat Tynarbekov
    Nov 10 at 20:14












  • I've posted an answer below, Marat. Hope it would help!
    – Kamal
    Nov 10 at 22:35
















Don't think it is possible. You would require that to be managed at your application/service layer or denormalize the data in such a way that you'd have single index, querying which would display all the required information. Other alternatives would be to make use of nested datatype. Refer to this link for more info: elastic.co/guide/en/elasticsearch/reference/current/…
– Kamal
Nov 10 at 20:09






Don't think it is possible. You would require that to be managed at your application/service layer or denormalize the data in such a way that you'd have single index, querying which would display all the required information. Other alternatives would be to make use of nested datatype. Refer to this link for more info: elastic.co/guide/en/elasticsearch/reference/current/…
– Kamal
Nov 10 at 20:09














Kamal, thanks for answer. And can you give one example in my case of this nested datatype ? I have very little time for this
– Marat Tynarbekov
Nov 10 at 20:14






Kamal, thanks for answer. And can you give one example in my case of this nested datatype ? I have very little time for this
– Marat Tynarbekov
Nov 10 at 20:14














I've posted an answer below, Marat. Hope it would help!
– Kamal
Nov 10 at 22:35




I've posted an answer below, Marat. Hope it would help!
– Kamal
Nov 10 at 22:35












2 Answers
2






active

oldest

votes


















1














Please have a look at the multi search feature: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html



The response is a array of the search response and status for each search request preserving the order of the multi search request






share|improve this answer





























    0














    I have created the below data models as a sample. My index would have data model in the below format.



    Posts:
    - post_id
    - title
    - description
    * comments
    - user_id
    - firstname
    - comment

    * - meaning multiple values


    Basically what I am doing is saving all the data of a single post in a single document.



    Sample Mapping



    PUT post
    {
    "mappings":{
    "mydocs":{
    "properties":{
    "comments":{
    "type":"nested",
    "properties":{
    "userid":{
    "type":"text"
    },
    "firstname":{
    "type":"text"
    },
    "comment":{
    "type":"text"
    }
    }
    },
    "post_id":{
    "type":"text"
    },
    "post_description":{
    "type":"text"
    },
    "post_title":{
    "type":"text"
    },
    "owner":{
    "type":"text"
    }
    }
    }
    }
    }


    Sample Document



    POST post/mydocs/1
    {
    "post_id": "1",
    "owner": "1",
    "post_description": "I'm doing some analysis on this and its very confusing. Can anyone help me here?",
    "post_title": "neo4j vs elasticsearch",
    "comments": [
    {
    "userid": "2",
    "firstname": "John",
    "comment": "Both are totally different here"
    },
    {
    "userid": "3",
    "firstname": "Jack",
    "comment": "Depends on the user case, doesn't it. "
    }
    ]

    }


    Sample Query



    POST post/_search
    {
    "_source":[
    "post_id",
    "comments.userid",
    "comments.firstname"
    ],
    "query":{
    "bool":{
    "must":[
    {
    "match_all":{} // you can put any condition here
    },
    {
    "nested":{
    "path":"comments",
    "query":{
    "match":{
    "comments.userid":"2"
    }
    }
    }
    }
    ]
    }
    }
    }


    Well it may not be perfect and might looks vague/amusing, however I hope this would help you in your understanding.



    Infact you can actually check stackoverflow's data model(entity called POST) and their elasticsearch implementation. Refer to this LINK to see how they've modeled their post in their rdbms database and this LINK to see how they've created index for the very same table.



    I'm really hoping this helps :)






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242856%2felasticsearch-get-data-from-two-indices-in-one-object%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Please have a look at the multi search feature: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html



      The response is a array of the search response and status for each search request preserving the order of the multi search request






      share|improve this answer


























        1














        Please have a look at the multi search feature: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html



        The response is a array of the search response and status for each search request preserving the order of the multi search request






        share|improve this answer
























          1












          1








          1






          Please have a look at the multi search feature: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html



          The response is a array of the search response and status for each search request preserving the order of the multi search request






          share|improve this answer












          Please have a look at the multi search feature: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html



          The response is a array of the search response and status for each search request preserving the order of the multi search request







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 21:37









          ibexit

          760313




          760313

























              0














              I have created the below data models as a sample. My index would have data model in the below format.



              Posts:
              - post_id
              - title
              - description
              * comments
              - user_id
              - firstname
              - comment

              * - meaning multiple values


              Basically what I am doing is saving all the data of a single post in a single document.



              Sample Mapping



              PUT post
              {
              "mappings":{
              "mydocs":{
              "properties":{
              "comments":{
              "type":"nested",
              "properties":{
              "userid":{
              "type":"text"
              },
              "firstname":{
              "type":"text"
              },
              "comment":{
              "type":"text"
              }
              }
              },
              "post_id":{
              "type":"text"
              },
              "post_description":{
              "type":"text"
              },
              "post_title":{
              "type":"text"
              },
              "owner":{
              "type":"text"
              }
              }
              }
              }
              }


              Sample Document



              POST post/mydocs/1
              {
              "post_id": "1",
              "owner": "1",
              "post_description": "I'm doing some analysis on this and its very confusing. Can anyone help me here?",
              "post_title": "neo4j vs elasticsearch",
              "comments": [
              {
              "userid": "2",
              "firstname": "John",
              "comment": "Both are totally different here"
              },
              {
              "userid": "3",
              "firstname": "Jack",
              "comment": "Depends on the user case, doesn't it. "
              }
              ]

              }


              Sample Query



              POST post/_search
              {
              "_source":[
              "post_id",
              "comments.userid",
              "comments.firstname"
              ],
              "query":{
              "bool":{
              "must":[
              {
              "match_all":{} // you can put any condition here
              },
              {
              "nested":{
              "path":"comments",
              "query":{
              "match":{
              "comments.userid":"2"
              }
              }
              }
              }
              ]
              }
              }
              }


              Well it may not be perfect and might looks vague/amusing, however I hope this would help you in your understanding.



              Infact you can actually check stackoverflow's data model(entity called POST) and their elasticsearch implementation. Refer to this LINK to see how they've modeled their post in their rdbms database and this LINK to see how they've created index for the very same table.



              I'm really hoping this helps :)






              share|improve this answer


























                0














                I have created the below data models as a sample. My index would have data model in the below format.



                Posts:
                - post_id
                - title
                - description
                * comments
                - user_id
                - firstname
                - comment

                * - meaning multiple values


                Basically what I am doing is saving all the data of a single post in a single document.



                Sample Mapping



                PUT post
                {
                "mappings":{
                "mydocs":{
                "properties":{
                "comments":{
                "type":"nested",
                "properties":{
                "userid":{
                "type":"text"
                },
                "firstname":{
                "type":"text"
                },
                "comment":{
                "type":"text"
                }
                }
                },
                "post_id":{
                "type":"text"
                },
                "post_description":{
                "type":"text"
                },
                "post_title":{
                "type":"text"
                },
                "owner":{
                "type":"text"
                }
                }
                }
                }
                }


                Sample Document



                POST post/mydocs/1
                {
                "post_id": "1",
                "owner": "1",
                "post_description": "I'm doing some analysis on this and its very confusing. Can anyone help me here?",
                "post_title": "neo4j vs elasticsearch",
                "comments": [
                {
                "userid": "2",
                "firstname": "John",
                "comment": "Both are totally different here"
                },
                {
                "userid": "3",
                "firstname": "Jack",
                "comment": "Depends on the user case, doesn't it. "
                }
                ]

                }


                Sample Query



                POST post/_search
                {
                "_source":[
                "post_id",
                "comments.userid",
                "comments.firstname"
                ],
                "query":{
                "bool":{
                "must":[
                {
                "match_all":{} // you can put any condition here
                },
                {
                "nested":{
                "path":"comments",
                "query":{
                "match":{
                "comments.userid":"2"
                }
                }
                }
                }
                ]
                }
                }
                }


                Well it may not be perfect and might looks vague/amusing, however I hope this would help you in your understanding.



                Infact you can actually check stackoverflow's data model(entity called POST) and their elasticsearch implementation. Refer to this LINK to see how they've modeled their post in their rdbms database and this LINK to see how they've created index for the very same table.



                I'm really hoping this helps :)






                share|improve this answer
























                  0












                  0








                  0






                  I have created the below data models as a sample. My index would have data model in the below format.



                  Posts:
                  - post_id
                  - title
                  - description
                  * comments
                  - user_id
                  - firstname
                  - comment

                  * - meaning multiple values


                  Basically what I am doing is saving all the data of a single post in a single document.



                  Sample Mapping



                  PUT post
                  {
                  "mappings":{
                  "mydocs":{
                  "properties":{
                  "comments":{
                  "type":"nested",
                  "properties":{
                  "userid":{
                  "type":"text"
                  },
                  "firstname":{
                  "type":"text"
                  },
                  "comment":{
                  "type":"text"
                  }
                  }
                  },
                  "post_id":{
                  "type":"text"
                  },
                  "post_description":{
                  "type":"text"
                  },
                  "post_title":{
                  "type":"text"
                  },
                  "owner":{
                  "type":"text"
                  }
                  }
                  }
                  }
                  }


                  Sample Document



                  POST post/mydocs/1
                  {
                  "post_id": "1",
                  "owner": "1",
                  "post_description": "I'm doing some analysis on this and its very confusing. Can anyone help me here?",
                  "post_title": "neo4j vs elasticsearch",
                  "comments": [
                  {
                  "userid": "2",
                  "firstname": "John",
                  "comment": "Both are totally different here"
                  },
                  {
                  "userid": "3",
                  "firstname": "Jack",
                  "comment": "Depends on the user case, doesn't it. "
                  }
                  ]

                  }


                  Sample Query



                  POST post/_search
                  {
                  "_source":[
                  "post_id",
                  "comments.userid",
                  "comments.firstname"
                  ],
                  "query":{
                  "bool":{
                  "must":[
                  {
                  "match_all":{} // you can put any condition here
                  },
                  {
                  "nested":{
                  "path":"comments",
                  "query":{
                  "match":{
                  "comments.userid":"2"
                  }
                  }
                  }
                  }
                  ]
                  }
                  }
                  }


                  Well it may not be perfect and might looks vague/amusing, however I hope this would help you in your understanding.



                  Infact you can actually check stackoverflow's data model(entity called POST) and their elasticsearch implementation. Refer to this LINK to see how they've modeled their post in their rdbms database and this LINK to see how they've created index for the very same table.



                  I'm really hoping this helps :)






                  share|improve this answer












                  I have created the below data models as a sample. My index would have data model in the below format.



                  Posts:
                  - post_id
                  - title
                  - description
                  * comments
                  - user_id
                  - firstname
                  - comment

                  * - meaning multiple values


                  Basically what I am doing is saving all the data of a single post in a single document.



                  Sample Mapping



                  PUT post
                  {
                  "mappings":{
                  "mydocs":{
                  "properties":{
                  "comments":{
                  "type":"nested",
                  "properties":{
                  "userid":{
                  "type":"text"
                  },
                  "firstname":{
                  "type":"text"
                  },
                  "comment":{
                  "type":"text"
                  }
                  }
                  },
                  "post_id":{
                  "type":"text"
                  },
                  "post_description":{
                  "type":"text"
                  },
                  "post_title":{
                  "type":"text"
                  },
                  "owner":{
                  "type":"text"
                  }
                  }
                  }
                  }
                  }


                  Sample Document



                  POST post/mydocs/1
                  {
                  "post_id": "1",
                  "owner": "1",
                  "post_description": "I'm doing some analysis on this and its very confusing. Can anyone help me here?",
                  "post_title": "neo4j vs elasticsearch",
                  "comments": [
                  {
                  "userid": "2",
                  "firstname": "John",
                  "comment": "Both are totally different here"
                  },
                  {
                  "userid": "3",
                  "firstname": "Jack",
                  "comment": "Depends on the user case, doesn't it. "
                  }
                  ]

                  }


                  Sample Query



                  POST post/_search
                  {
                  "_source":[
                  "post_id",
                  "comments.userid",
                  "comments.firstname"
                  ],
                  "query":{
                  "bool":{
                  "must":[
                  {
                  "match_all":{} // you can put any condition here
                  },
                  {
                  "nested":{
                  "path":"comments",
                  "query":{
                  "match":{
                  "comments.userid":"2"
                  }
                  }
                  }
                  }
                  ]
                  }
                  }
                  }


                  Well it may not be perfect and might looks vague/amusing, however I hope this would help you in your understanding.



                  Infact you can actually check stackoverflow's data model(entity called POST) and their elasticsearch implementation. Refer to this LINK to see how they've modeled their post in their rdbms database and this LINK to see how they've created index for the very same table.



                  I'm really hoping this helps :)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 22:33









                  Kamal

                  1,6221820




                  1,6221820






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242856%2felasticsearch-get-data-from-two-indices-in-one-object%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud

                      Zucchini