elasticSearch get data from two indices in one object
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 |
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 |
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
498
498
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
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 |
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 |
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
});
}
});
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
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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
760313
760313
add a comment |
add a comment |
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 |
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 |
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,6221820
1,6221820
add a comment |
add a comment |
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.
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