Elasticsearch document sorting based on nested values - sorting

I have the following data indexed in my elasticsearch version 7.4.
[
{
"users": [
{
"age": "31",
"dept": "IT",
"city": "Chennai"
},
{
"age": "25",
"dept": "Accounts",
"city": "Bengaluru"
},
{
"age": "26",
"dept": "Finance",
"city": "Kochi"
}
],
"isActive": true
},
{
"users": [
{
"age": "23",
"dept": "IT",
"city": "Chennai"
},
{
"age": "22",
"dept": "Finance",
"city": "Kochi"
},
{
"age": "21",
"dept": "Accounts",
"city": "Bengaluru"
}
],
"isActive": true
},
{
"users": [
{
"age": "28",
"dept": "IT",
"city": "Chennai"
},
{
"age": "22",
"dept": "Finance",
"city": "Kochi"
},
{
"age": "21",
"dept": "Accounts",
"city": "Bengaluru"
}
],
"isActive": true
},
{
"users": [
{
"age": "31",
"dept": "IT",
"city": "Kochi"
},
{
"age": "25",
"dept": "Finance",
"city": "Bengaluru"
},
{
"age": "26",
"dept": "Accounts",
"city": "Chennai"
}
],
"isActive": true
},
{
"users": [
{
"age": "21",
"dept": "IT",
"city": "Kochi"
},
{
"age": "45",
"dept": "Accounts",
"city": "Bengaluru"
},
{
"age": "26",
"dept": "Finance",
"city": "Chennai"
}
],
"isActive": false
}
]
the users is nested type
I want to sort the documents based on the following condition
isActive == true && users.dept == "IT" && users.city == "Chennai" with the age sorts in ascending
I am expecting the following result
[
{
"users": [
{
"age": "23",
"dept": "IT",
"city": "Chennai"
},
{
"age": "22",
"dept": "Finance",
"city": "Kochi"
},
{
"age": "21",
"dept": "Accounts",
"city": "Bengaluru"
}
],
"isActive": true
},
{
"users": [
{
"age": "28",
"dept": "IT",
"city": "Chennai"
},
{
"age": "22",
"dept": "Finance",
"city": "Kochi"
},
{
"age": "21",
"dept": "Accounts",
"city": "Bengaluru"
}
],
"isActive": true
},
{
"users": [
{
"age": "31",
"dept": "IT",
"city": "Chennai"
},
{
"age": "25",
"dept": "Accounts",
"city": "Bengaluru"
},
{
"age": "26",
"dept": "Finance",
"city": "Kochi"
}
],
"isActive": true
},
{
"users": [
{
"age": "31",
"dept": "IT",
"city": "Kochi"
},
{
"age": "25",
"dept": "Finance",
"city": "Bengaluru"
},
{
"age": "26",
"dept": "Accounts",
"city": "Chennai"
}
],
"isActive": true
}
]
I tried with script based sorting, I couldn't able to achieve the result.
Please help me to sort this out.
Thanks in advance.

Related

Elsa Document Approval Workflow (using VIsual Designer) always return 400

