{"id":9072,"date":"2022-06-14T17:16:47","date_gmt":"2022-06-14T22:16:47","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=9072"},"modified":"2022-06-14T17:17:50","modified_gmt":"2022-06-14T22:17:50","slug":"elasticsearch-search-api-script-fields","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=9072","title":{"rendered":"ElasticSearch Search API &#8211; Script Fields"},"content":{"rendered":"\n<p>I&#8217;ve been playing around with script fields to manipulate data returned by ElasticSearch queries. As an example, data where there are a few nested objects with values that need to be multiplied together:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n{\n\t&quot;order&quot;: {\n\t\t&quot;item1&quot;: {\n\t\t\t&quot;cost&quot;: 31.55,\n\t\t\t&quot;count&quot;: 111\n\t\t},\n\t\t&quot;item2&quot;: {\n\t\t\t&quot;cost&quot;: 62.55,\n\t\t\t&quot;count&quot;: 222\n\t\t},\n\t\t&quot;item3&quot;: {\n\t\t\t&quot;cost&quot;: 93.55,\n\t\t\t&quot;count&quot;: 333\n\t\t}\n\t}\n}\n<\/pre><\/div>\n\n\n<p>And to retrieve records and multiply cost by count:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n{\n  &quot;query&quot;  : { &quot;match_all&quot; : {} },\n\t&quot;_source&quot;: &#x5B;&quot;order.item*.item&quot;, &quot;order.item*.count&quot;, &quot;order.item*.cost&quot;],\n  &quot;script_fields&quot; : {\n    &quot;total_cost_1&quot; : {\n      &quot;script&quot; : \n      {\n        &quot;lang&quot;: &quot;painless&quot;,\n        &quot;source&quot;: &quot;return doc&#x5B;&#039;order.item1.cost&#039;].value * doc&#x5B;&#039;order.item1.count&#039;].value;&quot;\n      }\n    },\n    &quot;total_cost_2&quot; : {\n      &quot;script&quot; : \n      {\n        &quot;lang&quot;: &quot;painless&quot;,\n        &quot;source&quot;: &quot;return doc&#x5B;&#039;order.item2.cost&#039;].value * doc&#x5B;&#039;order.item2.count&#039;].value;&quot;\n      }\n    },\n    &quot;total_cost_3&quot; : {\n      &quot;script&quot; : \n      {\n        &quot;lang&quot;: &quot;painless&quot;,\n        &quot;source&quot;: &quot;return doc&#x5B;&#039;order.item3.cost&#039;].value * doc&#x5B;&#039;order.item3.count&#039;].value;&quot;\n      }\n    }    \n  }\n}\n<\/pre><\/div>\n\n\n<p>Unfortunately, I cannot find any way to iterate across an arbitrary number of item# objects nested in the order object. So, for now, I think the data manipulation will be done in the program using the API to retrieve data. Still, it was good to learn how to address values in the doc record. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been playing around with script fields to manipulate data returned by ElasticSearch queries. As an example, data where there are a few nested objects with values that need to be multiplied together: And to retrieve records and multiply cost by count: Unfortunately, I cannot find any way to iterate across an arbitrary number of &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1588],"tags":[1590,1589],"class_list":["post-9072","post","type-post","status-publish","format-standard","hentry","category-elk","tag-elasticsearch","tag-elk"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/9072","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9072"}],"version-history":[{"count":1,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/9072\/revisions"}],"predecessor-version":[{"id":9073,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/9072\/revisions\/9073"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}