Modifying the search bar to show only the results of a page

Hello,
I have a page structure like this:

  • Page A > Page AA > Page AAA
  • Page B > Page BB > Page BBB
  • Page C > Page CC > Page CCC

In Page AAA, I use the Velocity Macro to integrate the search bar. Here is the code I use:

#panelhiddenheader($services.localization.render('panels.search.title'))
  {{html clean="false"}}
    <form action="$xwiki.getURL('Main.Search')" class='xformInline'>
      <div class="globalsearch input-group">
        <label for="globalsearchinput" class="hidden">$services.localization.render('panels.search.query')</label>
        <input id="globalsearchinput" class="globalsearchinput withTip useTitleAsTip" type="text" name="text" title="$services.localization.render('panels.search.inputText')" size="15"/>
        <span class="input-group-btn buttonwrapper">
          <button type="submit" class="btn btn-primary">
            $services.icon.renderHTML('search')<span class="hidden">$services.localization.render('panels.search.submit')</span>
          </button>
        </span>
      </div>
    </form>
  {{/html}}
#panelfooter()

My question: How can I modify this code so that the results presented are only those of Page A and its children (Page AA, Page AAA).

Thanks :slight_smile:

I have been looking for this very functionality, as well, and the Document tree macro is the closest I’ve gotten. But that’s not really the right fit, and I know this must be possible, either with solr or a macro (or both).

Being able to do this would allow us to plug a functionality hole we’re facing in moving to Xwiki from Confluence.

1 Like

I think you can just pass the f_space_facet query string parameter. For example if your top level space is named Test then you would pass f_space_facet=0%2FTest.

PS: you can try the standard search page and play with the location facet and you’ll see what it adds to the query string.

1 Like

Thank you for your reply

Hi,
Thank you for your reply. Sorry, I’m not a developer, how can I apply the f_space_facet query string parameter ? In advance, thank you

Ah, that’s very helpful. So using @christophe1983’s example, our search URL might look like this:
https://mysite.myxwiki.org/xwiki/bin/view/Main/Search?text=testing&f_type=DOCUMENT&f_locale=en&f_locale=&r=1&f_space_facet=0%2FA

@vmassol, what if, continuing that example, we wanted to restrict our focus just to children of AA?

[edit] After some more experimenting, I see you can nest specific locations in f_space_facet, like this, such that search is constrained to the A/AA path:
&f_space_facet=1%2FA.AA.

I’m new to XWiki scripting, so this is an excellent exercise. The significance of the 0 vs 1 immediately following &f_space_facet isn’t clear to me yet (tho it clearly matters). %2F is just “/”, so that absolutely makes sense: we’re constructing a location that specifies “/” (root) → “A” → “AA”.

In digging through snippets and forum posts, I’ve gathered that we can construct a URL referencing local page context using the $xwiki.getDocument endpoint, so I have some confidence I could derive the relative path to the current page without too much difficulty. How might one construct an input form that would allow search input as @christophe1983 has done, but then modify the search URL suffix to include (for instance) the location of the current page?

This looks nearly the idea we wanted as a macro. We wanted provide a search you can place inside (sub-)spaces just to search inside the (sub-)space. With a parameter you can search (sub-)spaces siblings too. Our macro code is:

{{velocity}}

{{html clean="false" wiki="true"}}
## start at the point where the page is
#set($docSpace = $xwiki.getDocument($xcontext.getDoc()))
## convert to string for later replacements
#set($docSpace = $docSpace.toString())
## we use a boolean macro parameter "sibling" to search in siblings too
#if($wikimacro.parameters.siblings == true)
  ##  climb up hierarchy
  #set($docSpace = $doc.parent)
#end
#set($docSpace = $docSpace.replaceAll('.WebHome', ''))
#set($docSpace = $escapetool.json($docSpace))
##
<div class="search-ui" id="spaceSearchDiv">
  <form class="search-form" id="" action="/wiki/ndr/view/Main/Search?sort=score&sortOrder=desc&highlight=true&facet=true&r=1&f_type=DOCUMENT" role="search">
    ## f_space_prefix is the parameter to define the space
    <input type="hidden" name="f_space_prefix" value="$escapetool.json($docSpace)">
    <input type="hidden" name="facet" value="true">
    <div class="input-group">
      <label class="hidden" for="spaceSearchInput">Search</label>
      <input type="search" name="text" placeholder='Search in space &quot;$escapetool.json($docSpace)&quot;' id="spaceSearchInput">
      <span class="input-group-btn">
        <button type="submit" class="btn btn-primary" title="Search">
          <span class="fa fa-search"></span>
          <span class="sr-only">Search</span>
        </button>
      </span>
    </div>
  </form>
