Example usage for org.apache.solr.common SolrDocumentList getNumFound

List of usage examples for org.apache.solr.common SolrDocumentList getNumFound

Introduction

In this page you can find the example usage for org.apache.solr.common SolrDocumentList getNumFound.

Prototype

public long getNumFound() 

Source Link

Usage

From source file:edu.unc.lib.dl.data.ingest.solr.action.DeleteSolrTreeTest.java

License:Apache License

@Test
public void deleteTree() throws Exception {

    when(metadata.getId()).thenReturn("uuid:2");
    when(metadata.getAncestorPath()).thenReturn(Arrays.asList("1,uuid:1,Collections"));
    when(metadata.getResourceType()).thenReturn("Collection");
    CutoffFacet path = mock(CutoffFacet.class);
    when(path.getSearchValue()).thenReturn("2,uuid:2");
    when(metadata.getPath()).thenReturn(path);

    action.performAction(new SolrUpdateRequest("uuid:2", IndexingActionType.DELETE_SOLR_TREE));
    server.commit();/*w w  w.j a  va  2s .  c o m*/

    SolrDocumentList docListAfter = getDocumentList();

    assertEquals(2, docListAfter.getNumFound());

    for (SolrDocument docAfter : docListAfter) {
        String id = (String) docAfter.getFieldValue("id");
        if ("uuid:2".equals(id) || "uuid:6".equals(id))
            fail("Object was not deleted: " + id);
    }
}

From source file:edu.unc.lib.dl.data.ingest.solr.action.DeleteSolrTreeTest.java

License:Apache License

@Test
public void deleteNonexistent() throws Exception {

    when(solrSearchService.getObjectById(any(SimpleIdRequest.class))).thenReturn(null);

    action.performAction(new SolrUpdateRequest("uuid:doesnotexist", IndexingActionType.DELETE_SOLR_TREE));
    server.commit();//  w w  w.j a  v a2s .  c  o  m

    SolrDocumentList docListAfter = getDocumentList();

    assertEquals(4, docListAfter.getNumFound());
}

From source file:edu.unc.lib.dl.data.ingest.solr.action.DeleteSolrTreeTest.java

License:Apache License

@Test
public void deleteSimple() throws Exception {

    when(metadata.getResourceType()).thenReturn("File");

    action.performAction(new SolrUpdateRequest("uuid:6", IndexingActionType.DELETE_SOLR_TREE));
    server.commit();//from  w w w  . ja  v  a2s . c  o m

    SolrDocumentList docListAfter = getDocumentList();

    assertEquals("One object should have been removed", 3, docListAfter.getNumFound());

    for (SolrDocument docAfter : docListAfter) {
        String id = (String) docAfter.getFieldValue("id");
        if ("uuid:6".equals(id))
            fail("Object was not deleted: " + id);
    }
}

From source file:edu.unc.lib.dl.data.ingest.solr.action.DeleteSolrTreeTest.java

License:Apache License

@Test
public void deleteEverything() throws Exception {

    action.performAction(//from  w  w w .  jav a 2s.c o m
            new SolrUpdateRequest(AbstractIndexingAction.TARGET_ALL, IndexingActionType.DELETE_SOLR_TREE));
    server.commit();

    SolrDocumentList docListAfter = getDocumentList();

    assertEquals("Index should be empty", 0, docListAfter.getNumFound());
}

From source file:edu.unc.lib.dl.data.ingest.solr.action.IndexTreeInplaceActionTest.java

License:Apache License

@Test
public void verifyOrphanCleanup() throws Exception {

    when(metadata.getId()).thenReturn("uuid:2");
    when(metadata.getAncestorPath()).thenReturn(Arrays.asList("1,uuid:1,Collections"));
    when(path.getSearchValue()).thenReturn("2,uuid:2");

    SolrDocumentList docListBefore = getDocumentList();

    SolrUpdateRequest request = new SolrUpdateRequest("uuid:2", IndexingActionType.RECURSIVE_ADD);
    request.setStatus(ProcessingStatus.ACTIVE);

    action.performAction(request);/*from  w  w w. java 2s  . c  o m*/
    server.commit();

    SolrDocumentList docListAfter = getDocumentList();

    log.debug("Docs: " + docListBefore);
    log.debug("Docs: " + docListAfter);

    // Verify that the number of results has decreased
    assertEquals(6, docListBefore.getNumFound());
    assertEquals(5, docListAfter.getNumFound());

    // Verify that the orphan is not in the new result set
    for (SolrDocument docAfter : docListAfter) {
        String id = (String) docAfter.getFieldValue("id");
        assertFalse("uuid:5".equals(id));
    }

}

From source file:edu.unc.lib.dl.data.ingest.solr.action.IndexTreeInplaceActionTest.java

License:Apache License

