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

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

Introduction

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

Prototype

public SolrQuery setRows(Integer rows) 

Source Link

Usage

From source file:eu.europeana.solr.SimpleCollectionTest2.java

License:Apache License

private static SolrDocumentList getFilteredResults(String type, String query) throws SolrServerException {
    System.out.println("query performed: " + query);
    SolrQuery q = new SolrQuery(query);
    q.set("debugQuery", "on");
    q.set("defType", "bm25f");
    q.set("fl", "*,score");
    q.set("fq", "description:" + type);
    q.setRows(10);
    QueryResponse qr = instance.query(q);
    // Map<String, String> explainmap = qr.getExplainMap();
    return qr.getResults();
}

From source file:eu.europeana.solr.SolrServerTester.java

License:Apache License

public static void main(String[] args) throws SolrServerException, IOException {
    SolrServerTester tester = new SolrServerTester();
    tester.setSolrdir(new File(new File(new File(new File("src"), "test"), "resources"), "solr/" + CORE1));
    SolrServerIndexer indexer = new SolrServerIndexer();
    indexer.index(tester);/*from   ww w.j a  v  a 2 s . c o  m*/
    tester.commit();

    SolrQuery q = new SolrQuery("Watermark");
    q.set("debugQuery", "on");
    q.set("defType", "bm25f");
    q.set("qf", "title text");

    q.setRows(10); // don't actually request any data

    QueryResponse qr = tester.query(q);
    Map<String, String> explainmap = qr.getExplainMap();
    System.out.println("results " + qr.getResults().getNumFound());
    for (SolrDocument doc : qr.getResults()) {
        System.out.println("Title: " + doc.getFieldValue("title"));
        System.out.println("Expl: " + explainmap.get(doc.getFieldValue("europeana_id")));
    }

    tester.close();
}

From source file:eu.europeana.suggest.QueryCompletionClient.java

License:Apache License

public Response getCompletions(String query, int n, String language, boolean addExplain)
        throws SolrServerException {

    if (language == null || language.isEmpty()) {
        logger.info("no language for query {}, using default English. ", language, query);
        return getCompletions(query, n);
    }//  ww w  .ja va 2 s .co  m

    language = language.toLowerCase();

    if (!language.contains(language)) {
        logger.info("unknown language {} for query {}, using default English. ", language, query);
        language = "en";
    }

    SolrQuery sq = new SolrQuery(query);
    if (language.equals("en")) {
        sq.setRequestHandler("/suggest");
    } else {
        sq.setRequestHandler("/" + language + "_suggest");
    }
    // set the results number
    sq.setRows(n);
    if (addExplain) {
        // if requested add the explain (how the score is computed) to the
        // results, used in the logging
        sq.setFields("*", "[explain]");
    }
    // send the query to the server, each document returned is an entity
    QueryResponse qr = server.query(sq);
    SolrDocumentList sdl = qr.getResults();

    Response result = new Response(query);

    result.setItemCount(n);
    result.setTotalResults(sdl.getNumFound());
    result.setLanguage(language);

    List<Suggestion> annotations = new ArrayList<Suggestion>();
    for (SolrDocument sd : sdl) {
        Suggestion annotation = new Suggestion();
        String enTitle = (decode((String) sd.get("en_title")));
        // short titles usually are not really relevant and have high
        // ranking since they are ambigous.. remove them :)
        if (enTitle.length() <= 5)
            continue;
        int wikipediaClicks = ((Integer) sd.get("wikipedia_clicks"));
        int europeanaDf = ((Integer) sd.get("europeana_df"));
        int enrichment = ((Integer) sd.get("enrichment"));
        String type = (decode((String) sd.get("type"))).toLowerCase();
        annotation.setType(type);
        annotation.addPrefLabel("en", enTitle);
        annotation.setEuropeana_df(europeanaDf);
        annotation.setWikipedia_clicks(wikipediaClicks);
        annotation.setEnrichment(enrichment);
        if (addExplain) {
            annotation.setExplain((String) sd.get("[explain]"));
        }

        for (String lang : languages) {
            String label = decode((String) sd.get(lang + "_title"));

            if (label == null || label.isEmpty())
                continue;
            // if (label == null || label.isEmpty()) {
            // label = enTitle;
            //
            // }
            annotation.addPrefLabel(lang, label);
        }
        annotation.setUri((String) sd.get("uri"));
        annotation.setImage(((String) sd.get("uri")).replace("http://dbpedia.org/resource/",
                "http://wikiname2image.herokuapp.com/"));
        annotation.setUri(annotation.getUri().replace("http://dbpedia.org/resource/",
                "http://data.europeana.eu/" + type + "/"));
        annotation.setSearch("entity:" + annotation.getUri());
        annotations.add(annotation);
    }
    result.setSuggestions(annotations);

    return result;

}

