Afficher les derniers auteurs
1 {{velocity output="false"}}
2 #**
3 * Search Suggest parameters are normally parsed from the query property of a Search Suggest source. Each parameter is
4 * specified on a separate line using 'key=value' and can be later passed to the search query using
5 * Query#bindValue(key, value) . Parameters can have multiple values, in which case each value is specified on a
6 * separate line with:
7 * key=value1
8 * key=value2
9 *#
10 #macro (parseSearchSuggestParameters $queryString $params)
11 #set ($return = {})
12 ## Each line sets a query parameter.
13 #foreach ($line in $queryString.split('\r?\n'))
14 #set ($separatorPosition = $line.indexOf('='))
15 #if ($separatorPosition < 0)
16 ## For backward compatibility, if the parameter name is omitted then we consider it to be the actual search query.
17 #set ($key = 'q')
18 #set ($value = $line)
19 #else
20 #set ($key = $line.substring(0, $separatorPosition))
21 #set ($value = $line.substring($mathtool.add($separatorPosition, 1)))
22 #end
23 ## Parameters can have multiple values.
24 #set ($values = $return.get($key))
25 #if (!$values)
26 ## This is the first value (and maybe the only one).
27 #set ($discard = $return.put($key, $value))
28 #else
29 #if (!$values.subList(0, 0))
30 ## This is the second value. We need a list.
31 #set ($values = [$values])
32 #set ($discard = $return.put($key, $values))
33 #end
34 #set ($discard = $values.add($value))
35 #end
36 #end
37 #set ($params = $NULL)
38 #setVariable("$params" $return)
39 #end
40
41 #macro (setDefaultSearchSuggestParameters $params)
42 ## By default the given text input is the query statement. We don't do prefix search by default because it is costly,
43 ## but you can easily overwrite this from the Search Suggest source by adding the line:
44 ## q=__INPUT__*
45 ## or simply:
46 ## __INPUT__*
47 ## to the query property. Of course, you can do prefix search even from the Search Suggest input by typing text*
48 ## (i.e. adding the wildcard character at the end) but this is more for advanced users.
49 #if (!$params.q)
50 #set ($params.q = '__INPUT__')
51 #end
52 ##
53 ## Default filters. Note that using separate filter queries improves the filter cache.
54 ## First, let's see which filters have been set from the Search Suggest source.
55 #set ($filterNames = $collectiontool.set)
56 #set ($filterQuery = $params.fq)
57 #if ($filterQuery)
58 #if (!$filterQuery.subList(0, 0))
59 ## A single filter was set.
60 #set ($filterQuery = [$filterQuery])
61 #end
62 #foreach ($item in $filterQuery)
63 #set ($discard = $filterNames.add($stringtool.substringBefore($item, ':')))
64 #end
65 #else
66 #set ($filterQuery = [])
67 #end
68 ## Add the default filters only if they aren't already set.
69 #if (!$filterNames.contains('locale'))
70 #set ($discard = $filterQuery.add("locale:(""$xcontext.locale"" OR """")"))
71 #end
72 #if (!$xcontext.isMainWiki() && !$filterNames.contains('wiki'))
73 ## Subwikis search by default in their content only.
74 #set ($discard = $filterQuery.add("wiki:$xcontext.database"))
75 #end
76 #if (!$filterNames.contains('hidden') && $xwiki.getUserPreference('displayHiddenDocuments') != 1)
77 #set ($discard = $filterQuery.add("hidden:false"))
78 #end
79 #set ($params.fq = $filterQuery)
80 #end
81
82 #macro (prepareSearchSuggestStatement $params $input)
83 #set ($queryStatement = $params.q)
84 ## Keep only the last value, if multiple values are specified (i.e. if the default value was overwritten from the
85 ## Search Suggest source).
86 #if ($queryStatement.subList(0, 0))
87 #set ($queryStatement = $queryStatement.get($mathtool.sub($queryStatement.size(), 1)))
88 #end
89 ## Inject the given text input.
90 #set ($queryStatement = $queryStatement.replace('__INPUT__', $input))
91 #set ($params.q = $queryStatement)
92 #end
93
94 #macro (createSearchSuggestQuery $queryString $input $query)
95 #parseSearchSuggestParameters($queryString $params)
96 #setDefaultSearchSuggestParameters($params)
97 #set ($queryStatement = $params.remove('q').replace('__INPUT__', $input))
98 #set ($return = $services.query.createQuery($queryStatement, 'solr'))
99 #foreach ($entry in $params.entrySet())
100 #set ($discard = $return.bindValue($entry.key, $entry.value))
101 #end
102 #set ($query = $NULL)
103 #setVariable("$query" $return)
104 #end
105
106 #macro (getSearchSuggestResults $results)
107 #createSearchSuggestQuery($request.query $request.input $query)
108 #set ($limit = $numbertool.toNumber($request.nb).intValue())
109 #if (!$limit)
110 #set ($limit = 3)
111 #end
112 #set ($discard = $query.setLimit($limit))
113 #set ($results = $NULL)
114 #setVariable("$results" $query.execute()[0].results)
115 #end
116 {{/velocity}}
#getBlogDocument($space $blogDoc)
XWiki Enterprise 14.6 - Documentation