</div>
{{/html}}

{{/velocity}}

As you can see we use the parameter “f_space_prefix” instead of “f_space_facet” because of those numbers after that key. We couldn’t handle the logic behind those numbers.

1 Like

Nice. Thanks for that.

I made some changes, only based on what I have gleaned from a short time working with Xwiki, such that this will search all pages below the current one.

If anyone knows about the solr integer necessary after the f_space_facet parameter, I’d love to see a TL;DR version of how that works.

{{velocity}}
{{html clean="false" wiki="true"}}
## start at the point where the page is
#set($docSpace = $doc.getSpace())
#set($docSpace = $docSpace.toString())

#*
 We calculate the depth of the search based on the number of "." characters
 in the Space string, minus one, as it's a zero-based index.
 This goes before the solr search facet string. 
 This breaks if there are "." characters in the page names.
*# 
#set($depthInt = 0)
#set($depth=$docSpace.split("\.").size() )

#set($depth=$depthInt.parseInt($depth) - 1 )

## we use a boolean macro parameter "sibling" to search in siblings too
#if($wikimacro.parameters.siblings == true)
  ##  climb up hierarchy
  #set($docSpace = $doc.parent)
#end
#set($docSpace = $docSpace.replaceAll('.WebHome', ''))
##
<div class="search-ui" id="spaceSearchDiv">
  <form class="search-form" id="" action="/xwiki/bin/view/Main/Search?sort=score&sortOrder=desc&highlight=true&facet=true&f_type=DOCUMENT" role="search">
    ## f_space_facet is the parameter to define the space
    <input type="hidden" name="f_space_facet" value="$depth/$docSpace.">
    <input type="hidden" name="facet" value="true">
    <div class="input-group">
      <label class="hidden" for="spaceSearchInput">Search</label>
      <input type="search" name="text" placeholder='Search in space &quot;($docSpace)&quot;' id="spaceSearchInput">
      <span class="input-group-btn">
        <button type="submit" class="btn btn-primary" title="Search">
          <span class="fa fa-search"></span>
          <span class="sr-only">Search</span>
        </button>
      </span>
    </div>
  </form>
</div>
{{/html}}
{{velocity}}

As mentioned in the comments, this will break if there are period characters in the page names, so that’s probably something someone with more experience on this platform could improve upon.

I also suspect there are ways to remove the hard-coded URL bits.

Nonetheless, I think this gets us where we would need to be. @christophe1983, does this look like something you could use?

@Simpel, thanks much for the example!

1 Like

Thanks lanedsmu and Simpel for your answers, I’ll try to integrate your bits of code. Good day to you

Hello,
Please find the modifications that solve the searching issue for the pages that contain “.” into their title.
The code searches for content within the current page, its children, and the FileManager space.

I use FileManager to store Office files that are embedded on the XWiki pages by URL and I would like to include them into the searching result.
How could I modify the query to include a filter that returns only the files that are present as URL into the current page and its children?

{{velocity}}
{{html clean="false" wiki="true"}}
## Start at the Current page point
#set($docSpace = $doc.getSpace())
#set($docSpace = $docSpace.toString())

##Calculate the depth of the searching eliminating the "." into the title of the pages.
#set($numbersDotsBackslash=$docSpace.split("\\.").size() )
#set($NumbersDots=$docSpace.split("\.").size() )
#set($depth=($NumbersDots - $numbersDotsBackslash))

## Use a boolean macro parameter "sibling" to search in siblings too
#if($wikimacro.parameters.siblings == true)
  ##  climb up hierarchy
  #set($docSpace = $doc.parent)
#end

## Remove the "".WeHome at the end of the &docSpace"
#set($docSpace = $docSpace.replaceAll('.WebHome', ''))
## Assign to $FileManager the path of the XWiki.Main.FileManager.
#set($FileManager = "FileManager")

<div class="search-ui" id="spaceSearchDiv">
  <form class="search-form" id="" action="/bin/view/Main/Search?sort=score&sortOrder=desc&highlight=true&facet=true&f_type=DOCUMENT" role="search">
    <input type="hidden" name="f_space_facet" value="$depth/$docSpace.">
    <input type="hidden" name="f_space_facet" value="0/$FileManager.">
    <input type="hidden" name="facet" value="true">
    <div class="input-group">
      <label class="hidden" for="spaceSearchInput">Search</label>
      <input type="search" name="text" placeholder='For local Searching: Type here.' id="spaceSearchInput">
      <span class="input-group-btn">
        <button type="submit" class="btn btn-primary" title="Search">
          <span class="fa fa-search"></span>
          <span class="sr-only">Search</span>
        </button>
      </span>
    </div>
  </form>
</div>
{{/html}}
{{velocity}}