We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 39251
    • 53 Posts
    So I needed to add search functionality to a site. Oddly, I've done 30+ MODX sites and never needed to add this feature before. I tried both SimpleSearch and AdvSearch and neither one really did what I needed.

    One, I needed to search TVs, which ruled out SimpleSearch. AdvSearch... well, I hate to say I found it confusing, but I was in a bit of a rush. That aside, I wanted the search to return rendered content, which neither did. (I use getResources on a lot of pages.) I did manage to hack AdvSearch to do so (thanks to a suggestion from Bob I found somewhere on here that involved creating temporary chunks to process the content), but that still left me with the problem that a lot of my pages display lists of child resources, so a search hit on child resources needed to display the parent. It sounded like the Zend mode in AdvSearch might have helped with a lot of my issues, but I never could get that to work.

    Anyway, after wasting nearly a day on this, for some reason it occurred to me to try getResources with getPage for the paging. Here's what I ended up with. Oh, since it doesn't seem that getIDs supports getting all of the top-level IDs (everything directly under "web"), that c0 is something I hacked into it, thus the renamed snippet. And that extra zero at the end of the resources property in getPage is because my ID list had a trailing comma.
    [[!getResources? &resources=`[[GetIds-swd? &ids=`c0, -130, -131, -132, -c6, 15, c17` &depth=`2`]]` &tpl=`@INLINE [[+id]],` &limit=`0` &toPlaceholder=`results01`
                     &where=`{"pagetitle:LIKE":"%[[!get? &param=`search`]]%", "OR:content:LIKE":"%[[!get? &param=`search`]]%"}`]]
    [[!getResources? &parents=`14,15` &tpl=`@INLINE [[+parent]],` &limit=`0` &toPlaceholder=`results02` 
                     &tvFilters=`leader-email==%[[!get? &param=`search`]]%||leader-expertise==%[[!get? &param=`search`]]%||leader-title==%[[!get? &param=`search`]]%||office-location==%[[!get? &param=`search`]]%`]]
    [[!getPage@template-overrides? &element=`getResources` &limit=`10` &resources=`[[+results01]][[+results02]] 0` &parents=`-1`
                                   &tpl=`search-result_tpl`&includeContent=`1` &includeTVs=`1` &processTVs=`1`
                                   &sortby=`menuindex` &sortdir=`ASC` &searchterm=`[[!get? &param=`search`]]` &toPlaceholder=`results`]]
    
    <div id="count">
      [[+results:isnot=``:then=`Results <b>[[+firstItem]]</b> to <b>[[+lastItem]]</b> of <b>[[+total]]</b>.`:else=`No results found for <b>[[!get? &param=`search`]]</b>.`]]
    </div>
    [[+results:notempty=`[[+results]]`]]
    

    Here's the output template:
    <div class="search-result">
      <h3><a href="[[+link:is=``:then=`[[~[[+id]]]]`:else=`[[+link]]`]]" title="[[+longtitle]]">[[+pagetitle]]</a></h3>
      <div class="extract">
        [[+content:striptags:ellipsis=`400`:replace=`[[+searchterm]]==<span class="search-highlight">[[+searchterm]]</span>`]]
      </div>
    </div>
    

    I won't say there may not have been a better way to do that (putting it in a snippet would be one), but I did a bunch of testing and it seemed to work. (Looking at my above output template, I'm not exactly sure what that link vs id thing is for, I think it should always be outputting the id. Like I said, I was in a hurry. smiley)

    Note that due to the structure of this particular site, using depth=2 for the first search worked because all of the child resources meant to display on their parents are at level 3. Others sites I've done, this would be a lot more complicated.

    Also, I used GetIds because unless I'm doing it wrong, excluding a parent from getResources doesn't exclude its children. Using the "where" property might work for that, but if whoever is currently in charge of getResources would consider adding the ability to exclude a folder and all of its children, its childrens' children, etc., that would be very useful. (I'll also add here that getResources is AWESOME and I use it in some way or another on pretty much every site I make, so thank you thank you thank you!)

    Anyway, if anyone is looking for an alternate way of searching a site and displaying the output more as it should be displayed, I just wanted to share. It obviously needs to be customized for each specific site, but it's a good start.

    Suggestions for improvements are definitely welcome. I suspect this won't be the last time I use this. [ed. note: ocdcoder last edited this post 12 years, 4 months ago.]
      If there's a better way to do it, I'll find it.
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You might be interested in pdoTools. It's a library for fetching stuff out of the database, and the package comes with several snippets, like pdoMenu and pdoResources, which are pretty much drop-in replacements for Wayfinder and getResources.

      Unfortunately for the rest of us, the documentation is all in Russian, as are the code comments. Google Translate helps to get the ideas across, but of course leaves a lot to be desired.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 4172
        • 5,888 Posts
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!