Search in sources :

Example 11 with MapSolrParams

use of org.apache.solr.common.params.MapSolrParams in project lucene-solr by apache.

the class AutoCommitTest method testMaxTime.

public void testMaxTime() throws Exception {
    SolrCore core = h.getCore();
    NewSearcherListener trigger = new NewSearcherListener();
    core.registerNewSearcherListener(trigger);
    DirectUpdateHandler2 updater = (DirectUpdateHandler2) core.getUpdateHandler();
    CommitTracker tracker = updater.softCommitTracker;
    // too low of a number can cause a slow host to commit before the test code checks that it
    // isn't there... causing a failure at "shouldn't find any"
    tracker.setTimeUpperBound(1500);
    tracker.setDocsUpperBound(-1);
    // updater.commitCallbacks.add(trigger);
    UpdateRequestHandler handler = new UpdateRequestHandler();
    handler.init(null);
    MapSolrParams params = new MapSolrParams(new HashMap<String, String>());
    // Add a single document
    SolrQueryResponse rsp = new SolrQueryResponse();
    SolrQueryRequestBase req = new SolrQueryRequestBase(core, params) {
    };
    req.setContentStreams(toContentStreams(adoc("id", "529", "field_t", "what's inside?", "subject", "info"), null));
    trigger.reset();
    handler.handleRequest(req, rsp);
    // Check it it is in the index
    assertQ("shouldn't find any", req("id:529"), "//result[@numFound=0]");
    // Wait longer than the autocommit time
    assertTrue(trigger.waitForNewSearcher(45000));
    trigger.reset();
    req.setContentStreams(toContentStreams(adoc("id", "530", "field_t", "what's inside?", "subject", "info"), null));
    handler.handleRequest(req, rsp);
    // Now make sure we can find it
    assertQ("should find one", req("id:529"), "//result[@numFound=1]");
    // But not this one
    assertQ("should find none", req("id:530"), "//result[@numFound=0]");
    // Delete the document
    assertU(delI("529"));
    assertQ("deleted, but should still be there", req("id:529"), "//result[@numFound=1]");
    // Wait longer than the autocommit time
    assertTrue(trigger.waitForNewSearcher(30000));
    trigger.reset();
    req.setContentStreams(toContentStreams(adoc("id", "550", "field_t", "what's inside?", "subject", "info"), null));
    handler.handleRequest(req, rsp);
    assertEquals(2, tracker.getCommitCount());
    assertQ("deleted and time has passed", req("id:529"), "//result[@numFound=0]");
    // now make the call 10 times really fast and make sure it 
    // only commits once
    req.setContentStreams(toContentStreams(adoc("id", "500"), null));
    for (int i = 0; i < 10; i++) {
        handler.handleRequest(req, rsp);
    }
    assertQ("should not be there yet", req("id:500"), "//result[@numFound=0]");
    // Wait longer than the autocommit time
    assertTrue(trigger.waitForNewSearcher(45000));
    trigger.reset();
    req.setContentStreams(toContentStreams(adoc("id", "531", "field_t", "what's inside?", "subject", "info"), null));
    handler.handleRequest(req, rsp);
    assertEquals(3, tracker.getCommitCount());
    assertQ("now it should", req("id:500"), "//result[@numFound=1]");
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) MapSolrParams(org.apache.solr.common.params.MapSolrParams) SolrCore(org.apache.solr.core.SolrCore) SolrQueryRequestBase(org.apache.solr.request.SolrQueryRequestBase) UpdateRequestHandler(org.apache.solr.handler.UpdateRequestHandler)

Example 12 with MapSolrParams

use of org.apache.solr.common.params.MapSolrParams in project lucene-solr by apache.

the class AutoCommitTest method testCommitWithin.

