{"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_1_3626\" id=\"identifier_1_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_1_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_1_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,"footnotes":""},"categories":[4],"tags":[480,485,517],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.9 - aioseo.com -->\n\t<meta name=\"description\" content=\"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 - these will end up in a file called &quot;managed-schema&quot;. This is a neat feature, but it doesn&#039;t occur by default when you\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"gary\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.garysieling.com\/blog\/update-data-solr-scala\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.9\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Gary Sieling - Software Engineer\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Update data in Solr from Scala - Gary Sieling\" \/>\n\t\t<meta property=\"og:description\" content=\"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 - these will end up in a file called &quot;managed-schema&quot;. This is a neat feature, but it doesn&#039;t occur by default when you\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.garysieling.com\/blog\/update-data-solr-scala\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2016-04-13T12:42:06+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2016-04-13T12:42:06+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Update data in Solr from Scala - Gary Sieling\" \/>\n\t\t<meta name=\"twitter:description\" content=\"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 - these will end up in a file called &quot;managed-schema&quot;. This is a neat feature, but it doesn&#039;t occur by default when you\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/update-data-solr-scala\\\/#blogposting\",\"name\":\"Update data in Solr from Scala - Gary Sieling\",\"headline\":\"Update data in Solr from Scala\",\"author\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#organization\"},\"datePublished\":\"2016-04-13T12:42:06+00:00\",\"dateModified\":\"2016-04-13T12:42:06+00:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/update-data-solr-scala\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/update-data-solr-scala\\\/#webpage\"},\"articleSection\":\"Code Examples, scala, scala-examples, solr\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/update-data-solr-scala\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.garysieling.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/code-examples\\\/#listItem\",\"name\":\"Code Examples\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/code-examples\\\/#listItem\",\"position\":2,\"name\":\"Code Examples\",\"item\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/code-examples\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/update-data-solr-scala\\\/#listItem\",\"name\":\"Update data in Solr from Scala\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/update-data-solr-scala\\\/#listItem\",\"position\":3,\"name\":\"Update data in Solr from Scala\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/category\\\/code-examples\\\/#listItem\",\"name\":\"Code Examples\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#organization\",\"name\":\"Gary Sieling\",\"description\":\"Software Engineer\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/\",\"name\":\"gary\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/update-data-solr-scala\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/0be925276d848ffe98a6a9dc8cf33e67?s=96&d=identicon&r=g\",\"width\":96,\"height\":96,\"caption\":\"gary\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/update-data-solr-scala\\\/#webpage\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/update-data-solr-scala\\\/\",\"name\":\"Update data in Solr from Scala - Gary Sieling\",\"description\":\"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 - these will end up in a file called \\\"managed-schema\\\". This is a neat feature, but it doesn't occur by default when you\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/update-data-solr-scala\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/author\\\/gary\\\/#author\"},\"datePublished\":\"2016-04-13T12:42:06+00:00\",\"dateModified\":\"2016-04-13T12:42:06+00:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/\",\"name\":\"Gary Sieling\",\"description\":\"Software Engineer\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.garysieling.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Update data in Solr from Scala - Gary Sieling","description":"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 - these will end up in a file called \"managed-schema\". This is a neat feature, but it doesn't occur by default when you","canonical_url":"https:\/\/www.garysieling.com\/blog\/update-data-solr-scala\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/www.garysieling.com\/blog\/update-data-solr-scala\/#blogposting","name":"Update data in Solr from Scala - Gary Sieling","headline":"Update data in Solr from Scala","author":{"@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author"},"publisher":{"@id":"https:\/\/www.garysieling.com\/blog\/#organization"},"datePublished":"2016-04-13T12:42:06+00:00","dateModified":"2016-04-13T12:42:06+00:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/www.garysieling.com\/blog\/update-data-solr-scala\/#webpage"},"isPartOf":{"@id":"https:\/\/www.garysieling.com\/blog\/update-data-solr-scala\/#webpage"},"articleSection":"Code Examples, scala, scala-examples, solr"},{"@type":"BreadcrumbList","@id":"https:\/\/www.garysieling.com\/blog\/update-data-solr-scala\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog#listItem","position":1,"name":"Home","item":"https:\/\/www.garysieling.com\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/#listItem","name":"Code Examples"}},{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/#listItem","position":2,"name":"Code Examples","item":"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/","nextItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/update-data-solr-scala\/#listItem","name":"Update data in Solr from Scala"},"previousItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/update-data-solr-scala\/#listItem","position":3,"name":"Update data in Solr from Scala","previousItem":{"@type":"ListItem","@id":"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/#listItem","name":"Code Examples"}}]},{"@type":"Organization","@id":"https:\/\/www.garysieling.com\/blog\/#organization","name":"Gary Sieling","description":"Software Engineer","url":"https:\/\/www.garysieling.com\/blog\/"},{"@type":"Person","@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author","url":"https:\/\/www.garysieling.com\/blog\/author\/gary\/","name":"gary","image":{"@type":"ImageObject","@id":"https:\/\/www.garysieling.com\/blog\/update-data-solr-scala\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/0be925276d848ffe98a6a9dc8cf33e67?s=96&d=identicon&r=g","width":96,"height":96,"caption":"gary"}},{"@type":"WebPage","@id":"https:\/\/www.garysieling.com\/blog\/update-data-solr-scala\/#webpage","url":"https:\/\/www.garysieling.com\/blog\/update-data-solr-scala\/","name":"Update data in Solr from Scala - Gary Sieling","description":"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 - these will end up in a file called \"managed-schema\". This is a neat feature, but it doesn't occur by default when you","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/www.garysieling.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/www.garysieling.com\/blog\/update-data-solr-scala\/#breadcrumblist"},"author":{"@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author"},"creator":{"@id":"https:\/\/www.garysieling.com\/blog\/author\/gary\/#author"},"datePublished":"2016-04-13T12:42:06+00:00","dateModified":"2016-04-13T12:42:06+00:00"},{"@type":"WebSite","@id":"https:\/\/www.garysieling.com\/blog\/#website","url":"https:\/\/www.garysieling.com\/blog\/","name":"Gary Sieling","description":"Software Engineer","inLanguage":"en-US","publisher":{"@id":"https:\/\/www.garysieling.com\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"Gary Sieling - Software Engineer","og:type":"article","og:title":"Update data in Solr from Scala - Gary Sieling","og:description":"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 - these will end up in a file called &quot;managed-schema&quot;. This is a neat feature, but it doesn't occur by default when you","og:url":"https:\/\/www.garysieling.com\/blog\/update-data-solr-scala\/","article:published_time":"2016-04-13T12:42:06+00:00","article:modified_time":"2016-04-13T12:42:06+00:00","twitter:card":"summary_large_image","twitter:title":"Update data in Solr from Scala - Gary Sieling","twitter:description":"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 - these will end up in a file called &quot;managed-schema&quot;. This is a neat feature, but it doesn't occur by default when you"},"aioseo_meta_data":{"post_id":"3626","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2023-02-04 16:48:35","updated":"2026-07-06 01:46:54","ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.garysieling.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/\" title=\"Code Examples\">Code Examples<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tUpdate data in Solr from Scala\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.garysieling.com\/blog"},{"label":"Code Examples","link":"https:\/\/www.garysieling.com\/blog\/category\/code-examples\/"},{"label":"Update data in Solr from Scala","link":"https:\/\/www.garysieling.com\/blog\/update-data-solr-scala\/"}],"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}]}}