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 ?

3
  • 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/… Nov 10, 2018 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 Nov 10, 2018 at 20:14
  • I've posted an answer below, Marat. Hope it would help! Nov 10, 2018 at 22:35

2 Answers 2

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

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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.