public void testCommitWithin() throws Exception {
    SolrCore core = h.getCore();
    NewSearcherListener trigger = new NewSearcherListener();
    core.registerNewSearcherListener(trigger);
    DirectUpdateHandler2 updater = (DirectUpdateHandler2) core.getUpdateHandler();
    CommitTracker tracker = updater.softCommitTracker;
    tracker.setTimeUpperBound(0);
    tracker.setDocsUpperBound(-1);
    UpdateRequestHandler handler = new UpdateRequestHandler();
    handler.init(null);
    MapSolrParams params = new MapSolrParams(new HashMap<String, String>());
    // Add a single document with commitWithin == 4 second
    SolrQueryResponse rsp = new SolrQueryResponse();
    SolrQueryRequestBase req = new SolrQueryRequestBase(core, params) {
    };
    req.setContentStreams(toContentStreams(adoc(4000, "id", "529", "field_t", "what's inside?", "subject", "info"), null));
    trigger.reset();
    handler.handleRequest(req, rsp);
    // Check it isn't in the index
    assertQ("shouldn't find any", req("id:529"), "//result[@numFound=0]");
    // Wait longer than the commitWithin time
    assertTrue("commitWithin failed to commit", trigger.waitForNewSearcher(30000));
    // Add one document without commitWithin
    req.setContentStreams(toContentStreams(adoc("id", "530", "field_t", "what's inside?", "subject", "info"), null));
    trigger.reset();
    handler.handleRequest(req, rsp);
    // Check it isn't in the index
    assertQ("shouldn't find any", req("id:530"), "//result[@numFound=0]");
    // Delete one document with commitWithin
    trigger.pause();
    req.setContentStreams(toContentStreams(delI("529", "commitWithin", "2000"), null));
    trigger.reset();
    handler.handleRequest(req, rsp);
    // Now make sure we can find it
    assertQ("should find one", req("id:529"), "//result[@numFound=1]");
    trigger.unpause();
    // Wait for the commit to happen
    assertTrue("commitWithin failed to commit", trigger.waitForNewSearcher(30000));
    // Now we shouldn't find it
    assertQ("should find none", req("id:529"), "//result[@numFound=0]");
    // ... but we should find the new one
    assertQ("should find one", req("id:530"), "//result[@numFound=1]");
    trigger.reset();
    // now make the call 10 times really fast and make sure it 
    // only commits once
    req.setContentStreams(toContentStreams(adoc(2000, "id", "500"), null));
    for (int i = 0; i < 10; i++) {
        handler.handleRequest(req, rsp);
    }
    assertQ("should not be there yet", req("id:500"), "//result[@numFound=0]");
    // the same for the delete
    req.setContentStreams(toContentStreams(delI("530", "commitWithin", "1000"), null));
    for (int i = 0; i < 10; i++) {
        handler.handleRequest(req, rsp);
    }
    assertQ("should be there", req("id:530"), "//result[@numFound=1]");
    assertTrue("commitWithin failed to commit", trigger.waitForNewSearcher(30000));
    assertQ("should be there", req("id:500"), "//result[@numFound=1]");
    assertQ("should not be there", req("id:530"), "//result[@numFound=0]");
    assertEquals(3, tracker.getCommitCount());
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) MapSolrParams(org.apache.solr.common.params.MapSolrParams) SolrCore(org.apache.solr.core.SolrCore) SolrQueryRequestBase(org.apache.solr.request.SolrQueryRequestBase) UpdateRequestHandler(org.apache.solr.handler.UpdateRequestHandler)

Example 13 with MapSolrParams

use of org.apache.solr.common.params.MapSolrParams in project lucene-solr by apache.

the class AutoCommitTest method testMaxDocs.

