Example usage for org.apache.solr.client.solrj SolrQuery setStart

List of usage examples for org.apache.solr.client.solrj SolrQuery setStart

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj SolrQuery setStart.

Prototype

public SolrQuery setStart(Integer start) 

Source Link

Usage

From source file:com.aboutdata.service.bean.SearchServiceImpl.java

/**
 * ? //  www.  j  a  v  a  2  s  .  c  om
 *
 * @param keyword
 * @param pageable
 * @return
 */
@Override
public Page<PhotosModel> search(String keyword, Pageable pageable) {
    SolrQuery query = new SolrQuery();
    query.setQuery("tags:(" + keyword + ")  OR title:(" + keyword + ")");
    try {
        //?
        query.setStart((pageable.getPageNumber() - 1) * pageable.getPageSize());
        query.setRows(pageable.getPageSize());
        //?
        QueryResponse rsp = solrServer.query(query);
        //?
        SolrDocumentList docs = rsp.getResults();
        List<PhotosModel> models = new ArrayList<PhotosModel>();
        for (SolrDocument solrDocument : docs) {
            String id = solrDocument.getFieldValue("id").toString();
            String title = solrDocument.getFieldValue("title").toString();
            String large = solrDocument.getFieldValue("large").toString();
            String medium = solrDocument.getFieldValue("medium").toString();
            String thumbnail = solrDocument.getFieldValue("thumbnail").toString();
            String source = solrDocument.getFieldValue("source").toString();
            String member_id = solrDocument.getFieldValue("member_id").toString();
            String member_User_name = solrDocument.getFieldValue("member_name").toString();

            //?? ??idusername
            MemberModel member = new MemberModel();
            member.setId(member_id);
            member.setUsername(member_User_name);

            String wallhaven = solrDocument.getFieldValue("wallhaven").toString();
            String create_date = solrDocument.getFieldValue("create_date").toString();
            String modify_date = solrDocument.getFieldValue("modify_date").toString();

            String tags = solrDocument.getFieldValue("tags").toString();

            Set<Tag> tagSet = tagService.getTagsFromString(tags);

            PhotosModel model = new PhotosModel();
            model.setId(id);
            model.setTitle(title);
            model.setLarge(large);
            model.setMedium(medium);
            model.setThumbnail(thumbnail);
            model.setSource(source);

            model.setWallhaven(wallhaven);
            //model.setCreateDate(new Date());
            // model.setModifyDate(new DateTime(modify_date).toDate());

            model.setMember(member);
            //?tag
            List<TagModel> tagList = TagDTO.getTagModelsDTO(tagSet);
            model.setTags(tagList);

            models.add(model);
        }
        Page<PhotosModel> result = new PageImpl<PhotosModel>(models, pageable, docs.getNumFound());
        return result;
    } catch (SolrServerException ex) {
        logger.error("build index error {}", ex);
    }
    return null;
}

From source file:com.buddycloud.channeldirectory.search.rsm.SolrRSMUtils.java

License:Apache License

public static void preprocess(SolrQuery query, RSM rsm) throws IllegalArgumentException {

    String after = rsm.getAfter();
    String before = rsm.getBefore();

    int initialIndex = rsm.getIndex();
    int lastIndex = -1;

    if (after != null) {
        initialIndex = Integer.valueOf(after);
    }/*from w ww  .j  a  v a  2  s .co m*/
    if (before != null && !before.isEmpty()) {
        lastIndex = Integer.valueOf(before) - 2;
    }

    if (rsm.getMax() != null) {
        if (before != null) {
            initialIndex = lastIndex - rsm.getMax() + 1;
        } else {
            lastIndex = initialIndex + rsm.getMax() - 1;
        }
    }

    if (lastIndex >= 0) {
        query.setStart(initialIndex);
        query.setRows(lastIndex - initialIndex + 1);
    }

    rsm.setIndex(initialIndex);
    rsm.setFirst(String.valueOf(initialIndex + 1));
    rsm.setLast(String.valueOf(lastIndex + 1));
}

