org.apache.solr.client.solrj.SolrClient Java Examples
The following examples show how to use
org.apache.solr.client.solrj.SolrClient.
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: JsonRequestApiTest.java From lucene-solr with Apache License 2.0 | 6 votes |
@Test public void testFacetWidenedUsingQueryDomain() throws Exception { SolrClient solrClient = cluster.getSolrClient(); //tag::solrj-json-facet-query-domain[] final TermsFacetMap inStockFacet = new TermsFacetMap("inStock").setLimit(2); final TermsFacetMap popularCategoriesFacet = new TermsFacetMap("cat") .withDomain(new DomainMap().withQuery("popularity:[8 TO 10]")) .setLimit(3); final JsonQueryRequest request = new JsonQueryRequest() .setQuery("apple") .withFacet("popular_categories", popularCategoriesFacet); QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME); //end::solrj-json-facet-query-domain[] assertEquals(0, queryResponse.getStatus()); assertEquals(1, queryResponse.getResults().getNumFound()); assertEquals(1, queryResponse.getResults().size()); final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse(); assertHasFacetWithBucketValues(topLevelFacetingData, "popular_categories", new FacetBucket("electronics", 1), new FacetBucket("music", 1), new FacetBucket("search", 1)); }
Example #2
Source File: PackageUtils.java From lucene-solr with Apache License 2.0 | 6 votes |
/** * Uploads a file to the package store / file store of Solr. * * @param client A Solr client * @param buffer File contents * @param name Name of the file as it will appear in the file store (can be hierarchical) * @param sig Signature digest (public key should be separately uploaded to ZK) */ public static void postFile(SolrClient client, ByteBuffer buffer, String name, String sig) throws SolrServerException, IOException { String resource = "/api/cluster/files" + name; ModifiableSolrParams params = new ModifiableSolrParams(); if (sig != null) { params.add("sig", sig); } V2Response rsp = new V2Request.Builder(resource) .withMethod(SolrRequest.METHOD.PUT) .withPayload(buffer) .forceV2(true) .withMimeType("application/octet-stream") .withParams(params) .build() .process(client); if (!name.equals(rsp.getResponse().get(CommonParams.FILE))) { throw new SolrException(ErrorCode.BAD_REQUEST, "Mismatch in file uploaded. Uploaded: " + rsp.getResponse().get(CommonParams.FILE)+", Original: "+name); } }
Example #3
Source File: DirectSolrClassicInputDocumentWriter.java From hbase-indexer with Apache License 2.0 | 6 votes |
/** * Has the same behavior as {@link org.apache.solr.client.solrj.SolrClient#deleteByQuery(String)}. * * @param deleteQuery delete query to be executed */ @Override public void deleteByQuery(String deleteQuery) throws SolrServerException, IOException { try { for (SolrClient server : solrServers) { server.deleteByQuery(deleteQuery); } } catch (SolrException e) { if (isDocumentIssue(e)) { documentDeleteErrorMeter.mark(1); } else { solrDeleteErrorMeter.mark(1); throw e; } } catch (SolrServerException sse) { solrDeleteErrorMeter.mark(1); throw sse; } }
Example #4
Source File: AbstractAlfrescoDistributedIT.java From SearchServices with GNU Lesser General Public License v3.0 | 6 votes |
protected QueryResponse query(SolrClient solrClient, boolean andShards, String json, ModifiableSolrParams params) throws Exception { params.set("distrib", "false"); QueryRequest request = getAlfrescoRequest(json, params); QueryResponse controlRsp = request.process(solrClient); SOLR_RESPONSE_COMPARATOR.validateResponse(controlRsp); if (andShards) { params.remove("distrib"); setDistributedParams(params); QueryResponse rsp = queryRandomShard(json, params); SOLR_RESPONSE_COMPARATOR.compareResponses(rsp, controlRsp); return rsp; } else { return controlRsp; } }
Example #5
Source File: MCROAIClassificationToSetHandler.java From mycore with GNU General Public License v3.0 | 6 votes |
@Override public boolean filter(MCRSet set) { if (!filterEmptySets()) { return false; } SolrClient solrClient = MCRSolrClientFactory.getMainSolrClient(); ModifiableSolrParams p = new ModifiableSolrParams(); String value = set.getSpec(); p.set(CommonParams.Q, MCROAIUtils.getDefaultSetQuery(value, getConfigPrefix())); String restriction = MCROAIUtils.getDefaultRestriction(getConfigPrefix()); if (restriction != null) { p.set(CommonParams.FQ, restriction); } p.set(CommonParams.ROWS, 1); p.set(CommonParams.FL, "id"); try { QueryResponse response = solrClient.query(p); return response.getResults().isEmpty(); } catch (Exception exc) { LOGGER.error("Unable to get results of solr server", exc); return true; } }
Example #6
Source File: TestPutSolrContentStream.java From localization_nifi with Apache License 2.0 | 6 votes |
@Test public void testUpdateWithXml() throws IOException, SolrServerException { final SolrClient solrClient = createEmbeddedSolrClient(DEFAULT_SOLR_CORE); final TestableProcessor proc = new TestableProcessor(solrClient); final TestRunner runner = createDefaultTestRunner(proc); runner.setProperty(PutSolrContentStream.CONTENT_STREAM_PATH, "/update"); runner.setProperty(PutSolrContentStream.CONTENT_TYPE, "application/xml"); try (FileInputStream fileIn = new FileInputStream(XML_MULTIPLE_DOCS_FILE)) { runner.enqueue(fileIn); runner.run(1, false); runner.assertTransferCount(PutSolrContentStream.REL_FAILURE, 0); runner.assertTransferCount(PutSolrContentStream.REL_CONNECTION_FAILURE, 0); runner.assertTransferCount(PutSolrContentStream.REL_SUCCESS, 1); verifySolrDocuments(proc.getSolrClient(), Arrays.asList(expectedDoc1, expectedDoc2)); } finally { try { proc.getSolrClient().close(); } catch (Exception e) { } } }
Example #7
Source File: JsonRequestApiTest.java From lucene-solr with Apache License 2.0 | 6 votes |
@Test public void testJsonQueryUsingParamsBlock() throws Exception { SolrClient solrClient = cluster.getSolrClient(); //tag::solrj-json-query-params-block[] final ModifiableSolrParams params = new ModifiableSolrParams(); params.set("fl", "name", "price"); final JsonQueryRequest simpleQuery = new JsonQueryRequest(params) .withParam("q", "memory") .withParam("rows", 1); QueryResponse queryResponse = simpleQuery.process(solrClient, COLLECTION_NAME); // end::solrj-json-query-params-block[] assertEquals(0, queryResponse.getStatus()); assertEquals(1, queryResponse.getResults().size()); final SolrDocument doc = queryResponse.getResults().get(0); final Collection<String> returnedFields = doc.getFieldNames(); assertEquals(2, doc.getFieldNames().size()); assertTrue("Expected returned field list to include 'name'", returnedFields.contains("name")); assertTrue("Expected returned field list to include 'price'", returnedFields.contains("price")); }
Example #8
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 #9
Source File: JsonRequestApiTest.java From lucene-solr with Apache License 2.0 | 6 votes |
@Test public void testSimpleJsonTermsFacet() throws Exception { SolrClient solrClient = cluster.getSolrClient(); //tag::solrj-json-simple-terms-facet[] final TermsFacetMap categoryFacet = new TermsFacetMap("cat").setLimit(3); final JsonQueryRequest request = new JsonQueryRequest() .setQuery("*:*") .withFacet("categories", categoryFacet); QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME); //end::solrj-json-simple-terms-facet[] assertEquals(0, queryResponse.getStatus()); assertEquals(32, queryResponse.getResults().getNumFound()); assertEquals(10, queryResponse.getResults().size()); final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse(); assertHasFacetWithBucketValues(topLevelFacetingData,"categories", new FacetBucket("electronics",12), new FacetBucket("currency", 4), new FacetBucket("memory", 3)); }
Example #10
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 #11
Source File: QuerySolrIT.java From nifi with Apache License 2.0 | 6 votes |
@Test public void testStandardResponse() throws IOException { SolrClient solrClient = createSolrClient(); TestRunner runner = createRunnerWithSolrClient(solrClient); runner.setProperty(QuerySolr.SOLR_PARAM_QUERY, "id:(doc0 OR doc1)"); runner.setProperty(QuerySolr.SOLR_PARAM_FIELD_LIST, "id"); runner.setProperty(QuerySolr.SOLR_PARAM_SORT, "id desc"); runner.setNonLoopConnection(false); runner.run(); runner.assertAllFlowFilesTransferred(QuerySolr.RESULTS, 1); MockFlowFile flowFile = runner.getFlowFilesForRelationship(QuerySolr.RESULTS).get(0); flowFile.assertAttributeExists(QuerySolr.ATTRIBUTE_CURSOR_MARK); flowFile.assertAttributeExists(QuerySolr.ATTRIBUTE_SOLR_STATUS); flowFile.assertAttributeExists(QuerySolr.ATTRIBUTE_QUERY_TIME); String expectedXml = "<docs><doc boost=\"1.0\"><field name=\"id\">doc1</field></doc><doc boost=\"1.0\"><field name=\"id\">doc0</field></doc></docs>"; assertThat(expectedXml, CompareMatcher.isIdenticalTo(new String(runner.getContentAsByteArray(flowFile)))); solrClient.close(); }
Example #12
Source File: AbstractAlfrescoDistributedIT.java From SearchServices with GNU Lesser General Public License v3.0 | 6 votes |
public static void waitForShardsCount(ModifiableSolrParams query, int count, long waitMillis, long start) throws Exception { long timeOut = start+waitMillis; int totalCount = 0; query.set("shards", shards); SolrClient clientShard = clientShards.get(0); while (System.currentTimeMillis() < timeOut) { QueryResponse response = clientShard.query(query); totalCount = (int) response.getResults().getNumFound(); if (totalCount == count) { return; } } throw new Exception("Cluster:Wait error expected "+count+" found "+totalCount+" : "+query.toString()); }
Example #13
Source File: TestQuerySolr.java From nifi with Apache License 2.0 | 6 votes |
@Test public void testRetrievalOfFullResults2() throws IOException { SolrClient solrClient = createSolrClient(); TestRunner runner = createRunnerWithSolrClient(solrClient); runner.setProperty(QuerySolr.SOLR_PARAM_FIELD_LIST, "id"); runner.setProperty(QuerySolr.SOLR_PARAM_SORT, "id asc"); runner.setProperty(QuerySolr.SOLR_PARAM_ROWS, "3"); runner.setProperty(QuerySolr.AMOUNT_DOCUMENTS_TO_RETURN, QuerySolr.RETURN_ALL_RESULTS); runner.setProperty("facet", "true"); runner.setProperty("stats", "true"); runner.enqueue(new byte[0]); runner.run(); runner.assertTransferCount(QuerySolr.RESULTS, 4); runner.assertTransferCount(QuerySolr.ORIGINAL, 1); runner.assertTransferCount(QuerySolr.FACETS, 1); runner.assertTransferCount(QuerySolr.STATS, 1); solrClient.close(); }
Example #14
Source File: SolrLocatorTest.java From kite with Apache License 2.0 | 6 votes |
@Test public void testSelectsEmbeddedSolrServerAndAddDocument() throws Exception { //Solr locator should select EmbeddedSolrServer only solrHome is specified SolrLocator solrLocator = new SolrLocator(new SolrMorphlineContext.Builder().build()); solrLocator.setSolrHomeDir(RESOURCES_DIR + "/solr"); solrLocator.setCollectionName("collection1"); SolrServerDocumentLoader documentLoader = (SolrServerDocumentLoader)solrLocator.getLoader(); SolrClient solrServer = documentLoader.getSolrServer(); assertTrue(solrServer instanceof EmbeddedSolrServer); SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", "myId"); doc.addField("text", "myValue"); solrServer.add(doc); solrServer.commit(); SolrDocument resultDoc = solrServer.getById("myId"); assertTrue(resultDoc.getFieldValues("text").contains("myValue")); UpdateResponse deleteResponse = solrServer.deleteById("myId"); assertEquals(0, deleteResponse.getStatus()); solrServer.commit(); solrServer.close(); }
Example #15
Source File: TestImpersonationWithHadoopAuth.java From lucene-solr with Apache License 2.0 | 6 votes |
@Test @AwaitsFix(bugUrl="https://issues.apache.org/jira/browse/HADOOP-9893") public void testForwarding() throws Exception { String collectionName = "forwardingCollection"; // create collection CollectionAdminRequest.Create create = CollectionAdminRequest.createCollection(collectionName, "conf1", 1, 1); try (SolrClient solrClient = newSolrClient()) { create.process(solrClient); } // try a command to each node, one of them must be forwarded for (JettySolrRunner jetty : cluster.getJettySolrRunners()) { try (HttpSolrClient client = new HttpSolrClient.Builder(jetty.getBaseUrl().toString() + "/" + collectionName).build()) { ModifiableSolrParams params = new ModifiableSolrParams(); params.set("q", "*:*"); params.set(USER_PARAM, "user"); client.query(params); } } }
Example #16
Source File: QuerySolrIT.java From nifi with Apache License 2.0 | 5 votes |
private TestRunner createRunnerWithSolrClient(SolrClient solrClient) { final TestableProcessor proc = new TestableProcessor(solrClient); TestRunner runner = TestRunners.newTestRunner(proc); runner.setProperty(SolrUtils.SOLR_TYPE, SolrUtils.SOLR_TYPE_CLOUD.getValue()); runner.setProperty(SolrUtils.SOLR_LOCATION, SOLR_LOCATION); runner.setProperty(SolrUtils.COLLECTION, SOLR_COLLECTION); return runner; }
Example #17
Source File: SolrMgr.java From ranger with Apache License 2.0 | 5 votes |
public SolrClient getSolrClient() { if(solrClient!=null){ return solrClient; }else{ synchronized(this){ connect(); } } return solrClient; }
Example #18
Source File: MCRSolrClassificationUtil.java From mycore with GNU General Public License v3.0 | 5 votes |
/** * Drops the whole solr classification index. */ public static void dropIndex() { try { SolrClient solrClient = getCore().getConcurrentClient(); solrClient.deleteByQuery("*:*"); } catch (Exception exc) { LOGGER.error("Unable to drop solr classification index", exc); } }
Example #19
Source File: TestSolrCoreSnapshots.java From lucene-solr with Apache License 2.0 | 5 votes |
private SnapshotMetaData createSnapshot (SolrClient adminClient, String coreName, String commitName) throws Exception { CreateSnapshot req = new CreateSnapshot(commitName); req.setCoreName(coreName); adminClient.request(req); Collection<SnapshotMetaData> snapshots = listSnapshots(adminClient, coreName); Optional<SnapshotMetaData> metaData = snapshots.stream().filter(x -> commitName.equals(x.getName())).findFirst(); assertTrue(metaData.isPresent()); return metaData.get(); }
Example #20
Source File: AdminHandlersProxy.java From lucene-solr with Apache License 2.0 | 5 votes |
/** * Makes a remote request and returns a future and the solr client. The caller is responsible for closing the client */ public static Pair<Future<NamedList<Object>>, SolrClient> callRemoteNode(String nodeName, String endpoint, SolrParams params, ZkController zkController) throws IOException, SolrServerException { log.debug("Proxying {} request to node {}", endpoint, nodeName); URL baseUrl = new URL(zkController.zkStateReader.getBaseUrlForNodeName(nodeName)); HttpSolrClient solr = new HttpSolrClient.Builder(baseUrl.toString()).build(); @SuppressWarnings({"rawtypes"}) SolrRequest proxyReq = new GenericSolrRequest(SolrRequest.METHOD.GET, endpoint, params); HttpSolrClient.HttpUriRequestResponse proxyResp = solr.httpUriRequest(proxyReq); return new Pair<>(proxyResp.future, solr); }
Example #21
Source File: TestConfigSetsAPI.java From lucene-solr with Apache License 2.0 | 5 votes |
private void verifyException(SolrClient solrClient, @SuppressWarnings({"rawtypes"})ConfigSetAdminRequest request, String errorContains) throws Exception { Exception e = expectThrows(Exception.class, () -> solrClient.request(request)); assertTrue("Expected exception message to contain: " + errorContains + " got: " + e.getMessage(), e.getMessage().contains(errorContains)); }
Example #22
Source File: BaseDistributedSearchTestCase.java From lucene-solr with Apache License 2.0 | 5 votes |
protected void index_specific(int serverNumber, Object... fields) throws Exception { SolrInputDocument doc = new SolrInputDocument(); for (int i = 0; i < fields.length; i += 2) { doc.addField((String) (fields[i]), fields[i + 1]); } controlClient.add(doc); SolrClient client = clients.get(serverNumber); client.add(doc); }
Example #23
Source File: ConcurrentDeleteAndCreateCollectionTest.java From lucene-solr with Apache License 2.0 | 5 votes |
public CreateDeleteCollectionThread(String name, String collectionName, String configName, long timeToRunSec, SolrClient solrClient, AtomicReference<Exception> failure) { super(name); this.collectionName = collectionName; this.timeToRunSec = timeToRunSec; this.solrClient = solrClient; this.failure = failure; this.configName = configName; }
Example #24
Source File: TestCloudPhrasesIdentificationComponent.java From lucene-solr with Apache License 2.0 | 5 votes |
/** * returns a random SolrClient -- either a CloudSolrClient, or an HttpSolrClient pointed * at a node in our cluster */ public static SolrClient getRandClient(Random rand) { int numClients = CLIENTS.size(); int idx = TestUtil.nextInt(rand, 0, numClients); return (idx == numClients) ? CLOUD_CLIENT : CLIENTS.get(idx); }
Example #25
Source File: SolrExampleXMLHttp2Test.java From lucene-solr with Apache License 2.0 | 5 votes |
@Override public SolrClient createNewSolrClient() { try { String url = jetty.getBaseUrl().toString() + "/collection1"; Http2SolrClient client = new Http2SolrClient.Builder(url).connectionTimeout(DEFAULT_CONNECTION_TIMEOUT).build(); client.setParser(new XMLResponseParser()); client.setRequestWriter(new RequestWriter()); return client; } catch (Exception ex) { throw new RuntimeException(ex); } }
Example #26
Source File: HttpPartitionTest.java From lucene-solr with Apache License 2.0 | 5 votes |
protected int sendDoc(int docId, Integer minRf, SolrClient solrClient, String collection) throws IOException, SolrServerException { SolrInputDocument doc = new SolrInputDocument(); doc.addField(id, String.valueOf(docId)); doc.addField("a_t", "hello" + docId); UpdateRequest up = new UpdateRequest(); if (minRf != null) { up.setParam(UpdateRequest.MIN_REPFACT, String.valueOf(minRf)); } up.add(doc); return cloudClient.getMinAchievedReplicationFactor(collection, solrClient.request(up, collection)); }
Example #27
Source File: MultiThreadedOCPTest.java From lucene-solr with Apache License 2.0 | 5 votes |
/** * Waits until the specified async task has completed or time ran out. * @return <code>null</code> if the task has not completed, the completion timestamp if the task has completed */ private Long waitForTaskToCompleted(SolrClient client, int requestId) throws Exception { for (int i = 0; i < 500; i++) { Long task = checkTaskHasCompleted(client, requestId); if (task != null) { return task; } Thread.sleep(100); } return null; }
Example #28
Source File: HttpSolrClientFactory.java From dubbox with Apache License 2.0 | 5 votes |
protected void appendCoreToBaseUrl(String core, SolrClient solrClient) { if (StringUtils.isNotEmpty(core) && isHttpSolrClient(solrClient)) { HttpSolrClient httpSolrClient = (HttpSolrClient) solrClient; String url = SolrClientUtils.appendCoreToBaseUrl(httpSolrClient.getBaseURL(), core); httpSolrClient.setBaseURL(url); } }
Example #29
Source File: SolrServerDocumentLoader.java From kite with Apache License 2.0 | 5 votes |
public SolrServerDocumentLoader(SolrClient server, int batchSize) { if (server == null) { throw new IllegalArgumentException("solr server must not be null"); } this.server = server; if (batchSize <= 0) { throw new IllegalArgumentException("batchSize must be a positive number: " + batchSize); } this.batchSize = batchSize; }
Example #30
Source File: AtlasJanusGraphIndexClient.java From atlas with Apache License 2.0 | 5 votes |
private SolrResponse createFreeTextRequestHandler(SolrClient solrClient, String collectionName, Map<String, Integer> indexFieldName2SearchWeightMap, Solr6Index.Mode mode) throws IOException, SolrServerException, AtlasBaseException { String searchWeightString = generateSearchWeightString(indexFieldName2SearchWeightMap); String payLoadString = generatePayLoadForFreeText("create-requesthandler", searchWeightString); return performRequestHandlerAction(collectionName, solrClient, payLoadString, mode); }