From source file:eu.europeana.web.timeline.server.SolrServiceImpl.java

License:EUPL

@Override
public List<Year> retrieveYears() {
    List<Year> years = new ArrayList<Year>();
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery("*:*");
    solrQuery.setFacet(true);/*from   w w  w .j  ava 2s  . c  om*/
    solrQuery.addFacetField("YEAR");
    solrQuery.setRows(0);
    solrQuery.setFacetLimit(20);
    solrQuery.setFacetSort(FacetParams.FACET_SORT);
    solrQuery.setSortField("YEAR", SolrQuery.ORDER.desc);
    solrQuery.setFilterQueries("YEAR:[* TO *]", "-YEAR:0000");
    LOG.fine(String.format("Query : %s", solrQuery));
    QueryResponse response = null;
    try {
        response = solrServer.query(solrQuery);

        List<FacetField> facetFields = response.getFacetFields();
        for (FacetField field : facetFields) {
            for (FacetField.Count count : field.getValues()) {
                Year year = new Year();
                year.setYear(count.getName());
                year.setAvailableItemCount(count.getCount());
                if (count.getCount() > 0) {
                    years.add(year);
                }
            }
        }
    } catch (SolrServerException e) {
        LOG.severe(String.format("Error during Solr query for years : %s", e));
    }
    return response == null ? null : years;
}

From source file:eu.europeana.web.timeline.server.SolrServiceImpl.java

License:EUPL

@Override
public List<Item> retrieveBriefItems(String query, Integer offset, Integer limit) {
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery(query);// w  w w . j a  va  2s.  c o m
    solrQuery.setParam("fl", "title,YEAR,TYPE,europeana_uri,europeana_object,dc_creator"); // todo: some of these fields are unused
    solrQuery.setFilterQueries("YEAR:[* TO *]", "title:[* TO *]", "-YEAR:0000");
    solrQuery.setRows(limit);
    solrQuery.setStart(offset);
    QueryResponse response = null;
    try {
        response = solrServer.query(solrQuery);
        LOG.fine(String.format("Query : %s Offset : %d Limit %d", query, offset, limit));
    } catch (SolrServerException e) {
        LOG.severe(String.format("Error during Solr query for items: %s", e));
    }
    if (response != null) {
        return response.getBeans(Item.class);
    } else {
        return null;
    }
}

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

