1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.search.solr.internal.rest

File SOLRSearchSource.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

24
67
1
1
214
143
15
0.22
67
1
15

Classes

Class Line # Actions
SOLRSearchSource 64 67 0% 15 37
0.5978260659.8%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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 $Id: 42bd975bb53fe759e1e2f3675e454e0a0dc6bd58 $
59    * @since 6.4M1
60    */
61    @Component
62    @Named("solr")
63    @Singleton
 
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   
 
76  2 toggle @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    * One of the two must be non-null. If default wiki name is non-null and wikis is null, then it's a local search
89    * in a specific wiki. If wiki name is null and wikis is non-null it's a global query on different wikis. If
90    * both of them are non-null then the wikis parameter takes the precedence.
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    // Show only what the current user has the right to see
100  2 ((SecureQuery) query).checkCurrentUser(true);
101    }
102   
103  2 List<String> fq = new ArrayList<String>();
104   
105    // We want only documents
106  2 fq.add("{!tag=type}type:(\"DOCUMENT\")");
107   
108    // Additional filter for non PR users
109  2 if (!hasProgrammingRights) {
110  2 fq.add("{!tag=hidden}hidden:(false)");
111    }
112   
113    // Wikis
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    // TODO: current locale filtering ?
133   
134  2 query.bindValue("fq", fq);
135   
136    // Boost
137    // FIXME: take it from configuration
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    // Order
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    // Limit
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    }