1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.search.solr.internal.rest; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.Calendar; |
24 |
|
import java.util.Date; |
25 |
|
import java.util.List; |
26 |
|
import java.util.Locale; |
27 |
|
|
28 |
|
import javax.inject.Inject; |
29 |
|
import javax.inject.Named; |
30 |
|
import javax.inject.Singleton; |
31 |
|
import javax.ws.rs.core.UriInfo; |
32 |
|
|
33 |
|
import org.apache.commons.lang3.StringUtils; |
34 |
|
import org.apache.solr.client.solrj.response.QueryResponse; |
35 |
|
import org.apache.solr.common.SolrDocument; |
36 |
|
import org.apache.solr.common.SolrDocumentList; |
37 |
|
import org.xwiki.component.annotation.Component; |
38 |
|
import org.xwiki.localization.LocaleUtils; |
39 |
|
import org.xwiki.model.reference.DocumentReference; |
40 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
41 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
42 |
|
import org.xwiki.query.Query; |
43 |
|
import org.xwiki.query.QueryManager; |
44 |
|
import org.xwiki.query.SecureQuery; |
45 |
|
import org.xwiki.query.solr.internal.SolrQueryExecutor; |
46 |
|
import org.xwiki.rest.Relations; |
47 |
|
import org.xwiki.rest.internal.Utils; |
48 |
|
import org.xwiki.rest.internal.resources.search.AbstractSearchSource; |
49 |
|
import org.xwiki.rest.model.jaxb.Link; |
50 |
|
import org.xwiki.rest.model.jaxb.SearchResult; |
51 |
|
import org.xwiki.rest.resources.pages.PageResource; |
52 |
|
import org.xwiki.rest.resources.pages.PageTranslationResource; |
53 |
|
import org.xwiki.search.solr.internal.api.FieldUtils; |
54 |
|
|
55 |
|
import com.xpn.xwiki.XWikiException; |
56 |
|
|
57 |
|
|
58 |
|
@version |
59 |
|
@since |
60 |
|
|
61 |
|
@Component |
62 |
|
@Named("solr") |
63 |
|
@Singleton |
|
|
| 59.8% |
Uncovered Elements: 37 (92) |
Complexity: 15 |
Complexity Density: 0.22 |
|
64 |
|
public class SOLRSearchSource extends AbstractSearchSource |
65 |
|
{ |
66 |
|
@Inject |
67 |
|
protected QueryManager queryManager; |
68 |
|
|
69 |
|
@Inject |
70 |
|
private DocumentReferenceResolver<SolrDocument> solrDocumentReferenceResolver; |
71 |
|
|
72 |
|
@Inject |
73 |
|
@Named("local") |
74 |
|
private EntityReferenceSerializer<String> localEntityReferenceSerializer; |
75 |
|
|
|
|
| 59.3% |
Uncovered Elements: 37 (91) |
Complexity: 15 |
Complexity Density: 0.22 |
|
76 |
2 |
@Override... |
77 |
|
public List<SearchResult> search(String queryString, String defaultWikiName, String wikis, |
78 |
|
boolean hasProgrammingRights, String orderField, String order, boolean distinct, int number, int start, |
79 |
|
Boolean withPrettyNames, String className, UriInfo uriInfo) throws Exception |
80 |
|
{ |
81 |
2 |
List<SearchResult> result = new ArrayList<SearchResult>(); |
82 |
|
|
83 |
2 |
if (queryString == null) { |
84 |
0 |
return result; |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
2 |
if (defaultWikiName == null && wikis == null) { |
93 |
0 |
return result; |
94 |
|
} |
95 |
|
|
96 |
2 |
Query query = this.queryManager.createQuery(queryString, SolrQueryExecutor.SOLR); |
97 |
|
|
98 |
2 |
if (query instanceof SecureQuery) { |
99 |
|
|
100 |
2 |
((SecureQuery) query).checkCurrentUser(true); |
101 |
|
} |
102 |
|
|
103 |
2 |
List<String> fq = new ArrayList<String>(); |
104 |
|
|
105 |
|
|
106 |
2 |
fq.add("{!tag=type}type:(\"DOCUMENT\")"); |
107 |
|
|
108 |
|
|
109 |
2 |
if (!hasProgrammingRights) { |
110 |
2 |
fq.add("{!tag=hidden}hidden:(false)"); |
111 |
|
} |
112 |
|
|
113 |
|
|
114 |
2 |
if (StringUtils.isNotBlank(wikis)) { |
115 |
0 |
String[] strings = StringUtils.split(wikis, ','); |
116 |
0 |
if (strings.length == 1) { |
117 |
0 |
fq.add("{!tag=wiki}wiki:(\"" + strings[0] + "\")"); |
118 |
0 |
} else if (strings.length > 1) { |
119 |
0 |
StringBuilder builder = new StringBuilder(); |
120 |
0 |
for (String str : strings) { |
121 |
0 |
if (builder.length() > 0) { |
122 |
0 |
builder.append(" OR "); |
123 |
|
} |
124 |
0 |
builder.append('\''); |
125 |
0 |
builder.append(str); |
126 |
0 |
builder.append('\''); |
127 |
|
} |
128 |
0 |
fq.add("{!tag=wiki}wiki:(" + builder + ")"); |
129 |
|
} |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
|
|
134 |
2 |
query.bindValue("fq", fq); |
135 |
|
|
136 |
|
|
137 |
|
|
138 |
2 |
query.bindValue("qf", |
139 |
|
"title^10.0 name^10.0 doccontent^2.0 objcontent^0.4 filename^0.4 attcontent^0.4 doccontentraw^0.4 " |
140 |
|
+ "author_display^0.08 creator_display^0.08 " + "comment^0.016 attauthor_display^0.016 space^0.016"); |
141 |
|
|
142 |
|
|
143 |
2 |
if (!StringUtils.isBlank(orderField)) { |
144 |
0 |
if ("desc".equals(order)) { |
145 |
0 |
query.bindValue("sort", orderField + " desc"); |
146 |
|
} else { |
147 |
0 |
query.bindValue("sort", orderField + " asc"); |
148 |
|
} |
149 |
|
} |
150 |
|
|
151 |
|
|
152 |
2 |
query.setLimit(number).setOffset(start); |
153 |
|
|
154 |
2 |
try { |
155 |
2 |
QueryResponse response = (QueryResponse) query.execute().get(0); |
156 |
|
|
157 |
2 |
SolrDocumentList documents = response.getResults(); |
158 |
|
|
159 |
2 |
for (SolrDocument document : documents) { |
160 |
2 |
SearchResult searchResult = this.objectFactory.createSearchResult(); |
161 |
|
|
162 |
2 |
DocumentReference documentReference = this.solrDocumentReferenceResolver.resolve(document); |
163 |
2 |
searchResult.setPageFullName(this.localEntityReferenceSerializer.serialize(documentReference)); |
164 |
2 |
searchResult.setTitle((String) document.get(FieldUtils.TITLE)); |
165 |
2 |
searchResult.setWiki(documentReference.getWikiReference().getName()); |
166 |
2 |
searchResult.setSpace(this.localEntityReferenceSerializer.serialize(documentReference.getParent())); |
167 |
2 |
searchResult.setPageName(documentReference.getName()); |
168 |
2 |
searchResult.setVersion((String) document.get(FieldUtils.VERSION)); |
169 |
|
|
170 |
2 |
searchResult.setType("page"); |
171 |
2 |
searchResult.setId(Utils.getPageId(searchResult.getWiki(), |
172 |
|
Utils.getSpacesFromSpaceId(searchResult.getSpace()), searchResult.getPageName())); |
173 |
|
|
174 |
2 |
searchResult.setScore(((Number) document.get(FieldUtils.SCORE)).floatValue()); |
175 |
2 |
searchResult.setAuthor((String) document.get(FieldUtils.AUTHOR)); |
176 |
2 |
Calendar calendar = Calendar.getInstance(); |
177 |
2 |
calendar.setTime((Date) document.get(FieldUtils.DATE)); |
178 |
2 |
searchResult.setModified(calendar); |
179 |
|
|
180 |
2 |
if (withPrettyNames) { |
181 |
0 |
searchResult.setAuthorName((String) document.get(FieldUtils.AUTHOR_DISPLAY)); |
182 |
|
} |
183 |
|
|
184 |
2 |
Locale locale = LocaleUtils.toLocale((String) document.get(FieldUtils.DOCUMENT_LOCALE)); |
185 |
|
|
186 |
2 |
List<String> spaces = Utils.getSpacesHierarchy(documentReference.getLastSpaceReference()); |
187 |
|
|
188 |
2 |
String pageUri = null; |
189 |
2 |
if (Locale.ROOT == locale) { |
190 |
2 |
pageUri = |
191 |
|
Utils.createURI(uriInfo.getBaseUri(), PageResource.class, searchResult.getWiki(), |
192 |
|
spaces, searchResult.getPageName()).toString(); |
193 |
|
} else { |
194 |
0 |
searchResult.setLanguage(locale.toString()); |
195 |
0 |
pageUri = |
196 |
|
Utils.createURI(uriInfo.getBaseUri(), PageTranslationResource.class, spaces, |
197 |
|
searchResult.getPageName(), locale).toString(); |
198 |
|
} |
199 |
|
|
200 |
2 |
Link pageLink = new Link(); |
201 |
2 |
pageLink.setHref(pageUri); |
202 |
2 |
pageLink.setRel(Relations.PAGE); |
203 |
2 |
searchResult.getLinks().add(pageLink); |
204 |
|
|
205 |
2 |
result.add(searchResult); |
206 |
|
} |
207 |
|
} catch (Exception e) { |
208 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI, XWikiException.ERROR_XWIKI_UNKNOWN, |
209 |
|
"Error performing solr search", e); |
210 |
|
} |
211 |
|
|
212 |
2 |
return result; |
213 |
|
} |
214 |
|
} |