{"id":5319,"date":"2018-06-26T11:42:01","date_gmt":"2018-06-26T09:42:01","guid":{"rendered":"https:\/\/florianbrinkmann.com\/en\/?p=5319"},"modified":"2020-02-09T11:22:03","modified_gmt":"2020-02-09T10:22:03","slug":"elasticpress-related-posts-elasticsearch-6","status":"publish","type":"post","link":"https:\/\/florianbrinkmann.com\/en\/elasticpress-related-posts-elasticsearch-6-5319\/","title":{"rendered":"Use ElasticPress\u2019 related posts feature with Elasticsearch 6"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A while back I updated to Elasticsearch 6, and with that, the related posts feature of ElasticPress stopped working. First I thought it has to do with an <a href=\"https:\/\/github.com\/10up\/ElasticPress\/issues\/1077\">ElasticPress issue<\/a>, but the reason was a parameter used by ElasticPress that is not supported by Elasticsearch 6 that needs to be modified.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"wp-block-paragraph\">ElasticPress uses <code class=\"lang-php\">$formatted_args['query']['more_like_this']['ids']<\/code> to define the post to search related content for. The <code class=\"lang-php\">ids<\/code> key is no longer present in Elasticsearch 6. Instead, you have to create an array per content for that you want related content for, under <code class=\"lang-php\">$formatted_args['query']['more_like_this']['like']<\/code>. The <code class=\"lang-php\">_id<\/code> key then gets the ID of the content that was previously stored in <code class=\"lang-php\">ids<\/code> (<a href=\"https:\/\/www.elastic.co\/guide\/en\/elasticsearch\/reference\/6.2\/query-dsl-mlt-query.html\">that is described in the \u00bbMore Like This Query\u00ab post of the Elasticsearch 6.2 documentation<\/a>).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That looks like that, using the <code class=\"lang-php\">ep_formatted_args<\/code> filter by ElasticPress:<\/p>\n\n\n<pre class=\"wp-block-code lang-php\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">fbn_ep_formatted_args<\/span><span class=\"hljs-params\">( $formatted_args )<\/span> <\/span>{\n\t<span class=\"hljs-comment\">\/\/ Get the ids (usually only one) and unset the unused key.<\/span>\n\t$ids = $formatted_args&#91;<span class=\"hljs-string\">'query'<\/span>]&#91;<span class=\"hljs-string\">'more_like_this'<\/span>]&#91;<span class=\"hljs-string\">'ids'<\/span>];\n\t<span class=\"hljs-keyword\">unset<\/span>($formatted_args&#91;<span class=\"hljs-string\">'query'<\/span>]&#91;<span class=\"hljs-string\">'more_like_this'<\/span>]&#91;<span class=\"hljs-string\">'ids'<\/span>]);\n\n\t<span class=\"hljs-comment\">\/\/ Set the id in the Elasticsearch 6 compatible way.<\/span>\n\t$formatted_args&#91;<span class=\"hljs-string\">'query'<\/span>]&#91;<span class=\"hljs-string\">'more_like_this'<\/span>]&#91;<span class=\"hljs-string\">'like'<\/span>]&#91;] = &#91;\n\t\t<span class=\"hljs-string\">'_id'<\/span> =&gt; <span class=\"hljs-string\">\"$ids&#91;0]\"<\/span>,\n\t];\n\n\t<span class=\"hljs-keyword\">return<\/span> $formatted_args;\n}\n\nadd_filter( <span class=\"hljs-string\">'ep_formatted_args'<\/span>, <span class=\"hljs-string\">'fbn_ep_formatted_args'<\/span>, <span class=\"hljs-number\">20<\/span> );<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">First, I save the IDs and remove the key from the array. Usually, that will only be one ID, so I do not need to loop over multiple but can directly set <code class=\"lang-php\">$ids[0]<\/code> as the value for <code class=\"lang-php\">_id<\/code> in an array under <code class=\"lang-php\">$formatted_args['query']['more_like_this']['like']<\/code>. After that, I correctly get related posts again instead of the latest posts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A while back I updated to Elasticsearch 6, and with that, the related posts feature of ElasticPress stopped working. First I thought it has to do with an ElasticPress issue, but the reason was a parameter used by ElasticPress that is not supported by Elasticsearch 6 that needs to be modified.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","webmentions_disabled_pings":false,"webmentions_disabled":false,"lazy_load_responsive_images_disabled":false,"footnotes":""},"categories":[37],"tags":[],"class_list":["post-5319","post","type-post","status-publish","format-standard","hentry","category-wordpress"],"wp-worthy-pixel":{"ignored":false,"public":null,"server":null,"url":null},"wp-worthy-type":"normal","_links":{"self":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/5319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/comments?post=5319"}],"version-history":[{"count":4,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/5319\/revisions"}],"predecessor-version":[{"id":5957,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/5319\/revisions\/5957"}],"wp:attachment":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/media?parent=5319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/categories?post=5319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/tags?post=5319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}