From source file:com.databasepreservation.visualization.utils.SolrUtils.java

public static <T extends Serializable> IndexResult<T> find(SolrClient index, Class<T> classToRetrieve,
        Filter filter, Sorter sorter, Sublist sublist, Facets facets)
        throws GenericException, RequestNotValidException {
    IndexResult<T> ret;// ww w  .j  a va 2s. c  o m
    SolrQuery query = new SolrQuery();
    query.setQuery(parseFilter(filter));
    query.setSorts(parseSorter(sorter));
    query.setStart(sublist.getFirstElementIndex());
    query.setRows(sublist.getMaximumElementCount());
    parseAndConfigureFacets(facets, query);

    try {
        QueryResponse response = index.query(getIndexName(classToRetrieve), query);
        ret = queryResponseToIndexResult(response, classToRetrieve, facets);
    } catch (SolrServerException | IOException
            | org.apache.solr.client.solrj.impl.HttpSolrClient.RemoteSolrException e) {
        String message = e.getMessage();
        String bodytag = "<body>";
        if (message != null && message.contains(bodytag)) {
            message = message.substring(message.indexOf(bodytag) + bodytag.length(),
                    message.indexOf("</body>"));
            message = message.replaceAll("\\<[^>]*?>", "");
        }
        throw new GenericException("Could not query index, message: " + message, e);
    }

    return ret;
}

From source file:com.databasepreservation.visualization.utils.SolrUtils.java

public static <T extends Serializable> IndexResult<T> find(SolrClient index, Class<T> classToRetrieve,
        Filter filter, Sorter sorter, Sublist sublist, Facets facets, RodaUser user, boolean showInactive)
        throws GenericException, RequestNotValidException {
    IndexResult<T> ret;/*from www  . j  av a  2s .  c  o m*/
    SolrQuery query = new SolrQuery();
    query.setQuery(parseFilter(filter));
    query.setSorts(parseSorter(sorter));
    query.setStart(sublist.getFirstElementIndex());
    query.setRows(sublist.getMaximumElementCount());
    parseAndConfigureFacets(facets, query);
    if (hasPermissionFilters(classToRetrieve)) {
        query.addFilterQuery(getFilterQueries(user, showInactive));
    }

    try {
        QueryResponse response = index.query(getIndexName(classToRetrieve), query);
        ret = queryResponseToIndexResult(response, classToRetrieve, facets);
    } catch (SolrServerException | IOException e) {
        throw new GenericException("Could not query index", e);
    }

    return ret;
}

From source file:com.databasepreservation.visualization.utils.SolrUtils.java

public static <T extends Serializable> IndexResult<T> find(SolrClient index, Class<T> classToRetrieve,
        String tableUUID, Filter filter, Sorter sorter, Sublist sublist, Facets facets)
        throws GenericException, RequestNotValidException {
    IndexResult<T> ret;/*w  w  w .  j  av a  2 s  . c  o  m*/
    SolrQuery query = new SolrQuery();
    query.setQuery(parseFilter(filter));
    query.setSorts(parseSorter(sorter));
    query.setStart(sublist.getFirstElementIndex());
    query.setRows(sublist.getMaximumElementCount());

    parseAndConfigureFacets(facets, query);

    try {
        QueryResponse response = index.query(getTableCollectionName(tableUUID), query);
        ret = queryResponseToIndexResult(response, classToRetrieve, facets);
    } catch (SolrServerException | IOException e) {
        throw new GenericException("Could not query index", e);
    }

    return ret;
}

From source file:com.databasepreservation.visualization.utils.SolrUtils.java

