Allow article to be excluded from search results

11 Comments

  • Kay
    Comment actions Permalink

    It is correct there is no native way to do this right now. But there is maybe another solution for this.

    How it works

    1. Add label hide_in_search to any article you don't want showing up in search
    2. With Javascript add -labels:hide_in_search to the input value of the search query
    3. With CSS set text-indent negative on the search input field until the above query is hidden text-indent: -120px;

    This way you don't have to alter the search result page. And everything works there as expected.

    0
  • Karen D Snyder
    Comment actions Permalink

    Kay, that is a really good idea, but I'm not sure that I understand what you mean with #2. I added the label hide_in_search to the articles with Forms in the title. Then I commented out the code in script.js that hides search results with Forms, and then I typed Forms -labels:hide_in_search in the search bar, and I still saw the articles with Forms in the title. So I think I'm misunderstanding #2.

    I'm also not sure how to implement  #2 if I did understand it. Do you mean add code in script.js that could intercept any time that a search is done, and could access the input value of the search query? Or do you mean change the generated HTML code of the search bar (shown below)?

    <form role="search" class="search" data-search data-instant="true" autocomplete="off" action="/hc/en-us/search" accept-charset=UTF-8" method="get">

      <input name="utf8" type="hidden" value="✓">

      <input type="search" name="query" id="query" placeholder="How can we help?" autocomplete="off" aria-label="How can we help?">

    </form>

     

     

    0
  • Kay
    Comment actions Permalink

    You are right. This is my bad. I just learned that the search in the Help Center behaves a little bit different than the Guide API. However, there still is a solution, and the concept is pretty much the same.

    ⚠️Fair warning: I haven't tested this yet

    1. Add the word hideMe to the title you don't want to display
    2. Wait 5 min (for caching)
    3. Manually search Forms -hideMe (should give you results without those pages, if so move to next step)
    4. With Javascript add -hideMe to the input value of the search query
    5. With CSS set text-indent negative on the search input field until the above query is hidden text-indent: -120px;
    6. With Javascript replace all text hideMe on all pages

      document.querySelector('body').replace(/hideMe/g, '');
    0
  • Karen D Snyder
    Comment actions Permalink

    Hi Kay,

    I tried your idea, but had a problem with step 5. I used the following code to add -hideMe to the input value of the search query:

    var searchInputElt = document.getElementById('query');
    searchInputElt.value = '-hideMe';

    But when I set text-indent to -120px to hide -hideMe, I couldn't type into the search input. I also tried setting text-indent to a large enough positive value to hide -hideMe, but that had the same effect that I couldn't type into the search input. Any suggestions?

    0
  • Kay
    Comment actions Permalink

    On all pages where the search input field is

    var search = document.querySelector('form.search');
    var query = document.querySelector('#query');
    search.addEventListener("submit", (e) => query.value += ' -hideMe');

    On the search results page

    document.querySelector('body').innerHTML = document.querySelector('body').innerHTML.replace(/-hideMe/g, '');
    0
  • Karen D Snyder
    Comment actions Permalink

    Hi Kay,

    I tried your suggestion, but it didn't work. All the articles with hideMe in the title appear in the search results when I search for Forms, and the page URL is https://zdtestsite.zendesk.com/hc/en-us/search?utf8=√&query=forms.

    If I manually search for Forms -hideMe, then the articles with hideMe in the title are not in the search results, and the page URL is https://zdtestsite.zendesk.com/hc/en-us/search?utf8=√&query=forms+-hideMe.

    So it seems that the code to add -hideMe to the input value of the search query is not working. I tried the variation below, but it also didn't work:

    var query = $('#query');
    $('form.search').submit(function () {
      var queryValue = query.val();
      query.val(queryValue + ' -hideMe');
    });

    0
  • Kay
    Comment actions Permalink

    Just tested my code, and your code. Both of them appear to be working before executing the search.
    Can you make sure the code is inside documentready function of jQuery, and on all pages where the searchbar is?

    0
  • Karen D Snyder
    Comment actions Permalink

    Hi Kay,

    That's really strange that it works for you, but not for me. Do you see the -hideMe in the URL of your search results page?

    I put the code inside the documentready function in header.hbs, because my searchbar code is in header.hbs

    There might be a submit event handler defined in the Zendesk code for the search element, and according to https://stackoverflow.com/questions/4507956/two-submit-event-handlers-on-a-form-one-must-stop-the-other, if that handler contains event.stopImmediatePropagation(), that would stop my handler from being executed. But if that were the case, then your code wouldn't work for you either.

    0
  • Kay
    Comment actions Permalink

    Yes, the -hideMe is in the URL. Something is blocking it, but it's not Zendesk. Tested it against the default Copenhagen theme.

    Could you share a snippet of your header.hbs and script.js code?

    0
  • Karen D Snyder
    Comment actions Permalink

    Hi Kay,

    I believe something in Zendesk may be blocking it, related to unified search. As I am using a theme with the unified search results feature, it's not relevant to test with the default Copenhagen theme. I made a new theme by downloading the Copenhagen theme with unified search (from https://github.com/zendesk/copenhagen_theme, branch unified-search). I added the following to home_page.hbs:

    <script>
      $(document).ready(function () {
        var search = document.querySelector('form.search');
        var query = document.querySelector('#query');
        search.addEventListener("submit", (e) => query.value += ' -hideMe');
      });
    </script>

    When I tested in the home page, the search results included articles with hideMe in the title. So even if this submit handler works in the Copenhagen default theme, it does not work in the Copenhagen unified search results theme.

    0
  • Karen D Snyder
    Comment actions Permalink

    Hi Kay,

    I wanted to test this in the Copenhagen default theme, so I imported it from  https://github.com/zendesk/copenhagen_theme, branch master. However, the search results page in the Copenhagen default theme does not work correctly. Although the number of results is correctly displayed, no actual results are displayed.

    0

Please sign in to leave a comment.

Powered by Zendesk