View Javadoc
1   /*
2    * Copyright (C) 2003-2013 eXo Platform SAS.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program. If not, see <http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.services.wcm.search.connector;
18  
19  import org.apache.commons.lang.StringUtils;
20  import org.exoplatform.commons.api.search.data.SearchContext;
21  import org.exoplatform.commons.api.search.data.SearchResult;
22  import org.exoplatform.commons.search.es.ElasticSearchFilter;
23  import org.exoplatform.commons.search.es.ElasticSearchFilterType;
24  import org.exoplatform.commons.search.es.client.ElasticSearchingClient;
25  import org.exoplatform.container.xml.InitParams;
26  import org.exoplatform.services.cms.documents.DocumentService;
27  import org.exoplatform.services.jcr.RepositoryService;
28  import org.exoplatform.services.log.ExoLogger;
29  import org.exoplatform.services.log.Log;
30  import org.exoplatform.web.controller.metadata.ControllerDescriptor;
31  import org.exoplatform.web.controller.router.Router;
32  
33  import java.util.ArrayList;
34  import java.util.Collection;
35  import java.util.Collections;
36  import java.util.List;
37  import java.util.Locale;
38  
39  /**
40   * Search connector for files for the Sites Explorer application
41   */
42  public class FileApplicationSearchServiceConnector extends FileSearchServiceConnector {
43  
44    private static final Log LOG = ExoLogger.getLogger(FileApplicationSearchServiceConnector.class.getName());
45  
46    private String filteredWorkspace;
47  
48    private String filteredPath;
49  
50    public FileApplicationSearchServiceConnector(InitParams initParams, ElasticSearchingClient client, RepositoryService repositoryService, DocumentService documentService) {
51      super(initParams, client, repositoryService, documentService);
52    }
53  
54    public Collection<SearchResult> appSearch(String workspace, String path, Locale locale, String query, int offset, int limit, String sort, String order) {
55      filteredWorkspace = workspace;
56      filteredPath = path;
57      if(StringUtils.isNotEmpty(filteredPath) && !filteredPath.endsWith("/")) {
58        filteredPath += "/";
59      }
60  
61      try {
62        SearchContext context = new SearchContext(new Router(new ControllerDescriptor()), "");
63        locale = locale != null ? locale : Locale.ENGLISH;
64        context.lang(locale.toString());
65        return super.search(context, query, null, offset, limit, sort, order);
66      } catch (Exception ex) {
67        LOG.error("Can not create SearchContext", ex);
68        return Collections.emptyList();
69      }
70    }
71  
72    @Override
73    protected String getAdditionalFilters(List<ElasticSearchFilter> filters) {
74      if(StringUtils.isNotEmpty(filteredWorkspace)) {
75        filters = addFilter(filters, new ElasticSearchFilter(ElasticSearchFilterType.FILTER_BY_TERM, "workspace", filteredWorkspace));
76      }
77  
78      if(StringUtils.isNotEmpty(filteredPath)) {
79        filters = addFilter(filters, new ElasticSearchFilter(ElasticSearchFilterType.FILTER_CUSTOM, "", "{ " +
80                  "\"prefix\" : { " +
81                    "\"path\" :  { " +
82                      "\"value\" : \"" + filteredPath + "\" " +
83                    "} " +
84                  "} " +
85                "}"));
86      }
87      return super.getAdditionalFilters(filters);
88    }
89  
90    private List<ElasticSearchFilter> addFilter(List<ElasticSearchFilter> filters, ElasticSearchFilter elasticSearchFilter) {
91      if(filters == null) {
92        filters = new ArrayList<>();
93      }
94      filters.add(elasticSearchFilter);
95      return filters;
96    }
97  }