public static InputStream findCSV(SolrClient index, String collection, Filter filter, Sorter sorter,
        Sublist sublist, List<String> fields) throws GenericException, RequestNotValidException {
    SolrQuery query = new SolrQuery();
    query.setQuery(parseFilter(filter));
    query.setSorts(parseSorter(sorter));
    if (sublist != null) {
        query.setStart(sublist.getFirstElementIndex());
        query.setRows(sublist.getMaximumElementCount());
    }/*from   ww w .  ja v a  2 s.  co m*/
    query.setFields(fields.toArray(new String[0]));

    LOGGER.debug("CSV export query object: " + query.toString());
    LOGGER.debug("CSV export query: " + query.toQueryString());

    try {
        QueryRequest queryRequest = new QueryRequest(query);
        queryRequest.setResponseParser(new InputStreamResponseParser("csv"));
        QueryResponse response = queryRequest.process(index, collection);

        Object stream = response.getResponse().get("stream");
        if (stream instanceof InputStream) {
            return (InputStream) stream;
        } else {
            throw new GenericException(
                    "Result was not an input stream. Its string representation was: " + stream.toString());
        }
    } catch (SolrServerException | IOException e) {
        throw new GenericException("Could not query index", e);
    }
}

From source file:com.databasepreservation.visualization.utils.SolrUtils.java

public static String getSolrQuery(Filter filter, Sorter sorter, Sublist sublist, Facets facets)
        throws GenericException, RequestNotValidException {
    SolrQuery query = new SolrQuery();
    query.setQuery(parseFilter(filter));
    // query.setSorts(parseSorter(sorter));
    query.setStart(sublist.getFirstElementIndex());
    query.setRows(sublist.getMaximumElementCount());
    // parseAndConfigureFacets(facets, query);

    return query.toQueryString();
}

From source file:com.doculibre.constellio.services.ClusteringServicesImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from   ww w  . j a v  a 2s . c om*/
public List<SimpleOrderedMap<Object>> cluster(SimpleSearch simpleSearch, CollectionFacet facet, int start,
        int row, ConstellioUser user) {
    //      String luceneQuery = simpleSearch.getLuceneQuery();
    RecordCollectionServices collectionServices = ConstellioSpringUtils.getRecordCollectionServices();
    String solrServerName = simpleSearch.getCollectionName();
    RecordCollection collection = collectionServices.get(solrServerName);

    //Remarque useDismax est  false car nous allons utiliser le type de requete "/clustering"
    //         donc pas besoin d'ajouter les paramtres necessaires  dismax
    SolrQuery query = SearchServicesImpl.toSolrQuery(simpleSearch, false, true, true);
    query.setRequestHandler("/clustering");

    query.setParam(ClusteringComponent.COMPONENT_NAME, Boolean.TRUE);
    query.setParam(ClusteringParams.USE_COLLECTION, facet.isClusteringUseCollection());
    query.setParam(ClusteringParams.USE_SEARCH_RESULTS, facet.isClusteringUseSearchResults());
    query.setParam(ClusteringParams.USE_DOC_SET, facet.isClusteringUseDocSet());
    query.setParam(ClusteringParams.ENGINE_NAME, facet.getClusteringEngine());

    //The maximum number of labels to produce
    query.setParam(CarrotParams.NUM_DESCRIPTIONS, "" + maxClusters);
    //      query.setParam(CarrotParams.NUM_DESCRIPTIONS, "" + facet.getCarrotNumDescriptions());

    query.setParam(CarrotParams.OUTPUT_SUB_CLUSTERS, facet.isCarrotOutputSubclusters());
    query.setParam(ConstellioSolrQueryParams.LUCENE_QUERY, simpleSearch.getLuceneQuery());
    query.setParam(ConstellioSolrQueryParams.COLLECTION_NAME, simpleSearch.getCollectionName());
    query.setParam(ConstellioSolrQueryParams.SIMPLE_SEARCH, simpleSearch.toSimpleParams().toString());
    if (user != null) {
        query.setParam(ConstellioSolrQueryParams.USER_ID, "" + user.getId());
    }

    //If true, then the snippet field (if no snippet field, then the title field) will be highlighted and the highlighted text will be used for the snippet. 
    query.setParam(CarrotParams.PRODUCE_SUMMARY, facet.isCarrotProduceSummary());
    if (facet.getCarrotTitleField() != null) {
        IndexField indexField = facet.getCarrotTitleField();
        query.setParam(CarrotParams.TITLE_FIELD_NAME, indexField.getName());
    }
    if (facet.getCarrotUrlField() != null) {
        IndexField indexField = facet.getCarrotUrlField();
        query.setParam(CarrotParams.URL_FIELD_NAME, indexField.getName());
    }
    if (facet.getCarrotSnippetField() != null) {
        IndexField indexField = facet.getCarrotSnippetField();
        query.setParam(CarrotParams.SNIPPET_FIELD_NAME, indexField.getName());
    }

    // Requte Lucene
    //      query.setQuery(luceneQuery);

    // nb rsultats par page
    query.setRows(row);

    // page de dbut
    query.setStart(start);
    //Les resultats ne vont pas etre affichs
    /*query.setHighlight(true);
    query.setHighlightFragsize(100);
    query.setHighlightSnippets(2);*/

    if (collection.isOpenSearch()) {
        query.setParam("openSearchURL", collection.getOpenSearchURL());
    }

    SolrServer server = SolrCoreContext.getSolrServer(solrServerName);

    QueryResponse queryResponse;
    try {
        queryResponse = server.query(query);
    } catch (SolrServerException e) {
        throw new RuntimeException(e);
    }

    NamedList<Object> values = queryResponse.getResponse();
    Object clusters = values.get("clusters");
    return (List<SimpleOrderedMap<Object>>) clusters;
}

