{"id":139,"date":"2021-01-19T10:33:24","date_gmt":"2021-01-19T10:33:24","guid":{"rendered":"https:\/\/multisite.korebots.com\/SearchAssist\/?p=139"},"modified":"2021-06-28T07:46:25","modified_gmt":"2021-06-28T07:46:25","slug":"custom-script","status":"publish","type":"post","link":"https:\/\/multisite.korebots.com\/SearchAssist\/manage-index\/custom-script\/","title":{"rendered":"Custom Script"},"content":{"rendered":"<section class=\"l-section wpb_row height_auto\"><div class=\"l-section-h i-cf\"><div class=\"g-cols vc_row via_grid cols_1 laptops-cols_inherit tablets-cols_inherit mobiles-cols_1 valign_top type_default stacking_default\"><div class=\"wpb_column vc_column_container\"><div class=\"vc_column-inner\"><div class=\"wpb_text_column\"><div class=\"wpb_wrapper\"><p><span style=\"font-weight: 400;\">The Custom Script stage in\u00a0<a href=\"\/SearchAssist\/lifecycle\/manage-index\/workbench\/\" target=\"_blank\" rel=\"noopener noreferrer\">Index Pipeline<\/a> allows for custom processing. You can write a painless script to perform actions such as adding a new field, deleting a field, setting\/resetting values to a field, coping fields, etc. <\/span><\/p>\n<p>Ensure to <strong>Train your app<\/strong> each time you make changes to any index configuration. This builds the index based on the updated configurations.<\/p>\n<\/div><\/div><div class=\"w-separator size_small with_line width_default thick_1 style_solid color_border align_center\"><div class=\"w-separator-h\"><\/div><\/div><div class=\"wpb_text_column\"><div class=\"wpb_wrapper\"><h2><span class=\"ez-toc-section\" id=\"Configuration\"><\/span>Configuration<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">To configure a custom script, follow the below steps:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Click the <\/span><b>Indices <\/b><span style=\"font-weight: 400;\">tab on the top.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">On the left pane, under the <\/span><b>Index Configuration<\/b><span style=\"font-weight: 400;\"> section, click <\/span><b>Workbench<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">On the Workbench (Index Configuration) page, on the <\/span><b>Stages <\/b><span style=\"font-weight: 400;\">column, click the <\/span><b>+<\/b><span style=\"font-weight: 400;\"> icon.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">On the right column, select <\/span><b>Custom Script<\/b><span style=\"font-weight: 400;\"> from the <\/span><b>Stage Type<\/b><span style=\"font-weight: 400;\"> drop-down list.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Enter a name in the <\/span><b>Stage Name<\/b><span style=\"font-weight: 400;\"> field.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Enter a condition in the <\/span><b>Condition <\/b><span style=\"font-weight: 400;\">field. \u00a0You can add multiple conditions using the AND\/OR connectors. Documents that satisfy the condition will be executed as part of the stage. <a href=\"#Conditions\">See below for details<\/a>.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Under the <\/span><b>&lt;\/&gt;Painless<\/b><span style=\"font-weight: 400;\"> section, enter the custom script. <a href=\"#Script\">See below for details<\/a>.<\/span><\/li>\n<li>Click\u00a0<b>Simulate<\/b> to verify the configurations. The simulator displays the Source and the number of documents to which the mapping was applied, and the result. You can change the Source (if not mentioned in the condition) and the number of documents.<\/li>\n<li>Once done, click\u00a0<b>Save Configuration<\/b>\u00a0on the top-right.<\/li>\n<\/ol>\n<\/div><\/div><div class=\"wpb_text_column\"><div class=\"wpb_wrapper\"><p><a ref=\"magnificPopup\" href=\"http:\/\/docs.kore.ai\/searchassist\/wp-content\/uploads\/sites\/4\/2021\/01\/indices_script.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1277\" src=\"http:\/\/docs.kore.ai\/searchassist\/wp-content\/uploads\/sites\/4\/2021\/01\/indices_script.png\" alt=\"\" width=\"1119\" height=\"703\" \/><\/a><\/p>\n<\/div><\/div><div class=\"w-separator size_small with_line width_default thick_1 style_solid color_border align_center\"><div class=\"w-separator-h\"><\/div><\/div><div class=\"wpb_text_column\"><div class=\"wpb_wrapper\"><h3><span class=\"ez-toc-section\" id=\"Script\"><\/span><span style=\"font-weight: 400;\">Script<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><b>Example<\/b><\/p>\n<p><span style=\"font-weight: 400;\">To copy a value from <\/span><i><span style=\"font-weight: 400;\">&#8220;defaultAnswers[*].payload&#8221;<\/span><\/i><span style=\"font-weight: 400;\"> to<\/span><i><span style=\"font-weight: 400;\"> &#8220;answer[]&#8221;<\/span><\/i><span style=\"font-weight: 400;\">, use the below script:<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\"><code>ctx.answer =[]; for (def item : ctx.defaultAnswers) { ctx.answer.add(item.payload) }<\/code><\/span><\/i><\/p>\n<\/div><\/div><div class=\"wpb_text_column\"><div class=\"wpb_wrapper\"><h3><span class=\"ez-toc-section\" id=\"Conditions\"><\/span><span id=\"Conditions\" class=\"ez-toc-section\">Conditions<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Condition is of the following format:\u00a0<code>ctx.fieldtype==value<\/code>\u00a0or\u00a0<code>ctx.fieldtype!=value<\/code>. The\u00a0<em>field_name<\/em>\u00a0can be obtained from the Fields table under Index Configuration.<\/p>\n<p>For example,\u00a0<code>ctx.contentType==\"web\"<\/code>\u00a0to restrict the extraction to the content from a web source.<\/p>\n<\/div><\/div><div class=\"w-separator size_small with_line width_default thick_1 style_solid color_border align_center\"><div class=\"w-separator-h\"><\/div><\/div>[vc_raw_html]JTNDZGl2JTIwY2xhc3MlM0QlMjJ3LXBvc3QtZWxtJTIwcG9zdF9uYXZpZ2F0aW9uJTIwbGF5b3V0X3NpbXBsZSUyMGludl9mYWxzZSUyMiUzRSUwQSUwOSUzQ2ElMjBjbGFzcyUzRCUyMnBvc3RfbmF2aWdhdGlvbi1pdGVtJTIwb3JkZXJfZmlyc3QlMjB0b19wcmV2JTIyJTIwaHJlZiU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U=[\/vc_raw_html]<div class=\"w-separator size_small with_line width_default thick_1 style_solid color_border align_center\"><div class=\"w-separator-h\"><\/div><\/div><\/div><\/div><\/div><\/div><\/section>\n","protected":false},"excerpt":{"rendered":"The Custom Script stage in\u00a0Index Pipeline allows for custom processing. You can write a painless script to perform actions such as adding a new field, deleting a field, setting\/resetting values to a field, coping fields, etc. Ensure to Train your app each time you make changes to any index configuration. This builds the index based...","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/multisite.korebots.com\/SearchAssist\/wp-json\/wp\/v2\/posts\/139"}],"collection":[{"href":"https:\/\/multisite.korebots.com\/SearchAssist\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/multisite.korebots.com\/SearchAssist\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/multisite.korebots.com\/SearchAssist\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/multisite.korebots.com\/SearchAssist\/wp-json\/wp\/v2\/comments?post=139"}],"version-history":[{"count":12,"href":"https:\/\/multisite.korebots.com\/SearchAssist\/wp-json\/wp\/v2\/posts\/139\/revisions"}],"predecessor-version":[{"id":1223,"href":"https:\/\/multisite.korebots.com\/SearchAssist\/wp-json\/wp\/v2\/posts\/139\/revisions\/1223"}],"wp:attachment":[{"href":"https:\/\/multisite.korebots.com\/SearchAssist\/wp-json\/wp\/v2\/media?parent=139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multisite.korebots.com\/SearchAssist\/wp-json\/wp\/v2\/categories?post=139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multisite.korebots.com\/SearchAssist\/wp-json\/wp\/v2\/tags?post=139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}