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:eu.europeana.core.BeanQueryModelFactory.java

License:EUPL

@Override
public QueryResponse getSolrResponse(SolrQuery solrQuery, boolean isBriefDoc, Map<String, String[]> params)
        throws EuropeanaQueryException { // add bean to ???
    // since we make a defensive copy before the start is decremented we must do it here
    if (solrQuery.getStart() != null && solrQuery.getStart() > 0) {
        solrQuery.setStart(solrQuery.getStart() - 1);
    }//  w w  w.j a  v  a2s .  c o  m
    // set facets
    if (isBriefDoc) {
        // only show spelling-suggestion on the first result page
        if ((solrQuery.getStart() == null || solrQuery.getStart() == 0)
                && solrQuery.getFilterQueries() == null) {
            // give spelling suggestions
            solrQuery.setParam("spellcheck", true);
            solrQuery.setParam("spellcheck.collate", true);
            solrQuery.setParam("spellcheck.extendedResults", true);
            solrQuery.setParam("spellcheck.onlyMorePopular", true);
            //                solrQuery.setParam("spellcheck.count", "4");
        }
        solrQuery.setFacet(true);
        solrQuery.setFacetMinCount(1);
        //solrQuery.setFacetLimit(100); solr default is 100 so doesn't need to be set explicitly
        if (solrQuery.getRows() == null) {
            solrQuery.setRows(12);
        }
        solrQuery.addFacetField(ThemeFilter.getTheme().getRecordDefinition().getFacetFieldStrings());
        // todo now hard-coded but these values must be retrieved from the RecordDefinition later
        if (solrQuery.getFields() == null) {
            solrQuery.setFields(
                    "europeana_uri,dc_title,europeana_object,dc_creator,europeana_year,europeana_provider,"
                            + "europeana_dataProvider,europeana_language,europeana_type,dc_description,dc_type");
            //            solrQuery.setFields("*,score");
            //            solrQuery.setFields(metadataModel.getRecordDefinition().getFieldStrings());
        }
        if (solrQuery.getQueryType().equalsIgnoreCase(QueryType.SIMPLE_QUERY.toString())) {
            solrQuery.setQueryType(queryAnalyzer
                    .findSolrQueryType(solrQuery.getQuery(), ThemeFilter.getTheme().getRecordDefinition())
                    .toString());
        }
    }
    SolrQuery dCopy = addHiddenQueryFilters(solrQuery, params);
    return getSolrResponseFromServer(dCopy, false);
}

From source file:eu.europeana.core.BeanQueryModelFactory.java

License:EUPL

@Override
public QueryResponse getPagingQueryResponse(SolrQuery solrQuery, Map<String, String[]> params, int solrStartRow)
        throws EuropeanaQueryException, SolrServerException {
    SolrQuery dCopy = addHiddenQueryFilters(solrQuery, params);
    dCopy.setFields("europeana_uri");
    dCopy.setStart(solrStartRow);/*  w  w w.  j a  va  2  s .com*/
    dCopy.setRows(3);
    //        this.breadcrumbs = Breadcrumb.createList(originalBriefSolrQuery); //todo decide for or queries
    return getSolrResponseFromServer(dCopy, true); // todo should this not be false
}

From source file:eu.europeana.core.querymodel.query.SolrQueryUtil.java

License:EUPL