From source file:com.doculibre.constellio.services.FacetServicesImpl.java

License:Open Source License

public static SolrQuery toSolrQuery(SimpleSearch simpleSearch, int start, int row,
        boolean includeSingleValueFacets, boolean notIncludedOnly, List<String> customFieldFacets,
        List<String> customQueryFacets, ConstellioUser user) {
    String solrServerName = simpleSearch.getCollectionName();
    RecordCollectionServices collectionServices = ConstellioSpringUtils.getRecordCollectionServices();
    RecordCollection collection = collectionServices.get(solrServerName);
    SolrServices solrServices = ConstellioSpringUtils.getSolrServices();
    Boolean usesDisMax = solrServices.usesDisMax(collection);
    SolrQuery query;
    if (!collection.isOpenSearch()) {
        query = SearchServicesImpl.toSolrQuery(simpleSearch, usesDisMax, true, includeSingleValueFacets,
                notIncludedOnly);/*  w w w. ja  va2 s.c om*/
    } else {
        query = SearchServicesImpl.toSolrQuery(simpleSearch, usesDisMax, false, true, false);
    }

    query.setParam(ConstellioSolrQueryParams.COLLECTION_NAME, simpleSearch.getCollectionName());
    query.setParam(ConstellioSolrQueryParams.LUCENE_QUERY,
            simpleSearch.getLuceneQuery(includeSingleValueFacets, true));
    query.setParam(ConstellioSolrQueryParams.SIMPLE_SEARCH, simpleSearch.toSimpleParams().toString());
    if (user != null) {
        query.setParam(ConstellioSolrQueryParams.USER_ID, "" + user.getId());
    }

    if (StringUtils.isEmpty(query.getQuery())) {
        query.setQuery(SimpleSearch.SEARCH_ALL);
        query.setRequestHandler("/elevate");
    }

    query.set("shards.qt", "/elevate");
    query.setRequestHandler("/elevate");

    query.setRows(row);
    query.setStart(start);
    query.setHighlight(true);
    query.setHighlightFragsize(100);
    query.setHighlightSnippets(2);

    query.setFacet(true);
    query.setFacetLimit(400);
    query.setFacetMinCount(1);

    if (collection.isOpenSearch()) {
        query.setParam("openSearchURL", collection.getOpenSearchURL());
        Locale locale = simpleSearch.getSingleSearchLocale();
        if (locale != null) {
            query.setParam("lang", locale.getLanguage());
        }
    } else {
        for (CollectionFacet collectionFacet : collection.getCollectionFacets()) {
            if (customFieldFacets == null && collectionFacet.isFieldFacet()) {
                IndexField indexField = collectionFacet.getFacetField();
                String indexFieldName = indexField.getName();
                if (!notIncludedOnly) {
                    query.addFacetField(indexFieldName);
                } else {
                    SearchedFacet searchedFacet = simpleSearch.getSearchedFacet(indexFieldName);
                    if (searchedFacet != null) {
                        if (!searchedFacet.getIncludedValues().isEmpty()) {
                            StringBuffer sbEx = new StringBuffer();
                            sbEx.append("{!ex=dt}");
                            //                                sbEx.append("{!ex=");
                            //                                boolean first = true;
                            //                                for (String includedValue : searchedFacet.getIncludedValues()) {
                            //                                    if (first) {
                            //                                        first = false;
                            //                                    } else {
                            //                                        sbEx.append(",");
                            //                                    }
                            //                                    sbEx.append(includedValue); 
                            //                                }
                            //                                sbEx.append("}");
                            //                                query.setParam("facet.field", sbEx.toString() + indexFieldName);
                            query.addFacetField(sbEx.toString() + indexFieldName);
                        } else {
                            query.addFacetField(indexFieldName);
                        }
                    }
                }
            } else if (customQueryFacets == null && collectionFacet.isQueryFacet()) {
                // Modification Rida, remplacement de collectionFacet.getLabels() par
                // collectionFacet.getLabelledValues()
                // for (I18NLabel valueLabels : collectionFacet.getLabels()) {
                for (I18NLabel valueLabels : collectionFacet.getLabelledValues()) {
                    String facetQuery = valueLabels.getKey();
                    query.addFacetQuery(facetQuery);
                }
            }
        }
        if (customFieldFacets != null) {
            for (String facetField : customFieldFacets) {
                if (!notIncludedOnly) {
                    query.addFacetField(facetField);
                } else {
                    StringBuffer sbEx = new StringBuffer();
                    sbEx.append("{!ex=dt}");
                    //                        sbEx.append("{!ex=");
                    //                        boolean first = true;
                    //                        for (String includedValue : searchedFacet.getIncludedValues()) {
                    //                            if (first) {
                    //                                first = false;
                    //                            } else {
                    //                                sbEx.append(",");
                    //                            }
                    //                            sbEx.append(includedValue); 
                    //                        }
                    //                        sbEx.append("}");
                    query.setParam("facet.field", sbEx.toString() + facetField);
                }
            }
        }

        if (customQueryFacets != null) {
            for (String facetQuery : customQueryFacets) {
                query.addFacetQuery(facetQuery);
            }
        }
    }

    return query;
}

