Example usage for com.liferay.portal.kernel.search Hits getStart

List of usage examples for com.liferay.portal.kernel.search Hits getStart

Introduction

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

Prototype

public long getStart();

Source Link

Usage

From source file:com.liferay.journal.content.search.web.internal.util.ContentHits.java

License:Open Source License

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<>();
    List<Float> scores = new ArrayList<>();

    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);//from w w  w .j  a  v  a 2 s.  c  om

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

From source file:com.liferay.portlet.journalcontentsearch.util.ContentHits.java

License:Open Source License

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>();
    List<String> snippets = new ArrayList<String>();

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

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

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

            docs.add(hits.doc(i));/*  ww  w . jav a  2 s .c o  m*/
            scores.add(hits.score(i));
            snippets.add(hits.snippet(i));
        } else if (!isShowListed() && (articleGroupId == groupId)) {
            docs.add(hits.doc(i));
            scores.add(hits.score(i));
            snippets.add(hits.snippet(i));
        }
    }

    int length = docs.size();

    hits.setLength(length);

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

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

    hits.setDocs(docs.toArray(new Document[docs.size()]));
    hits.setScores(scores.toArray(new Float[docs.size()]));
    hits.setSnippets(snippets.toArray(new String[docs.size()]));

    hits.setSearchTime((float) (System.currentTimeMillis() - hits.getStart()) / Time.SECOND);
}

From source file:com.rivetlogic.portal.search.elasticsearch.util.ElasticsearchHelper.java

License:Open Source License

/**
 * Gets the search hits./*  ww w.j  a va2  s .com*/
 *
 * @param searchContext the search context
 * @param query the query
 * @return the search hits
 */
public Hits getSearchHits(SearchContext searchContext, Query query) {
    if (_log.isInfoEnabled()) {
        _log.info("Search against Elasticsearch with SearchContext");
    }
    Hits hits = new HitsImpl();
    hits.setStart(new Date().getTime());

    Client client = this._esConnector.getClient();

    String keywords = searchContext.getKeywords();
    SearchRequestBuilder searchRequestBuilder = client
            .prepareSearch(ElasticsearchIndexerConstants.ELASTIC_SEARCH_LIFERAY_INDEX)
            .setQuery(_esQueryBuilder.doSearch(query));

    // Handle Search Facet queries
    handleFacetQueries(searchContext, searchRequestBuilder);
    SearchResponse response = null;
    if (getSort(searchContext.getSorts()) != null) {
        searchRequestBuilder = searchRequestBuilder.setFrom(searchContext.getStart())
                .setSize(searchContext.getEnd()).addSort(getSort(searchContext.getSorts()));
    } else {
        searchRequestBuilder = searchRequestBuilder.setFrom(searchContext.getStart())
                .setSize(searchContext.getEnd());
    }
    response = searchRequestBuilder.execute().actionGet();
    collectFacetResults(searchContext, response);

    SearchHits searchHits = response.getHits();
    hits.setDocs(getDocuments(searchHits, searchContext));
    hits.setScores(getScores(searchHits));
    hits.setSearchTime((float) (System.currentTimeMillis() - hits.getStart()) / Time.SECOND);
    hits.setQuery(query);
    if (keywords != null) {
        hits.setQueryTerms(keywords.split(StringPool.SPACE));
    }
    hits.setLength((int) searchHits.getTotalHits());
    hits.setStart(hits.getStart());

    return hits;
}

From source file:com.rivetlogic.portal.search.elasticsearch.util.ElasticsearchHelper.java

License:Open Source License

/**
 * Gets the search hits.//w  w  w .j a  v a  2s.c  o  m
 *
 * @param searchEngineId the search engine id
 * @param companyId the company id
 * @param query the query
 * @param sort the sort
 * @param start the start
 * @param end the end
 * @return the search hits
 */
public Hits getSearchHits(String searchEngineId, long companyId, Query query, Sort[] sort, int start, int end) {
    if (_log.isInfoEnabled()) {
        _log.info(
                "Search against Elasticsearch with searchEngineId, companyId, query, sort, start and end parameters");
    }
    Hits hits = new HitsImpl();
    Client client = this._esConnector.getClient();

    SearchRequestBuilder searchRequestBuilder = client
            .prepareSearch(ElasticsearchIndexerConstants.ELASTIC_SEARCH_LIFERAY_INDEX)
            .setQuery(_esQueryBuilder.doSearch(query));
    SearchResponse response = null;
    if (getSort(sort) != null) {
        response = searchRequestBuilder.setFrom(start).setSize(end).addSort(getSort(sort)).execute()
                .actionGet();
    } else {
        response = searchRequestBuilder.setFrom(start).setSize(end).execute().actionGet();
    }
    SearchHits searchHits = response.getHits();
    hits.setDocs(getDocuments(searchHits));
    hits.setScores(getScores(searchHits));
    hits.setSearchTime((float) (System.currentTimeMillis() - hits.getStart()) / Time.SECOND);
    hits.setQuery(query);
    hits.setLength((int) searchHits.getTotalHits());
    hits.setStart(hits.getStart());

    return hits;
}