public static SolrQuery createFromQueryParams(Map<String, String[]> params, QueryAnalyzer queryAnalyzer,
        Locale locale, RecordDefinition recordDefinition) throws EuropeanaQueryException {
    SolrQuery solrQuery = new SolrQuery();
    if (params.containsKey("query") || params.containsKey("query1")) {
        if (!params.containsKey("query")) { // support advanced search
            solrQuery.setQuery(queryAnalyzer.createAdvancedQuery(params, locale));
        } else {//from  w w  w .j a  va2s .  co  m
            if (params.containsKey("zoeken_in") && !params.get("zoeken_in")[0].equalsIgnoreCase("text")) {
                String zoekenIn = params.get("zoeken_in")[0];
                solrQuery.setQuery(zoekenIn + ":\""
                        + queryAnalyzer.sanitizeAndTranslate(params.get("query")[0], locale) + "\""); // only get the first one
            } else {
                solrQuery.setQuery(queryAnalyzer.sanitizeAndTranslate(params.get("query")[0], locale)); // only get the first one
            }
        }
    } else {
        throw new EuropeanaQueryException(QueryProblem.MALFORMED_QUERY.toString());
    }
    if (solrQuery.getQuery().trim().length() == 0) { // throw exception when no query is specified
        throw new EuropeanaQueryException(QueryProblem.MALFORMED_QUERY.toString());
    }
    if (params.containsKey("start")) {
        try {
            Integer start = Integer.valueOf(params.get("start")[0]);
            solrQuery.setStart(start);
        } catch (NumberFormatException e) {
            // if number exception is thrown take default setting 0 (hardening parameter handling)
        }
    }
    if (params.containsKey("rows")) {
        try {
            Integer rows = Integer.valueOf(params.get("rows")[0]);
            solrQuery.setRows(rows);
        } catch (NumberFormatException e) {
            // number exception is thrown take default setting 12 (hardening parameter handling)
        }
    }
    if (solrQuery.getQueryType() == null) {
        solrQuery.setQueryType(
                queryAnalyzer.findSolrQueryType(solrQuery.getQuery(), recordDefinition).toString());
    }

    // set sort field
    if (params.containsKey("sortBy") && !params.get("sortBy")[0].isEmpty()) {
        String sortField = params.get("sortBy")[0];
        if (sortField.equalsIgnoreCase("title")) {
            sortField = "sort_title";
        } else if (sortField.equalsIgnoreCase("creator")) {
            sortField = "sort_creator";
        } else if (sortField.equalsIgnoreCase("year")) {
            sortField = "sort_all_year";
        } else if (sortField.equalsIgnoreCase("random")) {
            sortField = SolrQueryUtil.createRandomSortKey();
        }

        if (params.containsKey("sortOrder") && !params.get("sortOrder")[0].isEmpty()) {
            String sortOrder = params.get("sortOrder")[0];
            if (sortOrder.equalsIgnoreCase("desc")) {
                solrQuery.setSortField(sortField, SolrQuery.ORDER.desc);
            }
        } else {
            solrQuery.setSortField(sortField, SolrQuery.ORDER.asc);
        }
    }

    //set constraints
    List<String> filterQueries = new ArrayList<String>();
    if (params.containsKey("qf")) {
        Collections.addAll(filterQueries, params.get("qf"));
    }
    if (params.containsKey("qf[]")) {
        Collections.addAll(filterQueries, params.get("qf[]"));
    }
    if (filterQueries != null) {
        for (String filterQuery : filterQueries) {
            solrQuery.addFilterQuery(filterQuery);
        }
    }
    // determine which fields will be returned
    if (params.containsKey("fl")) {
        String displayFields = params.get("fl")[0];
        if (!displayFields.isEmpty()) {
            solrQuery.setFields(displayFields);
        }
    }
    // find rq and add to filter queries
    if (params.containsKey("rq") && params.get("rq").length != 0) {
        String refineSearchFilterQuery = queryAnalyzer.createRefineSearchFilterQuery(params, locale);
        if (!refineSearchFilterQuery.isEmpty()) {
            solrQuery.addFilterQuery(refineSearchFilterQuery);
        }
    }
    solrQuery.setFilterQueries(SolrQueryUtil.getFilterQueriesAsPhrases(solrQuery));
    return solrQuery;
}

From source file:eu.europeana.querylog.learn.query.BM25FSolrResults.java

License:Apache License

public List<String> results(String q, int n) {
    SolrQuery query = new SolrQuery(q);
    query.setRows(n);//from   ww w .j  a  v a2s .c o  m
    query.setFields("europeana_id");
    return retrieveTopDocuments(query);
}

From source file:eu.europeana.querylog.learn.query.BM25FSolrResults.java

