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

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

Introduction

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

Prototype

public SolrQuery setFields(String... fields) 

Source Link

Usage

From source file:org.codelibs.fess.helper.IndexingHelper.java

License:Apache License

protected SolrDocumentList getSolrDocumentList(final SolrGroup solrGroup, final String fq, final String q,
        final String[] fields, final int row) {
    final SolrQuery sq = new SolrQuery();
    if (fq != null) {
        sq.setFilterQueries(fq);/*w w w  .ja  va  2s . co  m*/
    }
    sq.setQuery(q);
    if (fields != null) {
        sq.setFields(fields);
    }
    sq.setRows(row);
    final SolrDocumentList docList = solrGroup.query(sq).getResults();
    if (docList.getNumFound() <= row) {
        return docList;
    }
    return getSolrDocumentList(solrGroup, fq, q, fields, (int) docList.getNumFound());
}

From source file:org.codelibs.fess.helper.IndexingHelper.java

License:Apache License

public SolrDocument getSolrDocument(final SolrGroup solrGroup, final String id, final String[] fields) {
    final FieldHelper fieldHelper = ComponentUtil.getFieldHelper();

    final SolrQuery solrQuery = new SolrQuery();
    final StringBuilder queryBuf = new StringBuilder(500);
    queryBuf.append("{!raw f=").append(fieldHelper.idField).append("}");
    queryBuf.append(id);/*from w w  w . j ava2 s.  co m*/
    solrQuery.setQuery(queryBuf.toString());
    if (fields != null) {
        solrQuery.setFields(fields);
    }
    final QueryResponse response = solrGroup.query(solrQuery);
    final SolrDocumentList docList = response.getResults();
    if (docList.isEmpty()) {
        return null;
    }
    if (docList.size() > 1) {
        logger.error("Invalid multiple docs for " + id);
        for (final SolrDocument doc : docList) {
            final Object idValue = doc.getFieldValue(fieldHelper.idField);
            if (idValue != null) {
                deleteDocument(solrGroup, idValue.toString());
            }
        }
        return null;
    }
    return docList.get(0);
}

From source file:org.codelibs.fess.helper.IndexingHelper.java

License:Apache License

public SolrDocumentList getSolrDocumentListByPrefixId(final SolrGroup solrGroup, final String id,
        final String[] fields) {
    final FieldHelper fieldHelper = ComponentUtil.getFieldHelper();
    final SolrQuery solrQuery = new SolrQuery();
    final StringBuilder queryBuf = new StringBuilder(500);
    queryBuf.append("{!prefix f=").append(fieldHelper.idField).append("}");
    queryBuf.append(id);//from   w w w  . j a  v  a  2 s  .  c  om
    solrQuery.setQuery(queryBuf.toString());
    if (fields != null) {
        solrQuery.setFields(fields);
    }
    final QueryResponse response = solrGroup.query(solrQuery);
    final SolrDocumentList docList = response.getResults();
    if (docList.isEmpty()) {
        return null;
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Found solr documents: " + docList);
    }
    return docList;
}

From source file:org.codelibs.fess.helper.IndexingHelper.java

License:Apache License

protected SolrDocumentList getChildSolrDocumentList(final SolrGroup solrGroup, final String id,
        final String[] fields, final int row) {
    final FieldHelper fieldHelper = ComponentUtil.getFieldHelper();
    final SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery("{!raw f=" + fieldHelper.parentIdField + " v=\"" + id + "\"}");
    if (fields != null) {
        solrQuery.setFields(fields);
    }//from w  w w. ja v a 2 s.c  om
    solrQuery.setRows(row);
    final SolrDocumentList docList = solrGroup.query(solrQuery).getResults();
    if (docList.getNumFound() <= row) {
        return docList;
    }
    return getChildSolrDocumentList(solrGroup, id, fields, (int) docList.getNumFound());
}

From source file:org.codelibs.fess.service.SearchService.java

License:Apache License

