Java Code Examples for org.apache.solr.client.solrj.response.QueryResponse#getResults()
The following examples show how to use
org.apache.solr.client.solrj.response.QueryResponse#getResults() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonQueryRequestIntegrationTest.java From lucene-solr with Apache License 2.0 | 6 votes |
@Test public void canSpecifyFieldsToBeReturned() throws Exception { final JsonQueryRequest simpleQuery = new JsonQueryRequest() .setQuery("*:*") .returnFields("id", "name"); QueryResponse queryResponse = simpleQuery.process(cluster.getSolrClient(), COLLECTION_NAME); assertEquals(0, queryResponse.getStatus()); final SolrDocumentList docs = queryResponse.getResults(); assertEquals(NUM_BOOKS_TOTAL, docs.getNumFound()); for (SolrDocument returnedDoc : docs) { final Collection<String> fields = returnedDoc.getFieldNames(); assertEquals(2, fields.size()); assertTrue("Expected field list to contain 'id'", fields.contains("id")); assertTrue("Expected field list to contain 'name'", fields.contains("name")); } }
Example 2
Source File: TestTolerantUpdateProcessorRandomCloud.java From lucene-solr with Apache License 2.0 | 6 votes |
/** uses a Cursor to iterate over every doc in the index, recording the 'id_i' value in a BitSet */ private static final BitSet allDocs(final SolrClient c, final int maxDocIdExpected) throws Exception { BitSet docs = new BitSet(maxDocIdExpected+1); String cursorMark = CURSOR_MARK_START; int docsOnThisPage = Integer.MAX_VALUE; while (0 < docsOnThisPage) { final SolrParams p = params("q","*:*", "rows","100", // note: not numeric, but we don't actual care about the order "sort", "id asc", CURSOR_MARK_PARAM, cursorMark); QueryResponse rsp = c.query(p); cursorMark = rsp.getNextCursorMark(); docsOnThisPage = 0; for (SolrDocument doc : rsp.getResults()) { docsOnThisPage++; int id_i = ((Integer)doc.get("id_i")).intValue(); assertTrue("found id_i bigger then expected "+maxDocIdExpected+": " + id_i, id_i <= maxDocIdExpected); docs.set(id_i); } cursorMark = rsp.getNextCursorMark(); } return docs; }
Example 3
Source File: DistributedVersionInfoTest.java From lucene-solr with Apache License 2.0 | 6 votes |
protected long getVersionFromIndex(Replica replica, String docId) throws IOException, SolrServerException { Long vers = null; String queryStr = (docId != null) ? "id:" + docId : "_version_:[0 TO *]"; SolrQuery query = new SolrQuery(queryStr); query.setRows(1); query.setFields("id", "_version_"); query.addSort(new SolrQuery.SortClause("_version_", SolrQuery.ORDER.desc)); query.setParam("distrib", false); try (SolrClient client = getHttpSolrClient(replica.getCoreUrl())) { QueryResponse qr = client.query(query); SolrDocumentList hits = qr.getResults(); if (hits.isEmpty()) fail("No results returned from query: "+query); vers = (Long) hits.get(0).getFirstValue("_version_"); } if (vers == null) fail("Failed to get version using query " + query + " from " + replica.getCoreUrl()); return vers.longValue(); }
Example 4
Source File: SolrAPI.java From common-project with Apache License 2.0 | 6 votes |
/** * response解析对象 * @param queryResponse * @param tClass * @param <T> * @return */ public static <T> T parseObject(QueryResponse queryResponse, Class<T> tClass) { SolrDocumentList results = queryResponse.getResults(); SolrDocument document = results.get(0); Field[] fields = tClass.getDeclaredFields(); T instance = null; try { instance = tClass.newInstance(); for (Field field : fields) { String fieldName = field.getName(); Object value = document.get(fieldName); String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); Method declaredMethod = tClass.getDeclaredMethod(methodName, field.getType()); value = ClassUtil.parse(field.getType(), value); declaredMethod.invoke(instance, value); } } catch (Exception e) { logger.error("SolrAPI数据解析出错啦!------ " + e); } return instance; }
Example 5
Source File: TextIndexSolr5.java From BioSolr with Apache License 2.0 | 6 votes |
private SolrDocumentList solrQuery(String qs, int limit) { SolrQuery sq = new SolrQuery(qs) ; sq.setIncludeScore(true) ; if ( limit > 0 ) sq.setRows(limit) ; else sq.setRows(MAX_N) ; // The Solr default is 10. try { // Set default field. sq.add(CommonParams.DF, docDef.getPrimaryField()) ; QueryResponse rsp = solrClient.query(sq) ; SolrDocumentList docs = rsp.getResults() ; return docs ; } catch (SolrServerException | IOException e) { exception(e) ; return null ; } }
Example 6
Source File: CdcrVersionReplicationTest.java From lucene-solr with Apache License 2.0 | 6 votes |
void doRealTimeGet(String ids, String versions) throws Exception { Map<String, Object> expectedIds = new HashMap<>(); List<String> strs = StrUtils.splitSmart(ids, ",", true); List<String> verS = StrUtils.splitSmart(versions, ",", true); for (int i = 0; i < strs.size(); i++) { if (!verS.isEmpty()) { expectedIds.put(strs.get(i), Long.valueOf(verS.get(i))); } } QueryResponse rsp = solrServer.query(params("qt", "/get", "ids", ids)); Map<String, Object> obtainedIds = new HashMap<>(); for (SolrDocument doc : rsp.getResults()) { obtainedIds.put((String) doc.get("id"), doc.get(vfield)); } assertEquals(expectedIds, obtainedIds); }
Example 7
Source File: DocumentShrinker.java From thoth with BSD 3-Clause Clear License | 6 votes |
/** * Tag slower documents and add them to the shrank core */ private void tagAndAddSlowThothDocuments() throws IOException, SolrServerException { // Query to return top MAX_NUMBER_SLOW_THOTH_DOCS slower thoth documents QueryResponse qr = realTimeServer.query( new SolrQuery() .setQuery(createThothDocsAggregationQuery()) .addSort(QTIME, SolrQuery.ORDER.desc) .setRows(MAX_NUMBER_SLOW_THOTH_DOCS) ); for (SolrDocument solrDocument: qr.getResults()){ SolrInputDocument si = ClientUtils.toSolrInputDocument(solrDocument); // Remove old ID and version si.removeField(ID); si.removeField("_version_"); // Tag document as slow si.addField(SLOW_QUERY_DOCUMENT, true); LOG.debug("Adding slow query document for server " + serverDetail.getName()); shrankServer.add(si); } }
Example 8
Source File: AlfrescoFieldMapperTransformerIT.java From SearchServices with GNU Lesser General Public License v3.0 | 5 votes |
@Test public void transformDocument_docTransformerIsUsedWithOtherTransformer_shouldExecuteBothTranformers() throws Exception { putHandleDefaults(); QueryResponse resp = query(getDefaultTestClient(), true, ALFRESCO_JSON, params("q", "*", "qt", "/afts", "shards.qt", "/afts","fl","cm_title, [explain], [fmap]", "sort", "id asc")); assertNotNull(resp); SolrDocumentList results = resp.getResults(); SolrDocument docWithAllFields = results.get(0); assertEquals(2, docWithAllFields.size()); assertNotNull(docWithAllFields.get("cm_title")); assertNotNull(docWithAllFields.get("[explain]")); }
Example 9
Source File: SolrSearchResults.java From aem-solr-search with Apache License 2.0 | 5 votes |
SolrSearchResults(SolrQuery solrQuery, QueryResponse queryResponse) { this.solrQuery = solrQuery; this.queryResponse = queryResponse; if (null != queryResponse && null != queryResponse.getResponseHeader()) { // The NamedList thats returned by queryResponse.getResponseHeader() is not JSP friendly. // Convert it to a map to make it more accessible. final Map map = new LinkedHashMap(); final NamedList<?> namedList = queryResponse.getResponseHeader(); for (Map.Entry entry : namedList) { map.put(entry.getKey(), entry.getValue()); } responseHeaderMap = Collections.unmodifiableMap(map); } else { responseHeaderMap = Collections.emptyMap(); } if (null != queryResponse && null != queryResponse.getResults()) { final SolrDocumentList solrDocumentList = queryResponse.getResults(); final Long numFound = solrDocumentList.getNumFound(); final Long start = solrDocumentList.getStart(); solrDocumentListNumFound = numFound.intValue(); solrDocumentListStart = start.intValue(); solrDocumentListSize = solrDocumentList.size(); } else { solrDocumentListNumFound = 0; solrDocumentListStart = 0; solrDocumentListSize = 0; } cleanQueryAndResponseFacets(); }
Example 10
Source File: MCRClassificationEditorResource.java From mycore with GNU General Public License v3.0 | 5 votes |
@GET @Path("link/{id}") @Produces(MediaType.APPLICATION_JSON) public Response retrieveLinkedObjects(@PathParam("id") String id, @QueryParam("start") Integer start, @QueryParam("rows") Integer rows) throws SolrServerException, IOException { // do solr query SolrClient solrClient = MCRSolrClientFactory.getMainSolrClient(); ModifiableSolrParams params = new ModifiableSolrParams(); params.set("start", start != null ? start : 0); params.set("rows", rows != null ? rows : 50); params.set("fl", "id"); String configQuery = MCRConfiguration2.getString("MCR.Solr.linkQuery").orElse("category.top:{0}"); String query = new MessageFormat(configQuery, Locale.ROOT).format(new String[] { id.replaceAll(":", "\\\\:") }); params.set("q", query); QueryResponse solrResponse = solrClient.query(params); SolrDocumentList solrResults = solrResponse.getResults(); // build json response JsonObject response = new JsonObject(); response.addProperty("numFound", solrResults.getNumFound()); response.addProperty("start", solrResults.getStart()); JsonArray docList = new JsonArray(); for (SolrDocument doc : solrResults) { docList.add(new JsonPrimitive((String) doc.getFieldValue("id"))); } response.add("docs", docList); return Response.ok().entity(response.toString()).build(); }
Example 11
Source File: SolrEntityQueryMixin.java From attic-polygene-java with Apache License 2.0 | 5 votes |
@Override public SolrDocumentList search( String queryString ) throws SolrServerException { SolrServer server = solr.solrServer(); NamedList<Object> list = new NamedList<>(); list.add( "q", queryString ); QueryResponse query = server.query( SolrParams.toSolrParams( list ) ); return query.getResults(); }
Example 12
Source File: AtomicUpdateRemovalJavabinTest.java From lucene-solr with Apache License 2.0 | 5 votes |
private void ensureFieldHasValues(String identifyingDocId, String fieldName, Object... expectedValues) throws Exception { final SolrClient client = cluster.getSolrClient(); final QueryResponse response = client.query(COLLECTION, new SolrQuery("id:" + identifyingDocId)); final SolrDocumentList docs = response.getResults(); assertEquals(1, docs.getNumFound()); final Collection<Object> valuesAfterUpdate = docs.get(0).getFieldValues(fieldName); assertEquals(expectedValues.length, valuesAfterUpdate.size()); for (Object expectedValue: expectedValues) { assertTrue("Expected value [" + expectedValue + "] was not found in field", valuesAfterUpdate.contains(expectedValue)); } }
Example 13
Source File: Solr07TestUtil.java From datacollector with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") public List<Map<String,Object>> query(Map<String, String> q) throws Exception { SolrQuery parameters = new SolrQuery(); for(Map.Entry<String, String> entry : q.entrySet()) { parameters.set(entry.getKey(), entry.getValue()); } QueryResponse response = solrServer.query(parameters); List<SolrDocument> solrDocumentList = response.getResults(); List<Map<String, Object>> result = new ArrayList(); for(SolrDocument document : solrDocumentList) { result.add(document); } return result; }
Example 14
Source File: ServiceLogsManager.java From ambari-logsearch with Apache License 2.0 | 5 votes |
private Date getDocDateFromNextOrLastPage(ServiceLogRequest request, String keyword, boolean isNext, int currentPageNumber, int maxRows) { int lastOrFirstLogIndex; if (isNext) { lastOrFirstLogIndex = ((currentPageNumber + 1) * maxRows); } else { if (currentPageNumber == 0) { throw new NotFoundException("This is the first Page"); } lastOrFirstLogIndex = (currentPageNumber * maxRows) - 1; } SimpleQuery sq = conversionService.convert(request, SimpleQuery.class); SolrQuery nextPageLogTimeQuery = new DefaultQueryParser().doConstructSolrQuery(sq); nextPageLogTimeQuery.remove("start"); nextPageLogTimeQuery.remove("rows"); nextPageLogTimeQuery.setStart(lastOrFirstLogIndex); nextPageLogTimeQuery.setRows(1); QueryResponse queryResponse = serviceLogsSolrDao.process(nextPageLogTimeQuery); if (queryResponse == null) { throw new MalformedInputException(String.format("Cannot process next page query for \"%s\" ", keyword)); } SolrDocumentList docList = queryResponse.getResults(); if (docList == null || docList.isEmpty()) { throw new MalformedInputException(String.format("Next page element for \"%s\" is not found", keyword)); } SolrDocument solrDoc = docList.get(0); return (Date) solrDoc.get(LOGTIME); }
Example 15
Source File: AlfrescoFieldMapperTransformerIT.java From SearchServices with GNU Lesser General Public License v3.0 | 5 votes |
@Test public void transformDocument_docTransformerFieldsAndScoreRequested_shouldReturnScoreAndSelectedFields() throws Exception { putHandleDefaults(); QueryResponse resp = query(getDefaultTestClient(), true, ALFRESCO_JSON, params("q", "*", "qt", "/afts", "shards.qt", "/afts","fl","cm_title, cm_created, DBID, score, [fmap]", "sort", "id asc")); assertNotNull(resp); SolrDocumentList results = resp.getResults(); SolrDocument docWithAllFields = results.get(0); assertEquals(4, docWithAllFields.size()); assertNotNull(docWithAllFields.get("cm_title")); assertNotNull(docWithAllFields.get("cm_created")); assertNotNull(docWithAllFields.get("score")); assertNotNull(docWithAllFields.get("DBID")); }
Example 16
Source File: SearchITCase.java From apache-solr-essentials with Apache License 2.0 | 5 votes |
/** * Selects all documents using an handler configured with SolrQueryParser * * @throws Exception hopefully never, otherwise the test fails. */ @Test public void selectAll() throws Exception { // 1. Prepare the Query object // The query string can be directly injected in the constructor final SolrQuery query = new SolrQuery("*:*"); query.setRequestHandler("/h1"); // These settings will override the "defaults" section query.setFacet(false); query.setHighlight(false); query.setSort("released", ORDER.desc); // We are asking 5 documents per page query.setRows(5); // 2. Send the query request and get the corresponding response. final QueryResponse response = SEARCHER.query(query); // 3. Get the result object, containing documents and metadata. final SolrDocumentList documents = response.getResults(); // If not explicitly requested (or set in the handler) the start is set to 0 assertEquals(0, documents.getStart()); // Total number of documents found must be equals to all documents we previously indexed assertEquals(sampleData().size(), documents.getNumFound()); // Page size must be 5, as requested assertEquals(5, documents.size()); }
Example 17
Source File: TestRealTimeGet.java From incubator-sentry with Apache License 2.0 | 5 votes |
private void assertFl(CloudSolrServer server, String [] ids, Set<String> expectedIds, String fl, List<String> expectedFields) throws Exception { { QueryResponse idRsp = getIdResponse(new ExpectedResult(server, ids, expectedIds.size(), fl)); SolrDocumentList idList = idRsp.getResults(); assertFlOnDocList(idList, expectedIds, expectedFields); } { QueryResponse idsRsp = getIdsResponse(new ExpectedResult(server, ids, expectedIds.size(), fl)); SolrDocumentList idsList = idsRsp.getResults(); assertFlOnDocList(idsList, expectedIds, expectedFields); } }
Example 18
Source File: SolrDeleteDuplicates.java From anthelion with Apache License 2.0 | 4 votes |
public RecordReader<Text, SolrRecord> getRecordReader(final InputSplit split, final JobConf job, Reporter reporter) throws IOException { SolrServer solr = SolrUtils.getCommonsHttpSolrServer(job); SolrInputSplit solrSplit = (SolrInputSplit) split; final int numDocs = solrSplit.getNumDocs(); SolrQuery solrQuery = new SolrQuery(SOLR_GET_ALL_QUERY); solrQuery.setFields(SolrConstants.ID_FIELD, SolrConstants.BOOST_FIELD, SolrConstants.TIMESTAMP_FIELD, SolrConstants.DIGEST_FIELD); solrQuery.setStart(solrSplit.getDocBegin()); solrQuery.setRows(numDocs); QueryResponse response; try { response = solr.query(solrQuery); } catch (final SolrServerException e) { throw new IOException(e); } final SolrDocumentList solrDocs = response.getResults(); return new RecordReader<Text, SolrRecord>() { private int currentDoc = 0; public void close() throws IOException { } public Text createKey() { return new Text(); } public SolrRecord createValue() { return new SolrRecord(); } public long getPos() throws IOException { return currentDoc; } public float getProgress() throws IOException { return currentDoc / (float) numDocs; } public boolean next(Text key, SolrRecord value) throws IOException { if (currentDoc >= numDocs) { return false; } SolrDocument doc = solrDocs.get(currentDoc); String digest = (String) doc.getFieldValue(SolrConstants.DIGEST_FIELD); key.set(digest); value.readSolrDocument(doc); currentDoc++; return true; } }; }
Example 19
Source File: DistribCursorPagingTest.java From lucene-solr with Apache License 2.0 | 4 votes |
private SolrDocumentList extractDocList(QueryResponse rsp) { SolrDocumentList docs = rsp.getResults(); assertNotNull("docList is null", docs); return docs; }
Example 20
Source File: TestRandomCollapseQParserPlugin.java From lucene-solr with Apache License 2.0 | 4 votes |
public void testRandomCollpaseWithSort() throws Exception { final int numMainQueriesPerCollapseField = atLeast(5); for (String collapseField : ALL_COLLAPSE_FIELD_NAMES) { for (int i = 0; i < numMainQueriesPerCollapseField; i++) { final String topSort = CursorPagingTest.buildRandomSort(ALL_SORT_FIELD_NAMES); final String collapseSort = CursorPagingTest.buildRandomSort(ALL_SORT_FIELD_NAMES); final String q = random().nextBoolean() ? "*:*" : CursorPagingTest.buildRandomQuery(); final SolrParams mainP = params("q", q, "fl", "id,"+collapseField); final String csize = random().nextBoolean() ? "" : " size=" + TestUtil.nextInt(random(),1,10000); final String nullPolicy = randomNullPolicy(); final String nullPs = NULL_IGNORE.equals(nullPolicy) // ignore is default, randomly be explicit about it ? (random().nextBoolean() ? "" : " nullPolicy=ignore") : (" nullPolicy=" + nullPolicy); final SolrParams collapseP = params("sort", topSort, "rows", "200", "fq", ("{!collapse" + csize + nullPs + " field="+collapseField+" sort='"+collapseSort+"'}")); try { final QueryResponse mainRsp = SOLR.query(SolrParams.wrapDefaults(collapseP, mainP)); for (SolrDocument doc : mainRsp.getResults()) { final Object groupHeadId = doc.getFieldValue("id"); final Object collapseVal = doc.getFieldValue(collapseField); if (null == collapseVal) { if (NULL_EXPAND.equals(nullPolicy)) { // nothing to check for this doc, it's in its own group continue; } assertFalse(groupHeadId + " has null collapseVal but nullPolicy==ignore; " + "mainP: " + mainP + ", collapseP: " + collapseP, NULL_IGNORE.equals(nullPolicy)); } // workaround for SOLR-8082... // // what's important is that we already did the collapsing on the *real* collapseField // to verify the groupHead returned is really the best our verification filter // on docs with that value in a different field containing the exact same values final String checkField = collapseField.replace("float_dv", "float"); final String checkFQ = ((null == collapseVal) ? ("-" + checkField + ":[* TO *]") : ("{!field f="+checkField+"}" + collapseVal.toString())); final SolrParams checkP = params("fq", checkFQ, "rows", "1", "sort", collapseSort); final QueryResponse checkRsp = SOLR.query(SolrParams.wrapDefaults(checkP, mainP)); assertTrue("not even 1 match for sanity check query? expected: " + doc, ! checkRsp.getResults().isEmpty()); final SolrDocument firstMatch = checkRsp.getResults().get(0); final Object firstMatchId = firstMatch.getFieldValue("id"); assertEquals("first match for filtered group '"+ collapseVal + "' not matching expected group head ... " + "mainP: " + mainP + ", collapseP: " + collapseP + ", checkP: " + checkP, groupHeadId, firstMatchId); } } catch (Exception e) { throw new RuntimeException("BUG using params: " + collapseP + " + " + mainP, e); } } } }