@Override
protected Map<String, Object> search(String searchTerms, String filterString, int from, int size) {
    Map<String, Object> data = new HashMap<>();
    // put "session" parameters here rightaway so it can be used in template even when empty result
    data.put("search_terms", searchTerms == null ? "*:*" : searchTerms);
    data.put("filter_terms", filterString == null ? "" : filterString);

    Stopwatch stopwatch = Stopwatch.createStarted();

    try {//from  w w  w  .  j a v a2  s .co m
        SolrQuery query = new SolrQuery();

        query.setQuery(searchTerms);
        query.setStart(from == -1 ? 0 : from);
        query.setRows(size);
        query.setFields("id", "title", "description", "thumbnail_s", "status_s", "score"); // "exclude" xmldoc
        query.setParam("qt", "edismax"); // probably default already

        // boosting
        query.setParam("qf", "title^10 description status^2 keywords");

        // set highlight, see also https://cwiki.apache.org/confluence/display/solr/Standard+Highlighter
        query.setHighlight(true).setHighlightSnippets(17).setHighlightFragsize(0); // http://wiki.apache.org/solr/HighlightingParameters
        query.setParam("hl.preserveMulti", "true"); // preserve non-matching keywords
        query.setParam("hl.fl", "id", "title", "description", "keywords"); // "*"); // select fields to highlight
        // override defaults:
        query.setParam("hl.simple.pre", "<em><strong>");
        query.setParam("hl.simple.post", "</strong></em>");

        // configure faceting, see also http://wiki.apache.org/solr/SolrFacetingOverview and http://wiki.apache.org/solr/Solrj and https://wiki.apache.org/solr/SimpleFacetParameters and 
        query.setFacet(true).setFacetLimit(4).setFacetMissing(true);
        // not in API, probably normally set in schema.xml:
        query.setParam("facet.field", "satellite_s", "instrument_s", "category", "societalBenefitArea_ss",
                "distribution_ss");

        // filtering
        Set<String> hiddenFacets = new HashSet<>(); // hiding no facets yet
        if (filterString != null && !filterString.isEmpty()) {
            Multimap<String, String> filterTermsMap = parseFiltersTerms(filterString);

            if (filterTermsMap.size() > 0) {
                for (Map.Entry<String, String> entry : filterTermsMap.entries()) {
                    String filter = " +" + entry.getKey() + ":" + entry.getValue();
                    query.addFilterQuery(filter);

                    hiddenFacets.add(entry.getKey() + ":" + entry.getValue());
                }
            }
        }
        data.put("tohide", hiddenFacets);

        log.debug("Solr query: {}", query);
        QueryResponse response = solr.query(query);

        if (response == null) {
            log.error("Response from {} is null!", this.name);
            data.put("total_hits", 0);
            data = addMessage(data, MessageLevel.danger, "Response is null from " + this.name);
        } else {
            log.trace("Got response: {}", response);

            if (response.getStatus() == 0) {
                List<Map<String, Object>> resHits = new ArrayList<>();
                SolrDocumentList results = response.getResults();
                Map<String, Map<String, List<String>>> highlights = response.getHighlighting();

                data.put("total_hits", results.getNumFound());
                if (results.getNumFound() < 1) {
                    addMessage(data, MessageLevel.info, "No results found!");
                }

                data.put("max_score", results.getMaxScore());
                Map<String, Object> pagination = computePaginationParams(
                        ((Long) (data.get("total_hits"))).intValue(), from);
                data.put("pagination", pagination);

                for (SolrDocument result : results) {
                    HashMap<String, Object> resHit = new HashMap<>();

                    String currentId = (String) result.getFieldValue("id");
                    Map<String, List<String>> currentHighlights = highlights.get(currentId);
                    resHit.put("id", currentId);
                    resHit.put("score", String.format("%.4g", result.getFieldValue("score")));

                    resHit.put("abstract", hightlightIfGiven(result, currentHighlights, "description"));

                    resHit.put("title", hightlightIfGiven(result, currentHighlights, "title"));
                    resHit.put("keywords", Joiner.on(", ").join(
                            (Collection<String>) hightlightIfGiven(result, currentHighlights, "keywords")));
                    resHit.put("satellite", result.get("satellite_s"));
                    resHit.put("thumbnail", result.get("thumbnail_s"));
                    resHit.put("status", result.get("status_s"));
                    resHit.put("distribution", result.get("distribution_ss"));

                    resHits.add(resHit);
                }

                data.put("hits", resHits);

                // faceting information:
                List<FacetField> facets = response.getFacetFields();
                log.trace("Facets ({}): {}", facets.size(), facets);

                //jsObj.get("facets").get("categories").get("terms") - then term und count
                // convert to format of Elasticsearch:
                Map<String, Object> facetsJson = new HashMap<>();
                for (FacetField facet : facets) {
                    Map<String, Object> facetMap = new HashMap<>();
                    facetMap.put("total", facet.getValueCount());
                    List<Map<String, Object>> terms = new ArrayList<>();
                    for (Count count : facet.getValues()) {
                        if (count.getCount() > 0) {
                            Map<String, Object> termMap = new HashMap<>();
                            termMap.put("count", count.getCount());
                            termMap.put("term", count.getName() == null ? "N/A" : count.getName());
                            terms.add(termMap);
                        }
                    }
                    facetMap.put("terms", terms);
                    facetsJson.put(facet.getName(), facetMap);
                }
                data.put("facets", facetsJson);
            } else { // non-OK resonse
                log.error("Received non-200 response: {}", response);
                data = addMessage(data, MessageLevel.danger, "Non 200 response: " + response.toString());
            }
        }

        data.put("elapsed", (double) (stopwatch.elapsed(TimeUnit.MILLISECONDS)) / (double) 1000);
        log.trace("Prepared data for template: {}", data);
    } catch (SolrServerException e) {
        log.error("Error querying Solr", e);
        addMessage(data, MessageLevel.danger, "Error during search: " + e.getMessage());
        //            errorResponse(e);
    }

    stopwatch.stop();

    return data;
}

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

License:EUPL

public List<OrganisaatioPerustieto> searchExact(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);//from w  ww.jav  a2  s  .  c  om

    // max rows to return
    q.setRows(10000);

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

        final SolrDocumentToOrganisaatioPerustietoTypeFunction converter = new SolrDocumentToOrganisaatioPerustietoTypeFunction(
                null);

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

        LOG.debug("Total time :{} ms. Results :{}", (System.currentTimeMillis() - time),
                response.getResults().getNumFound());
        return result;
    } catch (SolrServerException e) {
        throw new RuntimeException(e);
    }

}

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);/*from w w  w.  j  a v a2 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

/**
 * Search Organisations by oid//from   w  w  w  .ja v  a  2s .  c  o m
 *
 * @param organisationOids
 * @return
 */