I am following Elsa Workflow tutorial here: https://elsa-workflows.github.io/elsa-core/docs/next/guides/guides-document-approval#document-approval-workflow-visual-designer
I am using the docker version, which should be the latest.
But whenever I sent a POST request using postman, it always returns 400.
I believe I am following the tutorial exactly as described.
The workflow isn't being triggered at all..., I suspect http://localhost:13000/v2/documents is not the correct url.
Here is what I have designed.
This is what I POST using Postman.
This is the exported workflow definition.
{
"$id": "1",
"definitionId": "94e1b452fe8e4239aeff1cf779a65329",
"versionId": "cb2fbcfe31c44875b6f82b01ef477ea9",
"name": "VisualDocumentApprovalWorkflow",
"displayName": "Visual Document Approval Workflow",
"version": 3,
"variables": {
"$id": "2",
"data": {}
},
"customAttributes": {
"$id": "3",
"data": {}
},
"isSingleton": false,
"persistenceBehavior": "WorkflowBurst",
"deleteCompletedInstances": false,
"isPublished": true,
"isLatest": true,
"createdAt": "2022-11-23T08:43:45.6472218Z",
"activities": [
{
"$id": "4",
"activityId": "b1d82194-6a6b-4568-adde-382b16027c93",
"type": "HttpEndpoint",
"displayName": "HTTP Endpoint",
"persistWorkflow": false,
"loadWorkflowContext": false,
"saveWorkflowContext": false,
"properties": [
{
"$id": "5",
"name": "Path",
"expressions": {
"$id": "6",
"Literal": "/v2/documents/"
}
},
{
"$id": "7",
"name": "Methods",
"expressions": {
"$id": "8",
"Json": "[\"POST\"]"
}
},
{
"$id": "9",
"name": "ReadContent",
"expressions": {
"$id": "10",
"Literal": "true"
}
},
{
"$id": "11",
"name": "TargetType",
"expressions": {
"$id": "12"
}
},
{
"$id": "13",
"name": "Schema",
"syntax": "Literal",
"expressions": {
"$id": "14",
"Literal": ""
}
},
{
"$id": "15",
"name": "Authorize",
"expressions": {
"$id": "16",
"Literal": "false"
}
},
{
"$id": "17",
"name": "Policy",
"expressions": {
"$id": "18",
"Literal": "adaa"
}
}
],
"propertyStorageProviders": {
"$id": "19"
}
},
{
"$id": "20",
"activityId": "2e7f8800-d49f-4485-b47b-0c9f3cb82a48",
"type": "SetVariable",
"displayName": "Set Variable",
"persistWorkflow": false,
"loadWorkflowContext": false,
"saveWorkflowContext": false,
"properties": [
{
"$id": "21",
"name": "VariableName",
"expressions": {
"$id": "22",
"Literal": "Document"
}
},
{
"$id": "23",
"name": "Value",
"syntax": "JavaScript",
"expressions": {
"$id": "24",
"Literal": "input.Body",
"JavaScript": "input.Body"
}
}
],
"propertyStorageProviders": {
"$id": "25"
}
},
{
"$id": "26",
"activityId": "b10ffb0d-c9ae-439a-86fa-b4351294666d",
"type": "SendEmail",
"displayName": "Send Email",
"persistWorkflow": false,
"loadWorkflowContext": false,
"saveWorkflowContext": false,
"properties": [
{
"$id": "27",
"name": "From",
"expressions": {
"$id": "28",
"Literal": "workflow#acme.com"
}
},
{
"$id": "29",
"name": "To",
"expressions": {
"$id": "30",
"Json": "[\"josh#acme.com\"]"
}
},
{
"$id": "31",
"name": "Subject",
"syntax": "Liquid",
"expressions": {
"$id": "32",
"Literal": "Document received from {{Variables.Document.Author.Name}}",
"Liquid": "Document received from {{Variables.Document.Author.Name}}"
}
},
{
"$id": "33",
"name": "Attachments",
"expressions": {
"$id": "34"
}
},
{
"$id": "35",
"name": "Body",
"expressions": {
"$id": "36",
"Literal": "Document from {{ Variables.Document.Author.Name }} received for review.<br>Approve or Reject"
}
},
{
"$id": "37",
"name": "Cc",
"expressions": {
"$id": "38"
}
},
{
"$id": "39",
"name": "Bcc",
"expressions": {
"$id": "40"
}
}
],
"propertyStorageProviders": {
"$id": "41"
}
},
{
"$id": "42",
"activityId": "90942864-bd26-4309-82fd-51f27b8e3615",
"type": "WriteHttpResponse",
"displayName": "HTTP Response",
"persistWorkflow": false,
"loadWorkflowContext": false,
"saveWorkflowContext": false,
"properties": [
{
"$id": "43",
"name": "Content",
"expressions": {
"$id": "44",
"Literal": "<h1>Request for Approval Sent</h1><p>Your document has been received and will be reviewed shortly.</p>"
}
},
{
"$id": "45",
"name": "ContentType",
"expressions": {
"$id": "46",
"Literal": "text/html"
}
},
{
"$id": "47",
"name": "StatusCode",
"expressions": {
"$id": "48",
"Literal": "OK"
}
},
{
"$id": "49",
"name": "CharSet",
"expressions": {
"$id": "50",
"Literal": ""
}
},
{
"$id": "51",
"name": "ResponseHeaders",
"expressions": {
"$id": "52"
}
}
],
"propertyStorageProviders": {
"$id": "53"
}
},
{
"$id": "54",
"activityId": "c62c8ff0-db76-4dea-b1a6-6cc138415b17",
"type": "Fork",
"displayName": "Fork",
"persistWorkflow": false,
"loadWorkflowContext": false,
"saveWorkflowContext": false,
"properties": [
{
"$id": "55",
"name": "Branches",
"expressions": {
"$id": "56",
"Json": "[\"Approve\",\"Reject\",\"Remind\"]"
}
}
],
"propertyStorageProviders": {
"$id": "57"
}
},
{
"$id": "58",
"activityId": "aa201fb5-a50f-44b1-989f-7a7b57b3b52d",
"type": "SignalReceived",
"displayName": "Signal Received",
"persistWorkflow": false,
"loadWorkflowContext": false,
"saveWorkflowContext": false,
"properties": [
{
"$id": "59",
"name": "Signal",
"expressions": {
"$id": "60",
"Literal": "Approve"
}
}
],
"propertyStorageProviders": {
"$id": "61"
}
},
{
"$id": "62",
"activityId": "615f0e0d-9f7a-4a2f-ab9e-97b3bea10cbe",
"type": "SendEmail",
"displayName": "Send Email",
"persistWorkflow": false,
"loadWorkflowContext": false,
"saveWorkflowContext": false,
"properties": [
{
"$id": "63",
"name": "From",
"expressions": {
"$id": "64",
"Literal": "workflow#acme.com"
}
},
{
"$id": "65",
"name": "To",
"syntax": "JavaScript",
"expressions": {
"$id": "66",
"Json": "[\"[Document.Author.Email]\"]",
"JavaScript": "[Document.Author.Email]"
}
},
{
"$id": "67",
"name": "Subject",
"syntax": "Liquid",
"expressions": {
"$id": "68",
"Liquid": "Document {{ Variables.Document.Id }} Approved!"
}
},
{
"$id": "69",
"name": "Attachments",
"expressions": {
"$id": "70"
}
},
{
"$id": "71",
"name": "Body",
"syntax": "Liquid",
"expressions": {
"$id": "72",
"Liquid": "Great job {{ Variables.Document.Author.Name }}, that document is perfect."
}
},
{
"$id": "73",
"name": "Cc",
"expressions": {
"$id": "74"
}
},
{
"$id": "75",
"name": "Bcc",
"expressions": {
"$id": "76"
}
}
],
"propertyStorageProviders": {
"$id": "77"
}
},
{
"$id": "78",
"activityId": "af52dae7-254b-4602-be35-0c0ef23a4ba5",
"type": "SignalReceived",
"displayName": "Signal Received",
"persistWorkflow": false,
"loadWorkflowContext": false,
"saveWorkflowContext": false,
"properties": [
{
"$id": "79",
"name": "Signal",
"expressions": {
"$id": "80",
"Literal": "Reject"
}
}
],
"propertyStorageProviders": {
"$id": "81"
}
},
{
"$id": "82",
"activityId": "ce2c17ff-cb9e-430a-a11e-9c92f0f4aa46",
"type": "SendEmail",
"displayName": "Send Email",
"persistWorkflow": false,
"loadWorkflowContext": false,
"saveWorkflowContext": false,
"properties": [
{
"$id": "83",
"name": "From",
"expressions": {
"$id": "84",
"Literal": "workflow#acme.com"
}
},
{
"$id": "85",
"name": "To",
"syntax": "JavaScript",
"expressions": {
"$id": "86",
"JavaScript": "[Document.Author.Email]"
}
},
{
"$id": "87",
"name": "Subject",
"syntax": "Liquid",
"expressions": {
"$id": "88",
"Literal": "Document {{ Variables.Document.Id }} Rejected",
"JavaScript": "Document {{ Variables.Document.Id }} Rejected"
}
},
{
"$id": "89",
"name": "Attachments",
"expressions": {
"$id": "90"
}
},
{
"$id": "91",
"name": "Body",
"syntax": "Liquid",
"expressions": {
"$id": "92",
"Liquid": "Nice try {{ Variables.Document.Author.Name }}, but that document needs work."
}
},
{
"$id": "93",
"name": "Cc",
"expressions": {
"$id": "94"
}
},
{
"$id": "95",
"name": "Bcc",
"expressions": {
"$id": "96"
}
}
],
"propertyStorageProviders": {
"$id": "97"
}
},
{
"$id": "98",
"activityId": "0be8d291-f88e-4db6-866e-fc722df70b27",
"type": "Timer",
"displayName": "Timer",
"persistWorkflow": false,
"loadWorkflowContext": false,
"saveWorkflowContext": false,
"properties": [
{
"$id": "99",
"name": "Timeout",
"syntax": "JavaScript",
"expressions": {
"$id": "100",
"JavaScript": "Duration.FromSeconds(10)"
}
}
],
"propertyStorageProviders": {
"$id": "101"
}
},
{
"$id": "102",
"activityId": "01b73005-d48e-46eb-88ae-55a08f96c356",
"type": "SendEmail",
"displayName": "Send Email",
"persistWorkflow": false,
"loadWorkflowContext": false,
"saveWorkflowContext": false,
"properties": [
{
"$id": "103",
"name": "From",
"expressions": {
"$id": "104",
"Literal": "workflow#acme.com"
}
},
{
"$id": "105",
"name": "To",
"expressions": {
"$id": "106",
"Json": "[\"josh#acme.com\"]"
}
},
{
"$id": "107",
"name": "Subject",
"syntax": "Liquid",
"expressions": {
"$id": "108",
"Liquid": "{{ Variables.Document.Author.Name }} is waiting for your review!"
}
},
{
"$id": "109",
"name": "Attachments",
"expressions": {
"$id": "110"
}
},
{
"$id": "111",
"name": "Body",
"syntax": "Liquid",
"expressions": {
"$id": "112",
"Liquid": "Don't forget to review document {{ Variables.Document.Id }}.<br>Approve or Reject"
}
},
{
"$id": "113",
"name": "Cc",
"expressions": {
"$id": "114"
}
},
{
"$id": "115",
"name": "Bcc",
"expressions": {
"$id": "116"
}
}
],
"propertyStorageProviders": {
"$id": "117"
}
},
{
"$id": "118",
"activityId": "e1c0aedb-c75b-4386-a20a-b0121f3a1421",
"type": "Join",
"displayName": "Join",
"persistWorkflow": false,
"loadWorkflowContext": false,
"saveWorkflowContext": false,
"properties": [
{
"$id": "119",
"name": "EagerJoin",
"expressions": {
"$id": "120"
}
},
{
"$id": "121",
"name": "Mode",
"expressions": {
"$id": "122",
"Literal": "WaitAny"
}
}
],
"propertyStorageProviders": {
"$id": "123"
}
},
{
"$id": "124",
"activityId": "84247af4-66a8-41ab-a442-b72b1d256154",
"type": "WriteHttpResponse",
"displayName": "HTTP Response",
"persistWorkflow": false,
"loadWorkflowContext": false,
"saveWorkflowContext": false,
"properties": [
{
"$id": "125",
"name": "Content",
"expressions": {
"$id": "126",
"Literal": "Thanks for the hard work!"
}
},
{
"$id": "127",
"name": "ContentType",
"expressions": {
"$id": "128",
"Literal": "text/html"
}
},
{
"$id": "129",
"name": "StatusCode",
"expressions": {
"$id": "130",
"Literal": "OK"
}
},
{
"$id": "131",
"name": "CharSet",
"expressions": {
"$id": "132",
"Literal": ""
}
},
{
"$id": "133",
"name": "ResponseHeaders",
"expressions": {
"$id": "134"
}
}
],
"propertyStorageProviders": {
"$id": "135"
}
}
],
"connections": [
{
"$id": "136",
"sourceActivityId": "b1d82194-6a6b-4568-adde-382b16027c93",
"targetActivityId": "2e7f8800-d49f-4485-b47b-0c9f3cb82a48",
"outcome": "Done"
},
{
"$id": "137",
"sourceActivityId": "2e7f8800-d49f-4485-b47b-0c9f3cb82a48",
"targetActivityId": "b10ffb0d-c9ae-439a-86fa-b4351294666d",
"outcome": "Done"
},
{
"$id": "138",
"sourceActivityId": "b10ffb0d-c9ae-439a-86fa-b4351294666d",
"targetActivityId": "90942864-bd26-4309-82fd-51f27b8e3615",
"outcome": "Done"
},
{
"$id": "139",
"sourceActivityId": "90942864-bd26-4309-82fd-51f27b8e3615",
"targetActivityId": "c62c8ff0-db76-4dea-b1a6-6cc138415b17",
"outcome": "Done"
},
{
"$id": "140",
"sourceActivityId": "c62c8ff0-db76-4dea-b1a6-6cc138415b17",
"targetActivityId": "aa201fb5-a50f-44b1-989f-7a7b57b3b52d",
"outcome": "Approve"
},
{
"$id": "141",
"sourceActivityId": "aa201fb5-a50f-44b1-989f-7a7b57b3b52d",
"targetActivityId": "615f0e0d-9f7a-4a2f-ab9e-97b3bea10cbe",
"outcome": "Done"
},
{
"$id": "142",
"sourceActivityId": "c62c8ff0-db76-4dea-b1a6-6cc138415b17",
"targetActivityId": "af52dae7-254b-4602-be35-0c0ef23a4ba5",
"outcome": "Reject"
},
{
"$id": "143",
"sourceActivityId": "af52dae7-254b-4602-be35-0c0ef23a4ba5",
"targetActivityId": "ce2c17ff-cb9e-430a-a11e-9c92f0f4aa46",
"outcome": "Done"
},
{
"$id": "144",
"sourceActivityId": "c62c8ff0-db76-4dea-b1a6-6cc138415b17",
"targetActivityId": "0be8d291-f88e-4db6-866e-fc722df70b27",
"outcome": "Remind"
},
{
"$id": "145",
"sourceActivityId": "0be8d291-f88e-4db6-866e-fc722df70b27",
"targetActivityId": "01b73005-d48e-46eb-88ae-55a08f96c356",
"outcome": "Done"
},
{
"$id": "146",
"sourceActivityId": "01b73005-d48e-46eb-88ae-55a08f96c356",
"targetActivityId": "0be8d291-f88e-4db6-866e-fc722df70b27",
"outcome": "Done"
},
{
"$id": "147",
"sourceActivityId": "615f0e0d-9f7a-4a2f-ab9e-97b3bea10cbe",
"targetActivityId": "e1c0aedb-c75b-4386-a20a-b0121f3a1421",
"outcome": "Done"
},
{
"$id": "148",
"sourceActivityId": "ce2c17ff-cb9e-430a-a11e-9c92f0f4aa46",
"targetActivityId": "e1c0aedb-c75b-4386-a20a-b0121f3a1421",
"outcome": "Done"
},
{
"$id": "149",
"sourceActivityId": "e1c0aedb-c75b-4386-a20a-b0121f3a1421",
"targetActivityId": "84247af4-66a8-41ab-a442-b72b1d256154",
"outcome": "Done"
}
],
"id": "cb2fbcfe31c44875b6f82b01ef477ea9"
}
Nevermind..., the correct url is http://localhost:13000/workflows/v2/documents instead of http://localhost:13000/v2/documents.
I found this by trial and error.
I will submit a PR to amend the documentation.

