Last modified by Anca Luca on 2021/12/10

Show last authors
1 This tutorial shows how to write a customized SOLR search screen in XWiki for an AWM application. For this example we have used the [["Expense Report" application>>extensions:Extension.Expense Report Application]].
2
3 The objective was to build a search screen that would only return expense reports and would display facets using some of the expense report fields. In the example we will be adding facets for the status, organisation, currency, client and user fields of the expense report class.
4
5 Also we will build a "List Facet" allowing to display the facet for StaticList as well as DBList fields. This facet code can be reused for any field of this type.
6
7 == Configuration Code for the SOLR Search ==
8
9 We customize the "filterQuery", "facetFields" and "facetDisplayers" fields of the solrConfig object. The other configurations options will be the default ones.
10
11 * filterQuery: we limit to items of type DOCUMENT, including a class ERCode.ERClass.
12 * facetFields: we add some additional facet fields for the properties of the class ERCode.ERCodeClass. We also remove some fields we don't want from the default configuration.
13 * facetDisplays: we declare which facet code to use for each of the fields.
14
15 Create the ##ExpenseReport.Search## page with the following content:
16
17 {{code language="none"}}
18 {{velocity output="false"}}
19 #set ($solrConfig = {
20 'filterQuery': [
21 'type:DOCUMENT',
22 'class:ERCode.ERCodeClass'
23 ],
24 'facetFields': [
25 'property.ERCode.ERCodeClass.status_string',
26 'property.ERCode.ERCodeClass.organisation_string',
27 'property.ERCode.ERCodeClass.currency_string',
28 'property.ERCode.ERCodeClass.user_string',
29 'author',
30 'creator',
31 'date',
32 'creationdate'
33 ],
34 'facetDisplayers': {
35 'type': 'Main.SolrTypeFacet',
36 'wiki': 'Main.SolrWikiFacet',
37 'locale': 'Main.SolrLocaleFacet',
38 'author': 'Main.SolrUserFacet',
39 'creator': 'Main.SolrUserFacet',
40 'attauthor': 'Main.SolrUserFacet',
41 'date': 'Main.SolrDateFacet',
42 'creationdate': 'Main.SolrDateFacet',
43 'attdate': 'Main.SolrDateFacet',
44 'class': 'Main.SolrClassFacet',
45 'attsize': 'Main.SolrFileSizeFacet',
46 'mimetype': 'Main.SolrMediaTypeFacet',
47 'property.ERCode.ERCodeClass.status_string' : 'ExpenseReport.ListFacet',
48 'property.ERCode.ERCodeClass.organisation_string' : 'ExpenseReport.ListFacet',
49 'property.ERCode.ERCodeClass.currency_string' : 'ExpenseReport.ListFacet',
50 'property.ERCode.ERCodeClass.client_string' : 'ExpenseReport.ListFacet',
51 'property.ERCode.ERCodeClass.user_string' : 'Main.SolrUserFacet'
52 }
53 })
54 {{/velocity}}
55 {{include reference="Main.SolrSearch" /}}
56 {{/code}}
57
58 == Facet Code for the List field ==
59
60 We need to provide a code for the facet for list fields, as it is not provided by default in XWiki. Put the following code in the content of ##ExpenseReport.ListFacet##:
61
62 {{code}}
63 {{velocity}}
64 #macro (displaySearchFacetValue_list $value)
65 #set($class = $xwiki.getDocument($pclass).getxWikiClass())
66 #set($prop = $class.get($propName))
67 ## here we convert the raw value in a nicely displayed value
68 $prop.getMapValues().get($value).value
69 #end
70 #if ($facetValues)
71 ## here we extract the class name and field name so that we can avoid hardcoding it
72 #set($index1 = $facetField.name.lastIndexOf("."))
73 #set($index2 = $facetField.name.lastIndexOf("_"))
74 #set($pclass = $facetField.name.substring(0, $index1).substring(9))
75 #set($index1 = $index1 + 1)
76 #set($propName = $facetField.name.substring($index1, $index2))
77 {{html}}
78 <ul class="${propName}">
79 #displaySearchFacetValuesLimited($facetValues {} 'displaySearchFacetValue_list')
80 </ul>
81 {{/html}}
82 #end
83 {{/velocity}}
84 {{/code}}
85
86 == Result ==
87
88 Here is the result of our customized SOLR search (from the ##ExpenseReport.Search## page):
89
90 image:customsolrsearch.png
91
92
93 If you want to customize the title of the facet that is displayed in the facet bar, you need to add translations with keys of the following format in a translations file somewhere:
94
95 {{code}}
96 solr.field.<full facet field>
97 {{/code}}
98
99 where full facet field is the facet field id, as it was added in the 'facetDisplayers' map above.
100
101 For our example, we'd add new facet titles like this:
102
103 {{code}}
104 solr.field.property.ERCode.ERCodeClass.status_string=Expense report status
105 solr.field.property.ERCode.ERCodeClass.organisation_string=Expense report organisation
106 solr.field.property.ERCode.ERCodeClass.currency_string=Expense report currency
107 solr.field.property.ERCode.ERCodeClass.client_string=Expense report client
108 solr.field.property.ERCode.ERCodeClass.user_string=Beneficiary
109 {{/code}}
110
111 If these translations are not present, the engine will pick-up automatically as facet titles the display names of the properties of the facets.

Get Connected