public List<Map<String, Object>> getDocumentList(final String query, final int start, final int rows,
        final FacetInfo facetInfo, final GeoInfo geoInfo, final MoreLikeThisInfo mltInfo,
        final String[] responseFields, final String[] docValuesFields, final boolean forUser) {
    if (start > queryHelper.getMaxSearchResultOffset()) {
        throw new ResultOffsetExceededException("The number of result size is exceeded.");
    }//from   ww  w.  ja  v  a  2  s  . c o  m

    final long startTime = System.currentTimeMillis();

    final SolrGroup solrGroup = solrGroupManager.getSolrGroup(QueryType.QUERY);

    QueryResponse queryResponse = null;
    final SolrQuery solrQuery = new SolrQuery();
    final SearchQuery searchQuery = queryHelper.build(query, forUser);
    final String q = searchQuery.getQuery();
    if (StringUtil.isNotBlank(q)) {
        // fields
        solrQuery.setFields(responseFields);
        // query
        solrQuery.setQuery(q);
        solrQuery.setStart(start);
        solrQuery.setRows(rows);
        solrQuery.set("mm", searchQuery.getMinimumShouldMatch());
        solrQuery.set("defType", searchQuery.getDefType());
        for (final Map.Entry<String, String[]> entry : queryHelper.getQueryParamMap().entrySet()) {
            solrQuery.set(entry.getKey(), entry.getValue());
        }
        // filter query
        if (searchQuery.hasFilterQueries()) {
            solrQuery.addFilterQuery(searchQuery.getFilterQueries());
        }
        // sort
        final SortField[] sortFields = searchQuery.getSortFields();
        if (sortFields.length != 0) {
            for (final SortField sortField : sortFields) {
                solrQuery.addSort(sortField.getField(),
                        Constants.DESC.equals(sortField.getOrder()) ? SolrQuery.ORDER.desc
                                : SolrQuery.ORDER.asc);
            }
        } else if (queryHelper.hasDefaultSortFields()) {
            for (final SortField sortField : queryHelper.getDefaultSortFields()) {
                solrQuery.addSort(sortField.getField(),
                        Constants.DESC.equals(sortField.getOrder()) ? SolrQuery.ORDER.desc
                                : SolrQuery.ORDER.asc);
            }
        }
        // highlighting
        if (queryHelper.getHighlightingFields() != null && queryHelper.getHighlightingFields().length != 0) {
            for (final String hf : queryHelper.getHighlightingFields()) {
                solrQuery.addHighlightField(hf);
            }
            solrQuery.setHighlightSnippets(queryHelper.getHighlightSnippetSize());
        }
        // shards
        if (queryHelper.getShards() != null) {
            solrQuery.setParam("shards", queryHelper.getShards());
        }
        // geo
        if (geoInfo != null && geoInfo.isAvailable()) {
            solrQuery.addFilterQuery(geoInfo.toGeoQueryString());
            final String additionalGeoQuery = queryHelper.getAdditionalGeoQuery();
            if (StringUtil.isNotBlank(additionalGeoQuery)) {
                solrQuery.addFilterQuery(additionalGeoQuery);
            }
        }
        // facets
        if (facetInfo != null) {
            solrQuery.setFacet(true);
            if (facetInfo.field != null) {
                for (final String f : facetInfo.field) {
                    if (queryHelper.isFacetField(f)) {
                        solrQuery.addFacetField(f);
                    } else {
                        throw new FessSolrQueryException("EFESS0002", new Object[] { f });
                    }
                }
            }
            if (facetInfo.query != null) {
                for (final String fq : facetInfo.query) {
                    final String facetQuery = queryHelper.buildFacetQuery(fq);
                    if (StringUtil.isNotBlank(facetQuery)) {
                        solrQuery.addFacetQuery(facetQuery);
                    } else {
                        throw new FessSolrQueryException("EFESS0003", new Object[] { fq, facetQuery });
                    }
                }
            }
            if (facetInfo.limit != null) {
                solrQuery.setFacetLimit(Integer.parseInt(facetInfo.limit));
            }
            if (facetInfo.minCount != null) {
                solrQuery.setFacetMinCount(Integer.parseInt(facetInfo.minCount));
            }
            if (facetInfo.missing != null) {
                solrQuery.setFacetMissing(Boolean.parseBoolean(facetInfo.missing));
            }
            if (facetInfo.prefix != null) {
                solrQuery.setFacetPrefix(facetInfo.prefix);
            }
            if (facetInfo.sort != null && queryHelper.isFacetSortValue(facetInfo.sort)) {
                solrQuery.setFacetSort(facetInfo.sort);
            }
        }
        // mlt
        if (mltInfo != null) {
            final String mltField = queryHelper.getMoreLikeThisField(mltInfo.field);
            if (mltField != null) {
                solrQuery.set("mlt", true);
                if (mltInfo.count != null) {
                    solrQuery.set("mlt.count", Integer.parseInt(mltInfo.count));
                }
                solrQuery.set("mlt.fl", mltField);
            }
        }

        if (queryHelper.getTimeAllowed() >= 0) {
            solrQuery.setTimeAllowed(queryHelper.getTimeAllowed());
        }
        final Set<Entry<String, String[]>> paramSet = queryHelper.getRequestParameterSet();
        if (!paramSet.isEmpty()) {
            for (final Map.Entry<String, String[]> entry : paramSet) {
                solrQuery.set(entry.getKey(), entry.getValue());
            }
        }

        if (docValuesFields != null) {
            for (final String docValuesField : docValuesFields) {
                solrQuery.add(Constants.DCF, docValuesField);
            }
        }

        queryResponse = solrGroup.query(solrQuery, SolrRequest.METHOD.POST);
    }
    final long execTime = System.currentTimeMillis() - startTime;

    final QueryResponseList queryResponseList = ComponentUtil.getQueryResponseList();
    queryResponseList.init(queryResponse, rows);
    queryResponseList.setSearchQuery(q);
    queryResponseList.setSolrQuery(solrQuery.toString());
    queryResponseList.setExecTime(execTime);
    return queryResponseList;
}

