• Create a new resource at the root of the tree

    Today is a lesson on how to implement site search in Modx Revolution using the SimpleSearch component.

    Basic snippet parameters:

    Name Description Default
    tpl The part that is used to display the content of each search result. SearchResult
    containerTpl A block that is used to wrap search results, pagination, and posts. SearchResult
    useAllWords If true, only results with all specified search words will be found. 0
    maxWords The maximum number of words to include in the search. Only applicable if useAllWords is disabled. 7
    minChars Minimum number of characters to start a search. 3
    searchStyle To search using a "partial" LIKE search or a relevance-based "match" search. partial
    andTerms Is it necessary to add logical AND between words. 1
    matchWildcard Enable wildcard search. Set to false to perform an exact search based on the search term. 1
    docFields List of individual document fields to search. pagetitle,
    longtitle,
    description,
    introtext,
    alias
    ,content
    fieldPotency Evaluating and sorting results
    perPage Number of search results for one page. 10
    showExtract Whether to show a cutout of the content of each search result. 1
    extractSource Allows the user to determine where the extraction comes from. If the value of this parameter is the name of a resource field (including TV if & includeTVs is set), then that resource field is used for the extract. Otherwise, the parameter is taken as the name of the fragment to be executed. The fragment is passed an array of resources as parameters. If this name does not have a fragment, then the extract will be empty. content
    extractLength The number of characters to extract from the content for each result. 200
    extractEllipsis The string used to carry the results of the extraction. The default is ellipsis.
    includeTVs Specifies whether TemplateVar values ​​should be included in the properties available for each resource template. Default is 0. Enabling this setting may slow things down if you have many TVs. 0
    processTVs Indicates whether TemplateVar values ​​should be displayed as they would be on a summarized resource. Default is 0. Some notes: TVs can be accessed by their TV name [[+ myTV]] By default, SimpleSearch does not use a prefix, for example [[+ tv.myTV]] will NOT be displayed.
    TVs are processed during indexing for Solr search, so there is no need to do that here.
    0
    highlightResults Select or leave out the search query in the results. 1
    highlightClass The name of the CSS class to add to the highlighted conditions in the results. sisea-highlight
    highlightTag An html tag to wrap the highlighted term in search results. span
    pageTpl The part used to link to pages. PageLink
    currentPageTpl The block used for the current link on the page. CurrentPageLink
    pagingSeparator Separator (separator) for use between page links. |
    ids List of document IDs, separated by commas, to limit the search
    idType The type of constraint for the ids parameter. If parents, then all identifier elements will be added to the ids parameter for the search. If documents, only the specified identifiers will be used in the search. parents
    exclude List of resource identifiers, separated by commas, to exclude from the search, for example. "10,15,19." This excludes resources with IDs of "10", "15" or "19".
    depth If idtype is set to parent, the depth down the resource tree that will be searched with the specified identifiers. 10
    hideMenu Do I need to return Resources that have hidemenu. 0 shows only visible resources, 1 shows only hidden resources, 2 shows both. 2
    contexts Search contexts. The default is to use the current context if none is specified explicitly.
    searchIndex The name of the REQUEST parameter that will be used by the search. search
    offsetIndex The name of the REQUEST parameter used to offset the page. sisea_offset
    placeholderPrefix Prefix for global placeholders, given by the snippet. sisea.
    toPlaceholder Whether to set the output to return directly or set to a placeholder with the name of this property.
    urlScheme Required URL scheme: http, https, full, abs, relative, etc. See $modx->makeUrl() documentation. This is used when pagination links are generated.
    customPackages Set to search for custom tables by downloading their package. See below for details.
    postHooks A comma-separated list of hooks to run that can add faceted sets to the final results.
    activeFacet Current active edge. Leave this alone if you don't want the result to be shown from a non-standard aspect received via postHook. default
    facetLimit The number of non-active-facet results to display on the main results page. 5
    sortBy List of resource fields, separated by commas, for sorting results. Leave the field blank to sort by relevance and rating.
    sortDir A list of instructions for sorting the results, separated by commas. Must match the number of elements in the sortBy parameter. DESC
    noResultsTpl Chunk to use if search results are not found.
    Implementation of search on the MODX website

    1. SimpleSearch

    2. We solve various problems with encodings, etc.

    Go to core/components/simplesearch/model/simplesearch/simplesearch.class.php and replace the following lines in it:

    line 297:

    $text = preg_replace("/(\[\[\+.*?\]\])/i", "", $text);

    replace with

    $text = preg_replace("/(\[\[\+.*?\]\])/iu", "", $text);

    line 311:

    $text = trim(preg_replace("/\s+/", " ", $this->sanitize($text)));

    replace with

    $text = trim(preg_replace("/\s+/u", " ", $this->sanitize($text)));

    line 413:

    $string = preg_replace("/" . $quoteValue . "/i", "$0", $string);

    replace with

    $string = preg_replace("/" . $quoteValue . "/iu", "$0", $string);

    Let's comment out line 179:

    If (!empty($str)) $this->searchString = strip_tags($this->modx->sanitizeString($str));

    those. it should look like this

    /*if (!empty($str)) $this->searchString = strip_tags($this->modx->sanitizeString($str)); */

    3. Make copies of all standard chunks that are located in the directory core/components/simplesearch/elements/chunks/

    We create new chunks with the same names and the prefix -new, and the same contents (only they are no longer file-based, but regular), for which you will understand all this further.

    For example, we take the first chunk currentpagelink.chunk.tpl and create a new chunk currentpagelink-new (elements - create a new chunk) and transfer the code to it.

    [[+text]]

    Responsible for controlling the button in pagination for the active page.

    And in the same way we create the remaining chunks:

    — pagelink-new — is responsible for displaying a separate pagination button.

    [[+text]]

    • [[+link]] - link to document
    • [[+text]] - document number

    — searchform-new — search form

    [[%sisea.search? &namespace=`sisea` &topic=`default`]]

    - searchnoresults can not be transferred - there is no design there;

    — searchresult-new — output of each result item.

    [[+idx]]. [[+pagetitle]]

    [[+extract]]

    • [[+idx]] - search result number in the list
    • [[+link:is=``:then=`[[~[[+id]]]]`:else=`[[+link]]`]]
    • [[+longtitle]] - extended title
    • [[+pagetitle]] - title
    • [[+extract]] - the text by which this document was found

    - searchresultli - in fact, it can also not be transferred.

    - searchresults-new - wrapper of all results

    [[+resultInfo]]

    [[+paging]] [[+results]] [[%sisea.result_pages? &namespace=`sisea` &topic=`default`]][[+paging]]

    • [[+resultInfo]] - information about the number of documents found and for what phrase.
    • [[+sisea.result_pages? &namespace=`sisea` &topic=`default`]]

      Displaying the phrase “Search results pages:”

    • [[+paging]] - pagination
    • [[+results]] - search results

    After all this, you should have 5 new chunks.

    4. Create a new document on which search results will be displayed

    Title: Search results, check the “Do not show in menu” checkbox. Go to “Settings” and uncheck the “Searchable” and “Use HTML editor” checkboxes. Save. We refresh the page and add it to the content

    [[!SimpleSearch? &tpl=`searchresult-new` &containerTpl=`searchresults-new` &pageTpl=`pagelink-new` ¤tPageTpl=`currentpagelink-new`]]

    design chunks that were created in step 3 (appearance).

    If you want the search results to include pictures, then add them at the end of this output

    &includeTVs=`1` &processTVs=`1`

    In the searchresult chunk, the image can be called like this: [[+img]].

    5. Display the search form

    In the place where you need to display the search form, write

    [[!SimpleSearchForm? &landing=`11` &tpl=`searchform-new`]]

    Here landing is the id of the page with search results, in tpl are the form design chunks that were created in step 3.

    We did steps 4 and 5 so that you could edit the external design at your discretion: the form itself, search results, etc. without getting into the sources. And the standard design is quite sad; for example, this is what the standard search form looks like.

    6. We change the design.

    I don’t see the point in describing how to design all the chunks; as an example, I’ll give a new design for the searchform-new chunk

    This is the usual standard form markup from Bootstrap 3, on the site it looks like this:

    Official documentation here: docs.modx.com/extras/revo/simplesearch

    If you have questions, write and ask them in the comments.

    SimpleSearch is a standard package for site search in modx revolution. We will add an ajax request to it for live search. An example can be seen on the same site; it works quite simply and, mind you, with pagination. The SimpleSearchForm snippet (search form) requests an ajax request from a separate results page, which displays the SimpleSearch snippet.

    1 Download the popular SimpleSearch site search package from the modx revo repository and install it.
    We create a resource with an empty template for ajax to collect data and send it when making requests in the search form.
    For example, let’s call the alias “search-results” and it will be accessed by ajax.
    Resource contents:

    search results if(typeof jQuery !== undefined) ( $(document).ready(function() ( $(".sisea-page a").click(function(event) ( // pagination event.preventDefault(); // Prevents the browser from navigating through pages // Loads the next search page and shows the container $("#site-search-results").load($(this).attr("href")).show(); // stub )); )) [[!SimpleSearch]]

    2 Now let’s place the search form in a place convenient for you, the snippet “SimpleSearchForm” is responsible for this.
    As a rule, the search form is shoved into modx templates.
    The div element displays the results of the ajax request.

    [[!SimpleSearchForm]]

    3 Let’s connect ajax to send requests from the form to the page we created in step 1.
    (!) Don't forget to connect jquery
    (!) Make sure at what address ajax accesses the resource. The load() method is responsible for this in 2 places.

    // SimpleSearch ajax $(document).ready(function() ( // Button $(".sisea-search-form").submit(function () ( // uncomment if you need a button // $("#site- search-results").load("/search-results/",$(".sisea-search-form").serialize()).slideDown("fast"); return false; )); // Live search $(".sisea-search-form input").keyup(function() ( if(this.value.length > 2) ( // User types more than 2 characters in the search string // hides/shows results outside the window $(document).click(function(event)( // hide if ($(event.target).closest(".site-search-results").length) return; $(".site-search-results" ).slideUp("fast"); //event.stopPropagation()); $("#search").click(function() ( // display $(".site-search-results").slideDown ("fast"); return false; )); // ajax request loading search results from the page and displaying the container $("#site-search-results").load("/search-results/",$(". sisea-search-form").serialize()).slideDown("fast"); ) else ( // If less than 2 characters are typed, hide the container (CSS display:none;) $("#site-search-results" ).slideUp("fast"); ) )); ));

    4 We connect css to display the results of the ajax effect of hiding and revealing the window and its necessary display styles, all to a minimum. Complete the rest of the styles yourself as described in the manual of the SimpleSearch package in modx revolution.

    /* SimpleSearch ajax */ .site-search-results ( display:none; position:absolute; text-align:left; padding:15px; background:#F7F7F7; border-radius:5px; -webkit-border-radius:5px ; -moz-border-radius:5px; box-shadow: 2px 3px 9px -2px rgba(50, 50, 50, 0.55); 0.55); -moz-box-shadow: 2px 3px 9px -2px rgba(50, 50, 50, 0.55);

    The search button in the SimpleSearchForm snippet can be removed altogether
    since ajax already intercepts pressing the Enter button.

    This snippet displays search results based on the search criteria sent.

    Usage

    Simply place the snippet in the Resource you would like to display search results in.

    [[!SimpleSearch]]

    Available Properties Name Description Default
    tpl The chunk that will be used to display the contents of each search result. SearchResult
    containerTpl The chunk that will be used to wrap all the search results, pagination and message. SearchResults
    useAllWords If true, will only find results with all the specified search words. 0
    maxWords The maximum number of words to include in the search. Only applicable if useAllWords is off. 7
    minChars The minimum number of characters to trigger the search. 3
    searchStyle To search either with a "partial" LIKE search, or a relevance-based "match" search. partial
    andTerms Whether or not to add a logical AND between words. 1
    matchWildcard Enable wildcard search. Set to false to do exact searching on a search term. 1
    docFields A comma-separated list of specific Resource fields to search. pagetitle,longtitle,alias,description,introtext,content
    fieldPotency Score and sort the results (see https://github.com/splittingred/SimpleSearch/pull/29 for more infos/usage)
    perPage The number of search results to show per page. 10
    showExtract Whether or not to show an extract of the content of each search result. 1
    extractSource (new in version 1.9) Allows the user to define where the extract comes from. If the value of this parameter is a resource field name (including TVs if &includeTVs is set) then that resource field is used for the extract. Otherwise the parameter is taken as the name of a Snippet to run. The Snippet is passed the resource array as parameters. If there is no Snippet by that name, then the extract will be empty. content
    extractLength The number of characters for the content extraction of each search result. 200
    extractEllipsis The string used to wrap extract results with. Defaults to an ellipsis. ...
    includeTVs Indicates if TemplateVar values ​​should be included in the properties available to each resource template. Defaults to 0. Turning this on might make your search slower if you have lots of TVs. 0
    includeTVList An optional comma-delimited list of TemplateVar names to include explicitly if includeTVs is 1.
    process TVs Indicates if TemplateVar values ​​should be rendered as they would on the resource being summarized. Defaults to 0. Some notes:
    TVs can be accessed by their TV name [[+myTV]] By default SimpleSearch does not use a prefix, e.g. [[+tv.myTV]] will NOT render. TVs are processed during indexing for Solr searching, so there is no need to do this here.
    0
    highlightResults Whether or not to highlight the search term in results. 1
    highlightClass The CSS class name to add to highlighted terms in results. simplesearch-highlight
    highlightTag The html tag to wrap the highlighted term with in search results. span
    pageTpl The chunk to use for a pagination link. PageLink
    currentPageTpl The chunk to use for the current pagination link. CurrentPageLink
    pagingSeparator The separator to use between pagination links.
    ids A comma-separated list of IDs to restrict the search to.
    idType The type of restriction for the ids parameter. If parents, will add all the children of the IDs in the ids parameter to the search. If documents, will only use the specified IDs in the search. parents
    exclude A comma-separated list of resource IDs to exclude from search eg. "10,15,19". This will exclude the resources with the ID "10","15" or "19".
    depth If idtype is set to parents, the depth down the Resource tree that will be searched with the specified IDs. 10
    hideMenu Whether or not to return Resources that have hidemenu on. 0 shows only visible Resources, 1 shows only hidden Resources, 2 shows both. 2
    contexts The contexts to search. Defaults to the current context if none are explicitly specified.
    searchIndex The name of the REQUEST parameter that the search will use. search
    offsetIndex The name of the REQUEST parameter to use for the pagination offset. simplesearch_offset
    placeholderPrefix The prefix for global placeholders set by this snippet. simplesearch.
    toPlaceholder Whether to set the output to directly return, or set to a placeholder with this propertys name.
    urlScheme The URL scheme you want: http, https, full, abs, relative, etc. See the $modx->makeUrl() documentation. This is used when the pagination links are generated.
    customPackages Set to search custom tables by loading their package. See below for more details.
    postHooks A comma-separated list of hooks to run that can add faceted sets to the end results.
    activeFacet The current active facet. Leave this alone unless you want a result to show from a non-standard facet derived through a postHook. default
    facetLimit The number of non-active-facet results to show on the main results page. 5
    sortBy A comma-separated list of Resource fields to sort the results by. Leave blank to sort by relevance and score.
    sortDir A comma-separated list of directions to sort the results by. Must match the number of items in the sortBy parameter. DESC
    noResultsTpl The chunk to use when no search results are found.
    SimpleSearch Chunks

    There are 4 chunks that are processed in SimpleSearch. Their corresponding SimpleSearch parameters are:

    • tpl - The Chunk to use for each result displayed.
    • containerTpl - The Chunk that will be used to wrap all the search results, pagination and message.
    • pageTpl - The Chunk to use for a pagination link.
    • currentPageTpl - The Chunk to use for the current pagination link.
    Searching Custom Tables

    Searching custom tables is available in SimpleSearch using the &customPackages property; however, you must have a custom package built for it. The format is:

    ClassName:fieldName(s):packageName:packagePath:joinCriteria||class2Name:fieldName(s):package2Name:package2Path:join2Criteria

    In other words, each custom package is separated by ||. Then, each part of it is separated by colons (:). An example to search Quip comments:

    &customPackages=`quipComment:body:quip:(core_path)components/quip/model/:quipComment.resource = modResource.id`

    Let's break down each part:

    • className - The class name of the table you want to search. Here, it's QuipComment.
    • fieldName(s) - A comma-separated list of column names to search. We did "body", you could also have done "body,email" or whatever.
    • packageName - The name of the schema Package to add. This one is called quip.
    • packagePath - The path to the model/ directory where the package is located.
    • joinCriteria - The SQL to join the table you want to search and the modResource table. Your table must have some connection to the Resource it"s on (otherwise SimpleSearch won"t know how to load a URL for it!)

    Once you"ve added it, it will search those fields as well for data. If it finds it in that table, it will display the result as a link to the Resource you specified in your joinCriteria. In our example, that would be the resource the Quip comment is located on.

    Today is a lesson on how to implement site search in Modx Revolution using the SimpleSearch component.

    Basic snippet parameters:

    Name Description Default
    tpl The part that is used to display the content of each search result. SearchResult
    containerTpl A block that is used to wrap search results, pagination, and posts. SearchResult
    useAllWords If true, only results with all specified search words will be found. 0
    maxWords The maximum number of words to include in the search. Only applicable if useAllWords is disabled. 7
    minChars Minimum number of characters to start a search. 3
    searchStyle To search using a "partial" LIKE search or a relevance-based "match" search. partial
    andTerms Is it necessary to add logical AND between words. 1
    matchWildcard Enable wildcard search. Set to false to perform an exact search based on the search term. 1
    docFields List of individual document fields to search. pagetitle,
    longtitle,
    description,
    introtext,
    alias
    ,content
    fieldPotency Evaluating and sorting results
    perPage Number of search results for one page. 10
    showExtract Whether to show a cutout of the content of each search result. 1
    extractSource Allows the user to determine where the extraction comes from. If the value of this parameter is the name of a resource field (including TV if & includeTVs is set), then that resource field is used for the extract. Otherwise, the parameter is taken as the name of the fragment to be executed. The fragment is passed an array of resources as parameters. If this name does not have a fragment, then the extract will be empty. content
    extractLength The number of characters to extract from the content for each result. 200
    extractEllipsis The string used to carry the results of the extraction. The default is ellipsis.
    includeTVs Specifies whether TemplateVar values ​​should be included in the properties available for each resource template. Default is 0. Enabling this setting may slow things down if you have many TVs. 0
    processTVs Indicates whether TemplateVar values ​​should be displayed as they would be on a summarized resource. Default is 0. Some notes: TVs can be accessed by their TV name [[+ myTV]] By default, SimpleSearch does not use a prefix, for example [[+ tv.myTV]] will NOT be displayed.
    TVs are processed during indexing for Solr search, so there is no need to do that here.
    0
    highlightResults Select or leave out the search query in the results. 1
    highlightClass The name of the CSS class to add to the highlighted conditions in the results. sisea-highlight
    highlightTag An html tag to wrap the highlighted term in search results. span
    pageTpl The part used to link to pages. PageLink
    currentPageTpl The block used for the current link on the page. CurrentPageLink
    pagingSeparator Separator (separator) for use between page links. |
    ids List of document IDs, separated by commas, to limit the search
    idType The type of constraint for the ids parameter. If parents, then all identifier elements will be added to the ids parameter for the search. If documents, only the specified identifiers will be used in the search. parents
    exclude List of resource identifiers, separated by commas, to exclude from the search, for example. "10,15,19." This excludes resources with IDs of "10", "15" or "19".
    depth If idtype is set to parent, the depth down the resource tree that will be searched with the specified identifiers. 10
    hideMenu Do I need to return Resources that have hidemenu. 0 shows only visible resources, 1 shows only hidden resources, 2 shows both. 2
    contexts Search contexts. The default is to use the current context if none is specified explicitly.
    searchIndex The name of the REQUEST parameter that will be used by the search. search
    offsetIndex The name of the REQUEST parameter used to offset the page. sisea_offset
    placeholderPrefix Prefix for global placeholders, given by the snippet. sisea.
    toPlaceholder Whether to set the output to return directly or set to a placeholder with the name of this property.
    urlScheme Required URL scheme: http, https, full, abs, relative, etc. See $modx->makeUrl() documentation. This is used when pagination links are generated.
    customPackages Set to search for custom tables by downloading their package. See below for details.
    postHooks A comma-separated list of hooks to run that can add faceted sets to the final results.
    activeFacet Current active edge. Leave this alone if you don't want the result to be shown from a non-standard aspect received via postHook. default
    facetLimit The number of non-active-facet results to display on the main results page. 5
    sortBy List of resource fields, separated by commas, for sorting results. Leave the field blank to sort by relevance and rating.
    sortDir A list of instructions for sorting the results, separated by commas. Must match the number of elements in the sortBy parameter. DESC
    noResultsTpl Chunk to use if search results are not found.
    Implementation of search on the MODX website

    1. SimpleSearch

    2. We solve various problems with encodings, etc.

    Go to core/components/simplesearch/model/simplesearch/simplesearch.class.php and replace the following lines in it:

    line 297:

    $text = preg_replace("/(\[\[\+.*?\]\])/i", "", $text);

    replace with

    $text = preg_replace("/(\[\[\+.*?\]\])/iu", "", $text);

    line 311:

    $text = trim(preg_replace("/\s+/", " ", $this->sanitize($text)));

    replace with

    $text = trim(preg_replace("/\s+/u", " ", $this->sanitize($text)));

    line 413:

    $string = preg_replace("/" . $quoteValue . "/i", "$0", $string);

    replace with

    $string = preg_replace("/" . $quoteValue . "/iu", "$0", $string);

    Let's comment out line 179:

    If (!empty($str)) $this->searchString = strip_tags($this->modx->sanitizeString($str));

    those. it should look like this

    /*if (!empty($str)) $this->searchString = strip_tags($this->modx->sanitizeString($str)); */

    3. Make copies of all standard chunks that are located in the directory core/components/simplesearch/elements/chunks/

    We create new chunks with the same names and the prefix -new, and the same contents (only they are no longer file-based, but regular), for which you will understand all this further.

    For example, we take the first chunk currentpagelink.chunk.tpl and create a new chunk currentpagelink-new (elements - create a new chunk) and transfer the code to it.

    [[+text]]

    Responsible for controlling the button in pagination for the active page.

    And in the same way we create the remaining chunks:

    — pagelink-new — is responsible for displaying a separate pagination button.

    [[+text]]

    • [[+link]] - link to document
    • [[+text]] - document number

    — searchform-new — search form

    [[%sisea.search? &namespace=`sisea` &topic=`default`]]

    - searchnoresults can not be transferred - there is no design there;

    — searchresult-new — output of each result item.

    [[+idx]]. [[+pagetitle]]

    [[+extract]]

    • [[+idx]] - search result number in the list
    • [[+link:is=``:then=`[[~[[+id]]]]`:else=`[[+link]]`]]
    • [[+longtitle]] - extended title
    • [[+pagetitle]] - title
    • [[+extract]] - the text by which this document was found

    - searchresultli - in fact, it can also not be transferred.

    - searchresults-new - wrapper of all results

    [[+resultInfo]]

    [[+paging]] [[+results]] [[%sisea.result_pages? &namespace=`sisea` &topic=`default`]][[+paging]]

    • [[+resultInfo]] - information about the number of documents found and for what phrase.
    • [[+sisea.result_pages? &namespace=`sisea` &topic=`default`]]

      Displaying the phrase “Search results pages:”

    • [[+paging]] - pagination
    • [[+results]] - search results

    After all this, you should have 5 new chunks.

    4. Create a new document on which search results will be displayed

    Title: Search results, check the “Do not show in menu” checkbox. Go to “Settings” and uncheck the “Searchable” and “Use HTML editor” checkboxes. Save. We refresh the page and add it to the content

    [[!SimpleSearch? &tpl=`searchresult-new` &containerTpl=`searchresults-new` &pageTpl=`pagelink-new` ¤tPageTpl=`currentpagelink-new`]]

    design chunks that were created in step 3 (appearance).

    If you want the search results to include pictures, then add them at the end of this output

    &includeTVs=`1` &processTVs=`1`

    In the searchresult chunk, the image can be called like this: [[+img]].

    5. Display the search form

    In the place where you need to display the search form, write

    [[!SimpleSearchForm? &landing=`11` &tpl=`searchform-new`]]

    Here landing is the id of the page with search results, in tpl are the form design chunks that were created in step 3.

    We did steps 4 and 5 so that you could edit the external design at your discretion: the form itself, search results, etc. without getting into the sources. And the standard design is quite sad; for example, this is what the standard search form looks like.

    6. We change the design.

    I don’t see the point in describing how to design all the chunks; as an example, I’ll give a new design for the searchform-new chunk

    This is the usual standard form markup from Bootstrap 3, on the site it looks like this:

    Official documentation here: docs.modx.com/extras/revo/simplesearch

    If you have questions, write and ask them in the comments.