License:Apache License

@Override
public List<String> results(SolrQuery query, int n) {
    query.setRows(n);//w  w w. j  ava 2  s  .  c  om
    query.setFields("europeana_id");
    return retrieveTopDocuments(query);
}

From source file:eumetsat.pn.solr.webapp.SolrApp.java

@Override
protected Map<String, Object> describeProduct(String id) throws MalformedURLException, ParseException {
    Map<String, Object> data = new HashMap<>();
    //URL url = new URL(this.productEndpointUrlString + "?" + id);

    SolrQuery query = new SolrQuery();
    query.setRequestHandler(productEndpointUrlString);
    query.set("id", id);
    query.setFields("*");
    log.trace("Solr query: {}", query);

    try {/*from   w w  w .  j  av a 2s . c o  m*/
        QueryResponse response = solr.query(query);

        SolrDocument result = (SolrDocument) response.getResponse().get("doc");
        log.trace("Result document: {}", result);

        data.put("id", result.getFieldValue("id"));
        data.put("title", result.getFieldValue("title"));
        data.put("abstract", result.getFieldValue("description"));

        if (result.getFieldValue("thumbnail_s") != null) {
            data.put("thumbnail", result.getFieldValue("thumbnail_s"));
        }
        if (result.getFieldValue("status_s") != null) {
            data.put("status", result.getFieldValue("status_s"));
        }
        if (result.getFieldValue("satellite_s") != null) {
            data.put("satellite", result.get("satellite_s"));
        }
        if (result.getFieldValue("keywords") != null) {
            data.put("keywords", Joiner.on(", ").join((Collection<String>) result.get("keywords")));
        }
        if (result.getFieldValue("distribution_ss") != null) {
            data.put("distribution", Joiner.on(", ").join((Collection<String>) result.get("distribution_ss")));
        }
        if (result.getFieldValue("category") != null) {
            data.put("category", Joiner.on(", ").join((Collection<String>) result.getFieldValue("category")));
        }
        if (result.getFieldValue("instrument_s") != null) {
            data.put("instrument", result.getFieldValue("instrument_s"));
        }
        if (result.getFieldValue("boundingbox") != null) {
            data.put("boundingbox", result.getFieldValue("boundingbox"));
        }
        if (result.getFieldValue("address_s") != null) {
            data.put("address", result.getFieldValue("address_s"));
        }
        if (result.getFieldValue("email_s") != null) {
            data.put("email", result.getFieldValue("email_s"));
        }
        if (result.getFieldValue("societalBenefitArea_ss") != null) {
            data.put("sba",
                    Joiner.on(", ").join((Collection<String>) result.getFieldValue("societalBenefitArea_ss")));
        }
        if (result.getFieldValue("xmldoc") != null) {
            data.put("xmldoc", result.getFieldValue("xmldoc"));
        }
    } catch (SolrServerException e) {
        log.error("Error querying Solr", e);
        addMessage(data, MessageLevel.danger, "Error during search: " + e.getMessage());
        //            errorResponse(e);
    }
    return data;
}

From source file:fi.vm.sade.organisaatio.service.search.OrganisaatioSearchService.java

License:EUPL

