Ejemplo n.º 1
0
  public void recordHits(Hits hits, long groupId, boolean privateLayout) throws Exception {

    setSearcher(((HitsImpl) hits).getSearcher());

    // This can later be optimized according to LEP-915.

    List docs = new ArrayList(hits.getLength());
    List scores = new ArrayList(hits.getLength());

    for (int i = 0; i < hits.getLength(); i++) {
      Document doc = hits.doc(i);

      String articleId = doc.get("articleId");
      long articleGroupId = GetterUtil.getLong(doc.get(LuceneFields.GROUP_ID));

      if (JournalContentSearchLocalServiceUtil.getLayoutIdsCount(groupId, privateLayout, articleId)
          > 0) {

        docs.add(hits.doc(i));
        scores.add(new Float(hits.score(i)));
      } else if (!isShowListed() && (articleGroupId == groupId)) {
        docs.add(hits.doc(i));
        scores.add(new Float(hits.score(i)));
      }
    }

    setLength(docs.size());
    setDocs((Document[]) docs.toArray(new Document[0]));
    setScores((Float[]) scores.toArray(new Float[0]));

    setSearchTime((float) (System.currentTimeMillis() - getStart()) / Time.SECOND);
  }
  public void recordHits(Hits hits, long groupId, boolean privateLayout, int start, int end)
      throws Exception {

    // This can later be optimized according to LEP-915.

    List<Document> docs = new ArrayList<Document>();
    List<Float> scores = new ArrayList<Float>();

    Document[] docsArray = hits.getDocs();

    for (int i = 0; i < docsArray.length; i++) {
      Document doc = hits.doc(i);

      String articleId = doc.get(Field.ARTICLE_ID);
      long articleGroupId = GetterUtil.getLong(doc.get(Field.GROUP_ID));

      int layoutIdsCount =
          JournalContentSearchLocalServiceUtil.getLayoutIdsCount(groupId, privateLayout, articleId);

      if ((layoutIdsCount > 0) || (!isShowListed() && (articleGroupId == groupId))) {

        docs.add(hits.doc(i));
        scores.add(hits.score(i));
      }
    }

    int length = docs.size();

    hits.setLength(length);

    if (end > length) {
      end = length;
    }

    docs = docs.subList(start, end);
    scores = scores.subList(start, end);

    hits.setDocs(docs.toArray(new Document[docs.size()]));
    hits.setScores(ArrayUtil.toFloatArray(scores));

    hits.setSearchTime((float) (System.currentTimeMillis() - hits.getStart()) / Time.SECOND);
  }
Ejemplo n.º 3
0
  public String search(
      HttpServletRequest request,
      long groupId,
      long userId,
      String keywords,
      int startPage,
      int itemsPerPage,
      String format)
      throws SearchException {

    try {
      ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

      int start = (startPage * itemsPerPage) - itemsPerPage;
      int end = startPage * itemsPerPage;

      Hits results =
          CompanyLocalServiceUtil.search(themeDisplay.getCompanyId(), userId, keywords, start, end);

      String[] queryTerms = results.getQueryTerms();

      int total = results.getLength();

      Object[] values =
          addSearchResults(
              queryTerms,
              keywords,
              startPage,
              itemsPerPage,
              total,
              start,
              "Liferay Portal Search: " + keywords,
              SEARCH_PATH,
              format,
              themeDisplay);

      com.liferay.portal.kernel.xml.Document doc =
          (com.liferay.portal.kernel.xml.Document) values[0];
      Element root = (Element) values[1];

      for (int i = 0; i < results.getDocs().length; i++) {
        Document result = results.doc(i);

        String portletId = result.get(Field.PORTLET_ID);

        Portlet portlet =
            PortletLocalServiceUtil.getPortletById(themeDisplay.getCompanyId(), portletId);

        if (portlet == null) {
          continue;
        }

        String portletTitle = PortalUtil.getPortletTitle(portletId, themeDisplay.getUser());

        long resultGroupId = GetterUtil.getLong(result.get(Field.GROUP_ID));

        String entryClassName = GetterUtil.getString(result.get(Field.ENTRY_CLASS_NAME));

        long entryClassPK = GetterUtil.getLong(result.get(Field.ENTRY_CLASS_PK));

        String title = StringPool.BLANK;

        PortletURL portletURL = getPortletURL(request, portletId, resultGroupId);

        String url = portletURL.toString();

        Date modifedDate = result.getDate(Field.MODIFIED);

        String content = StringPool.BLANK;

        if (Validator.isNotNull(portlet.getIndexerClass())) {
          Indexer indexer = (Indexer) InstancePool.get(portlet.getIndexerClass());

          String snippet = results.snippet(i);

          Summary summary = indexer.getSummary(result, snippet, portletURL);

          title = summary.getTitle();
          url = portletURL.toString();
          content = summary.getContent();

          if (portlet.getPortletId().equals(PortletKeys.JOURNAL)) {
            url = getJournalURL(themeDisplay, resultGroupId, result);
          }
        }

        double score = results.score(i);

        addSearchResult(
            root,
            resultGroupId,
            entryClassName,
            entryClassPK,
            portletTitle + " " + CharPool.RAQUO + " " + title,
            url,
            modifedDate,
            content,
            score,
            format);
      }

      if (_log.isDebugEnabled()) {
        _log.debug("Return\n" + doc.asXML());
      }

      return doc.asXML();

    } catch (Exception e) {
      throw new SearchException(e);
    }
  }
Ejemplo n.º 4
0
  @Override
  protected void doDelete(Object obj) throws Exception {
    SearchContext searchContext = new SearchContext();

    searchContext.setSearchEngineId(getSearchEngineId());

    if (obj instanceof Account) {
      Account account = (Account) obj;

      BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create(searchContext);

      booleanQuery.addRequiredTerm(Field.PORTLET_ID, PORTLET_ID);

      booleanQuery.addRequiredTerm("accountId", account.getAccountId());

      Hits hits =
          SearchEngineUtil.search(
              getSearchEngineId(),
              account.getCompanyId(),
              booleanQuery,
              QueryUtil.ALL_POS,
              QueryUtil.ALL_POS);

      for (int i = 0; i < hits.getLength(); i++) {
        Document document = hits.doc(i);

        SearchEngineUtil.deleteDocument(
            getSearchEngineId(), account.getCompanyId(), document.get(Field.UID));
      }
    } else if (obj instanceof Folder) {
      Folder folder = (Folder) obj;

      BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create(searchContext);

      booleanQuery.addRequiredTerm(Field.PORTLET_ID, PORTLET_ID);

      booleanQuery.addRequiredTerm("folderId", folder.getFolderId());

      Hits hits =
          SearchEngineUtil.search(
              getSearchEngineId(),
              folder.getCompanyId(),
              booleanQuery,
              QueryUtil.ALL_POS,
              QueryUtil.ALL_POS);

      for (int i = 0; i < hits.getLength(); i++) {
        Document document = hits.doc(i);

        SearchEngineUtil.deleteDocument(
            getSearchEngineId(), folder.getCompanyId(), document.get(Field.UID));
      }
    } else if (obj instanceof Message) {
      Message message = (Message) obj;

      Document document = new DocumentImpl();

      document.addUID(PORTLET_ID, message.getMessageId());

      SearchEngineUtil.deleteDocument(
          getSearchEngineId(), message.getCompanyId(), document.get(Field.UID));
    }
  }