{"id":3626,"date":"2016-04-13T12:42:06","date_gmt":"2016-04-13T12:42:06","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3626"},"modified":"2016-04-13T12:42:06","modified_gmt":"2016-04-13T12:42:06","slug":"update-data-solr-scala","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/update-data-solr-scala\/","title":{"rendered":"Update data in Solr from Scala"},"content":{"rendered":"<p>Updating or adding fields in Solr is a little trickier than inserting new documents.<\/p>\n<p>When you add documents to Solr through the API, it can create all the fields you send automatically &#8211; these will end up in a file called &#8220;managed-schema&#8221;. This is a neat feature, but it doesn&#8217;t occur by default when you update so you&#8217;ll have to add any fields you want manually:<\/p>\n<pre lang=\"xml\">\n<field name=\"short_certificate\" type=\"strings\"\/>\n<field name=\"long_certificate\" type=\"strings\"\/>\n<\/pre>\n<p>The API I&#8217;ve been using (jp.sf.amateras.solr.scala) doesn&#8217;t support updating fields, so we&#8217;ll have to use HTTP directly, by doing a POST to the update endpoint:<\/p>\n<pre>\nhttp:\/\/localhost:8983\/solr\/ssl_certificates\/update?commit=true\n<\/pre>\n<p>Normally you can post new documents to this API. Be careful not to do this, because it overwrites whatever was there If you&#8217;re doing this in development, you might want to take a backup of your Solr core before you do this.<\/p>\n<p>To modify fields, you can set the value of the attribute to an object, which is in the format &#8220;attribute: {&#8220;set&#8221;: &#8220;value&#8221;}&#8221;. The &#8220;set&#8221; command will set the value of the specified attribute. There are also commands to append to a list, remove a value, and increment a number<sup><a href=\"#footnote_0_3626\" id=\"identifier_0_3626\" class=\"footnote-link footnote-identifier-link\" title=\"http:\/\/solr.pl\/en\/2012\/07\/09\/solr-4-0-partial-documents-update\/\">1<\/a><\/sup><\/p>\n<p>According to the docs, the &#8220;set&#8221; command creates a new document, which replaces the old one. This is important: if you don&#8217;t store fields it wouldn&#8217;t be able to pull them out to create the new document.<\/p>\n<p>Here&#8217;s how we can make the JSON:<\/p>\n<pre lang=\"scala\">\nval solrUpdate =\n  Json.arr(\n    Json.obj(\n      \"id\" -> id,\n      \"short_certificate\" ->\n        Json.obj(\n          \"set\" -> chain(0).toString\n        )\n      )\n    )\n<\/pre>\n<p>And for completeness, here is the code to do the actual update:<\/p>\n<pre lang=\"scala\">\nval updateUrl = \"http:\/\/localhost:8983\/solr\/ssl_certificates\/update\"\n\nval response =\n  Http.apply(updateUrl)\n      .header(\"Content-type\", \"application\/json\")\n      .postData(solrUpdate.toString)\n      .asString\n      .body\n\nprintln(response)\n<\/pre>\n<p>The output of this is useful, because this is where any Solr errors will be present.<\/p>\n<p>If you want to do a lot of individual updates, you don&#8217;t want to commit each time, or it will take forever. You can also do giant batch updates, depending on the nature of your situation.<\/p>\n<ol class=\"footnotes\"><li id=\"footnote_0_3626\" class=\"footnote\">http:\/\/solr.pl\/en\/2012\/07\/09\/solr-4-0-partial-documents-update\/<span class=\"footnote-back-link-wrapper\"> [<a href=\"#identifier_0_3626\" class=\"footnote-link footnote-back-link\">&#8617;<\/a>]<\/span><\/li><\/ol>","protected":false},"excerpt":{"rendered":"<p>Updating or adding fields in Solr is a little trickier than inserting new documents. When you add documents to Solr through the API, it can create all the fields you send automatically &#8211; these will end up in a file called &#8220;managed-schema&#8221;. This is a neat feature, but it doesn&#8217;t occur by default when you &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/update-data-solr-scala\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Update data in Solr from Scala&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[480,485,517],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3626"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3626"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3626\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}