public void testMaxDocs() throws Exception {
    SolrCore core = h.getCore();
    NewSearcherListener trigger = new NewSearcherListener();
    DirectUpdateHandler2 updateHandler = (DirectUpdateHandler2) core.getUpdateHandler();
    CommitTracker tracker = updateHandler.softCommitTracker;
    tracker.setTimeUpperBound(-1);
    tracker.setDocsUpperBound(14);
    core.registerNewSearcherListener(trigger);
    UpdateRequestHandler handler = new UpdateRequestHandler();
    handler.init(null);
    MapSolrParams params = new MapSolrParams(new HashMap<String, String>());
    // Add documents
    SolrQueryResponse rsp = new SolrQueryResponse();
    SolrQueryRequestBase req = new SolrQueryRequestBase(core, params) {
    };
    for (int i = 0; i < 14; i++) {
        req.setContentStreams(toContentStreams(adoc("id", Integer.toString(i), "subject", "info"), null));
        handler.handleRequest(req, rsp);
    }
    // It should not be there right away
    assertQ("shouldn't find any", req("id:1"), "//result[@numFound=0]");
    assertEquals(0, tracker.getCommitCount());
    req.setContentStreams(toContentStreams(adoc("id", "14", "subject", "info"), null));
    handler.handleRequest(req, rsp);
    assertTrue(trigger.waitForNewSearcher(15000));
    req.setContentStreams(toContentStreams(adoc("id", "15", "subject", "info"), null));
    handler.handleRequest(req, rsp);
    // Now make sure we can find it
    assertQ("should find one", req("id:14"), "//result[@numFound=1]");
    assertEquals(1, tracker.getCommitCount());
    // But not the one added afterward
    assertQ("should not find one", req("id:15"), "//result[@numFound=0]");
    assertEquals(1, tracker.getCommitCount());
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) MapSolrParams(org.apache.solr.common.params.MapSolrParams) SolrCore(org.apache.solr.core.SolrCore) SolrQueryRequestBase(org.apache.solr.request.SolrQueryRequestBase) UpdateRequestHandler(org.apache.solr.handler.UpdateRequestHandler)

Example 14 with MapSolrParams

use of org.apache.solr.common.params.MapSolrParams in project lucene-solr by apache.

the class DirectUpdateHandlerTest method testAddRollback.

@Test
public void testAddRollback() throws Exception {
    // re-init the core
    deleteCore();
    initCore("solrconfig.xml", "schema12.xml");
    assertU(adoc("id", "A"));
    // commit "A"
    SolrCore core = h.getCore();
    UpdateHandler updater = core.getUpdateHandler();
    assertTrue(updater instanceof DirectUpdateHandler2);
    DirectUpdateHandler2 duh2 = (DirectUpdateHandler2) updater;
    SolrQueryRequest ureq = req();
    CommitUpdateCommand cmtCmd = new CommitUpdateCommand(ureq, false);
    cmtCmd.waitSearcher = true;
    assertEquals(1, duh2.addCommands.longValue());
    assertEquals(1, duh2.addCommandsCumulative.getCount());
    assertEquals(0, duh2.commitCommands.getCount());
    updater.commit(cmtCmd);
    assertEquals(0, duh2.addCommands.longValue());
    assertEquals(1, duh2.addCommandsCumulative.getCount());
    assertEquals(1, duh2.commitCommands.getCount());
    ureq.close();
    assertU(adoc("id", "B"));
    // rollback "B"
    ureq = req();
    RollbackUpdateCommand rbkCmd = new RollbackUpdateCommand(ureq);
    assertEquals(1, duh2.addCommands.longValue());
    assertEquals(2, duh2.addCommandsCumulative.getCount());
    assertEquals(0, duh2.rollbackCommands.getCount());
    updater.rollback(rbkCmd);
    assertEquals(0, duh2.addCommands.longValue());
    assertEquals(1, duh2.addCommandsCumulative.getCount());
    assertEquals(1, duh2.rollbackCommands.getCount());
    ureq.close();
    // search - "B" should not be found.
    Map<String, String> args = new HashMap<>();
    args.put(CommonParams.Q, "id:A OR id:B");
    args.put("indent", "true");
    SolrQueryRequest req = new LocalSolrQueryRequest(core, new MapSolrParams(args));
    assertQ("\"B\" should not be found.", req, "//*[@numFound='1']", "//result/doc[1]/str[@name='id'][.='A']");
    // Add a doc after the rollback to make sure we can continue to add/delete documents
    // after a rollback as normal
    assertU(adoc("id", "ZZZ"));
    assertU(commit());
    assertQ("\"ZZZ\" must be found.", req("q", "id:ZZZ"), "//*[@numFound='1']", "//result/doc[1]/str[@name='id'][.='ZZZ']");
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) MapSolrParams(org.apache.solr.common.params.MapSolrParams) HashMap(java.util.HashMap) SolrCore(org.apache.solr.core.SolrCore) Test(org.junit.Test)