How to get one element from array of objects in elasticsearch

I have a products index which has an offers field. Offers is an array of objects.
I want to return one offer by seller_id in an array or in a new field.
Input:
with seller_id=5
{
"_index":"dev_products",
"_type":"_doc",
"_id":"138",
"_score":1.0,
"_source":{
"is_adult":false,
"status_id":3,
"allow_publish":false,
"name":"Consequuntur expedita sit perferendis est.",
"category_id":816,
"brand_id":363,
"description":"Nec.",
"type":3,
"vendor_code":"4968258909901",
"barcode":"98735976",
"code":"consequuntur-expedita-sit-perferendis-est",
"updated_at":"2022-11-15T10:42:33.000000Z",
"created_at":"2022-11-15T10:42:33.000000Z",
"id":138,
"offers":[
{
"product_id":"138",
"seller_id":"1",
"sale_status":"2",
"external_id":"1267631",
"store_id":"2",
"qty":"44",
"storage_address":"",
"base_price":"312.84",
"updated_at":"2022-11-15T10:42:49.000000Z",
"created_at":"2022-11-15T10:42:49.000000Z",
"id":74
},
{
"product_id":"138",
"seller_id":"2",
"sale_status":"1",
"external_id":"2795841",
"store_id":"2",
"qty":"1",
"storage_address":"",
"base_price":"1812.3",
"updated_at":"2022-11-15T10:44:50.000000Z",
"created_at":"2022-11-15T10:44:50.000000Z",
"id":76
},
{
"product_id":"138",
"seller_id":"3",
"sale_status":"1",
"external_id":"32219",
"store_id":"1",
"qty":"32",
"storage_address":"",
"base_price":"1556.25",
"updated_at":"2022-11-15T10:50:16.000000Z",
"created_at":"2022-11-15T10:50:16.000000Z",
"id":77
},
{
"product_id":"138",
"seller_id":"4",
"sale_status":"1",
"external_id":"967427",
"store_id":"1",
"qty":"35",
"storage_address":"",
"base_price":"137.62",
"updated_at":"2022-11-15T10:50:18.000000Z",
"created_at":"2022-11-15T10:50:18.000000Z",
"id":78
},
{
"product_id":"138",
"seller_id":"5",
"sale_status":"2",
"external_id":"209466",
"store_id":"1",
"qty":"45",
"storage_address":"",
"base_price":"187.03",
"updated_at":"2022-11-15T10:50:19.000000Z",
"created_at":"2022-11-15T10:50:19.000000Z",
"id":79
},
{
"product_id":"138",
"seller_id":"6",
"sale_status":"1",
"external_id":"522912",
"store_id":"1",
"qty":"61",
"storage_address":"",
"base_price":"306.39",
"updated_at":"2022-11-15T10:50:20.000000Z",
"created_at":"2022-11-15T10:50:20.000000Z",
"id":80
}
]
}
}
Expected:
{
"_index":"dev_products",
"_type":"_doc",
"_id":"138",
"_score":1.0,
"_source":{
"is_adult":false,
"status_id":3,
"allow_publish":false,
"name":"Consequuntur expedita sit perferendis est.",
"category_id":816,
"brand_id":363,
"description":"Nec.",
"type":3,
"vendor_code":"4968258909901",
"barcode":"98735976",
"code":"consequuntur-expedita-sit-perferendis-est",
"updated_at":"2022-11-15T10:42:33.000000Z",
"created_at":"2022-11-15T10:42:33.000000Z",
"id":138,
"offers":[
{
"product_id":"138",
"seller_id":"5",
"sale_status":"2",
"external_id":"209466",
"store_id":"1",
"qty":"45",
"storage_address":"",
"base_price":"187.03",
"updated_at":"2022-11-15T10:50:19.000000Z",
"created_at":"2022-11-15T10:50:19.000000Z",
"id":79
}
]
}
}
Or expected:
{
"_index":"dev_products",
"_type":"_doc",
"_id":"138",
"_score":1.0,
"_source":{
"is_adult":false,
"status_id":3,
"allow_publish":false,
"name":"Consequuntur expedita sit perferendis est.",
"category_id":816,
"brand_id":363,
"description":"Nec.",
"type":3,
"vendor_code":"4968258909901",
"barcode":"98735976",
"code":"consequuntur-expedita-sit-perferendis-est",
"updated_at":"2022-11-15T10:42:33.000000Z",
"created_at":"2022-11-15T10:42:33.000000Z",
"id":138,
"offer":{
"product_id":"138",
"seller_id":"5",
"sale_status":"2",
"external_id":"209466",
"store_id":"1",
"qty":"45",
"storage_address":"",
"base_price":"187.03",
"updated_at":"2022-11-15T10:50:19.000000Z",
"created_at":"2022-11-15T10:50:19.000000Z",
"id":79
}
}
}
Thanks for help!
If the offers field is nested type you can to use inner hits to get only object match in list.
The object you expected will in "inner_hits" response.
Query
GET idx_nested/_search?filter_path=hits.hits
{
"query": {
"nested": {
"path": "offers",
"query": {
"match": {
"offers.seller_id": "5"
}
},
"inner_hits": {}
}
}
}
Response:
{
"hits": {
"hits": [
{
"_index": "idx_nested",
"_id": "kYyYf4QBgXg8h_rctd1z",
"_score": 1.540445,
"_source": {
"is_adult": false,
"status_id": 3,
"allow_publish": false,
"name": "Consequuntur expedita sit perferendis est.",
"category_id": 816,
"brand_id": 363,
"description": "Nec.",
"type": 3,
"vendor_code": "4968258909901",
"barcode": "98735976",
"code": "consequuntur-expedita-sit-perferendis-est",
"updated_at": "2022-11-15T10:42:33.000000Z",
"created_at": "2022-11-15T10:42:33.000000Z",
"id": 138,
"offers": [
{
"product_id": "138",
"seller_id": "1",
"sale_status": "2",
"external_id": "1267631",
"store_id": "2",
"qty": "44",
"storage_address": "",
"base_price": "312.84",
"updated_at": "2022-11-15T10:42:49.000000Z",
"created_at": "2022-11-15T10:42:49.000000Z",
"id": 74
},
{
"product_id": "138",
"seller_id": "2",
"sale_status": "1",
"external_id": "2795841",
"store_id": "2",
"qty": "1",
"storage_address": "",
"base_price": "1812.3",
"updated_at": "2022-11-15T10:44:50.000000Z",
"created_at": "2022-11-15T10:44:50.000000Z",
"id": 76
},
{
"product_id": "138",
"seller_id": "3",
"sale_status": "1",
"external_id": "32219",
"store_id": "1",
"qty": "32",
"storage_address": "",
"base_price": "1556.25",
"updated_at": "2022-11-15T10:50:16.000000Z",
"created_at": "2022-11-15T10:50:16.000000Z",
"id": 77
},
{
"product_id": "138",
"seller_id": "4",
"sale_status": "1",
"external_id": "967427",
"store_id": "1",
"qty": "35",
"storage_address": "",
"base_price": "137.62",
"updated_at": "2022-11-15T10:50:18.000000Z",
"created_at": "2022-11-15T10:50:18.000000Z",
"id": 78
},
{
"product_id": "138",
"seller_id": "5",
"sale_status": "2",
"external_id": "209466",
"store_id": "1",
"qty": "45",
"storage_address": "",
"base_price": "187.03",
"updated_at": "2022-11-15T10:50:19.000000Z",
"created_at": "2022-11-15T10:50:19.000000Z",
"id": 79
},
{
"product_id": "138",
"seller_id": "6",
"sale_status": "1",
"external_id": "522912",
"store_id": "1",
"qty": "61",
"storage_address": "",
"base_price": "306.39",
"updated_at": "2022-11-15T10:50:20.000000Z",
"created_at": "2022-11-15T10:50:20.000000Z",
"id": 80
}
]
},
"inner_hits": {
"offers": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.540445,
"hits": [
{
"_index": "idx_nested",
"_id": "kYyYf4QBgXg8h_rctd1z",
"_nested": {
"field": "offers",
"offset": 4
},
"_score": 1.540445,
"_source": {
"store_id": "1",
"updated_at": "2022-11-15T10:50:19.000000Z",
"storage_address": "",
"product_id": "138",
"qty": "45",
"base_price": "187.03",
"sale_status": "2",
"created_at": "2022-11-15T10:50:19.000000Z",
"external_id": "209466",
"id": 79,
"seller_id": "5"
}
}
]
}
}
}
}
]
}
}

