We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23072
    • 150 Posts
    Cheers for this.

    I wondered if I was doing something wrong, but it appears some bits weren’t working as expected after all.

    Glad it wasn’t just me! laugh
      Notanotherdotcom Ltd

      Web | Print | Marketing
      • 19522
      • 17 Posts
      And an optimization (and optimized fix for the odd default behaviour when useAllWords=false) for ’partial’ (the default) mode.
      [tt](A OR B OR C) AND (A OR B OR D) AND (A OR B OR E)[/tt] is true if and only if
      A is true or B is true or (C AND D AND E) is true. Thus
      [tt]A OR B OR (C AND D AND E)[/tt]
      is equivalent.
      Therefore the search can be optimized by leaving A and B out of the foreach loop, avoding the resulting redundant check for [tt]A OR B[/tt]. The opmization is showed below.

      Lines 141 to 143 of AjaxSearch.inc.php (modx 0.95) for useAllWords=true:

      Replace:
      [tt]
      foreach ($search as $searchTerm){
      $sql .= "(sc.pagetitle LIKE ’%$searchString%’ OR sc.description LIKE ’%$searchString%’ OR sc.content LIKE ’%$searchTerm%’) AND ";
      }
      [/tt]
      with:
      [tt]
      $sql .= "(sc.pagetitle LIKE ’%$searchString%’ OR sc.description LIKE ’%$searchString%’ OR (";
      foreach ($search as $searchTerm){
      $sql .= "sc.content LIKE ’%$searchTerm%’ AND ";
      }
      $sql = preg_replace(’/ AND \z/’, ’)) AND ’, $sql);
      $sql = substr_replace($sql, ’)) AND ’, -5);
      [/tt]
      Line 145 for useAllWords=false:

      Replace
      [tt]
      $sql .= "(sc.pagetitle LIKE ’%$searchString%’ OR sc.description LIKE ’%$searchString%’ OR sc.content LIKE ’%$searchString%’) AND ";
      [/tt]
      with
      [tt]
      $sql .= "(";
      foreach ($search as $searchTerm){
      $sql .= "(sc.pagetitle LIKE ’%$searchString%’ OR sc.description LIKE ’%$searchString%’ OR sc.content LIKE ’%$searchTerm%’) OR ";
      }
      $sql = preg_replace(’/ OR \z/’, ’) AND ’, $sql);
      $sql = substr_replace($sql, ’) AND ’, -4);
      [/tt]
      * Edit December 25 2006, minor optimization *
        • 19522
        • 17 Posts
        This code needs some cleaning grin
        Highlighting is odd if several search terms appear near each other.
        AjaxSearch extracts a context of 200 words around the first occurence of each search term which is cool. However, if n search terms occur next to each other 1..n will be highlighted in the first extract. 2..n will be highlightet in the second. 3..n will be highlight in the third and so on until only the n-th term is highlightet in the last extract.
        The following fix will make it highlight only the relevant word in each extract.
        In snippet-ajaxSearch-tpl.php line 518

        Replace:
        [tt]
        $summary .= PrepareSearchContent( $text, $length=200, $searchTerm );
        $summary = preg_replace( ’/’ . preg_quote( $searchTerm, ’/’ ) . ’/i’, ’<span class="ajaxSearch_highlight ajaxSearch_highlight’.$count.’">\0</span>’, $summary );
        [/tt]
        with: *** EDIT See updated fix below ***
        [tt]
        $toAdd = PrepareSearchContent( $text, $length=200, $searchTerm ) . ’ ’;
        $summary .= preg_replace( ’/’ . preg_quote( $searchTerm, ’/’ ) . ’/i’, ’<span class="ajaxSearch_highlight ajaxSearch_highlight’.$count.’">\0</span>’, $toAdd );
        [/tt]
        AjaxSearch still behaves odd when using ’useAllWords=false’, returning n extracts for n search terms even if fewer terms match with the extracts for terms that didn’t match (naturally) containing no highligts. Will post a fix for that later.
          • 19522
          • 17 Posts
          Using the replacement code below instead fixes the no highligt extracts issue for useAllWords=false:
          [tt]
          if (preg_match("/$searchTerm/i", $text)) {
          $toAdd = PrepareSearchContent( $text, $length=200, $searchTerm ) . ’ ’;
          $summary .= preg_replace( ’/’ . preg_quote( $searchTerm, ’/’ ) . ’/i’, ’<span class="ajaxSearch_highlight ajaxSearch_highlight’.$count.’">\0</span>’, $toAdd );
          }
          [/tt]

          Using more matching search terms than the number of defined highlight styles makes it look odd too.
          Maybe I’ll post a fix for that as well cool
            • 19522
            • 17 Posts
            All right. Here is a fix for the multiple search terms issue using ’useAllWords=false’.
            In AjaxSearch.inc.php line 94 below [tt]$searchWords = explode(’ ’,$searchString);[/tt] insert the following code:
            [tt]
            $searchWords = array_slice($searchWords, 0, 5);
            [/tt]
            to limit the terms to 5 and ignore the rest

            To make the code less hackish, you could define $maxTerms=5 (or whatever is suitable) in snippet-ajaxSearch-tpl.php. Then change the declaration of initSearchString() in AjaxSearch.inc.php to include $maxTerms in the parameter list and change the two calls to initSearchString() in AjaxSearch.php and snippet-ajaxSearch-tpl.php to include the $maxTerms parameter, update AjaxSearch.php with $maxTerms = $_GET[’maxTerms’] at the beginning of the file; and finally change the above call to
            [tt]
            $searchWords = array_slice($searchWords, 0, $maxTerms);
            [/tt]
              • 12652
              • 228 Posts
              The "close" image seems to kind of stick out like a sore-thumb.... no offense, there really isn’t anyway to design an image that would work on all sites anyway.

              Obviously the image could be changed and just uploaded with the same name, but then there is always the likelihood that it will get copied over on an update.

              A couple nice additions for the future would be:

              - parameter to specify a different image
              - parameter to specify a text link or standard form button instead

              For less web savvy users, I’m not sure they’ll even understand what the close button does or that it even is a button, but being able to replace that with "Reset" or "Clear" should be understandable by just about everyone.
                | Identity Developments delivers SEO focused web design and web presence services
                - it&#39;s not about websites, it&#39;s about your identity. |
                • 12652
                • 228 Posts
                One very minor fix on the highlighting plugin and a slight hack addition....

                $highlightText .= '<br/><a href="'.$removeUrl.'">'.$removeText.'</a>';


                should have a space between the br and /:
                $highlightText .= '<br /><a href="'.$removeUrl.'">'.$removeText.'</a>';


                Also, for more flexibility in styling, it would be nice to have a div wrapper around everything, and actually a separate class on the "Remove Highlighting" link could even eliminate the need for the
                to begin with.

                For now, here is the minor hack that I incorporated:

                $termText = '<div class="searchTerms">Search Terms: ';

                Add your opening div and class to the $termText assignment and

                $highlightText .= '<br /><a href="'.$removeUrl.'">'.$removeText.'</a></div>';

                add your closing div to this part of the $highlightText build.
                  | Identity Developments delivers SEO focused web design and web presence services
                  - it&#39;s not about websites, it&#39;s about your identity. |
                  • 25663 MODX Staff
                  • 12,272 Posts
                  identity/Perrine/mikkelwe,

                  Great stuff guys. smiley It’d be very cool to see a "consolidated hacks" build of everything, and when Kyle gets off the slopes in Colorado, looks like another release is due. Thanks for taking the time to give back. laugh
                    Ryan Thrash, MODX Co-Founder
                    Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                    • 12652
                    • 228 Posts
                    Sorry I don’t have the skill yet to offer up anything more than a rough hack.

                    However, it may not mean much right now... it seems that the highlighting isn’t working in page on the site I’m working on, which is 0.9.5SL. It highlights the search results and highlights the search terms on page, but nothing within the contents itself. It works on another site, I don’t remember now, but I think I have only upgraded it to 0.9.2, so not sure if this is just an issue on my site or with the new release.
                      | Identity Developments delivers SEO focused web design and web presence services
                      - it&#39;s not about websites, it&#39;s about your identity. |
                      • 23072
                      • 150 Posts
                      It works in the content on my 0.9.5 install, but I’d have to check through my config settings for AjaxSearch. It’s possible one thing being switched on is causing another to be switched off.

                      If you view the source of your content which is supposed to be highlighted, does it have all the styles in place around the words that are supposed to be highlighted? I don’t see how it can as if the search results are being highlighted correctly then the same style is applied to the content - just ruling out all possibilities though wink
                        Notanotherdotcom Ltd

                        Web | Print | Marketing

                      This discussion is closed to further replies. Keep calm and carry on.