Search result for specific space

Hello,

I want to add a search function to a page, which shows only results from child pages.
The code is:

{{include reference="XWiki.SearchCode"/}}

{{velocity}}
#if($searchEngine == 'solr')
  #set ($solrConfig = {
    'filterQuery': [
      'space_prefix:$doc.space'
    ]
  })
#end

#if ("$searchPage" != '')
  {{include reference="$searchPage"/}}
#else

  $services.localization.render('search.page.noimplementation')
#end
{{/velocity}}

The problem is, that the $doc.space doesn’t work and I have to add the space name directly (e.g. space_prefix:Test) and if there are spaces in the name I have to add a \ before the space (e.g. space_prefix:Test\ A).

Is there a possibility to get the script working with the $doc.space variable? Because I don’t want to edit the code when I copy it into another page or if I rename/move the page for some reason.

BR, Andreas Hartl

1 Like

Hi.

I myself wanted to have a space search and found your code to start with. I think I solved two of your problems. First my code:

{{include reference="XWiki.SearchCode"/}}

{{velocity}}
#set($docSpace = $doc.space)

## to search in siblings too uncomment next two lines and comment line above
##set($docSpace = $doc.parent)
##set($docSpace = $docSpace.replaceAll('.WebHome', ''))

## not sure whether this is the perfect escaping but it works w/ white space + dots in spaces
#set($docSpace = $escapetool.css($docSpace))

#if($searchEngine == 'solr')
  #set ($solrConfig = {
  'queryFields': {
    'DOCUMENT': 'spaces^15.0
                 title^10.0 name^10.0
                 doccontent^2.0
                 objcontent^0.4 filename^0.4 attcontent^0.4 doccontentraw^0.4
                 author_display^0.08 creator_display^0.08
                 comment^0.016 attauthor_display^0.016',
    'ATTACHMENT': 'filename^5.0 attcontent attauthor_display^0.2'
  },
    'filterQuery': [
      "space_prefix:$docSpace",
      "wiki:$doc.wiki"
    ]
  })
#end

#if ("$searchPage" != '')
  {{include reference="$searchPage"/}}
#end
{{/velocity}}

Your $doc.space did not work because it was inside single quotes. That means a literal string. Changing single to double quotes makes variables work inside the config.

With the $escapetool white spaces (and dots) in space names will escaped the way that the query worked. I’m not sure if the method “css” is the perfect one but it worked during my tests.

I added the filterQuery “wiki” because I got results from my sub wiki and the uninteresting main wiki too before. I had one space name in both wikis.

I added the queryFields because otherwise I got only results matching the search string with the location.


There are two things I’m about to wonder. The tabs “history”, “comments”, “information” and “attachements” are gone. Even with

{{velocity}}
#set($displayDocExtra = true)
#set ($showcomments = true)
#set ($showannotations = true)
#set ($showhistory = true)
#set ($showinformation = true)
{{/velocity}}

they won’t be shown. I don’t know how to display them.

Second problem isn’t easy to describe. If I have additional content to the search code above or below it then this content will be added a second, third … time every time I set or unset a facet on the right side.

Any ideas?
Regards, Simpel

Hi,

many thanks for your reply. It works fine :slight_smile:
To show search results of all languages I modified the /view/XWiki/SuggestSolrMacros page.

change the sourcecode from

  #if (!$filterNames.contains('locale'))
    #set ($discard = $filterQuery.add("locale:(""$xcontext.locale"" OR """")"))
  #end

to

  #if (!$filterNames.contains('locales'))
    #set ($discard = $filterQuery.add("locales:(""$xcontext.locale"" OR """")"))
  #end

For the other two problems I have unfortunately also no solution.

BR, Andreas