Elastic search query - all objects of nested array should have specific value

I want to write Elasticsearch query where I am able to look into a nested array, and check a key for all those elements inside the array, if all of them have that value then the root document should be selected.
this is a sample data :
[
{
"_index": "allproperties",
"_type": "_doc",
"_id": "5241050",
"_score": 0.0,
"_source": {
"id": 5241050,
"type": "HOUSE",
"state": "NSW",
"agency": {
"id": 31,
"code": "AU_LNT",
"name": "Luthor Properties",
"config": "{}"
},
"branch": {
"id": 89,
"name": "Luthor Sales Unit Trust",
"tradingName": "Luthor Properties",
"internalCode": "AU_LNT_G"
},
"images": null,
"leases": [
{
"id": 26439,
"bond": 2000,
"leased": true,
"source": "PORTAL",
"status": "SIGNED",
"endDate": "2022-11-06T00:00:00",
"tenants": [
{
"id": 11106,
"role": "TENANT",
"user": {
"id": 38817,
"dob": "1900-01-01",
"name": "Liam",
"email": "tempo#tempo.com",
"phone": "+61400000000",
"surname": "Tempo",
"salutation": "Mr.",
"invitations": null,
"lastInAppActivity": null
},
"address": "45675 Bruce Hwy, Coolbie QLD 4850, Australia",
"extendedData": "{\"rentSplitAmount\":22.62}",
"roleAdjective": "PRIMARY",
"addressComponents": {
"state": "QLD",
"suburb": "Coolbie",
"country": "Australia",
"postcode": "4850",
"streetName": "Bruce Hwy",
"unitNumber": null,
"poboxNumber": null,
"streetNumber": "45675",
"addressComponentsUnavailable": null
}
}
],
"signDate": "2022-10-05T04:58:08.887",
"startDate": "2022-10-06T00:00:00",
"moveInDate": "2022-10-06T00:00:00",
"appointments": [
{
"id": 3506,
"type": "REMOTE",
"date_time": "2022-10-12T04:56:00+00:00",
"createdBy": "Lex Luthor",
"createdDate": "2022-10-05T04:56:52.936",
"lastModifiedBy": "Lex Luthor",
"lastModifiedDate": "2022-10-05T04:56:53.51"
}
],
"createdDate": "2022-10-05T04:55:42.247",
"rentalAmount": 500,
"dateAvailable": null,
"extendedData": {
"last_rent_adjustment_date": 1665014400000
},
"leaseDuration": 32,
"rentalFrequency": "2",
"signedManually": false
}
],
"refId": "b66326eb-a6b2-42b6-b058-b46847e13399",
"source": "PT",
"status": null,
"suburb": "Hurstville",
"address": "456 Forest Road Hurstville NSW AUSTRALIA 2220",
"country": "Australia",
"bedrooms": 3,
"category": null,
"pmsCode": "84c34d15-a0ab-4791-b9e3-1bdac215b99c",
"postcode": "2220",
"agencyId": 31,
"bathrooms": 3,
"branchId": 89,
"carspaces": 3,
"createdBy": "system",
"streetname": "Forest Road",
"unitnumber": null,
"description": null,
"createdDate": "2022-10-05T04:51:12.619",
"entitlements": [
{
"id": 3453799,
"role": "LANDLORD",
"user": {
"id": 22855,
"dob": null,
"name": "please enter ownership name",
"email": "mag.m#rr.com.au",
"phone": "0400000000",
"surname": "",
"salutation": null,
"lastInAppActivity": null
},
"company": {
"id": 137,
"abn": "",
"acn": "",
"phone": "0400000000",
"address": "1234 Park Avenue New York NY USA 10037-1702",
"companyName": "please enter ownership name",
"displayName": "please enter ownership name",
"email": "mag.m#rr.com.au"
},
"roleAdjective": "GROUP"
},
{
"id": 3453800,
"role": "AGENT",
"user": {
"id": 20054,
"dob": null,
"name": "Paul",
"email": "p.b#mr.com",
"phone": "+61403084232",
"surname": "Botti",
"salutation": null,
"lastInAppActivity": null
},
"company": null,
"roleAdjective": "MANAGING"
},
{
"id": 3453801,
"role": "AGENT",
"user": {
"id": 20054,
"dob": null,
"name": "Paul",
"email": "p.b#mr.com",
"phone": "+61403084232",
"surname": "Botti",
"salutation": null,
"lastInAppActivity": null
},
"company": null,
"roleAdjective": "LEASING"
}
],
"streetnumber": "456",
"advertised": false,
"commission_type": null,
"lastModifiedBy": "system",
"lastModifiedDate": "2022-10-05T04:58:09.043",
"_meta": {
"branches": {
"id": [
89
]
},
"agencies": {
"id": [
31
]
},
"user_property_entitlement": {
"id": [
3453799,
3453800,
3453801
]
},
"users": {
"id": [
20054,
22855,
38817
]
},
"companies": {
"id": [
137
]
},
"leases": {
"id": [
26439
]
},
"user_lease_entitlement": {
"id": [
11106
]
},
"appointments": {
"id": [
3506
]
}
}
},
"sort": [
1664945472619,
0.0
]
}
]
In this particular case, leases is a nested document, I want to select the documents where all the leases are in cancelled state or leases is null, i.e; either all of the objects inside leases should have status as CANCELLED or the leases key should be null.
Already went through this question, but did'nt quite get it as its an old answer from 2015 and methods used in this got deprecated.
Try this query:
GET idx_test/_search?filter_path=hits.hits
{
"query": {
"bool": {
"must_not": [
{
"nested": {
"path": "leases",
"query": {
"bool": {
"must_not": [
{
"bool": {
"should": [
{
"term": {
"leases.status.keyword": "CANCELLED"
}
},
{
"bool": {
"must_not": [
{
"exists": {
"field": "leases"
}
}
]
}
}
]
}
}
]
}
}
}
}
]
}
}
}

