protected void searchBaseModelWithUpdate() throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId());

    SearchContext searchContext = SearchContextTestUtil.getSearchContext(group.getGroupId());

    BaseModel<?> parentBaseModel = getParentBaseModel(group, serviceContext);

    searchContext.setKeywords(getSearchKeywords());

    int initialBaseModelsSearchCount =
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext);

    baseModel = addBaseModel(parentBaseModel, true, getSearchKeywords(), serviceContext);

    Assert.assertEquals(
        initialBaseModelsSearchCount + 1,
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext));

    String updatedKeywords = RandomTestUtil.randomString();

    baseModel = updateBaseModel(baseModel, updatedKeywords, serviceContext);

    Assert.assertEquals(
        initialBaseModelsSearchCount,
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext));

    searchContext.setKeywords(updatedKeywords);

    Assert.assertEquals(
        initialBaseModelsSearchCount + 1,
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext));
  }
  protected void searchVersions() throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId());

    SearchContext searchContext = SearchContextTestUtil.getSearchContext(group.getGroupId());

    searchContext.setKeywords(getSearchKeywords());

    int initialBaseModelsCount =
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext);

    BaseModel<?> parentBaseModel = getParentBaseModel(group, serviceContext);

    baseModel = addBaseModel(parentBaseModel, true, getSearchKeywords(), serviceContext);

    Assert.assertEquals(
        initialBaseModelsCount + 1,
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext));

    baseModel = updateBaseModel(baseModel, "liferay", serviceContext);

    Assert.assertEquals(
        initialBaseModelsCount,
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext));

    baseModel = updateBaseModel(baseModel, "portal", serviceContext);

    searchContext.setKeywords("portal");

    Assert.assertEquals(
        initialBaseModelsCount + 1,
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext));
  }
  protected int searchBaseModelsCount(
      Class<?> clazz, long groupId, String keywords, SearchContext searchContext) throws Exception {

    searchContext.setKeywords(keywords);

    return searchBaseModelsCount(clazz, groupId, searchContext);
  }
  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 void searchByKeywordsInsideParentBaseModel() throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId());

    SearchContext searchContext = SearchContextTestUtil.getSearchContext(group.getGroupId());

    baseModel = addBaseModel(null, true, getSearchKeywords(), serviceContext);

    BaseModel<?> parentBaseModel1 = getParentBaseModel(group, serviceContext);

    searchContext.setFolderIds(new long[] {(Long) parentBaseModel1.getPrimaryKeyObj()});
    searchContext.setKeywords(getSearchKeywords());

    int initialBaseModelsSearchCount =
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext);

    baseModel = addBaseModel(parentBaseModel1, true, getSearchKeywords(), serviceContext);

    Assert.assertEquals(
        initialBaseModelsSearchCount + 1,
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext));

    BaseModel<?> parentBaseModel2 = getParentBaseModel(parentBaseModel1, serviceContext);

    baseModel = addBaseModel(parentBaseModel2, true, getSearchKeywords(), serviceContext);

    Assert.assertEquals(
        initialBaseModelsSearchCount + 2,
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext));
  }
  @Test
  public void testLocalizedSearch() throws Exception {
    SearchContext searchContext = SearchContextTestUtil.getSearchContext(group.getGroupId());

    int initialBaseModelsSearchCount =
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext);

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId());

    BaseModel<?> parentBaseModel = getParentBaseModel(group, serviceContext);

    Map<Locale, String> keywordsMap = new HashMap<Locale, String>();

    keywordsMap.put(LocaleUtil.getDefault(), "entity title");
    keywordsMap.put(LocaleUtil.HUNGARY, "entitas neve");

    baseModel = addBaseModelWithWorkflow(parentBaseModel, true, keywordsMap, serviceContext);

    Assert.assertEquals(
        initialBaseModelsSearchCount + 1,
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext));

    searchContext.setAttribute(Field.TITLE, "nev");
    searchContext.setKeywords("nev");
    searchContext.setLocale(LocaleUtil.HUNGARY);

    Assert.assertEquals(
        initialBaseModelsSearchCount + 1,
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext));
  }
  protected void search(
      FileEntry fileEntry, boolean rootFolder, String keywords, boolean assertTrue)
      throws Exception {

    SearchContext searchContext = new SearchContext();

    searchContext.setAttribute("paginationType", "regular");
    searchContext.setCompanyId(fileEntry.getCompanyId());
    searchContext.setFolderIds(new long[] {fileEntry.getFolderId()});
    searchContext.setGroupIds(new long[] {fileEntry.getRepositoryId()});
    searchContext.setKeywords(keywords);

    QueryConfig queryConfig = new QueryConfig();

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

    searchContext.setQueryConfig(queryConfig);

    Indexer indexer = IndexerRegistryUtil.getIndexer(DLFileEntryConstants.getClassName());

    Hits hits = indexer.search(searchContext);

    List<Document> documents = hits.toList();

    boolean found = false;

    for (Document document : documents) {
      long fileEntryId = GetterUtil.getLong(document.get(Field.ENTRY_CLASS_PK));

      if (fileEntryId == fileEntry.getFileEntryId()) {
        found = true;

        break;
      }
    }

    String message = "Search engine could not find ";

    if (rootFolder) {
      message += "root file entry by " + keywords;
    } else {
      message += "file entry by " + keywords;
    }

    message += " using query " + hits.getQuery();

    if (assertTrue) {
      Assert.assertTrue(message, found);
    } else {
      Assert.assertFalse(message, found);
    }
  }
  @Test
  public void testWildcardQuery() throws Exception {
    SearchContext searchContext = getSearchContext();

    searchContext.setKeywords("test*.jpg");

    BooleanQuery searchQuery = RepositorySearchQueryBuilderUtil.getFullQuery(searchContext);

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

    assertQueryEquals("(cmis:name LIKE 'test%.jpg' OR cmis:createdBy LIKE 'test%.jpg')", cmisQuery);
  }
  @Test
  public void testProximityQuery() throws Exception {
    SearchContext searchContext = getSearchContext();

    searchContext.setKeywords("\"test document\"~10");

    BooleanQuery searchQuery = RepositorySearchQueryBuilderUtil.getFullQuery(searchContext);

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

    assertQueryEquals(
        "(cmis:name = 'test document' OR cmis:createdBy = 'test " + "document')", cmisQuery);
  }
  public Hits search(
      long companyId,
      long[] groupIds,
      long userId,
      String className,
      String keywords,
      int status,
      int start,
      int end)
      throws SystemException {

    try {
      SearchContext searchContext = new SearchContext();

      Facet assetEntriesFacet = new AssetEntriesFacet(searchContext);

      assetEntriesFacet.setStatic(true);

      searchContext.addFacet(assetEntriesFacet);

      Facet scopeFacet = new ScopeFacet(searchContext);

      scopeFacet.setStatic(true);

      searchContext.addFacet(scopeFacet);

      searchContext.setAttribute("paginationType", "regular");
      searchContext.setAttribute("status", status);
      searchContext.setCompanyId(companyId);
      searchContext.setEnd(end);
      searchContext.setEntryClassNames(getClassNames(className));
      searchContext.setGroupIds(groupIds);
      searchContext.setKeywords(keywords);

      QueryConfig queryConfig = new QueryConfig();

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

      searchContext.setQueryConfig(queryConfig);

      searchContext.setStart(start);
      searchContext.setUserId(userId);

      Indexer indexer = FacetedSearcher.getInstance();

      return indexer.search(searchContext);
    } catch (Exception e) {
      throw new SystemException(e);
    }
  }
  @Test
  public void testPhraseQuery() throws Exception {
    SearchContext searchContext = getSearchContext();

    searchContext.setKeywords("\"My test document.jpg\"");

    BooleanQuery searchQuery = RepositorySearchQueryBuilderUtil.getFullQuery(searchContext);

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

    assertQueryEquals(
        "(cmis:name = 'My test document.jpg' OR cmis:createdBy = 'My " + "test document.jpg')",
        cmisQuery);
  }
  @Test
  public void testRangeQuery() throws Exception {
    SearchContext searchContext = getSearchContext();

    searchContext.setKeywords("createDate:[20091011000000 TO 20091110235959]");

    BooleanQuery searchQuery = RepositorySearchQueryBuilderUtil.getFullQuery(searchContext);

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

    assertQueryEquals(
        "cmis:creationDate >= 2009-10-11T00:00:00.000Z AND "
            + "cmis:creationDate <= 2009-11-10T23:59:59.000Z",
        cmisQuery);
  }
  @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 testBooleanQuery() throws Exception {
    SearchContext searchContext = getSearchContext();

    searchContext.setKeywords("+test* -test.doc");

    BooleanQuery searchQuery = RepositorySearchQueryBuilderUtil.getFullQuery(searchContext);

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

    assertQueryEquals(
        "((cmis:name LIKE 'test%' AND NOT(cmis:name = 'test.doc')) OR "
            + "(cmis:createdBy LIKE 'test%' AND NOT(cmis:createdBy = "
            + "'test.doc')))",
        cmisQuery);
  }
  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);
  }
  protected void testUserPermissions(
      boolean addBaseModelPermission, boolean addParentBaseModelPermission) throws Exception {

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId());

    SearchContext searchContext = SearchContextTestUtil.getSearchContext(group.getGroupId());

    searchContext.setKeywords(getSearchKeywords());

    int initialBaseModelsSearchCount =
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext);

    serviceContext.setAddGroupPermissions(addParentBaseModelPermission);
    serviceContext.setAddGuestPermissions(addParentBaseModelPermission);

    BaseModel<?> parentBaseModel = getParentBaseModel(group, serviceContext);

    serviceContext.setAddGroupPermissions(addBaseModelPermission);
    serviceContext.setAddGuestPermissions(addBaseModelPermission);

    baseModel = addBaseModel(parentBaseModel, true, getSearchKeywords(), serviceContext);

    User user = UserTestUtil.addUser(null, 0);

    PermissionChecker originalPermissionChecker = PermissionThreadLocal.getPermissionChecker();

    try {
      PermissionChecker permissionChecker = PermissionCheckerFactoryUtil.create(user);

      PermissionThreadLocal.setPermissionChecker(permissionChecker);

      searchContext.setUserId(user.getUserId());

      int baseModelsCount = initialBaseModelsSearchCount;

      if (addBaseModelPermission && !isCheckBaseModelPermission()) {
        baseModelsCount++;
      }

      Assert.assertEquals(
          baseModelsCount,
          searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext));
    } finally {
      PermissionThreadLocal.setPermissionChecker(originalPermissionChecker);
    }
  }
  @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 void searchWithinDDMStructure() throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId());

    SearchContext searchContext = SearchContextTestUtil.getSearchContext(group.getGroupId());

    searchContext.setKeywords(getSearchKeywords());

    int initialBaseModelsSearchCount =
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext);

    BaseModel<?> parentBaseModel = getParentBaseModel(group, serviceContext);

    baseModel = addBaseModelWithDDMStructure(parentBaseModel, getSearchKeywords(), serviceContext);

    Assert.assertEquals(
        initialBaseModelsSearchCount + 1,
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), 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;
  }
  protected void search(boolean rootFolder, String keywords) throws Exception {

    SearchContext searchContext = new SearchContext();

    searchContext.setCompanyId(_fileEntry.getCompanyId());
    searchContext.setFolderIds(new long[] {_fileEntry.getFolderId()});
    searchContext.setGroupIds(new long[] {_fileEntry.getRepositoryId()});
    searchContext.setKeywords(keywords);

    QueryConfig queryConfig = new QueryConfig();

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

    searchContext.setQueryConfig(queryConfig);

    Indexer indexer = IndexerRegistryUtil.getIndexer(DLFileEntry.class);

    List<Document> documents = indexer.search(searchContext).toList();

    boolean found = false;

    for (Document document : documents) {
      long fileEntryId = GetterUtil.getLong(document.get(Field.ENTRY_CLASS_PK));

      if (fileEntryId == _fileEntry.getFileEntryId()) {
        found = true;

        break;
      }
    }

    String message = "Search engine could not find ";

    if (rootFolder) {
      message += "root file entry by " + keywords;
    } else {
      message += "file entry by " + keywords;
    }

    assertTrue(message, found);
  }
  @Override
  public void indexKeyword(
      String searchEngineId,
      long companyId,
      String querySuggestion,
      float weight,
      String keywordType,
      Locale locale)
      throws SearchException {

    SearchEngine searchEngine = _searchEngineHelper.getSearchEngine(searchEngineId);

    IndexWriter indexWriter = searchEngine.getIndexWriter();

    SearchContext searchContext = new SearchContext();

    searchContext.setCompanyId(companyId);
    searchContext.setSearchEngineId(searchEngineId);
    searchContext.setKeywords(querySuggestion);
    searchContext.setLocale(locale);

    indexWriter.indexKeyword(searchContext, weight, keywordType);
  }
  protected void searchExpireVersions(boolean expireAllVersions) throws Exception {

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId());

    SearchContext searchContext = SearchContextTestUtil.getSearchContext(group.getGroupId());

    searchContext.setKeywords(getSearchKeywords());

    int initialBaseModelsCount =
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext);

    BaseModel<?> parentBaseModel = getParentBaseModel(group, serviceContext);

    baseModel = addBaseModel(parentBaseModel, true, getSearchKeywords(), serviceContext);

    Assert.assertEquals(
        initialBaseModelsCount + 1,
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext));

    baseModel = updateBaseModel(baseModel, "liferay", serviceContext);

    Assert.assertEquals(
        initialBaseModelsCount,
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext));

    expireBaseModelVersions(baseModel, expireAllVersions, serviceContext);

    if (expireAllVersions && isExpirableAllVersions()) {
      Assert.assertEquals(
          initialBaseModelsCount,
          searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext));
    } else {
      Assert.assertEquals(
          initialBaseModelsCount + 1,
          searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext));
    }
  }
  protected void searchBaseModelWithTrash() throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId());

    SearchContext searchContext = SearchContextTestUtil.getSearchContext(group.getGroupId());

    BaseModel<?> parentBaseModel = getParentBaseModel(group, serviceContext);

    int initialBaseModelsSearchCount =
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext);

    baseModel = addBaseModel(parentBaseModel, true, getSearchKeywords(), serviceContext);

    Assert.assertEquals(
        initialBaseModelsSearchCount + 1,
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext));

    moveBaseModelToTrash((Long) baseModel.getPrimaryKeyObj());

    searchContext.setKeywords(getSearchKeywords());

    Assert.assertEquals(
        initialBaseModelsSearchCount,
        searchBaseModelsCount(getBaseModelClass(), group.getGroupId(), searchContext));
  }