From source file:com.doculibre.constellio.services.SearchServicesImpl.java

License:Open Source License

@Override
public QueryResponse search(SimpleSearch simpleSearch, int start, int rows, SearchParams searchParams,
        ConstellioUser user) {// ww w. java 2  s.c  om
    QueryResponse queryResponse;

    String collectionName = simpleSearch.getCollectionName();
    if (collectionName != null) {
        RecordCollectionServices collectionServices = ConstellioSpringUtils.getRecordCollectionServices();
        RecordServices recordServices = ConstellioSpringUtils.getRecordServices();
        RecordCollection collection = collectionServices.get(collectionName);

        SolrServices solrServices = ConstellioSpringUtils.getSolrServices();
        Boolean usesDisMax = solrServices.usesDisMax(collection);

        SolrQuery query;
        if (!collection.isOpenSearch()) {
            query = toSolrQuery(simpleSearch, usesDisMax, true, true);
        } else {
            query = toSolrQuery(simpleSearch, usesDisMax, false, true);
        }
        // displayQuery(query);

        String luceneQuery = simpleSearch.getLuceneQuery();
        query.setParam(ConstellioSolrQueryParams.LUCENE_QUERY, luceneQuery);
        query.setParam(ConstellioSolrQueryParams.SIMPLE_SEARCH, simpleSearch.toSimpleParams().toString());
        query.setParam(ConstellioSolrQueryParams.COLLECTION_NAME, collectionName);
        if (user != null) {
            query.setParam(ConstellioSolrQueryParams.USER_ID, "" + user.getId());
        }

        String queryString = query.getQuery();
        if (StringUtils.isEmpty(queryString)) {
            queryString = SimpleSearch.SEARCH_ALL;
        }

        List<Record> pendingExclusions = recordServices.getPendingExclusions(collection);
        while (!pendingExclusions.isEmpty()) {
            IndexingManager indexingManager = IndexingManager.get(collection);
            if (indexingManager.isActive()) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                pendingExclusions = recordServices.getPendingExclusions(collection);
            } else {
                return null;
            }
        }

        // SolrQuery query = new SolrQuery();
        query.set("collectionName", simpleSearch.getCollectionName());
        // query.setQuery(luceneQuery);

        query.set("shards.qt", "/elevate");
        query.setRequestHandler("/elevate");

        // nb rsultats par page
        query.setRows(rows);

        // page de dbut
        query.setStart(start);
        query.setHighlight(searchParams.isHighlightingEnabled());
        if (searchParams.isHighlightingEnabled()) {
            query.setHighlightFragsize(searchParams.getFragsize());
            query.setHighlightSnippets(searchParams.getSnippets());
        }

        if (simpleSearch.getSortField() != null) {
            ORDER order = SimpleSearch.SORT_DESCENDING.equals(simpleSearch.getSortOrder()) ? ORDER.desc
                    : ORDER.asc;
            IndexFieldServices indexFieldServices = ConstellioSpringUtils.getIndexFieldServices();
            IndexField indexField = indexFieldServices.get(simpleSearch.getSortField(), collection);
            if (indexField != null) {
                IndexField sortIndexField = indexFieldServices.getSortFieldOf(indexField);
                if (sortIndexField != null) {
                    query.setSort(sortIndexField.getName(), order);
                }
            }
        }

        if (collection.isOpenSearch()) {
            query.setParam("openSearchURL", collection.getOpenSearchURL());
            Locale locale = simpleSearch.getSingleSearchLocale();
            if (locale != null) {
                query.setParam("lang", locale.getLanguage());
            }
        }

        if (searchParams.getHighlightedFields() == null) {
            IndexField defaultSearchField = collection.getDefaultSearchIndexField();
            query.addHighlightField(defaultSearchField.getName());
            for (CopyField copyFieldDest : defaultSearchField.getCopyFieldsDest()) {
                IndexField copyIndexFieldSource = copyFieldDest.getIndexFieldSource();
                if (copyIndexFieldSource != null && !copyIndexFieldSource.isTitleField()
                        && copyIndexFieldSource.isHighlighted()) {
                    query.addHighlightField(copyIndexFieldSource.getName());
                }
            }
            IndexField titleField = collection.getTitleIndexField();
            if (titleField != null && titleField.isHighlighted()) {
                query.addHighlightField(titleField.getName());
            }
        } else {
            for (String highlightedField : searchParams.getHighlightedFields()) {
                IndexField field = collection.getIndexField(highlightedField);
                if (field != null) {
                    query.addHighlightField(highlightedField);
                }
            }
        }
        SolrServer server = SolrCoreContext.getSolrServer(collectionName);
        if (server != null) {
            try {
                // displayQuery(query);
                queryResponse = server.query(query);
            } catch (SolrServerException e) {
                queryResponse = null;
            }
        } else {
            queryResponse = null;
        }

        //            if (queryResponse != null && !collection.isOpenSearch()) {
        //                StatsCompiler statCompiler = StatsCompiler.getInstance();
        //                try {
        //                    statCompiler.saveStats(collectionName, SolrLogContext.getStatsSolrServer(),
        //                        SolrLogContext.getStatsCompileSolrServer(), queryResponse, luceneQuery);
        //                } catch (SolrServerException e) {
        //                    throw new RuntimeException(e);
        //                } catch (IOException e) {
        //                    throw new RuntimeException(e);
        //                }
        //            }

    } else {
        queryResponse = null;
    }

    // improveQueryResponse(collectionName, queryResponse);
    // System.out.println("Response size" + queryResponse.getResults().getNumFound());
    return queryResponse;
}