public List<OrganisaatioPerustieto> searchHierarchy(final SearchCriteria searchCriteria) {
    long time = System.currentTimeMillis();
    final List<String> kunta = searchCriteria.getKunta();
    final List<String> restrictionList = searchCriteria.getOidRestrictionList();
    final String organisaatioTyyppi = searchCriteria.getOrganisaatioTyyppi();
    final List<String> kieli = searchCriteria.getKieli();
    String searchStr = searchCriteria.getSearchStr();
    String oid = searchCriteria.getOid();

    SolrQuery q = createOrgQuery(searchCriteria, kunta, restrictionList, organisaatioTyyppi, kieli, searchStr,
            oid);/*  w w  w  .  j a  va 2  s .  c o m*/

    q.set("fl", OID, PATH);
    // max rows to return
    q.setRows(10000);
    try {
        QueryResponse response = solr.query(q, METHOD.POST);

        LOG.debug("Sending query: " + q.getQuery() + ", filters: " + Joiner.on(" ").join(q.getFilterQueries()));
        LOG.debug("Search matched {} results, fetching docs...", response.getResults().getNumFound());
        if (response.getResults().getNumFound() == 0) {
            // short circuit no results here
            return Lists.newArrayList();
        }
        Set<String> oids = Sets.newHashSet();
        Set<String> paths = Sets.newHashSet();
        for (SolrDocument doc : response.getResults()) {
            if (!rootOrganisaatioOid.equals(doc.getFieldValue(OID))) {
                paths.add((String) doc.getFieldValue(OID));
            }

            if (!searchCriteria.getSkipParents()) {
                for (Object path : doc.getFieldValues(PATH)) {
                    if (!rootOrganisaatioOid.equals(path)) {
                        oids.add((String) path);
                    }
                }
            }
            oids.add((String) doc.getFirstValue(OID));
        }

        // get the actual docs
        q = new SolrQuery("*:*");
        q.setFields("*");
        addDateFilters(searchCriteria, q);

        // filter out oph (TODO do not index oph)
        q.addFilterQuery(String.format("-%s:%s", OID, rootOrganisaatioOid));

        // filter out types in upper hierarchy
        if (organisaatioTyyppi != null && organisaatioTyyppi.length() > 0) {
            final Set<String> limitToTypes = orgTypeLimit.get(organisaatioTyyppi);
            q.addFilterQuery(String.format("%s:(%s)", ORGANISAATIOTYYPPI, Joiner.on(" ").join(limitToTypes)));
        }

        // restrictions
        if (restrictionList.size() > 0) {
            // filter based on restriction list
            q.addFilterQuery(String.format("%s:(%s)", PATH, Joiner.on(" ").join(restrictionList)));
        }

        String query = String.format("%s:(%s)", OID, Joiner.on(" ").join(oids));
        if (paths.size() > 0) {
            query = query + String.format(" %s:(%s)", PATH, Joiner.on(" ").join(paths));
        }
        q.setQuery(query);
        q.setRows(10000);

        response = solr.query(q, METHOD.POST);

        LOG.debug("Search time :{} ms.", (System.currentTimeMillis() - time));

        final SolrDocumentToOrganisaatioPerustietoTypeFunction converter = new SolrDocumentToOrganisaatioPerustietoTypeFunction(
                oids);

        final List<OrganisaatioPerustieto> result = Lists
                .newArrayList(Lists.transform(response.getResults(), converter));

        LOG.debug("Total time :{} ms.", (System.currentTimeMillis() - time));
        return result;
    } catch (SolrServerException e) {
        LOG.error("Error executing search, q={}", q.getQuery());
        throw new RuntimeException(e);
    }
}

From source file:fi.vm.sade.organisaatio.service.search.OrganisaatioSearchService.java

License:EUPL

public List<String> findParentOids(String organisationOid) {
    final SolrQuery q = new SolrQuery(String.format(SolrOrgFields.OID + ":%s", organisationOid));
    q.setFields(SolrOrgFields.PATH);
    final List<String> oids = Lists.newArrayList();

    SolrDocumentList docList;//from  w  ww.j  a  va  2s.  c  om
    try {
        docList = solr.query(q).getResults();
        if (docList.getNumFound() == 1) {
            SolrDocument doc = docList.get(0);
            for (Object field : doc.getFieldValues(SolrOrgFields.PATH)) {
                if (!rootOrganisaatioOid.equals(field)) {
                    oids.add((String) field);
                }
            }
        }

    } catch (SolrServerException e) {
        throw new RuntimeException(e);
    }

    return oids;
}

From source file:fr.paris.lutece.plugins.search.solr.business.SolrSearchEngine.java

License:Open Source License

