elasticSearch get data from two indices in one object
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
Don't think it is possible. You would require that to be managed at yourapplication/service
layer or denormalize the data in such a way that you'd havesingle index
, querying which would display all the required information. Other alternatives would be to make use ofnested
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
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
elasticsearch elasticsearch-5
asked Nov 10 at 19:55
Marat Tynarbekov
478
478
Don't think it is possible. You would require that to be managed at yourapplication/service
layer or denormalize the data in such a way that you'd havesingle index
, querying which would display all the required information. Other alternatives would be to make use ofnested
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
add a comment |
Don't think it is possible. You would require that to be managed at yourapplication/service
layer or denormalize the data in such a way that you'd havesingle index
, querying which would display all the required information. Other alternatives would be to make use ofnested
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
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
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
add a comment |
up vote
0
down vote
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 :)
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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
add a comment |
up vote
0
down vote
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
add a comment |
up vote
0
down vote
up vote
0
down vote
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
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
answered Nov 10 at 21:37
ibexit
608313
608313
add a comment |
add a comment |
up vote
0
down vote
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 :)
add a comment |
up vote
0
down vote
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 :)
add a comment |
up vote
0
down vote
up vote
0
down vote
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 :)
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 :)
answered Nov 10 at 22:33
Kamal
1,203820
1,203820
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 havesingle index
, querying which would display all the required information. Other alternatives would be to make use ofnested
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