How do I query a null date inside an array in elasticsearch?
In an elasticsearch query I am trying to search Document objects that have an array of approval notifications. The notifications are considered complete when dateCompleted
is populated with a date, and considered pending when either dateCompleted
doesn't exist or exists with null
. If the document does not contain an array of approval notifications then it is out of the scope of the search.
I am aware of putting null_value
for field dateCompleted
and setting it to some arbitrary old date but that seems hackish to me.
I've tried to use Bool queries with must exist doc.approvalNotifications and must not exist doc.approvalNotifications.dateCompleted
but that does not work if a document contains a mix of complete and pending approvalNotifications. e.g. it only returns document with ID 2 below. I am expecting documents with IDs 1 and 2 to be found.
How can I find pending approval notifications using elasticsearch?
PUT my_index/_mapping/Document
"properties" :
"doc" :
"properties" :
"approvalNotifications" :
"properties" :
"approvalBatchId" :
"type" : "text",
"fields" :
"keyword" :
"type" : "keyword",
"ignore_above" : 256
,
"approvalTransitionState" :
"type" : "text",
"fields" :
"keyword" :
"type" : "keyword",
"ignore_above" : 256
,
"approvedByUser" :
"type" : "text",
"fields" :
"keyword" :
"type" : "keyword",
"ignore_above" : 256
,
"dateCompleted" :
"type" : "date"
Documents:
"id": 1,
"status": "Pending Notifications",
"approvalNotifications": [
"approvalBatchId": "e6c39194-5475-4168-9729-8ddcf46cf9ab",
"dateCompleted": "2018-11-15T16:09:15.346+0000"
,
"approvalBatchId": "05eaeb5d-d802-4a28-b699-5e593a59d445",
]
"id": 2,
"status": "Pending Notifications",
"approvalNotifications": [
"approvalBatchId": "e6c39194-5475-4168-9729-8ddcf46cf9ab",
]
"id": 3,
"status": "Complete",
"approvalNotifications": [
"approvalBatchId": "e6c39194-5475-4168-9729-8ddcf46cf9ab",
"dateCompleted": "2018-11-15T16:09:15.346+0000"
,
"approvalBatchId": "05eaeb5d-d802-4a28-b699-5e593a59d445",
"dateCompleted": "2018-11-16T16:09:15.346+0000"
]
"id": 4
"status": "No Notifications"
elasticsearch
add a comment |
In an elasticsearch query I am trying to search Document objects that have an array of approval notifications. The notifications are considered complete when dateCompleted
is populated with a date, and considered pending when either dateCompleted
doesn't exist or exists with null
. If the document does not contain an array of approval notifications then it is out of the scope of the search.
I am aware of putting null_value
for field dateCompleted
and setting it to some arbitrary old date but that seems hackish to me.
I've tried to use Bool queries with must exist doc.approvalNotifications and must not exist doc.approvalNotifications.dateCompleted
but that does not work if a document contains a mix of complete and pending approvalNotifications. e.g. it only returns document with ID 2 below. I am expecting documents with IDs 1 and 2 to be found.
How can I find pending approval notifications using elasticsearch?
PUT my_index/_mapping/Document
"properties" :
"doc" :
"properties" :
"approvalNotifications" :
"properties" :
"approvalBatchId" :
"type" : "text",
"fields" :
"keyword" :
"type" : "keyword",
"ignore_above" : 256
,
"approvalTransitionState" :
"type" : "text",
"fields" :
"keyword" :
"type" : "keyword",
"ignore_above" : 256
,
"approvedByUser" :
"type" : "text",
"fields" :
"keyword" :
"type" : "keyword",
"ignore_above" : 256
,
"dateCompleted" :
"type" : "date"
Documents:
"id": 1,
"status": "Pending Notifications",
"approvalNotifications": [
"approvalBatchId": "e6c39194-5475-4168-9729-8ddcf46cf9ab",
"dateCompleted": "2018-11-15T16:09:15.346+0000"
,
"approvalBatchId": "05eaeb5d-d802-4a28-b699-5e593a59d445",
]
"id": 2,
"status": "Pending Notifications",
"approvalNotifications": [
"approvalBatchId": "e6c39194-5475-4168-9729-8ddcf46cf9ab",
]
"id": 3,
"status": "Complete",
"approvalNotifications": [
"approvalBatchId": "e6c39194-5475-4168-9729-8ddcf46cf9ab",
"dateCompleted": "2018-11-15T16:09:15.346+0000"
,
"approvalBatchId": "05eaeb5d-d802-4a28-b699-5e593a59d445",
"dateCompleted": "2018-11-16T16:09:15.346+0000"
]
"id": 4
"status": "No Notifications"
elasticsearch
add a comment |
In an elasticsearch query I am trying to search Document objects that have an array of approval notifications. The notifications are considered complete when dateCompleted
is populated with a date, and considered pending when either dateCompleted
doesn't exist or exists with null
. If the document does not contain an array of approval notifications then it is out of the scope of the search.
I am aware of putting null_value
for field dateCompleted
and setting it to some arbitrary old date but that seems hackish to me.
I've tried to use Bool queries with must exist doc.approvalNotifications and must not exist doc.approvalNotifications.dateCompleted
but that does not work if a document contains a mix of complete and pending approvalNotifications. e.g. it only returns document with ID 2 below. I am expecting documents with IDs 1 and 2 to be found.
How can I find pending approval notifications using elasticsearch?
PUT my_index/_mapping/Document
"properties" :
"doc" :
"properties" :
"approvalNotifications" :
"properties" :
"approvalBatchId" :
"type" : "text",
"fields" :
"keyword" :
"type" : "keyword",
"ignore_above" : 256
,
"approvalTransitionState" :
"type" : "text",
"fields" :
"keyword" :
"type" : "keyword",
"ignore_above" : 256
,
"approvedByUser" :
"type" : "text",
"fields" :
"keyword" :
"type" : "keyword",
"ignore_above" : 256
,
"dateCompleted" :
"type" : "date"
Documents:
"id": 1,
"status": "Pending Notifications",
"approvalNotifications": [
"approvalBatchId": "e6c39194-5475-4168-9729-8ddcf46cf9ab",
"dateCompleted": "2018-11-15T16:09:15.346+0000"
,
"approvalBatchId": "05eaeb5d-d802-4a28-b699-5e593a59d445",
]
"id": 2,
"status": "Pending Notifications",
"approvalNotifications": [
"approvalBatchId": "e6c39194-5475-4168-9729-8ddcf46cf9ab",
]
"id": 3,
"status": "Complete",
"approvalNotifications": [
"approvalBatchId": "e6c39194-5475-4168-9729-8ddcf46cf9ab",
"dateCompleted": "2018-11-15T16:09:15.346+0000"
,
"approvalBatchId": "05eaeb5d-d802-4a28-b699-5e593a59d445",
"dateCompleted": "2018-11-16T16:09:15.346+0000"
]
"id": 4
"status": "No Notifications"
elasticsearch
In an elasticsearch query I am trying to search Document objects that have an array of approval notifications. The notifications are considered complete when dateCompleted
is populated with a date, and considered pending when either dateCompleted
doesn't exist or exists with null
. If the document does not contain an array of approval notifications then it is out of the scope of the search.
I am aware of putting null_value
for field dateCompleted
and setting it to some arbitrary old date but that seems hackish to me.
I've tried to use Bool queries with must exist doc.approvalNotifications and must not exist doc.approvalNotifications.dateCompleted
but that does not work if a document contains a mix of complete and pending approvalNotifications. e.g. it only returns document with ID 2 below. I am expecting documents with IDs 1 and 2 to be found.
How can I find pending approval notifications using elasticsearch?
PUT my_index/_mapping/Document
"properties" :
"doc" :
"properties" :
"approvalNotifications" :
"properties" :
"approvalBatchId" :
"type" : "text",
"fields" :
"keyword" :
"type" : "keyword",
"ignore_above" : 256
,
"approvalTransitionState" :
"type" : "text",
"fields" :
"keyword" :
"type" : "keyword",
"ignore_above" : 256
,
"approvedByUser" :
"type" : "text",
"fields" :
"keyword" :
"type" : "keyword",
"ignore_above" : 256
,
"dateCompleted" :
"type" : "date"
Documents:
"id": 1,
"status": "Pending Notifications",
"approvalNotifications": [
"approvalBatchId": "e6c39194-5475-4168-9729-8ddcf46cf9ab",
"dateCompleted": "2018-11-15T16:09:15.346+0000"
,
"approvalBatchId": "05eaeb5d-d802-4a28-b699-5e593a59d445",
]
"id": 2,
"status": "Pending Notifications",
"approvalNotifications": [
"approvalBatchId": "e6c39194-5475-4168-9729-8ddcf46cf9ab",
]
"id": 3,
"status": "Complete",
"approvalNotifications": [
"approvalBatchId": "e6c39194-5475-4168-9729-8ddcf46cf9ab",
"dateCompleted": "2018-11-15T16:09:15.346+0000"
,
"approvalBatchId": "05eaeb5d-d802-4a28-b699-5e593a59d445",
"dateCompleted": "2018-11-16T16:09:15.346+0000"
]
"id": 4
"status": "No Notifications"
elasticsearch
elasticsearch
edited Nov 15 '18 at 18:22
dukethrash
asked Nov 15 '18 at 18:16
dukethrashdukethrash
6652916
6652916
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You are almost there, you can achieve the desired behavior by using nested
datatype for the "approvalNotifications"
field.
What happens is that Elasticsearch flattens your approvalNotifications
objects, treating their subfields as subfields of the original document. The nested
field instead will tell ES to index each inner object as an implicit separate object, though related to the original one.
To query nested
objects one should use nested
query.
Hope that helps!
I can try that. It seems that I need to re-create the entire index as changing a type to nested in the/my_index/_mapping
API isn't allowed.
– dukethrash
Nov 18 '18 at 2:16
@dukthrash Yes, unfortunately the reindexing is necessary.
– Nikolay Vasiliev
Nov 18 '18 at 9:26
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%2f53325599%2fhow-do-i-query-a-null-date-inside-an-array-in-elasticsearch%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are almost there, you can achieve the desired behavior by using nested
datatype for the "approvalNotifications"
field.
What happens is that Elasticsearch flattens your approvalNotifications
objects, treating their subfields as subfields of the original document. The nested
field instead will tell ES to index each inner object as an implicit separate object, though related to the original one.
To query nested
objects one should use nested
query.
Hope that helps!
I can try that. It seems that I need to re-create the entire index as changing a type to nested in the/my_index/_mapping
API isn't allowed.
– dukethrash
Nov 18 '18 at 2:16
@dukthrash Yes, unfortunately the reindexing is necessary.
– Nikolay Vasiliev
Nov 18 '18 at 9:26
add a comment |
You are almost there, you can achieve the desired behavior by using nested
datatype for the "approvalNotifications"
field.
What happens is that Elasticsearch flattens your approvalNotifications
objects, treating their subfields as subfields of the original document. The nested
field instead will tell ES to index each inner object as an implicit separate object, though related to the original one.
To query nested
objects one should use nested
query.
Hope that helps!
I can try that. It seems that I need to re-create the entire index as changing a type to nested in the/my_index/_mapping
API isn't allowed.
– dukethrash
Nov 18 '18 at 2:16
@dukthrash Yes, unfortunately the reindexing is necessary.
– Nikolay Vasiliev
Nov 18 '18 at 9:26
add a comment |
You are almost there, you can achieve the desired behavior by using nested
datatype for the "approvalNotifications"
field.
What happens is that Elasticsearch flattens your approvalNotifications
objects, treating their subfields as subfields of the original document. The nested
field instead will tell ES to index each inner object as an implicit separate object, though related to the original one.
To query nested
objects one should use nested
query.
Hope that helps!
You are almost there, you can achieve the desired behavior by using nested
datatype for the "approvalNotifications"
field.
What happens is that Elasticsearch flattens your approvalNotifications
objects, treating their subfields as subfields of the original document. The nested
field instead will tell ES to index each inner object as an implicit separate object, though related to the original one.
To query nested
objects one should use nested
query.
Hope that helps!
answered Nov 16 '18 at 18:03
Nikolay VasilievNikolay Vasiliev
2,284718
2,284718
I can try that. It seems that I need to re-create the entire index as changing a type to nested in the/my_index/_mapping
API isn't allowed.
– dukethrash
Nov 18 '18 at 2:16
@dukthrash Yes, unfortunately the reindexing is necessary.
– Nikolay Vasiliev
Nov 18 '18 at 9:26
add a comment |
I can try that. It seems that I need to re-create the entire index as changing a type to nested in the/my_index/_mapping
API isn't allowed.
– dukethrash
Nov 18 '18 at 2:16
@dukthrash Yes, unfortunately the reindexing is necessary.
– Nikolay Vasiliev
Nov 18 '18 at 9:26
I can try that. It seems that I need to re-create the entire index as changing a type to nested in the
/my_index/_mapping
API isn't allowed.– dukethrash
Nov 18 '18 at 2:16
I can try that. It seems that I need to re-create the entire index as changing a type to nested in the
/my_index/_mapping
API isn't allowed.– dukethrash
Nov 18 '18 at 2:16
@dukthrash Yes, unfortunately the reindexing is necessary.
– Nikolay Vasiliev
Nov 18 '18 at 9:26
@dukthrash Yes, unfortunately the reindexing is necessary.
– Nikolay Vasiliev
Nov 18 '18 at 9:26
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.
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%2f53325599%2fhow-do-i-query-a-null-date-inside-an-array-in-elasticsearch%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