From source file:org.dbflute.solr.cbean.AbstractSolrConditionBean.java

License:Apache License

@Override
public SolrQuery buildSolrQuery() {
    SolrQuery query = new SolrQuery();

    if (this.isSpecified()) {
        query.setFields(this.getSpecifyFields());
    } else {/*from   w w w.  j av a  2  s . c om*/
        query.setFields(this.getAllFields());
    }
    if (this.isUseFilterQuery()) {
        query.setQuery(this.getMinimumQuery());
        query.setFilterQueries(this.getQueryArray());
    } else {
        query.setQuery(this.getQueryString());
        String[] filterQueryArray = this.getFilterQueryArray();
        if (filterQueryArray != null && filterQueryArray.length != 0) {
            query.setFilterQueries(this.getFilterQueryArray());
        }
    }
    if (this.isUseSort()) {
        for (SortClause sortClause : this.getSolrSortClauseList()) {
            query.addSort(sortClause);
        }
    }

    for (SolrQueryBean queryBean : this.getFacetQueryList()) {
        query.addFacetQuery(queryBean.getQueryString());
    }

    SolrSpecification facetSpecifyBean = getFacetSpecification();
    if (facetSpecifyBean.getSpecifyFields().length > 0) {
        query.addFacetField(facetSpecifyBean.getSpecifyFields());
    }

    query.setStart(this.getPageStart());
    query.setRows(this.getPageSize());

    return query;
}

From source file:org.dspace.app.cris.batch.ScriptAddPMCDataToRP.java

/**
 * Batch script to aggregate PMC data to RPs. See the technical
 * documentation for further details./* www  . j av a  2 s.c o m*/
 * 
 * @throws SearchServiceException
 */
