Example usage for com.liferay.portal.kernel.search DocumentHelper setEntryKey

List of usage examples for com.liferay.portal.kernel.search DocumentHelper setEntryKey

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.search DocumentHelper setEntryKey.

Prototype

public void setEntryKey(String className, long classPK) 

Source Link

Usage

From source file:com.liferay.document.library.repository.cmis.internal.CMISRepository.java

License:Open Source License

protected Hits doSearch(SearchContext searchContext, Query query) throws Exception {

    long startTime = System.currentTimeMillis();

    Session session = getSession();/*from  w  w w  .  j  av a 2s.  c om*/

    RepositoryInfo repositoryInfo = session.getRepositoryInfo();

    RepositoryCapabilities repositoryCapabilities = repositoryInfo.getCapabilities();

    QueryConfig queryConfig = searchContext.getQueryConfig();

    CapabilityQuery capabilityQuery = repositoryCapabilities.getQueryCapability();

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

    String productName = repositoryInfo.getProductName();
    String productVersion = repositoryInfo.getProductVersion();

    queryConfig.setAttribute("repositoryProductName", productName);
    queryConfig.setAttribute("repositoryProductVersion", productVersion);

    String queryString = _cmisSearchQueryBuilder.buildQuery(searchContext, query);

    if (_cmisRepositoryDetector.isNuxeo5_4()) {
        queryString += " AND (" + PropertyIds.IS_LATEST_VERSION + " = true)";
    }

    if (_log.isDebugEnabled()) {
        _log.debug("CMIS search query: " + queryString);
    }

    boolean searchAllVersions = _cmisRepositoryDetector.isNuxeo5_5OrHigher();

    ItemIterable<QueryResult> queryResults = session.query(queryString, searchAllVersions);

    int start = searchContext.getStart();
    int end = searchContext.getEnd();

    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS)) {
        start = 0;
    }

    int total = 0;

    List<com.liferay.portal.kernel.search.Document> documents = new ArrayList<>();
    List<String> snippets = new ArrayList<>();
    List<Float> scores = new ArrayList<>();

    for (QueryResult queryResult : queryResults) {
        total++;

        if (total <= start) {
            continue;
        }

        if ((total > end) && (end != QueryUtil.ALL_POS)) {
            continue;
        }

        com.liferay.portal.kernel.search.Document document = new DocumentImpl();

        String objectId = queryResult.getPropertyValueByQueryName(PropertyIds.OBJECT_ID);

        if (_log.isDebugEnabled()) {
            _log.debug("Search result object ID " + objectId);
        }

        FileEntry fileEntry = null;

        try {
            fileEntry = toFileEntry(objectId, true);
        } catch (Exception e) {
            if (_log.isDebugEnabled()) {
                Throwable cause = e.getCause();

                if (cause != null) {
                    cause = cause.getCause();
                }

                if (cause instanceof CmisObjectNotFoundException) {
                    _log.debug("Search result ignored for CMIS document which "
                            + "has a version with an invalid object ID " + cause.getMessage());
                } else {
                    _log.debug("Search result ignored for invalid object ID", e);
                }
            }

            total--;

            continue;
        }

        DocumentHelper documentHelper = new DocumentHelper(document);

        documentHelper.setEntryKey(fileEntry.getModelClassName(), fileEntry.getFileEntryId());

        document.addKeyword(Field.TITLE, fileEntry.getTitle());

        documents.add(document);

        if (queryConfig.isScoreEnabled()) {
            Object scoreObj = queryResult.getPropertyValueByQueryName("HITS");

            if (scoreObj != null) {
                scores.add(Float.valueOf(scoreObj.toString()));
            } else {
                scores.add(1.0F);
            }
        } else {
            scores.add(1.0F);
        }

        snippets.add(StringPool.BLANK);
    }

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

    Hits hits = new HitsImpl();

    hits.setDocs(documents.toArray(new com.liferay.portal.kernel.search.Document[documents.size()]));
    hits.setLength(total);
    hits.setQuery(query);
    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;
}