@Test
public void testIndexAll() throws Exception {

    SolrDocumentList docListBefore = getDocumentList();

    SolrUpdateRequest request = new SolrUpdateRequest(UpdateTreeAction.TARGET_ALL,
            IndexingActionType.RECURSIVE_ADD);
    request.setStatus(ProcessingStatus.ACTIVE);

    action.performAction(request);/*w w w.j  a v  a  2s  .co  m*/
    server.commit();

    SolrDocumentList docListAfter = getDocumentList();

    log.debug("Docs: " + docListBefore);
    log.debug("Docs: " + docListAfter);

    // Verify that the number of results has decreased
    assertEquals(6, docListBefore.getNumFound());
    assertEquals(5, docListAfter.getNumFound());

    // Verify that the orphan is not in the new result set
    for (SolrDocument docAfter : docListAfter) {
        String id = (String) docAfter.getFieldValue("id");
        assertFalse("uuid:5".equals(id));
    }
}

From source file:eu.annocultor.converters.solr.SolrDocumentTagger.java

License:Apache License

public int tag() throws Exception {
    int recordsPassed = 0;
    for (int page = 0; true; page++) {
        int queryStart = page * DOCUMENTS_PER_READ;
        int queryEnd = queryStart + DOCUMENTS_PER_READ;
        if (queryEnd > start) {

            SolrQuery solrQuery = new SolrQuery(query);
            solrQuery.setStart(queryStart);
            solrQuery.setRows(DOCUMENTS_PER_READ);
            if (FIELDS_TO_EXTRACT.length > 0) {
                solrQuery.setFields(FIELDS_TO_EXTRACT);
            }//  www  .j  av  a  2s  . c  o m
            QueryResponse response = solrServerFrom.query(solrQuery);
            SolrDocumentList sourceDocs = response.getResults();
            System.out.println("retrieved document query OK: " + sourceDocs.getNumFound());
            if (sourceDocs.isEmpty() || page > MAX_PAGES_TO_TAG) {
                log.flush();
                report();
                return recordsPassed;
            }

            int retry = 0;
            while (retry < MAX_RETRIES) {
                try {
                    List<SolrInputDocument> destDocs = new ArrayList<SolrInputDocument>();
                    tagDocumentList(sourceDocs, destDocs);
                    recordsPassed += sourceDocs.size();
                    System.out.println("Let's try");
                    solrServerTo.add(destDocs);
                    log.println(new Date() + "Completed " + recordsPassed + " SOLR documents");
                    log.println("Passed " + originalWordCount + " original words and added "
                            + enrichmentWordCount + " enrichment words");
                    break;
                } catch (Exception e) {
                    e.printStackTrace();
                    log.println(new Date() + "Stopped at document " + recordsPassed);
                    log.println(new Date() + "Retry " + retry + " failed, keep trying");
                    log.flush();
                    Thread.sleep(60000 * retry);
                    retry++;
                } finally {
                    log.flush();
                }
            }

            if (retry >= MAX_RETRIES) {
                throw new Exception("Failed completely.");
            }
        } else {
            log.println("Skipping page " + page);
            log.flush();
        }
    }
}

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

License:EUPL

@Override
@SuppressWarnings({ "AccessingNonPublicFieldOfAnotherObject" })
public void initialize(Map<String, String[]> httpParameters, BreadcrumbFactory breadcrumbFactory, Locale locale,
        SolrQuery originalBriefSolrQuery, QueryModelFactory queryModelFactory, RecordDefinition metadataModel)
        throws SolrServerException, EuropeanaQueryException {
    this.query = originalBriefSolrQuery.getQuery();
    int fullDocUriInt = getFullDocInt(httpParameters, originalBriefSolrQuery);
    this.fullDocUriInt = fullDocUriInt;
    int solrStartRow = getSolrStart(fullDocUriInt);
    QueryResponse queryResponse = queryModelFactory.getPagingQueryResponse(originalBriefSolrQuery,
            httpParameters, solrStartRow);
    if (queryResponse.getResults() == null) {
        throw new EuropeanaQueryException("no results for this query"); // if no results are found return null to signify that docIdPage can be created.
    } else if (queryResponse.getResults().size() == 0) {
        throw new EuropeanaQueryException("no results for this query"); // if no results are found return null to signify that docIdPage can be created.
    }// w  ww.  j  a  va2s. c  o m
    List<? extends DocId> list = SolrBindingService.getDocIds(queryResponse);
    final SolrDocumentList response = queryResponse.getResults();
    this.breadcrumbs = breadcrumbFactory.createList(originalBriefSolrQuery, locale); // todo comment out
    int offset = (int) response.getStart();
    int numFound = (int) response.getNumFound();
    this.numFound = numFound;
    setNextAndPrevious(fullDocUriInt, list, offset, numFound);
    this.docIdWindow = new DocIdWindowImpl(list, offset, numFound);
    if (this.hasNext) {
        this.setNextFullDocUrl(httpParameters);
    }
    if (this.hasPrevious) {
        this.setPreviousFullDocUrl(httpParameters);
    }
}

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);
    }//from www.ja v a2s. com

    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: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 {/*w  w w .ja va2  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;
}