public static void main(String[] args) throws ParseException, SQLException, SearchServiceException {
    log.info("#### START AddPMCDataToRP: -----" + new Date() + " ----- ####");

    DSpace dspace = new DSpace();
    ApplicationService applicationService = dspace.getServiceManager().getServiceByName("applicationService",
            ApplicationService.class);

    CrisSearchService searchService = dspace.getServiceManager()
            .getServiceByName(CrisSearchService.class.getName(), CrisSearchService.class);
    PMCPersistenceService pmcService = dspace.getServiceManager()
            .getServiceByName(PMCPersistenceService.class.getName(), PMCPersistenceService.class);

    List<ResearcherPage> rs = applicationService.getList(ResearcherPage.class);

    for (ResearcherPage rp : rs) {
        boolean updated = false;
        int itemsCited = 0;
        int citations = 0;
        SolrQuery query = new SolrQuery();
        query.setQuery("dc.identifier.pmid:[* TO *]");
        query.addFilterQuery("{!field f=author_authority}" + ResearcherPageUtils.getPersistentIdentifier(rp),
                "NOT(withdrawn:true)");
        query.setFields("dc.identifier.pmid");
        query.setRows(Integer.MAX_VALUE);

        QueryResponse response = searchService.search(query);
        SolrDocumentList results = response.getResults();
        for (SolrDocument doc : results) {
            Integer pmid = null;
            try {
                pmid = Integer.valueOf((String) doc.getFirstValue("dc.identifier.pmid"));
            } catch (NumberFormatException e) {
                log.warn("Found invalid pmid: " + doc.getFieldValue("dc.identifier.pmid") + " for rp: "
                        + ResearcherPageUtils.getPersistentIdentifier(rp));
            }
            if (pmid != null) {
                PMCCitation pmccitation = pmcService.get(PMCCitation.class, pmid);
                if (pmccitation != null && pmccitation.getNumCitations() > 0) {
                    itemsCited++;
                    citations += pmccitation.getNumCitations();
                }
            }
        }

        updated = setValue(applicationService, rp, itemsCitedTP, String.valueOf(itemsCited));
        // caution don't use the short-circuit OR operator (i.e || otherwise
        // only the first found pmcdata value will be recorded!) 
        updated = updated | setValue(applicationService, rp, citationsTP, String.valueOf(citations));
        updated = updated
                | setValue(applicationService, rp, itemsInPubmedTP, String.valueOf(results.getNumFound()));

        if (StringUtils.isNotEmpty(itemsInPMCTP)) {
            query = new SolrQuery();
            query.setQuery("dc.identifier.pmcid:[* TO *]");
            query.addFilterQuery(
                    "{!field f=author_authority}" + ResearcherPageUtils.getPersistentIdentifier(rp),
                    "NOT(withdrawn:true)");
            query.setRows(0);

            response = searchService.search(query);
            results = response.getResults();
            // caution don't use the short-circuit OR operator (i.e || otherwise
            // only the first found pmcdata value will be recorded!)
            updated = updated
                    | setValue(applicationService, rp, itemsInPMCTP, String.valueOf(results.getNumFound()));
        }

        if (updated) {
            applicationService.saveOrUpdate(ResearcherPage.class, rp);
        }
    }
    log.info("#### END AddPMCDataToRP: -----" + new Date() + " ----- ####");
}

From source file:org.dspace.app.cris.batch.ScriptCrisSubscribe.java

License:Open Source License

/**
 * Sends an email to the given e-person with details of new items in the
 * given dspace object (MUST be a community or a collection), items that
 * appeared yesterday. No e-mail is sent if there aren't any new items in
 * any of the dspace objects.//from   w ww .java  2  s.c  o  m
 * 
 * @param context
 *            DSpace context object
 * @param eperson
 *            eperson to send to
 * @param rpkeys
 *            List of DSpace Objects
 * @param test
 * @throws SearchServiceException
 */
