List of usage examples for org.apache.solr.client.solrj.impl CloudSolrClient add
public UpdateResponse add(String collection, Collection<SolrInputDocument> docs) throws SolrServerException, IOException
From source file:fr.jetoile.hadoopunit.component.SolrCloudBootstrapTest.java
License:Apache License
@Test public void solrCloudShouldStart() throws IOException, SolrServerException, KeeperException, InterruptedException, NotFoundServiceException { String collectionName = configuration.getString(SolrCloudBootstrap.SOLR_COLLECTION_NAME); // String zkHostString = configuration.getString(Config.ZOOKEEPER_HOST_KEY) + ":" + configuration.getInt(Config.ZOOKEEPER_PORT_KEY); // CloudSolrClient client = new CloudSolrClient(zkHostString); CloudSolrClient client = ((SolrCloudBootstrap) HadoopBootstrap.INSTANCE.getService(Component.SOLRCLOUD)) .getClient();//from w ww . j av a 2 s . co m for (int i = 0; i < 1000; ++i) { SolrInputDocument doc = new SolrInputDocument(); doc.addField("cat", "book"); doc.addField("id", "book-" + i); doc.addField("name", "The Legend of the Hobbit part " + i); client.add(collectionName, doc); if (i % 100 == 0) client.commit(collectionName); // periodically flush } client.commit("collection1"); SolrDocument collection1 = client.getById(collectionName, "book-1"); assertNotNull(collection1); assertThat(collection1.getFieldValue("name")).isEqualTo("The Legend of the Hobbit part 1"); client.close(); }
From source file:fr.jetoile.hadoopunit.integrationtest.IntegrationBootstrapTest.java
License:Apache License
@Test public void solrCloudShouldStart() throws IOException, SolrServerException, KeeperException, InterruptedException { String collectionName = configuration.getString(SolrCloudBootstrap.SOLR_COLLECTION_NAME); String zkHostString = configuration.getString(Config.ZOOKEEPER_HOST_KEY) + ":" + configuration.getInt(Config.ZOOKEEPER_PORT_KEY); CloudSolrClient client = new CloudSolrClient(zkHostString); for (int i = 0; i < 1000; ++i) { SolrInputDocument doc = new SolrInputDocument(); doc.addField("cat", "book"); doc.addField("id", "book-" + i); doc.addField("name", "The Legend of the Hobbit part " + i); client.add(collectionName, doc); if (i % 100 == 0) client.commit(collectionName); // periodically flush }//from w ww. ja v a2 s . c o m client.commit("collection1"); SolrDocument collection1 = client.getById(collectionName, "book-1"); assertNotNull(collection1); assertThat(collection1.getFieldValue("name")).isEqualTo("The Legend of the Hobbit part 1"); client.close(); }
From source file:org.apache.sentry.tests.e2e.solr.DocLevelGenerator.java
License:Apache License
/** * Generates docs according to the following parameters: * * @param client Solr client to use/*from w ww . j a v a 2s .c o m*/ * @param numDocs number of documents to generate * @param evenDocsToken every even number doc gets this token added to the authField * @param oddDocsToken every odd number doc gets this token added to the authField * @param extraAuthFieldsCount generates this number of bogus entries in the authField */ public void generateDocs(CloudSolrClient client, int numDocs, String evenDocsToken, String oddDocsToken, int extraAuthFieldsCount) throws Exception { // create documents ArrayList<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(); for (int i = 0; i < numDocs; ++i) { SolrInputDocument doc = new SolrInputDocument(); String iStr = Long.toString(i); doc.addField("id", iStr); doc.addField("description", "description" + iStr); // put some bogus tokens in for (int k = 0; k < extraAuthFieldsCount; ++k) { doc.addField(authField, authField + Long.toString(k)); } // even docs get evenDocsToken, odd docs get oddDocsToken if (i % 2 == 0) { doc.addField(authField, evenDocsToken); } else { doc.addField(authField, oddDocsToken); } // add a token to all docs so we can check that we can get all // documents returned doc.addField(authField, "doclevel_role"); docs.add(doc); } client.add(collection, docs); client.commit(collection, true, true); }
From source file:org.apache.sentry.tests.e2e.solr.DocLevelGenerator.java
License:Apache License
public void generateDocsForSubsetQueries(CloudSolrClient client, int numDocs, int numTokens, String tokenPrefix) throws Exception { Assert.assertTrue("Num Tokens should be divisible by numTokens", numDocs % numTokens == 0); // create documents ArrayList<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(); for (int i = 0; i < numDocs; ++i) { int tokenCount = 0; SolrInputDocument doc = new SolrInputDocument(); String iStr = Long.toString(i); doc.addField("id", iStr); doc.addField("description", "description" + iStr); for (int j = 0; j < numTokens; j++) { if ((i & 1 << j) == 1 << j) { doc.addField(authField, tokenPrefix + j); tokenCount++;//www. jav a 2 s . c o m } } doc.addField("sentry_auth_count", tokenCount); System.out.println(doc); docs.add(doc); } client.add(collection, docs); client.commit(collection, true, true); }
From source file:org.apache.sentry.tests.e2e.solr.TestAbacOperations.java
License:Apache License
@Test public void simpleTest() throws Exception { String collectionName = "simpleCollection"; createCollection(ADMIN_USER, collectionName, "cloud-minimal_abac", NUM_SERVERS, 1); CloudSolrClient client = cluster.getSolrClient(); /*abacuser1 has the following: orGroupsAttr: group1// w w w.j a va2 s. c o m orGroupsAttr: group2 andGroupsAttr: group11 andGroupsAttr: group12 lteAttr: THREE gteAttr: THIRTY */ // Add a set of docs that we know will match ArrayList<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(); for (int i = 0; i < numDocs; ++i) { SolrInputDocument doc = new SolrInputDocument(); String iStr = Long.toString(i); doc.addField("id", iStr); doc.addField("description", "description" + iStr); doc.addField(lteFieldName, "THREE"); doc.addField(gteFieldName, "THIRTY"); doc.addField(orGroupsFieldName, "group1"); doc.addField(andGroupsFieldName, "group11"); doc.addField(andGroupsCountFieldName, 1); docs.add(doc); } //Add one further doc that won't match SolrInputDocument doc = new SolrInputDocument(); doc.addField("id", numDocs); doc.addField("description", "description" + numDocs); doc.addField(lteFieldName, "ONE"); doc.addField(gteFieldName, "FIFTY"); doc.addField(orGroupsFieldName, "group9"); doc.addField(andGroupsFieldName, "group99"); doc.addField(andGroupsCountFieldName, 1); docs.add(doc); client.add(collectionName, docs); client.commit(collectionName, true, true); QueryRequest request = new QueryRequest(new SolrQuery("*:*")); setAuthenticationUser("abacuser1"); QueryResponse rsp = request.process(client, collectionName); SolrDocumentList docList = rsp.getResults(); assertEquals(numDocs, docList.getNumFound()); }
From source file:org.apache.sentry.tests.e2e.solr.TestAbacOperations.java
License:Apache License
@Test public void lteFilterTest() throws Exception { /*/* www.ja v a2 s . com*/ We're going to test with three users, each with different values for lteAttr, ONE, THREE and FIVE All other attributes for those users will be fixed. */ String collectionName = "lteCollection"; createCollection(ADMIN_USER, collectionName, "cloud-minimal_abac", NUM_SERVERS, 1); CloudSolrClient client = cluster.getSolrClient(); String[] lteValues = { "ONE", "TWO", "THREE", "FOUR", "FIVE" }; ArrayList<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(); for (int i = 0; i < numDocs * lteValues.length; ++i) { SolrInputDocument doc = new SolrInputDocument(); String iStr = Long.toString(i); doc.addField("id", iStr); doc.addField("description", "lte Test Doc: " + iStr); doc.addField(lteFieldName, lteValues[i % lteValues.length]); doc.addField(gteFieldName, "THIRTY"); doc.addField(orGroupsFieldName, "group1"); doc.addField(andGroupsFieldName, "group11"); doc.addField(andGroupsCountFieldName, 1); docs.add(doc); } client.add(collectionName, docs); client.commit(collectionName, true, true); QueryRequest request = new QueryRequest(new SolrQuery("*:*")); /* lteuser1 has lteAttr of ONE -> Should see numDocs docs lteuser2 has lteAttr of THREE -> Should see numDocs * 3 lteuser3 has lteAttr of FIVE -> Should see numDocs * 5 docs */ setAuthenticationUser("lteuser1"); QueryResponse rsp = request.process(client, collectionName); SolrDocumentList docList = rsp.getResults(); assertEquals(numDocs, docList.getNumFound()); setAuthenticationUser("lteuser2"); rsp = request.process(client, collectionName); docList = rsp.getResults(); assertEquals(numDocs * 3, docList.getNumFound()); setAuthenticationUser("lteuser3"); rsp = request.process(client, collectionName); docList = rsp.getResults(); assertEquals(numDocs * 5, docList.getNumFound()); }
From source file:org.apache.sentry.tests.e2e.solr.TestAbacOperations.java
License:Apache License
@Test public void gteFilterTest() throws Exception { /*//from www. java 2s . c om We're going to test with three users, each with different values for gteAttr, TEN, THIRTY and FIFTY All other attributes for those users will be fixed. */ String collectionName = "gteCollection"; createCollection(ADMIN_USER, collectionName, "cloud-minimal_abac", NUM_SERVERS, 1); CloudSolrClient client = cluster.getSolrClient(); String[] gteValues = { "TEN", "TWENTY", "THIRTY", "FORTY", "FIFTY" }; ArrayList<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(); for (int i = 0; i < numDocs * gteValues.length; ++i) { SolrInputDocument doc = new SolrInputDocument(); String iStr = Long.toString(i); doc.addField("id", iStr); doc.addField("description", "gte Test Doc: " + iStr); doc.addField(gteFieldName, gteValues[i % gteValues.length]); doc.addField(lteFieldName, "THREE"); doc.addField(orGroupsFieldName, "group1"); doc.addField(andGroupsFieldName, "group11"); doc.addField(andGroupsCountFieldName, 1); docs.add(doc); } client.add(collectionName, docs); client.commit(collectionName, true, true); QueryRequest request = new QueryRequest(new SolrQuery("*:*")); /* gteuser1 has gteAttr of TEN -> Should see numDocs * 5 docs gteuser2 has gteAttr of THIRTY -> Should see numDocs * 3 gteuser3 has gteAttr of FIFTY -> Should see numDocs docs */ setAuthenticationUser("gteuser1"); QueryResponse rsp = request.process(client, collectionName); SolrDocumentList docList = rsp.getResults(); assertEquals(numDocs * 5, docList.getNumFound()); setAuthenticationUser("gteuser2"); rsp = request.process(client, collectionName); docList = rsp.getResults(); assertEquals(numDocs * 3, docList.getNumFound()); setAuthenticationUser("gteuser3"); rsp = request.process(client, collectionName); docList = rsp.getResults(); assertEquals(numDocs, docList.getNumFound()); }
From source file:org.apache.sentry.tests.e2e.solr.TestAbacOperations.java
License:Apache License
@Test public void orGroupsFilterTest() throws Exception { /*/*ww w . j a v a2s .com*/ We're going to test with three users, each with different values for orGroups All other attributes for those users will be fixed. */ String collectionName = "orGroupsCollection"; createCollection(ADMIN_USER, collectionName, "cloud-minimal_abac", NUM_SERVERS, 1); CloudSolrClient client = cluster.getSolrClient(); int[] groupDocCount = new int[3]; ArrayList<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(); for (int i = 0; i < numDocs * 4; ++i) { SolrInputDocument doc = new SolrInputDocument(); String iStr = Long.toString(i); doc.addField("id", iStr); doc.addField("description", "Or Groups Test Doc: " + iStr); doc.addField(gteFieldName, "THIRTY"); doc.addField(lteFieldName, "THREE"); if (i % 2 == 0) { groupDocCount[0]++; doc.addField(orGroupsFieldName, "group1"); } if (i % 3 == 0) { doc.addField(orGroupsFieldName, "group2"); } if (i % 4 == 0) { doc.addField(orGroupsFieldName, "group3"); } if (i % 2 == 0 || i % 3 == 0) { groupDocCount[1]++; } if (i % 2 == 0 || i % 3 == 0 || i % 4 == 0) { groupDocCount[2]++; } doc.addField(andGroupsFieldName, "group11"); doc.addField(andGroupsCountFieldName, 1); docs.add(doc); } client.add(collectionName, docs); client.commit(collectionName, true, true); QueryRequest request = new QueryRequest(new SolrQuery("*:*")); /* oruser1 has group1 oruser2 has group1, group2 oruser3 has group1, group2, group3 */ setAuthenticationUser("oruser1"); QueryResponse rsp = request.process(client, collectionName); SolrDocumentList docList = rsp.getResults(); assertEquals(groupDocCount[0], docList.getNumFound()); setAuthenticationUser("oruser2"); rsp = request.process(client, collectionName); docList = rsp.getResults(); assertEquals(groupDocCount[1], docList.getNumFound()); setAuthenticationUser("oruser3"); rsp = request.process(client, collectionName); docList = rsp.getResults(); assertEquals(groupDocCount[2], docList.getNumFound()); }
From source file:org.apache.sentry.tests.e2e.solr.TestAbacOperations.java
License:Apache License
@Test public void andGroupsFilterTest() throws Exception { /*/*from w w w . ja v a 2 s . co m*/ We're going to test with three users, each with different values for andGroups All other attributes for those users will be fixed. */ String collectionName = "andGroupsCollection"; createCollection(ADMIN_USER, collectionName, "cloud-minimal_abac", NUM_SERVERS, 1); CloudSolrClient client = cluster.getSolrClient(); int[] groupDocCount = new int[3]; ArrayList<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(); for (int i = 0; i < numDocs * 4; ++i) { int andGroups = 0; SolrInputDocument doc = new SolrInputDocument(); String iStr = Long.toString(i); doc.addField("id", iStr); doc.addField("description", "Or Groups Test Doc: " + iStr); doc.addField(gteFieldName, "THIRTY"); doc.addField(lteFieldName, "THREE"); if (i % 2 == 0) { doc.addField(andGroupsFieldName, "group11"); andGroups++; } if (i % 3 == 0) { doc.addField(andGroupsFieldName, "group12"); andGroups++; } if (i % 4 == 0) { doc.addField(andGroupsFieldName, "group13"); andGroups++; } if (i % 2 == 0 && (i % 3 != 0 && i % 4 != 0)) { groupDocCount[0]++; } if ((i % 2 == 0 || i % 3 == 0) && i % 4 != 0) { groupDocCount[1]++; } if (i % 2 == 0 || i % 3 == 0 || i % 4 == 0) { groupDocCount[2]++; } doc.addField(andGroupsCountFieldName, andGroups); doc.addField(orGroupsFieldName, "group1"); docs.add(doc); } client.add(collectionName, docs); client.commit(collectionName, true, true); QueryRequest request = new QueryRequest(new SolrQuery("*:*")); /* anduser1 has group11 anduser2 has group11, group12 anduser3 has group11, group12, group13 */ setAuthenticationUser("anduser1"); QueryResponse rsp = request.process(client, collectionName); SolrDocumentList docList = rsp.getResults(); assertEquals(groupDocCount[0], docList.getNumFound()); setAuthenticationUser("anduser2"); rsp = request.process(client, collectionName); docList = rsp.getResults(); assertEquals(groupDocCount[1], docList.getNumFound()); setAuthenticationUser("anduser3"); rsp = request.process(client, collectionName); docList = rsp.getResults(); assertEquals(groupDocCount[2], docList.getNumFound()); }
From source file:org.apache.sentry.tests.e2e.solr.TestAbacOperations.java
License:Apache License
@Test public void nestedOrGroupsFilterTest() throws Exception { /*/*from w ww . j a v a2 s. c om*/ We're going to test with three users, each with different values for orGroups All other attributes for those users will be fixed. */ String collectionName = "nestedOrGroupsCollection"; createCollection(ADMIN_USER, collectionName, "cloud-minimal_abac", NUM_SERVERS, 1); CloudSolrClient client = cluster.getSolrClient(); int[] groupDocCount = new int[3]; ArrayList<SolrInputDocument> docs = new ArrayList<SolrInputDocument>(); for (int i = 0; i < numDocs * 4; ++i) { SolrInputDocument doc = new SolrInputDocument(); String iStr = Long.toString(i); doc.addField("id", iStr); doc.addField("description", "Nested Or Groups Test Doc: " + iStr); doc.addField(gteFieldName, "THIRTY"); doc.addField(lteFieldName, "THREE"); if (i % 2 == 0) { doc.addField(orGroupsFieldName, "nestedgroup1"); doc.addField(orGroupsFieldName, "nestedgroup4"); } if (i % 3 == 0) { doc.addField(orGroupsFieldName, "nestedgroup2"); doc.addField(orGroupsFieldName, "nestedgroup5"); } if (i % 4 == 0) { doc.addField(orGroupsFieldName, "nestedgroup3"); doc.addField(orGroupsFieldName, "nestedgroup6"); } if (i % 2 == 0 || i % 3 == 0 || i % 4 == 0) { groupDocCount[0]++; } doc.addField(andGroupsFieldName, "group11"); doc.addField(andGroupsCountFieldName, 1); docs.add(doc); } client.add(collectionName, docs); client.commit(collectionName, true, true); QueryRequest request = new QueryRequest(new SolrQuery("*:*")); /* nesteduser1 has nestedgroup1, nestedgroup2 and nestedgroup3 nesteduser2 has nestedgroup4, nestedgroup5 and nestedgroup6 oruser1 does not have any of the nested groups so should return zero */ setAuthenticationUser("oruser1"); QueryResponse rsp = request.process(client, collectionName); SolrDocumentList docList = rsp.getResults(); assertEquals(0, docList.getNumFound()); setAuthenticationUser("nesteduser1"); rsp = request.process(client, collectionName); docList = rsp.getResults(); assertEquals(groupDocCount[0], docList.getNumFound()); setAuthenticationUser("nesteduser2"); rsp = request.process(client, collectionName); docList = rsp.getResults(); assertEquals(groupDocCount[0], docList.getNumFound()); }