public List<OrganisaatioPerustieto> findByOidSet(Set<String> organisationOids) {

    if (organisationOids.isEmpty()) {
        return Collections.EMPTY_LIST;
    }

    SolrQuery q = new SolrQuery(
            String.format(SolrOrgFields.OID + ":(%s)", Joiner.on(" ").join(organisationOids)));
    q.setRows(organisationOids.size());

    final SolrDocumentToOrganisaatioPerustietoTypeFunction converter = new SolrDocumentToOrganisaatioPerustietoTypeFunction(
            null);

    try {
        List<OrganisaatioPerustieto> result = Lists
                .newArrayList(Lists.transform(solr.query(q, METHOD.POST).getResults(), converter));
        return result;
    } catch (SolrServerException e) {
        throw new RuntimeException(e);
    }

}

From source file:fr.aliacom.obm.common.contact.ContactDao.java

License:Open Source License

private List<Contact> searchContact(AccessToken at, List<AddressBook> addrBooks, Connection con, String querys,
        int limit) throws MalformedURLException, SQLException {
    List<Contact> ret = new LinkedList<Contact>();
    Set<Integer> evtIds = new HashSet<Integer>();

    PreparedStatement ps = null;//  w  ww .j a v a2  s. c  om
    ResultSet rs = null;
    try {
        if (addrBooks.size() > 0) {
            SolrHelper solrHelper = solrHelperFactory.createClient(at);
            CommonsHttpSolrServer solrServer = solrHelper.getSolrContact();
            StringBuilder sb = new StringBuilder();
            sb.append("-is:archive ");
            sb.append("+addressbookId:(");
            int idx = 0;
            for (AddressBook book : addrBooks) {
                if (idx > 0) {
                    sb.append(" OR ");
                }
                sb.append(book.getUid());
                idx++;
            }
            sb.append(")");
            if (querys != null && !"".equals(querys)) {
                sb.append(" +(displayname:(");
                sb.append(querys.toLowerCase());
                sb.append("*) OR firstname:(");
                sb.append(querys.toLowerCase());
                sb.append("*) OR lastname:(");
                sb.append(querys.toLowerCase());
                sb.append("*) OR email:(");
                sb.append(querys.toLowerCase());
                sb.append("*))");
            }
            SolrQuery params = new SolrQuery();
            params.setQuery(sb.toString());
            params.setIncludeScore(true);
            params.setRows(limit);

            try {
                QueryResponse resp = solrServer.query(params);

                SolrDocumentList results = resp.getResults();
                if (logger.isDebugEnabled()) {
                    logger.debug("SOLR query time for " + results.size() + " results: " + resp.getElapsedTime()
                            + "ms.");
                }

                for (int i = 0; i < limit && i < results.size(); i++) {
                    SolrDocument doc = results.get(i);
                    Map<String, Object> payload = doc.getFieldValueMap();
                    evtIds.add((Integer) payload.get("id"));
                }
            } catch (SolrServerException e) {
                logger.error("Error querying server for '" + sb.toString() + " url: "
                        + ClientUtils.toQueryString(params, false), e);
            }
        }

        IntegerSQLCollectionHelper eventIds = new IntegerSQLCollectionHelper(evtIds);
        String q = "SELECT " + CONTACT_SELECT_FIELDS + ", now() as last_sync FROM Contact, ContactEntity WHERE "
                + "contactentity_contact_id=contact_id AND contact_archive != 1 AND contact_id IN ("
                + eventIds.asPlaceHolders() + ")";

        ps = con.prepareStatement(q);
        eventIds.insertValues(ps, 1);
        rs = ps.executeQuery();
        Map<Integer, Contact> entityContact = new HashMap<Integer, Contact>();

        int i = 0;
        while (rs.next() && i < limit) {
            int entity = rs.getInt("contactentity_entity_id");
            if (!entityContact.containsKey(entity)) {
                Contact ct = contactFromCursor(rs);
                entityContact.put(ct.getEntityId(), ct);
                ret.add(ct);
                i++;
            }
        }
        rs.close();
        rs = null;

        if (!entityContact.isEmpty()) {
            loadPhones(con, entityContact);
            loadIMIdentifiers(con, entityContact);
            loadWebsites(con, entityContact);
            loadAddresses(at, con, entityContact);
            loadEmails(con, entityContact);
            loadBirthday(con, entityContact);
            loadAnniversary(con, entityContact);
        }

    } finally {
        obmHelper.cleanup(null, ps, rs);
    }
    return ret;
}