public static void sendEmail(Researcher researcher, Context context, EPerson eperson, List<String> rpkeys,
        boolean test, List<String> relationFields)
        throws IOException, MessagingException, SQLException, SearchServiceException {

    CrisSearchService searchService = researcher.getCrisSearchService();

    // Get a resource bundle according to the eperson language preferences
    Locale supportedLocale = I18nUtil.getEPersonLocale(eperson);

    StringBuffer emailText = new StringBuffer();
    boolean isFirst = true;

    for (String rpkey : rpkeys) {
        SolrQuery query = new SolrQuery();
        query.setFields("search.resourceid");
        query.addFilterQuery("{!field f=search.resourcetype}" + Constants.ITEM, "{!field f=inarchive}true");

        for (String tmpRelations : relationFields) {
            String fq = "{!field f=" + tmpRelations + "}" + rpkey;
            query.addFilterQuery(fq);
        }

        query.setRows(Integer.MAX_VALUE);

        if (ConfigurationManager.getBooleanProperty("eperson.subscription.onlynew", false)) {
            // get only the items archived yesterday
            query.setQuery("dateaccessioned:(NOW/DAY-1DAY)");
        } else {
            // get all item modified yesterday but not published the day
            // before
            // and all the item modified today and archived yesterday
            query.setQuery(
                    "(item.lastmodified:(NOW/DAY-1DAY) AND dateaccessioned:(NOW/DAY-1DAY)) OR ((item.lastmodified:(NOW/DAY) AND dateaccessioned:(NOW/DAY-1DAY)))");
        }

        QueryResponse qResponse = searchService.search(query);
        SolrDocumentList results = qResponse.getResults();

        // Only add to buffer if there are new items
        if (results.getNumFound() > 0)

        {
            if (!isFirst) {
                emailText.append("\n---------------------------------------\n");
            } else {
                isFirst = false;
            }

            emailText.append(I18nUtil.getMessage("org.dspace.eperson.Subscribe.new-items", supportedLocale))
                    .append(" ").append(rpkey).append(": ").append(results.getNumFound()).append("\n\n");

            for (SolrDocument solrDoc : results)

            {
                Item item = Item.find(context, (Integer) solrDoc.getFieldValue("search.resourceid"));

                DCValue[] titles = item.getDC("title", null, Item.ANY);
                emailText.append("      ")
                        .append(I18nUtil.getMessage("org.dspace.eperson.Subscribe.title", supportedLocale))
                        .append(" ");

                if (titles.length > 0) {
                    emailText.append(titles[0].value);
                } else {
                    emailText.append(
                            I18nUtil.getMessage("org.dspace.eperson.Subscribe.untitled", supportedLocale));
                }

                DCValue[] authors = item.getDC("contributor", Item.ANY, Item.ANY);

                if (authors.length > 0) {
                    emailText.append("\n    ").append(
                            I18nUtil.getMessage("org.dspace.eperson.Subscribe.authors", supportedLocale))
                            .append(" ").append(authors[0].value);

                    for (int k = 1; k < authors.length; k++) {
                        emailText.append("\n             ").append(authors[k].value);
                    }
                }

                emailText.append("\n         ")
                        .append(I18nUtil.getMessage("org.dspace.eperson.Subscribe.id", supportedLocale))
                        .append(" ").append(HandleManager.getCanonicalForm(item.getHandle())).append("\n\n");
                context.removeCached(item, item.getID());
            }
        }
    }

    // Send an e-mail if there were any new items
    if (emailText.length() > 0) {

        if (test) {
            log.info(LogManager.getHeader(context, "subscription:", "eperson=" + eperson.getEmail()));
            log.info(LogManager.getHeader(context, "subscription:", "text=" + emailText.toString()));

        } else {

            Email email = Email.getEmail(I18nUtil.getEmailFilename(supportedLocale, "subscription"));
            email.addRecipient(eperson.getEmail());
            email.addArgument(emailText.toString());
            email.send();

            log.info(LogManager.getHeader(context, "sent_subscription", "eperson_id=" + eperson.getID()));

        }
    }
}

From source file:org.dspace.app.cris.network.AVisualizationGraph.java

protected String getDepartmentFromSOLR(String a_authority) throws SearchServiceException {
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery("search.resourceid:"
            + ResearcherPageUtils.getRealPersistentIdentifier(a_authority, ResearcherPage.class)
            + " AND search.resourcetype:" + CrisConstants.RP_TYPE_ID);
    solrQuery.setFields("rp_dept");
    solrQuery.setRows(1);//  w  ww. jav a2 s  .c  o  m
    QueryResponse rsp = getService().getSearcher().search(solrQuery);
    SolrDocumentList publications = rsp.getResults();

    Iterator<SolrDocument> iter = publications.iterator();

    String rp_dept = "";
    while (iter.hasNext()) {
        SolrDocument publication = iter.next();

        rp_dept = (String) publication.getFirstValue("rp_dept");
        break;
    }

    return rp_dept;
}

From source file:org.dspace.app.cris.network.AVisualizationGraph.java

protected String getStatusFromSOLR(String a_authority) throws SearchServiceException {
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery("search.resourceid:"
            + ResearcherPageUtils.getRealPersistentIdentifier(a_authority, ResearcherPage.class)
            + " AND search.resourcetype:" + CrisConstants.RP_TYPE_ID);
    solrQuery.setFields("rp_boolean_status");
    solrQuery.setRows(1);/* ww  w. j  a v a 2 s  .  co m*/
    QueryResponse rsp = getService().getSearcher().search(solrQuery);
    SolrDocumentList publications = rsp.getResults();

    Iterator<SolrDocument> iter = publications.iterator();

    String rp_status = "";
    while (iter.hasNext()) {
        SolrDocument publication = iter.next();

        rp_status = (String) publication.getFirstValue("rp_boolean_status");
        break;
    }

    return rp_status;
}