Elasticsearch: has_child with outer join-like behavior
My Elasticsearch 6.x index contains documents of type "items" (parents) and "colors" (children), linked by "item"
{"item": "a", "type": "items"},
{"item": "b", "type": "items"},
{"item": "c", "type": "items"},
{"item": "d", "type": "items"},
{"item": "a", "color": "white", "type": "colors"}
{"item": "a", "color": "red", "type": "colors"}
{"item": "a", "color": "yellow", "type": "colors"}
{"item": "a", "color": "green", "type": "colors"}
{"item": "a", "color": "black", "type": "colors"}
{"item": "b", "color": "red", "type": "colors"}
{"item": "b", "color": "yellow", "type": "colors"}
{"item": "b", "color": "green", "type": "colors"}
{"item": "b", "color": "black", "type": "colors"}
{"item": "c", "color": "white", "type": "colors"}
{"item": "c", "color": "red", "type": "colors"}
{"item": "c", "color": "yellow", "type": "colors"}
{"item": "c", "color": "green", "type": "colors"}
{"item": "d", "color": "red", "type": "colors"}
{"item": "d", "color": "yellow", "type": "colors"}
{"item": "d", "color": "green", "type": "colors"}
Here items can have values a, b, c, d.
They can be one of the following color: white, yellow, red, green, black.
I am trying to write a single query that would return documents for type "items" that can be "white", but not "black" or "black", but not "white", or neither "white" nor "black". Given data presented above, this query would return:
{"item": "b", "type": "items"} // "black", but no "white"
{"item": "c", "type": "items"} // "white", but no "black"
{"item": "d", "type": "items"} // no "white", and no "black"
Query I tried:
{
"query": {
"bool": {
"must_not": [
{
"has_child": {
"max_children": 2147483647,
"min_children": 0,
"query": {
"bool": {
"must": [
{
"term": {
"color": {
"value": "black"
}
}
}
]
}
},
"type": "colors"
}
},
{
"has_child": {
"max_children": 2147483647,
"min_children": 0,
"query": {
"bool": {
"must": [
{
"term": {
"color": {
"value": "white"
}
}
}
]
}
},
"type": "colors"
}
}
]
}
}
}
returns all documents because what it does is "NOT(color=WHITE and color=BLACK)" and color=WHITE and color=BLACK is always FALSE, NOT FALSE = TRUE.
In SQL I could do it in one query using self and outer joins, which I can not do in Elastic.
I also tried (color=WHITE and color!=BLACK) OR (color=BLACK and color!=WHITE) OR (color!=WHITE and color!=BLACK), but that does not work, for the same reason.
Would appreciate any help.
elasticsearch
add a comment |
My Elasticsearch 6.x index contains documents of type "items" (parents) and "colors" (children), linked by "item"
{"item": "a", "type": "items"},
{"item": "b", "type": "items"},
{"item": "c", "type": "items"},
{"item": "d", "type": "items"},
{"item": "a", "color": "white", "type": "colors"}
{"item": "a", "color": "red", "type": "colors"}
{"item": "a", "color": "yellow", "type": "colors"}
{"item": "a", "color": "green", "type": "colors"}
{"item": "a", "color": "black", "type": "colors"}
{"item": "b", "color": "red", "type": "colors"}
{"item": "b", "color": "yellow", "type": "colors"}
{"item": "b", "color": "green", "type": "colors"}
{"item": "b", "color": "black", "type": "colors"}
{"item": "c", "color": "white", "type": "colors"}
{"item": "c", "color": "red", "type": "colors"}
{"item": "c", "color": "yellow", "type": "colors"}
{"item": "c", "color": "green", "type": "colors"}
{"item": "d", "color": "red", "type": "colors"}
{"item": "d", "color": "yellow", "type": "colors"}
{"item": "d", "color": "green", "type": "colors"}
Here items can have values a, b, c, d.
They can be one of the following color: white, yellow, red, green, black.
I am trying to write a single query that would return documents for type "items" that can be "white", but not "black" or "black", but not "white", or neither "white" nor "black". Given data presented above, this query would return:
{"item": "b", "type": "items"} // "black", but no "white"
{"item": "c", "type": "items"} // "white", but no "black"
{"item": "d", "type": "items"} // no "white", and no "black"
Query I tried:
{
"query": {
"bool": {
"must_not": [
{
"has_child": {
"max_children": 2147483647,
"min_children": 0,
"query": {
"bool": {
"must": [
{
"term": {
"color": {
"value": "black"
}
}
}
]
}
},
"type": "colors"
}
},
{
"has_child": {
"max_children": 2147483647,
"min_children": 0,
"query": {
"bool": {
"must": [
{
"term": {
"color": {
"value": "white"
}
}
}
]
}
},
"type": "colors"
}
}
]
}
}
}
returns all documents because what it does is "NOT(color=WHITE and color=BLACK)" and color=WHITE and color=BLACK is always FALSE, NOT FALSE = TRUE.
In SQL I could do it in one query using self and outer joins, which I can not do in Elastic.
I also tried (color=WHITE and color!=BLACK) OR (color=BLACK and color!=WHITE) OR (color!=WHITE and color!=BLACK), but that does not work, for the same reason.
Would appreciate any help.
elasticsearch
add a comment |
My Elasticsearch 6.x index contains documents of type "items" (parents) and "colors" (children), linked by "item"
{"item": "a", "type": "items"},
{"item": "b", "type": "items"},
{"item": "c", "type": "items"},
{"item": "d", "type": "items"},
{"item": "a", "color": "white", "type": "colors"}
{"item": "a", "color": "red", "type": "colors"}
{"item": "a", "color": "yellow", "type": "colors"}
{"item": "a", "color": "green", "type": "colors"}
{"item": "a", "color": "black", "type": "colors"}
{"item": "b", "color": "red", "type": "colors"}
{"item": "b", "color": "yellow", "type": "colors"}
{"item": "b", "color": "green", "type": "colors"}
{"item": "b", "color": "black", "type": "colors"}
{"item": "c", "color": "white", "type": "colors"}
{"item": "c", "color": "red", "type": "colors"}
{"item": "c", "color": "yellow", "type": "colors"}
{"item": "c", "color": "green", "type": "colors"}
{"item": "d", "color": "red", "type": "colors"}
{"item": "d", "color": "yellow", "type": "colors"}
{"item": "d", "color": "green", "type": "colors"}
Here items can have values a, b, c, d.
They can be one of the following color: white, yellow, red, green, black.
I am trying to write a single query that would return documents for type "items" that can be "white", but not "black" or "black", but not "white", or neither "white" nor "black". Given data presented above, this query would return:
{"item": "b", "type": "items"} // "black", but no "white"
{"item": "c", "type": "items"} // "white", but no "black"
{"item": "d", "type": "items"} // no "white", and no "black"
Query I tried:
{
"query": {
"bool": {
"must_not": [
{
"has_child": {
"max_children": 2147483647,
"min_children": 0,
"query": {
"bool": {
"must": [
{
"term": {
"color": {
"value": "black"
}
}
}
]
}
},
"type": "colors"
}
},
{
"has_child": {
"max_children": 2147483647,
"min_children": 0,
"query": {
"bool": {
"must": [
{
"term": {
"color": {
"value": "white"
}
}
}
]
}
},
"type": "colors"
}
}
]
}
}
}
returns all documents because what it does is "NOT(color=WHITE and color=BLACK)" and color=WHITE and color=BLACK is always FALSE, NOT FALSE = TRUE.
In SQL I could do it in one query using self and outer joins, which I can not do in Elastic.
I also tried (color=WHITE and color!=BLACK) OR (color=BLACK and color!=WHITE) OR (color!=WHITE and color!=BLACK), but that does not work, for the same reason.
Would appreciate any help.
elasticsearch
My Elasticsearch 6.x index contains documents of type "items" (parents) and "colors" (children), linked by "item"
{"item": "a", "type": "items"},
{"item": "b", "type": "items"},
{"item": "c", "type": "items"},
{"item": "d", "type": "items"},
{"item": "a", "color": "white", "type": "colors"}
{"item": "a", "color": "red", "type": "colors"}
{"item": "a", "color": "yellow", "type": "colors"}
{"item": "a", "color": "green", "type": "colors"}
{"item": "a", "color": "black", "type": "colors"}
{"item": "b", "color": "red", "type": "colors"}
{"item": "b", "color": "yellow", "type": "colors"}
{"item": "b", "color": "green", "type": "colors"}
{"item": "b", "color": "black", "type": "colors"}
{"item": "c", "color": "white", "type": "colors"}
{"item": "c", "color": "red", "type": "colors"}
{"item": "c", "color": "yellow", "type": "colors"}
{"item": "c", "color": "green", "type": "colors"}
{"item": "d", "color": "red", "type": "colors"}
{"item": "d", "color": "yellow", "type": "colors"}
{"item": "d", "color": "green", "type": "colors"}
Here items can have values a, b, c, d.
They can be one of the following color: white, yellow, red, green, black.
I am trying to write a single query that would return documents for type "items" that can be "white", but not "black" or "black", but not "white", or neither "white" nor "black". Given data presented above, this query would return:
{"item": "b", "type": "items"} // "black", but no "white"
{"item": "c", "type": "items"} // "white", but no "black"
{"item": "d", "type": "items"} // no "white", and no "black"
Query I tried:
{
"query": {
"bool": {
"must_not": [
{
"has_child": {
"max_children": 2147483647,
"min_children": 0,
"query": {
"bool": {
"must": [
{
"term": {
"color": {
"value": "black"
}
}
}
]
}
},
"type": "colors"
}
},
{
"has_child": {
"max_children": 2147483647,
"min_children": 0,
"query": {
"bool": {
"must": [
{
"term": {
"color": {
"value": "white"
}
}
}
]
}
},
"type": "colors"
}
}
]
}
}
}
returns all documents because what it does is "NOT(color=WHITE and color=BLACK)" and color=WHITE and color=BLACK is always FALSE, NOT FALSE = TRUE.
In SQL I could do it in one query using self and outer joins, which I can not do in Elastic.
I also tried (color=WHITE and color!=BLACK) OR (color=BLACK and color!=WHITE) OR (color!=WHITE and color!=BLACK), but that does not work, for the same reason.
Would appreciate any help.
elasticsearch
elasticsearch
asked Nov 17 '18 at 20:22
user5431794user5431794
134
134
add a comment |
add a comment |
0
active
oldest
votes
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%2f53355225%2felasticsearch-has-child-with-outer-join-like-behavior%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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%2f53355225%2felasticsearch-has-child-with-outer-join-like-behavior%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