Example 15 with MapSolrParams

use of org.apache.solr.common.params.MapSolrParams in project lucene-solr by apache.

the class TestRuleBasedAuthorizationPlugin method testBasicPermissions.

public void testBasicPermissions() {
    int STATUS_OK = 200;
    int FORBIDDEN = 403;
    int PROMPT_FOR_CREDENTIALS = 401;
    checkRules(makeMap("resource", "/update/json/docs", "httpMethod", "POST", "userPrincipal", "unknownuser", "collectionRequests", "freeforall", "handler", new UpdateRequestHandler()), STATUS_OK);
    checkRules(makeMap("resource", "/update/json/docs", "httpMethod", "POST", "userPrincipal", "tim", "collectionRequests", "mycoll", "handler", new UpdateRequestHandler()), STATUS_OK);
    checkRules(makeMap("resource", "/update/json/docs", "httpMethod", "POST", "collectionRequests", "mycoll", "handler", new UpdateRequestHandler()), PROMPT_FOR_CREDENTIALS);
    checkRules(makeMap("resource", "/schema", "userPrincipal", "somebody", "collectionRequests", "mycoll", "httpMethod", "POST", "handler", new SchemaHandler()), FORBIDDEN);
    checkRules(makeMap("resource", "/schema", "userPrincipal", "somebody", "collectionRequests", "mycoll", "httpMethod", "GET", "handler", new SchemaHandler()), STATUS_OK);
    checkRules(makeMap("resource", "/schema/fields", "userPrincipal", "somebody", "collectionRequests", "mycoll", "httpMethod", "GET", "handler", new SchemaHandler()), STATUS_OK);
    checkRules(makeMap("resource", "/schema", "userPrincipal", "somebody", "collectionRequests", "mycoll", "httpMethod", "POST", "handler", new SchemaHandler()), FORBIDDEN);
    checkRules(makeMap("resource", "/admin/collections", "userPrincipal", "tim", "requestType", RequestType.ADMIN, "collectionRequests", null, "httpMethod", "GET", "handler", new CollectionsHandler(), "params", new MapSolrParams(singletonMap("action", "LIST"))), STATUS_OK);
    checkRules(makeMap("resource", "/admin/collections", "userPrincipal", null, "requestType", RequestType.ADMIN, "collectionRequests", null, "httpMethod", "GET", "handler", new CollectionsHandler(), "params", new MapSolrParams(singletonMap("action", "LIST"))), STATUS_OK);
    checkRules(makeMap("resource", "/admin/collections", "userPrincipal", null, "requestType", RequestType.ADMIN, "collectionRequests", null, "handler", new CollectionsHandler(), "params", new MapSolrParams(singletonMap("action", "CREATE"))), PROMPT_FOR_CREDENTIALS);
    checkRules(makeMap("resource", "/admin/collections", "userPrincipal", null, "requestType", RequestType.ADMIN, "collectionRequests", null, "handler", new CollectionsHandler(), "params", new MapSolrParams(singletonMap("action", "RELOAD"))), PROMPT_FOR_CREDENTIALS);
    checkRules(makeMap("resource", "/admin/collections", "userPrincipal", "somebody", "requestType", RequestType.ADMIN, "collectionRequests", null, "handler", new CollectionsHandler(), "params", new MapSolrParams(singletonMap("action", "CREATE"))), FORBIDDEN);
    checkRules(makeMap("resource", "/admin/collections", "userPrincipal", "tim", "requestType", RequestType.ADMIN, "collectionRequests", null, "handler", new CollectionsHandler(), "params", new MapSolrParams(singletonMap("action", "CREATE"))), STATUS_OK);
    checkRules(makeMap("resource", "/select", "httpMethod", "GET", "handler", new SearchHandler(), "collectionRequests", singletonList(new CollectionRequest("mycoll")), "userPrincipal", "joe"), FORBIDDEN);
    Map rules = (Map) Utils.fromJSONString(permissions);
    ((Map) rules.get("user-role")).put("cio", "su");
    ((List) rules.get("permissions")).add(makeMap("name", "all", "role", "su"));
    checkRules(makeMap("resource", ReplicationHandler.PATH, "httpMethod", "POST", "userPrincipal", "tim", "handler", new ReplicationHandler(), "collectionRequests", singletonList(new CollectionRequest("mycoll"))), FORBIDDEN, rules);
    checkRules(makeMap("resource", ReplicationHandler.PATH, "httpMethod", "POST", "userPrincipal", "cio", "handler", new ReplicationHandler(), "collectionRequests", singletonList(new CollectionRequest("mycoll"))), STATUS_OK, rules);
    checkRules(makeMap("resource", "/admin/collections", "userPrincipal", "tim", "requestType", AuthorizationContext.RequestType.ADMIN, "collectionRequests", null, "handler", new CollectionsHandler(), "params", new MapSolrParams(singletonMap("action", "CREATE"))), STATUS_OK, rules);
    rules = (Map) Utils.fromJSONString(permissions);
    ((List) rules.get("permissions")).add(makeMap("name", "core-admin-edit", "role", "su"));
    ((List) rules.get("permissions")).add(makeMap("name", "core-admin-read", "role", "user"));
    ((Map) rules.get("user-role")).put("cio", "su");
    ((List) rules.get("permissions")).add(makeMap("name", "all", "role", "su"));
    permissions = Utils.toJSONString(rules);
    checkRules(makeMap("resource", "/admin/cores", "userPrincipal", null, "requestType", RequestType.ADMIN, "collectionRequests", null, "handler", new CoreAdminHandler(null), "params", new MapSolrParams(singletonMap("action", "CREATE"))), PROMPT_FOR_CREDENTIALS);
    checkRules(makeMap("resource", "/admin/cores", "userPrincipal", "joe", "requestType", RequestType.ADMIN, "collectionRequests", null, "handler", new CoreAdminHandler(null), "params", new MapSolrParams(singletonMap("action", "CREATE"))), FORBIDDEN);
    checkRules(makeMap("resource", "/admin/cores", "userPrincipal", "joe", "requestType", RequestType.ADMIN, "collectionRequests", null, "handler", new CoreAdminHandler(null), "params", new MapSolrParams(singletonMap("action", "STATUS"))), STATUS_OK);
    checkRules(makeMap("resource", "/admin/cores", "userPrincipal", "cio", "requestType", RequestType.ADMIN, "collectionRequests", null, "handler", new CoreAdminHandler(null), "params", new MapSolrParams(singletonMap("action", "CREATE"))), STATUS_OK);
    rules = (Map) Utils.fromJSONString(permissions);
    List permissions = (List) rules.get("permissions");
    //remove the 'all' permission
    permissions.remove(permissions.size() - 1);
    permissions.add(makeMap("name", "test-params", "role", "admin", "path", "/x", "params", makeMap("key", Arrays.asList("REGEX:(?i)val1", "VAL2"))));
    this.permissions = Utils.toJSONString(rules);
    checkRules(makeMap("resource", "/x", "userPrincipal", null, "requestType", RequestType.UNKNOWN, "collectionRequests", "go", "handler", new DumpRequestHandler(), "params", new MapSolrParams(singletonMap("key", "VAL1"))), PROMPT_FOR_CREDENTIALS);
    checkRules(makeMap("resource", "/x", "userPrincipal", null, "requestType", RequestType.UNKNOWN, "collectionRequests", "go", "handler", new DumpRequestHandler(), "params", new MapSolrParams(singletonMap("key", "Val1"))), PROMPT_FOR_CREDENTIALS);
    checkRules(makeMap("resource", "/x", "userPrincipal", null, "requestType", RequestType.UNKNOWN, "collectionRequests", "go", "handler", new DumpRequestHandler(), "params", new MapSolrParams(singletonMap("key", "Val1"))), PROMPT_FOR_CREDENTIALS);
    checkRules(makeMap("resource", "/x", "userPrincipal", "joe", "requestType", RequestType.UNKNOWN, "collectionRequests", "go", "handler", new DumpRequestHandler(), "params", new MapSolrParams(singletonMap("key", "Val1"))), FORBIDDEN);
    checkRules(makeMap("resource", "/x", "userPrincipal", "joe", "requestType", RequestType.UNKNOWN, "collectionRequests", "go", "handler", new DumpRequestHandler(), "params", new MapSolrParams(singletonMap("key", "Val2"))), STATUS_OK);
    checkRules(makeMap("resource", "/x", "userPrincipal", "joe", "requestType", RequestType.UNKNOWN, "collectionRequests", "go", "handler", new DumpRequestHandler(), "params", new MapSolrParams(singletonMap("key", "VAL2"))), FORBIDDEN);
    checkRules(makeMap("resource", "/update", "userPrincipal", "solr", "requestType", RequestType.UNKNOWN, "collectionRequests", "go", "handler", new UpdateRequestHandler(), "params", new MapSolrParams(singletonMap("key", "VAL2"))), FORBIDDEN, (Map<String, Object>) Utils.fromJSONString("{user-role:{" + "      admin:[admin_role]," + "      update:[update_role]," + "      solr:[read_role]}," + "    permissions:[" + "      {name:update, role:[admin_role,update_role]}," + "      {name:read, role:[admin_role,update_role,read_role]}" + "]}"));
}
Also used : SchemaHandler(org.apache.solr.handler.SchemaHandler) SearchHandler(org.apache.solr.handler.component.SearchHandler) CollectionRequest(org.apache.solr.security.AuthorizationContext.CollectionRequest) DumpRequestHandler(org.apache.solr.handler.DumpRequestHandler) CollectionsHandler(org.apache.solr.handler.admin.CollectionsHandler) CoreAdminHandler(org.apache.solr.handler.admin.CoreAdminHandler) MapSolrParams(org.apache.solr.common.params.MapSolrParams) Collections.singletonList(java.util.Collections.singletonList) LinkedList(java.util.LinkedList) List(java.util.List) ReplicationHandler(org.apache.solr.handler.ReplicationHandler) UpdateRequestHandler(org.apache.solr.handler.UpdateRequestHandler) HashMap(java.util.HashMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Utils.makeMap(org.apache.solr.common.util.Utils.makeMap)

Aggregations

MapSolrParams (org.apache.solr.common.params.MapSolrParams)50 HashMap (java.util.HashMap)33 SolrCore (org.apache.solr.core.SolrCore)24 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)20 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)19 SolrParams (org.apache.solr.common.params.SolrParams)14 Test (org.junit.Test)14 LinkedHashMap (java.util.LinkedHashMap)11 ArrayList (java.util.ArrayList)10 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)10 SolrException (org.apache.solr.common.SolrException)6 UpdateRequestHandler (org.apache.solr.handler.UpdateRequestHandler)6 SolrQueryRequestBase (org.apache.solr.request.SolrQueryRequestBase)6 Map (java.util.Map)4 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)4 Utils.makeMap (org.apache.solr.common.util.Utils.makeMap)4 IOException (java.io.IOException)3 SchemaField (org.apache.solr.schema.SchemaField)3 StringWriter (java.io.StringWriter)2 Collections.singletonMap (java.util.Collections.singletonMap)2