Using JSONPath get only 2 key-value pair when a condition is met

With the following json object.
{ "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
Using JSONPath. Is there any expression that can get only the key-value pair "author" and "title" for all books costing less that 13.00. The result will be something like:
[{
"author": "Nigel Rees",
"title": "Sayings of the Century"
}, {
"author": "Evelyn Waugh",
"title": "Sword of Honour"
}, {
"author": "Herman Melville",
"title": "Moby Dick"
}
]
I started with $.store.book[?(#.price < 13)] but this gets all the elements on each book costing less than 13.00. Not the desired behavior. Any help is much appreciated.
You are close; this expression:
$.store.book[?(#.price < 13)]["author","title"]
or, depending on the implementation
$.store.book[?(#.price < 13)][]author,title
should get you
[
{
"author" : "Nigel Rees",
"title" : "Sayings of the Century"
},
{
"author" : "Evelyn Waugh",
"title" : "Sword of Honour"
},
{
"author" : "Herman Melville",
"title" : "Moby Dick"
}
]
or
[
"Nigel Rees",
"Sayings of the Century",
"Evelyn Waugh",
"Sword of Honour",
"Herman Melville",
"Moby Dick"
]

Count Repeated Values in Json String and store value and label in another String

I need to count the number of repeating items in the json array , and show the repeating items as single item and count should be shown how many tiems it has repeated..
please check the below code and Output so that you can understand:
[{
"Name": "Jacob",
"Gender": "Male",
"City": "Newyork",
"State": "Newyork"
}, {
"Name": "Mason",
"Gender": "Male",
"City": "Los Angeles",
"State": "California"
}, {
"Name": "Ethan",
"Gender": "Male",
"City": "Chicago",
"State": "Illinois"
}, {
"Name": "Noah",
"Gender": "Male",
"City": "Newyork",
"State": "NewYork"
}, {
"Name": "Sophia",
"Gender": "Female",
"City": "Los Angeles",
"State": "California"
}, {
"Name": "Emma",
"Gender": "Female",
"City": "Los Angeles",
"State": "California"
}, {
"Name": "Isabella",
"Gender": "Female",
"City": "Chicago",
"State": "Illinois"
}, {
"Name": "Olivia",
"Gender": "Female",
"City": "Chicago",
"State": "Illinois"
}, {
"Name": "Elizabeth",
"Gender": "Female",
"City": "Newyork",
"State": "Newyork"
}, {
"Name": "zoey",
"Gender": "Female",
"City": "Newyork",
"State": "Newyork"
}]
The desired output from json is
Newyork : 4Times,
chicago:3 times,
Please find the fiddle :
http://jsfiddle.net/vamsikrishna981/dMrNv/
Try this http://jsfiddle.net/RHYFC/
See LONG VERSION and SHORT VERSION on fiddle
Javascript
var result = {};
for(var i = 0; i < data.length; i++){
for(var item in data[i]){
if(!result[item]){
result[item] = {};
}
if(!result[item][data[i][item]]){
result[item][data[i][item]] = 0;
}
result[item][data[i][item]]++;
}
}

Resources