@Override
  public List<AssetVocabulary> getVocabularies(Hits hits) throws PortalException {

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

    List<AssetVocabulary> vocabularies = new ArrayList<>(documents.size());

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

      AssetVocabulary vocabulary = fetchAssetVocabulary(vocabularyId);

      if (vocabulary == null) {
        vocabularies = null;

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

        long companyId = GetterUtil.getLong(document.get(Field.COMPANY_ID));

        indexer.delete(companyId, document.getUID());
      } else if (vocabularies != null) {
        vocabularies.add(vocabulary);
      }
    }

    return vocabularies;
  }
  @Test
  public void testSearchAndVerifyDocs() throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group.getGroupId());

    BookmarksFolder folder =
        BookmarksTestUtil.addFolder(_group.getGroupId(), RandomTestUtil.randomString());

    BookmarksEntry entry = BookmarksTestUtil.addEntry(folder.getFolderId(), true, serviceContext);

    SearchContext searchContext =
        BookmarksTestUtil.getSearchContext(
            entry.getCompanyId(), entry.getGroupId(), entry.getFolderId(), "test");

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

    Hits hits = indexer.search(searchContext);

    Assert.assertEquals(1, hits.getLength());

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

    for (Document doc : results) {
      Assert.assertEquals(entry.getCompanyId(), GetterUtil.getLong(doc.get(Field.COMPANY_ID)));
      Assert.assertEquals(BookmarksEntry.class.getName(), doc.get(Field.ENTRY_CLASS_NAME));
      Assert.assertEquals(entry.getEntryId(), GetterUtil.getLong(doc.get(Field.ENTRY_CLASS_PK)));
      AssertUtils.assertEqualsIgnoreCase(entry.getName(), doc.get(Field.TITLE));
      Assert.assertEquals(entry.getUrl(), doc.get(Field.URL));
    }
  }
  @Override
  public List<AssetCategory> getCategories(Hits hits) throws PortalException {
    List<Document> documents = hits.toList();

    List<AssetCategory> categories = new ArrayList<>(documents.size());

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

      AssetCategory category = fetchCategory(categoryId);

      if (category == null) {
        categories = null;

        Indexer<AssetCategory> indexer = IndexerRegistryUtil.getIndexer(AssetCategory.class);

        long companyId = GetterUtil.getLong(document.get(Field.COMPANY_ID));

        indexer.delete(companyId, document.getUID());
      } else if (categories != null) {
        categories.add(category);
      }
    }

    return categories;
  }
Ejemplo n.º 4
0
  public Document getIndexedDocument(long id, SearchContext searchContext) {
    searchContext.setPortletIds(new String[] {EMInfo.PORTLET_ID});
    BooleanQuery fullQuery = BooleanQueryFactoryUtil.create(searchContext);
    BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create(searchContext);
    booleanQuery.addRequiredTerm(Field.ENTRY_CLASS_NAME, EmpDiscipline.class.getName());
    booleanQuery.addExactTerm(EmpDisciplineField.ID, id);

    try {
      fullQuery.add(booleanQuery, BooleanClauseOccur.MUST);
      Hits hits = SearchEngineUtil.search(searchContext, fullQuery);
      return !hits.toList().isEmpty() ? hits.toList().get(0) : null;
    } catch (ParseException e) {
      LogFactoryUtil.getLog(EmpDisciplineLocalServiceImpl.class).info(e);
    } catch (SearchException e) {
      LogFactoryUtil.getLog(EmpDisciplineLocalServiceImpl.class).info(e);
    }

    return null;
  }
  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);
    }
  }