List of usage examples for org.apache.solr.common SolrDocumentList getNumFound
public long getNumFound()
From source file:nl.knaw.dans.common.solr.converter.SolrQueryResponseConverter.java
License:Apache License
public static SimpleSearchResult<Document> convert(QueryResponse queryResponse, Index index) { SimpleSearchResult<Document> result = new SimpleSearchResult<Document>(); Map<String, Map<String, List<String>>> hl = queryResponse.getHighlighting(); SolrDocumentList sdl = queryResponse.getResults(); // paging info result.setNumFound((int) sdl.getNumFound()); // Relevance scores in Solr are calculated from the base // of 1.0f. If any document is scored any different then // the maximum relevance score is not 1.0f anymore. The // chances of a maximum relevance score of 1.0f with actual // meaning is pretty slim. This therefore assumes that if // a maximum relevance score of 1.0f is returned that it // is then better for the receiver of the search results // to ignore the relevancy score completely. result.setUseRelevanceScore(sdl.getMaxScore() != 1.0f); // add the documents List<SearchHit<Document>> hits = new ArrayList<SearchHit<Document>>(sdl.size()); String primaryKeyValue = null; for (SolrDocument solrDoc : sdl) { // Don't change class type! SimpleSearchHit is assumed in SolrSearchEngine! SimpleDocument resultDoc = new SimpleDocument(); float score = 0; List<SnippetField> snippetFields = null; // copy all fields for (Entry<String, Object> fieldEntry : solrDoc.entrySet()) { if (index != null) { if (fieldEntry.getKey().equals(index.getPrimaryKey())) { primaryKeyValue = fieldEntry.getValue().toString(); }/*from w ww . j a v a 2 s . c o m*/ } if (fieldEntry.getKey().equals("score")) { score = ((Float) fieldEntry.getValue()).floatValue() / sdl.getMaxScore(); } else { SimpleField<Object> field = new SimpleField<Object>(fieldEntry.getKey(), fieldEntry.getValue()); resultDoc.addField(field); } } // add highlight info to SearchHit if (hl != null && primaryKeyValue != null) { Map<String, List<String>> hlMap = hl.get(primaryKeyValue); if (hlMap != null && hlMap.size() > 0) { snippetFields = new ArrayList<SnippetField>(hlMap.size()); for (Entry<String, List<String>> hlEntry : hlMap.entrySet()) { SimpleSnippetField snippetField = new SimpleSnippetField(hlEntry.getKey(), hlEntry.getValue()); snippetFields.add(snippetField); } } } SimpleSearchHit<Document> hit = new SimpleSearchHit<Document>(resultDoc); hit.setRelevanceScore(score); if (snippetFields != null) hit.setSnippets(snippetFields); hits.add(hit); } result.setHits(hits); // add facet fields to response List<org.apache.solr.client.solrj.response.FacetField> solrFacets = queryResponse.getFacetFields(); if (solrFacets != null) { List<FacetField> facetFields = new ArrayList<FacetField>(solrFacets.size()); for (org.apache.solr.client.solrj.response.FacetField solrFacet : solrFacets) { List<Count> solrFacetValues = solrFacet.getValues(); if (solrFacetValues == null) continue; List<FacetValue<?>> facetValues = new ArrayList<FacetValue<?>>(solrFacetValues.size()); for (Count solrFacetValue : solrFacetValues) { SimpleFacetValue<String> facetValue = new SimpleFacetValue<String>(); facetValue.setCount((int) solrFacetValue.getCount()); facetValue.setValue(solrFacetValue.getName()); facetValues.add(facetValue); } facetFields.add(new SimpleFacetField(solrFacet.getName(), facetValues)); } result.setFacets(facetFields); } return result; }
From source file:nl.knaw.huygens.facetedsearch.AbstractSolrServer.java
License:Open Source License
private Map<String, Object> getSearchData(ElaborateSearchParameters sp, String[] facetFields, SolrQuery query, String[] fieldsToReturn) throws IndexException { Map<String, Object> data = Maps.newHashMap(); data.put("term", query.getQuery()); try {//from ww w . j a v a 2s . co m Log.info("query=\n{}", query); QueryResponse response = server.query(query); Log.debug("response='{}'", response); SolrDocumentList documents = response.getResults(); data.put(KEY_NUMFOUND, documents.getNumFound()); Map<String, Map<String, List<String>>> highlighting = response.getHighlighting(); List<String> ids = Lists.newArrayList(); List<Map<String, Object>> results = Lists.newArrayList(); int occurrences = 0; for (SolrDocument document : documents) { String docId = document.getFieldValue(SolrFields.DOC_ID).toString(); ids.add(docId); Map<String, List<String>> map = (Map<String, List<String>>) ((highlighting == null) ? ImmutableMap.of() : highlighting.get(docId)); Map<String, Object> result = entryView(document, fieldsToReturn, map, sp.getTextFieldsToSearch()); results.add(result); for (Integer integer : ((Map<String, Integer>) result.get("terms")).values()) { occurrences += integer; } } data.put("ids", ids); data.put("results", results); data.put("occurrences", occurrences); data.put("facets", getFacetCountList(sp, facetFields, response)); } catch (SolrServerException e) { Log.error(e.getMessage()); throw new IndexException(e.getMessage()); } data.put("solrquery", query.toString()); return data; }
From source file:nl.knaw.huygens.timbuctoo.index.solr.SolrIndex.java
License:Open Source License
@Override public long getCount() throws IndexException { try {// ww w .j a va 2 s. c o m SolrDocumentList results = solrServer.search(COUNT_QUERY).getResults(); return results.getNumFound(); } catch (SolrServerException e) { throw new IndexException(e); } }
From source file:nl.knaw.huygens.timbuctoo.index.solr.SolrIndexTest.java
License:Open Source License
@Test public void testGetCount() throws SolrServerException, IndexException { // setup//from ww w . ja v a2 s .com QueryResponse queryResponseMock = mock(QueryResponse.class); SolrDocumentList resultsMock = mock(SolrDocumentList.class); long numFound = 42l; // when when(solrServerMock.search(SolrIndex.COUNT_QUERY)).thenReturn(queryResponseMock); when(queryResponseMock.getResults()).thenReturn(resultsMock); when(resultsMock.getNumFound()).thenReturn(numFound); // action long actualNumFound = instance.getCount(); // verify InOrder inOrder = inOrder(solrServerMock, resultsMock); inOrder.verify(solrServerMock).search(SolrIndex.COUNT_QUERY); inOrder.verify(resultsMock).getNumFound(); assertThat(actualNumFound, equalTo(numFound)); }
From source file:no.sesat.search.mode.command.SolrSearchCommand.java
License:Open Source License
@Override public ResultList<ResultItem> execute() { final ResultList<ResultItem> searchResult = null != facetToolkit ? new FacetedSearchResultImpl<ResultItem>() : new BasicResultList<ResultItem>(); try {/*from w ww. java2s . c o m*/ // set up query final SolrQuery query = new SolrQuery().setQuery(getTransformedQuery()).setStart(getOffset()) .setRows(getSearchConfiguration().getResultsToReturn()); modifyQuery(query); DUMP.info(query.toString()); // query final QueryResponse response = server.query(query); final SolrDocumentList docs = response.getResults(); searchResult.setHitCount((int) docs.getNumFound()); // iterate through docs for (SolrDocument doc : docs) { searchResult.addResult(createItem(doc)); } collectFacets(response, searchResult); } catch (SolrServerException ex) { LOG.error(ex.getMessage(), ex); } return searchResult; }
From source file:org.ala.dao.FulltextSearchDaoImplSolr.java
License:Open Source License
/** * Re-usable method for performing SOLR searches - takes SolrQuery input * * @param solrQuery// w w w . j a v a 2s . c om * @param filterQuery * @param pageSize * @param startIndex * @param sortField * @param sortDirection * @return * @throws SolrServerException */ private SearchResultsDTO doSolrQuery(SolrQuery solrQuery, String[] filterQuery, Integer pageSize, Integer startIndex, String sortField, String sortDirection) throws Exception { SearchResultsDTO searchResults = new SearchResultsDTO(); QueryResponse qr = getSolrQueryResponse(solrQuery, filterQuery, pageSize, startIndex, sortField, sortDirection); //process results SolrDocumentList sdl = qr.getResults(); List<FacetField> facets = qr.getFacetFields(); List<SearchDTO> results = new ArrayList<SearchDTO>(); List<FacetResultDTO> facetResults = new ArrayList<FacetResultDTO>(); searchResults.setTotalRecords(sdl.getNumFound()); searchResults.setStartIndex(sdl.getStart()); searchResults.setPageSize(pageSize); searchResults.setStatus("OK"); searchResults.setSort(sortField); searchResults.setDir(sortDirection); searchResults.setQuery(solrQuery.getQuery()); // populate SOLR search results if (!sdl.isEmpty()) { for (SolrDocument doc : sdl) { if (IndexedTypes.TAXON.toString().equalsIgnoreCase((String) doc.getFieldValue("idxtype"))) { results.add(createTaxonConceptFromIndex(qr, doc)); } else if (IndexedTypes.COLLECTION.toString() .equalsIgnoreCase((String) doc.getFieldValue("idxtype"))) { results.add(createCollectionFromIndex(qr, doc)); } else if (IndexedTypes.INSTITUTION.toString() .equalsIgnoreCase((String) doc.getFieldValue("idxtype"))) { results.add(createInstitutionFromIndex(qr, doc)); } else if (IndexedTypes.DATAPROVIDER.toString() .equalsIgnoreCase((String) doc.getFieldValue("idxtype"))) { results.add(createDataProviderFromIndex(qr, doc)); } else if (IndexedTypes.DATASET.toString() .equalsIgnoreCase((String) doc.getFieldValue("idxtype"))) { results.add(createDatasetFromIndex(qr, doc)); } else if (IndexedTypes.REGION.toString().equalsIgnoreCase((String) doc.getFieldValue("idxtype"))) { results.add(createRegionFromIndex(qr, doc)); } else if (IndexedTypes.WORDPRESS.toString() .equalsIgnoreCase((String) doc.getFieldValue("idxtype"))) { results.add(createWordpressFromIndex(qr, doc)); } else if (IndexedTypes.LAYERS.toString().equalsIgnoreCase((String) doc.getFieldValue("idxtype"))) { results.add(createLayerFromIndex(qr, doc)); } else { results.add(createSearchDTOFromIndex(qr, doc)); } } } else { logger.debug("No results for query."); } searchResults.setResults(results); // populate SOLR facet results if (facets != null) { for (FacetField facet : facets) { List<FacetField.Count> facetEntries = facet.getValues(); if ((facetEntries != null) && (facetEntries.size() > 0)) { ArrayList<FieldResultDTO> r = new ArrayList<FieldResultDTO>(); for (FacetField.Count fcount : facetEntries) { r.add(new FieldResultDTO(fcount.getName(), fcount.getCount())); } FacetResultDTO fr = new FacetResultDTO(facet.getName(), r); facetResults.add(fr); } } } searchResults.setFacetResults(facetResults); // The query result is stored in its original format so that all the information // returned is available later on if needed searchResults.setQr(qr); return searchResults; }
From source file:org.ala.dao.FulltextSearchDaoImplSolr.java
License:Open Source License
/** * Retrieves a simple count using the supplied query * /*from ww w. j a va 2 s . c o m*/ * @param query * @return * @throws SolrServerException */ private int doCountQuery(String query) throws Exception { logger.info("Count query: " + query); //do a query to retrieve a count SolrQuery solrQuery = new SolrQuery(); solrQuery.setQueryType("standard"); solrQuery.setFacet(false); solrQuery.setFacetMinCount(0); solrQuery.setFacetLimit(10000); solrQuery.setRows(0); solrQuery.setStart(0); solrQuery.setQuery(query); QueryResponse qr = solrUtils.getSolrServer().query(solrQuery); // can throw exception SolrDocumentList sdl = qr.getResults(); return (int) sdl.getNumFound(); }
From source file:org.alfresco.solr.query.DistributedAlfrescoSolrFingerPrintTest.java
License:Open Source License
@Test public void testFingerPrint() throws Exception { putHandleDefaults();/*from ww w . jav a 2s . co m*/ QueryResponse response = query(getDefaultTestClient(), true, "{\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}", params("q", "FINGERPRINT:" + NODES[0].getId(), "qt", "/afts", "shards.qt", "/afts", "start", "0", "fl", "DBID,score", "rows", "100")); SolrDocumentList docs = response.getResults(); assertEquals(4, docs.getNumFound()); SolrDocument doc0 = docs.get(0); long dbid0 = (long) doc0.getFieldValue("DBID"); assertEquals(dbid0, NODES[0].getId()); SolrDocument doc1 = docs.get(1); long dbid1 = (long) doc1.getFieldValue("DBID"); assertEquals(dbid1, NODES[2].getId()); SolrDocument doc2 = docs.get(2); long dbid2 = (long) doc2.getFieldValue("DBID"); assertEquals(dbid2, NODES[1].getId()); SolrDocument doc3 = docs.get(3); long dbid3 = (long) doc3.getFieldValue("DBID"); assertEquals(dbid3, NODES[3].getId()); }
From source file:org.alfresco.solr.query.DistributedAlfrescoSolrFingerPrintTest.java
License:Open Source License
@Test public void testFingerPrint2() throws Exception { putHandleDefaults();/*w ww . j av a2s . com*/ QueryResponse response = query(getDefaultTestClient(), true, "{\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}", params("q", "FINGERPRINT:" + NODES[0].getId() + "_70", "qt", "/afts", "shards.qt", "/afts", "start", "0", "fl", "DBID,score", "rows", "100")); SolrDocumentList docs = response.getResults(); assertEquals(2, docs.getNumFound()); SolrDocument doc0 = docs.get(0); long dbid0 = (long) doc0.getFieldValue("DBID"); assertEquals(dbid0, NODES[0].getId()); SolrDocument doc1 = docs.get(1); long dbid1 = (long) doc1.getFieldValue("DBID"); assertEquals(dbid1, NODES[2].getId()); }
From source file:org.alfresco.solr.query.DistributedAlfrescoSolrFingerPrintTest.java
License:Open Source License
@Test public void testFingerPrint3() throws Exception { putHandleDefaults();// ww w .j ava 2 s .c o m QueryResponse response = query(getDefaultTestClient(), true, "{\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}", params("q", "FINGERPRINT:" + NODES[0].getId() + "_45", "qt", "/afts", "shards.qt", "/afts", "start", "0", "fl", "DBID,score", "rows", "100")); SolrDocumentList docs = response.getResults(); assertEquals(3, docs.getNumFound()); SolrDocument doc0 = docs.get(0); long dbid0 = (long) doc0.getFieldValue("DBID"); assertEquals(dbid0, NODES[0].getId()); SolrDocument doc1 = docs.get(1); long dbid1 = (long) doc1.getFieldValue("DBID"); assertEquals(dbid1, NODES[2].getId()); SolrDocument doc2 = docs.get(2); long dbid2 = (long) doc2.getFieldValue("DBID"); assertEquals(dbid2, NODES[1].getId()); }