{"id":6120,"date":"2013-11-05T13:41:05","date_gmt":"2013-11-05T12:41:05","guid":{"rendered":"http:\/\/jonas.kihlsten.se\/blog\/?p=6120"},"modified":"2013-11-06T20:18:31","modified_gmt":"2013-11-06T19:18:31","slug":"episerver-7search-prevent-content-from-being-indexed","status":"publish","type":"post","link":"http:\/\/jonas.kihlsten.se\/blog\/2013\/11\/episerver-7search-prevent-content-from-being-indexed\/","title":{"rendered":"EPiServer 7\/Search: Prevent content from being indexed"},"content":{"rendered":"<p>Let&#8217;s say you have a page or a page type that you do not want to get indexed by the EPiServer Search service. The solution is to implement the <a href=\"http:\/\/world.episerver.com\/Documentation\/Class-library\/?documentId=cms\/7\/4b1ee78f-c8b0-7c89-9f2f-e50512a20d75\" target=\"_blank\">ISearchable<\/a> interface:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nbool AllowReIndexChildren { get; }\r\nbool IsSearchable { get; }\r\n<\/pre>\n<p>It requires you to implement two boolean properties, <em>AllowReIndexChildren<\/em> and <em>IsSearchable<\/em>. By letting <em>IsSearchable<\/em> return <em>false<\/em> you&#8217;ll prevent the page from being indexed. You could either implement it as a page property, having the editor choose which pages should be excluded:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;Display(Name = &quot;Exclude page from site search&quot;,\r\n    Description = &quot;Check this box to prevent page from being indexed by the site's search engine.&quot;,\r\n    GroupName = SystemTabNames.Settings,\r\n    Order = 100)]\r\npublic virtual bool IsSearchable\r\n{\r\n    get\r\n    {\r\n        bool isSearchable = this.GetPropertyValue(p =&gt; p.IsSearchable);\r\n        return !isSearchable;\r\n    }\r\n    set\r\n    {\r\n        this.SetPropertyValue(p =&gt; p.IsSearchable, value);\r\n    }\r\n}\r\n<\/pre>\n<p>Or you could prevent the the entire page type from being indexed:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;ContentType(DisplayName = &quot;Sneaky page that won't get indexed.&quot;, GUID = &quot;00000000-0000-0000-0000-000000000000&quot;)]\r\npublic class SneakyPage : PageData, ISearchable\r\n{\r\n    public bool AllowReIndexChildren { get { return true; } }\r\n    public bool IsSearchable { get { return false; } }\r\n}\r\n<\/pre>\n<p>But what about content that is already in the index? Ticking the box to prevent indexing on a page that has previouly been index will only prevent updates to this item, not prevent it from showing in search results.<\/p>\n<p>In order to make sure indexed items are removed add the code below to Global.asax<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprotected void Application_Start()\r\n{\r\n    DataFactory.Instance.PublishedContent += OnPublishedContent;\r\n}\r\n\r\nprivate void OnPublishedContent(object sender, ContentEventArgs contentEventArgs)\r\n{\r\n    ISearchable seachable = contentEventArgs.Content as ISearchable;\r\n    if (seachable == null) {\r\n        return;\r\n    }\r\n\r\n    if (seachable.IsSearchable) {\r\n        return;\r\n    }\r\n\r\n    CultureInfo language = null;\r\n    ILocalizable localizable = contentEventArgs.Content as ILocalizable;\r\n\r\n    if (localizable != null) {\r\n        language = localizable.Language;\r\n    }\r\n\r\n    string searchId = string.Concat(contentEventArgs.Content.ContentGuid, &quot;|&quot;, language);\r\n    IndexRequestItem indexItem = new IndexRequestItem(searchId, IndexAction.Remove);\r\n    SearchHandler.Instance.UpdateIndex(indexItem);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s say you have a page or a page type that you do not want to get indexed by the EPiServer Search service. The solution is to implement the ISearchable interface: bool AllowReIndexChildren { get; } bool IsSearchable { get; } It requires you to implement two boolean properties, AllowReIndexChildren and IsSearchable. By letting IsSearchable &hellip; <a href=\"http:\/\/jonas.kihlsten.se\/blog\/2013\/11\/episerver-7search-prevent-content-from-being-indexed\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">EPiServer 7\/Search: Prevent content from being indexed<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,28],"tags":[23,35,36],"class_list":["post-6120","post","type-post","status-publish","format-standard","hentry","category-episerver","category-search","tag-c","tag-episerver","tag-search"],"_links":{"self":[{"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/posts\/6120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/comments?post=6120"}],"version-history":[{"count":14,"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/posts\/6120\/revisions"}],"predecessor-version":[{"id":6136,"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/posts\/6120\/revisions\/6136"}],"wp:attachment":[{"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/media?parent=6120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/categories?post=6120"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/jonas.kihlsten.se\/blog\/wp-json\/wp\/v2\/tags?post=6120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}