public String getDocumentHighLighting(String strDocumentId, String terms) {
    String strDocumentIdPrefixed = SolrIndexerService.getWebAppName() + SolrConstants.CONSTANT_UNDERSCORE
            + strDocumentId;// ww w .j a v a  2 s  . com
    String xmlContent = null;
    SolrClient solrServer = SolrServerService.getInstance().getSolrServer();
    SolrQuery query = new SolrQuery(terms);
    query.setHighlight(true);
    query.setHighlightSimplePre(SOLR_HIGHLIGHT_PRE);
    query.setHighlightSimplePost(SOLR_HIGHLIGHT_POST);
    query.setHighlightFragsize(0); //return all the content, not fragments
    query.setParam("hl.fl", SolrItem.FIELD_XML_CONTENT); //return only the field xml_content HighLighting
    query.setFields(SearchItem.FIELD_UID); //return only the field uid
    query.setRows(1);
    query.addFilterQuery(SearchItem.FIELD_UID + ":" + strDocumentIdPrefixed);

    try {
        QueryResponse response = solrServer.query(query);

        if (response.getResults().size() == 1) {
            SolrHighlights highlights = new SolrHighlights(response.getHighlighting());

            if (highlights.getHighlights(strDocumentIdPrefixed).getMap().size() > 0) {
                xmlContent = highlights.getHighlights(strDocumentIdPrefixed).getMap()
                        .get(SolrItem.FIELD_XML_CONTENT).get(0);
            }
        }
    } catch (SolrServerException e) {
        AppLogService.error(e.getMessage(), e);
    } catch (IOException e) {
        AppLogService.error(e.getMessage(), e);
    }

    return xmlContent;
}

From source file:net.peacesoft.nutch.crawl.RaovatPostSignature.java

License:Apache License

public void dedup(String solrUrl, boolean noCommit) throws IOException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    long start = System.currentTimeMillis();
    LOG.info("RaovatPostSignature: starting at " + sdf.format(start));
    LOG.info("RaovatPostSignature: Solr url: " + solrUrl);

    JobConf job = new NutchJob(getConf());

    job.set(ReSolrConstants.SERVER_URL, solrUrl);

    //        job.setBoolean("noCommit", noCommit);
    //        job.setInputFormat(RaovatPostSignature.SolrInputFormat.class);
    //        job.setOutputFormat(NullOutputFormat.class);
    //        job.setMapOutputKeyClass(Text.class);
    //        job.setMapOutputValueClass(RaovatPostSignature.SolrRecord.class);
    //        job.setMapperClass(IdentityMapper.class);
    //        job.setReducerClass(RaovatPostSignature.class);
    ///*from w w w  . ja  va  2 s  .com*/
    //        JobClient.runJob(job);

    solr = SolrUtils.getCommonsHttpSolrServer(job);

    SolrQuery solrQuery = new SolrQuery(SOLR_GET_ALL_QUERY);
    solrQuery.setFields(ReSolrConstants.ID_FIELD);
    solrQuery.setRows(1);

    QueryResponse response;
    try {
        response = solr.query(solrQuery);
    } catch (final SolrServerException e) {
        throw new IOException(e);
    }

    int numResults = (int) response.getResults().getNumFound();
    LOG.info("Total items:" + numResults);
    int s = 1;
    int value = 200;
    int page = numResults / value + 1;

    updateRequest = new UpdateRequest();

    for (int i = 0; i < page; i++) {
        solrQuery = new SolrQuery(SOLR_GET_ALL_QUERY);
        solrQuery.setStart(s);
        solrQuery.setRows(value);

        try {
            response = solr.query(solrQuery);
        } catch (final SolrServerException e) {
            throw new IOException(e);
        }

        SolrDocumentList solrDocs = response.getResults();
        for (SolrDocument solrDocument : solrDocs) {
            updateRequest.add(ClientUtils.toSolrInputDocument(solrDocument));
        }

        s += value;
    }

    try {
        solr.commit();
    } catch (SolrServerException ex) {
    }

    long end = System.currentTimeMillis();
    LOG.info("RaovatPostSignature: finished at " + sdf.format(end) + ", elapsed: "
            + TimingUtil.elapsedTime(start, end));
}