protected SearchContext buildSearchContext(
      long companyId,
      long[] groupIds,
      String title,
      long[] parentCategoryIds,
      long[] vocabularyIds,
      int start,
      int end,
      Sort sort) {

    SearchContext searchContext = new SearchContext();

    Map<String, Serializable> attributes = new HashMap<>();

    attributes.put(Field.ASSET_PARENT_CATEGORY_IDS, parentCategoryIds);
    attributes.put(Field.ASSET_VOCABULARY_IDS, vocabularyIds);
    attributes.put(Field.TITLE, title);

    searchContext.setAttributes(attributes);

    searchContext.setCompanyId(companyId);
    searchContext.setEnd(end);
    searchContext.setGroupIds(groupIds);
    searchContext.setKeywords(title);
    searchContext.setSorts(sort);
    searchContext.setStart(start);

    QueryConfig queryConfig = searchContext.getQueryConfig();

    queryConfig.setHighlightEnabled(false);
    queryConfig.setScoreEnabled(false);

    return searchContext;
  }
  protected String buildFolderQuery(boolean searchSubfolders) throws Exception {

    SearchContext searchContext = getSearchContext();

    searchContext.setFolderIds(new long[] {_repository.getDlFolderId()});
    searchContext.setKeywords("test");

    BooleanQuery searchQuery = RepositorySearchQueryBuilderUtil.getFullQuery(searchContext);

    QueryConfig queryConfig = searchContext.getQueryConfig();

    queryConfig.setAttribute("capabilityQuery", CapabilityQuery.BOTHCOMBINED.value());
    queryConfig.setSearchSubfolders(searchSubfolders);

    return _cmisSearchQueryBuilder.buildQuery(searchContext, searchQuery);
  }
  @Test
  public void testContainsOnlySupportedWithApostrophe() throws Exception {
    SearchContext searchContext = getSearchContext();

    searchContext.setKeywords("test's");

    BooleanQuery searchQuery = RepositorySearchQueryBuilderUtil.getFullQuery(searchContext);

    QueryConfig queryConfig = searchContext.getQueryConfig();

    queryConfig.setAttribute("capabilityQuery", CapabilityQuery.FULLTEXTONLY.value());

    String cmisQuery = _cmisSearchQueryBuilder.buildQuery(searchContext, searchQuery);

    assertQueryEquals("CONTAINS('test\\'s')", cmisQuery);
  }
  @Test
  public void testContainsCombinedSupportedQuery() throws Exception {
    SearchContext searchContext = getSearchContext();

    searchContext.setKeywords("test");

    BooleanQuery searchQuery = RepositorySearchQueryBuilderUtil.getFullQuery(searchContext);

    QueryConfig queryConfig = searchContext.getQueryConfig();

    queryConfig.setAttribute("capabilityQuery", CapabilityQuery.BOTHCOMBINED.value());

    String cmisQuery = _cmisSearchQueryBuilder.buildQuery(searchContext, searchQuery);

    assertQueryEquals(
        "((cmis:name = 'test' OR cmis:createdBy = 'test') OR " + "CONTAINS('test'))", cmisQuery);
  }
  protected SearchContext buildSearchContext(
      long companyId,
      String name,
      String description,
      LinkedHashMap<String, Object> params,
      boolean andSearch,
      int start,
      int end,
      Sort sort) {

    SearchContext searchContext = new SearchContext();

    searchContext.setAndSearch(andSearch);

    Map<String, Serializable> attributes = new HashMap<>();

    attributes.put("description", description);
    attributes.put("name", name);

    searchContext.setAttributes(attributes);

    searchContext.setCompanyId(companyId);
    searchContext.setEnd(end);

    if (params != null) {
      String keywords = (String) params.remove("keywords");

      if (Validator.isNotNull(keywords)) {
        searchContext.setKeywords(keywords);
      }
    }

    if (sort != null) {
      searchContext.setSorts(sort);
    }

    searchContext.setStart(start);

    QueryConfig queryConfig = searchContext.getQueryConfig();

    queryConfig.setHighlightEnabled(false);
    queryConfig.setScoreEnabled(false);

    return searchContext;
  }
  protected SearchContext buildSearchContext(
      long companyId, long groupId, String title, int start, int end) {

    SearchContext searchContext = new SearchContext();

    searchContext.setAttribute(Field.TITLE, title);
    searchContext.setCompanyId(companyId);
    searchContext.setEnd(end);
    searchContext.setGroupIds(new long[] {groupId});
    searchContext.setKeywords(title);
    searchContext.setStart(start);

    QueryConfig queryConfig = searchContext.getQueryConfig();

    queryConfig.setHighlightEnabled(false);
    queryConfig.setScoreEnabled(false);

    return searchContext;
  }
  @Override
  public BooleanQuery getFullQuery(SearchContext searchContext) throws SearchException {

    try {
      BooleanQuery contextQuery = new BooleanQueryImpl();

      addContext(contextQuery, searchContext);

      BooleanQuery searchQuery = new BooleanQueryImpl();

      addSearchKeywords(searchQuery, searchContext);

      BooleanQuery fullQuery = new BooleanQueryImpl();

      if (contextQuery.hasClauses()) {
        fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
      }

      if (searchQuery.hasClauses()) {
        fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
      }

      BooleanClause<Query>[] booleanClauses = searchContext.getBooleanClauses();

      if (booleanClauses != null) {
        for (BooleanClause<Query> booleanClause : booleanClauses) {
          fullQuery.add(booleanClause.getClause(), booleanClause.getBooleanClauseOccur());
        }
      }

      fullQuery.setQueryConfig(searchContext.getQueryConfig());

      return fullQuery;
    } catch (Exception e) {
      throw new SearchException(e);
    }
  }
  @Override
  public Hits search(SearchContext searchContext, Query query) throws SearchException {

    long startTime = System.currentTimeMillis();

    List<ExtRepositorySearchResult<?>> extRepositorySearchResults = null;

    try {
      extRepositorySearchResults =
          _extRepository.search(searchContext, query, new ExtRepositoryQueryMapperImpl(this));
    } catch (PortalException | SystemException e) {
      throw new SearchException("Unable to perform search", e);
    }

    QueryConfig queryConfig = searchContext.getQueryConfig();

    List<Document> documents = new ArrayList<>();
    List<String> snippets = new ArrayList<>();
    List<Float> scores = new ArrayList<>();

    int total = 0;

    for (ExtRepositorySearchResult<?> extRepositorySearchResult : extRepositorySearchResults) {

      try {
        ExtRepositoryObjectAdapter<?> extRepositoryEntryAdapter =
            _toExtRepositoryObjectAdapter(
                ExtRepositoryObjectAdapterType.OBJECT, extRepositorySearchResult.getObject());

        Document document = new DocumentImpl();

        document.addKeyword(Field.ENTRY_CLASS_NAME, extRepositoryEntryAdapter.getModelClassName());
        document.addKeyword(Field.ENTRY_CLASS_PK, extRepositoryEntryAdapter.getPrimaryKey());
        document.addKeyword(Field.TITLE, extRepositoryEntryAdapter.getName());

        documents.add(document);

        if (queryConfig.isScoreEnabled()) {
          scores.add(extRepositorySearchResult.getScore());
        } else {
          scores.add(1.0F);
        }

        snippets.add(extRepositorySearchResult.getSnippet());

        total++;
      } catch (PortalException | SystemException e) {
        if (_log.isWarnEnabled()) {
          _log.warn("Invalid entry returned from search", e);
        }
      }
    }

    float searchTime = (float) (System.currentTimeMillis() - startTime) / Time.SECOND;

    Hits hits = new HitsImpl();

    hits.setDocs(documents.toArray(new Document[documents.size()]));
    hits.setLength(total);
    hits.setQueryTerms(new String[0]);
    hits.setScores(ArrayUtil.toFloatArray(scores));
    hits.setSearchTime(searchTime);
    hits.setSnippets(snippets.toArray(new String[snippets.size()]));
    hits.setStart(startTime);

    return hits;
  }