Search in sources :

Example 81 with LocalSolrQueryRequest

use of org.apache.solr.request.LocalSolrQueryRequest in project lucene-solr by apache.

the class DefaultValueUpdateProcessorTest method processAdd.

/**
   * Runs a document through the specified chain, and returns the final 
   * document used when the chain is completed (NOTE: some chains may 
   * modify the document in place
   */
SolrInputDocument processAdd(final String chain, final SolrInputDocument docIn) throws IOException {
    SolrCore core = h.getCore();
    UpdateRequestProcessorChain pc = core.getUpdateProcessingChain(chain);
    assertNotNull("No Chain named: " + chain, pc);
    SolrQueryResponse rsp = new SolrQueryResponse();
    SolrQueryRequest req = new LocalSolrQueryRequest(core, new ModifiableSolrParams());
    try {
        SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
        AddUpdateCommand cmd = new AddUpdateCommand(req);
        cmd.solrDoc = docIn;
        UpdateRequestProcessor processor = pc.createProcessor(req, rsp);
        processor.processAdd(cmd);
        return cmd.solrDoc;
    } finally {
        SolrRequestInfo.clearRequestInfo();
        req.close();
    }
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrCore(org.apache.solr.core.SolrCore) UpdateRequestProcessorChain(org.apache.solr.update.processor.UpdateRequestProcessorChain) UpdateRequestProcessor(org.apache.solr.update.processor.UpdateRequestProcessor) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 82 with LocalSolrQueryRequest

use of org.apache.solr.request.LocalSolrQueryRequest in project lucene-solr by apache.

the class XsltUpdateRequestHandlerTest method testUpdate.

@Test
public void testUpdate() throws Exception {
    String xml = "<random>" + " <document>" + "  <node name=\"id\" value=\"12345\"/>" + "  <node name=\"name\" value=\"kitten\"/>" + "  <node name=\"text\" enhance=\"3\" value=\"some other day\"/>" + "  <node name=\"title\" enhance=\"4\" value=\"A story\"/>" + "  <node name=\"timestamp\" enhance=\"5\" value=\"2011-07-01T10:31:57.140Z\"/>" + " </document>" + "</random>";
    Map<String, String> args = new HashMap<>();
    args.put(CommonParams.TR, "xsl-update-handler-test.xsl");
    SolrCore core = h.getCore();
    LocalSolrQueryRequest req = new LocalSolrQueryRequest(core, new MapSolrParams(args));
    ArrayList<ContentStream> streams = new ArrayList<>();
    streams.add(new ContentStreamBase.StringStream(xml));
    req.setContentStreams(streams);
    SolrQueryResponse rsp = new SolrQueryResponse();
    UpdateRequestHandler handler = new UpdateRequestHandler();
    handler.init(new NamedList<String>());
    handler.handleRequestBody(req, rsp);
    StringWriter sw = new StringWriter(32000);
    QueryResponseWriter responseWriter = core.getQueryResponseWriter(req);
    responseWriter.write(sw, req, rsp);
    req.close();
    String response = sw.toString();
    assertU(response);
    assertU(commit());
    assertQ("test document was correctly committed", req("q", "*:*"), "//result[@numFound='1']", "//int[@name='id'][.='12345']");
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) HashMap(java.util.HashMap) SolrCore(org.apache.solr.core.SolrCore) ArrayList(java.util.ArrayList) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) ContentStream(org.apache.solr.common.util.ContentStream) MapSolrParams(org.apache.solr.common.params.MapSolrParams) StringWriter(java.io.StringWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase) Test(org.junit.Test)

Example 83 with LocalSolrQueryRequest

use of org.apache.solr.request.LocalSolrQueryRequest in project lucene-solr by apache.

the class MBeansHandlerTest method testDiff.

@Test
public void testDiff() throws Exception {
    String xml = h.query(req(CommonParams.QT, "/admin/mbeans", "stats", "true", CommonParams.WT, "xml"));
    List<ContentStream> streams = new ArrayList<>();
    streams.add(new ContentStreamBase.StringStream(xml));
    LocalSolrQueryRequest req = lrf.makeRequest(CommonParams.QT, "/admin/mbeans", "stats", "true", CommonParams.WT, "xml", "diff", "true");
    req.setContentStreams(streams);
    xml = h.query(req);
    NamedList<NamedList<NamedList<Object>>> diff = SolrInfoMBeanHandler.fromXML(xml);
    // The stats bean for SolrInfoMBeanHandler
    NamedList stats = (NamedList) diff.get("ADMIN").get("/admin/mbeans").get("stats");
    //System.out.println("stats:"+stats);
    Pattern p = Pattern.compile("Was: (?<was>[0-9]+), Now: (?<now>[0-9]+), Delta: (?<delta>[0-9]+)");
    String response = stats.get("ADMIN./admin/mbeans.requests").toString();
    Matcher m = p.matcher(response);
    if (!m.matches()) {
        fail("Response did not match pattern: " + response);
    }
    assertEquals(1, Integer.parseInt(m.group("delta")));
    int was = Integer.parseInt(m.group("was"));
    int now = Integer.parseInt(m.group("now"));
    assertEquals(1, now - was);
    xml = h.query(req(CommonParams.QT, "/admin/mbeans", "stats", "true", "key", "org.apache.solr.handler.admin.CollectionsHandler"));
    NamedList<NamedList<NamedList<Object>>> nl = SolrInfoMBeanHandler.fromXML(xml);
    assertNotNull(nl.get("ADMIN").get("org.apache.solr.handler.admin.CollectionsHandler"));
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) ContentStream(org.apache.solr.common.util.ContentStream) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase) Test(org.junit.Test)

Example 84 with LocalSolrQueryRequest

use of org.apache.solr.request.LocalSolrQueryRequest in project lucene-solr by apache.

the class SolrIndexSplitterTest method testSplitByPaths.

@Test
public void testSplitByPaths() throws Exception {
    LocalSolrQueryRequest request = null;
    try {
        // add two docs
        String id1 = "dorothy";
        assertU(adoc("id", id1));
        String id2 = "kansas";
        assertU(adoc("id", id2));
        assertU(commit());
        assertJQ(req("q", "*:*"), "/response/numFound==2");
        // find minHash/maxHash hash ranges
        List<DocRouter.Range> ranges = getRanges(id1, id2);
        request = lrf.makeRequest("q", "dummy");
        SplitIndexCommand command = new SplitIndexCommand(request, Lists.newArrayList(indexDir1.getAbsolutePath(), indexDir2.getAbsolutePath()), null, ranges, new PlainIdRouter(), null, null);
        new SolrIndexSplitter(command).split();
        Directory directory = h.getCore().getDirectoryFactory().get(indexDir1.getAbsolutePath(), DirectoryFactory.DirContext.DEFAULT, h.getCore().getSolrConfig().indexConfig.lockType);
        DirectoryReader reader = DirectoryReader.open(directory);
        assertEquals("id:dorothy should be present in split index1", 1, reader.docFreq(new Term("id", "dorothy")));
        assertEquals("id:kansas should not be present in split index1", 0, reader.docFreq(new Term("id", "kansas")));
        assertEquals("split index1 should have only one document", 1, reader.numDocs());
        reader.close();
        h.getCore().getDirectoryFactory().release(directory);
        directory = h.getCore().getDirectoryFactory().get(indexDir2.getAbsolutePath(), DirectoryFactory.DirContext.DEFAULT, h.getCore().getSolrConfig().indexConfig.lockType);
        reader = DirectoryReader.open(directory);
        assertEquals("id:dorothy should not be present in split index2", 0, reader.docFreq(new Term("id", "dorothy")));
        assertEquals("id:kansas should be present in split index2", 1, reader.docFreq(new Term("id", "kansas")));
        assertEquals("split index2 should have only one document", 1, reader.numDocs());
        reader.close();
        h.getCore().getDirectoryFactory().release(directory);
    } finally {
        // decrefs the searcher
        if (request != null)
            request.close();
    }
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) DirectoryReader(org.apache.lucene.index.DirectoryReader) PlainIdRouter(org.apache.solr.common.cloud.PlainIdRouter) Term(org.apache.lucene.index.Term) Directory(org.apache.lucene.store.Directory) Test(org.junit.Test)

Example 85 with LocalSolrQueryRequest

use of org.apache.solr.request.LocalSolrQueryRequest in project lucene-solr by apache.

the class SolrIndexSplitterTest method testSplitAlternately.

@Test
public void testSplitAlternately() throws Exception {
    LocalSolrQueryRequest request = null;
    Directory directory = null;
    try {
        // add an even number of docs
        int max = (1 + random().nextInt(10)) * 3;
        log.info("Adding {} number of documents", max);
        for (int i = 0; i < max; i++) {
            assertU(adoc("id", String.valueOf(i)));
        }
        assertU(commit());
        request = lrf.makeRequest("q", "dummy");
        SplitIndexCommand command = new SplitIndexCommand(request, Lists.newArrayList(indexDir1.getAbsolutePath(), indexDir2.getAbsolutePath(), indexDir3.getAbsolutePath()), null, null, new PlainIdRouter(), null, null);
        new SolrIndexSplitter(command).split();
        directory = h.getCore().getDirectoryFactory().get(indexDir1.getAbsolutePath(), DirectoryFactory.DirContext.DEFAULT, h.getCore().getSolrConfig().indexConfig.lockType);
        DirectoryReader reader = DirectoryReader.open(directory);
        assertEquals("split index1 has wrong number of documents", max / 3, reader.numDocs());
        reader.close();
        h.getCore().getDirectoryFactory().release(directory);
        directory = h.getCore().getDirectoryFactory().get(indexDir2.getAbsolutePath(), DirectoryFactory.DirContext.DEFAULT, h.getCore().getSolrConfig().indexConfig.lockType);
        reader = DirectoryReader.open(directory);
        assertEquals("split index2 has wrong number of documents", max / 3, reader.numDocs());
        reader.close();
        h.getCore().getDirectoryFactory().release(directory);
        directory = h.getCore().getDirectoryFactory().get(indexDir3.getAbsolutePath(), DirectoryFactory.DirContext.DEFAULT, h.getCore().getSolrConfig().indexConfig.lockType);
        reader = DirectoryReader.open(directory);
        assertEquals("split index3 has wrong number of documents", max / 3, reader.numDocs());
        reader.close();
        h.getCore().getDirectoryFactory().release(directory);
        directory = null;
    } finally {
        // decrefs the searcher
        if (request != null)
            request.close();
        if (directory != null) {
            // perhaps an assert failed, release the directory
            h.getCore().getDirectoryFactory().release(directory);
        }
    }
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) DirectoryReader(org.apache.lucene.index.DirectoryReader) PlainIdRouter(org.apache.solr.common.cloud.PlainIdRouter) Directory(org.apache.lucene.store.Directory) Test(org.junit.Test)

Aggregations

LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)107 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)61 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)49 SolrCore (org.apache.solr.core.SolrCore)47 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)41 Test (org.junit.Test)41 HashMap (java.util.HashMap)32 NamedList (org.apache.solr.common.util.NamedList)26 ArrayList (java.util.ArrayList)23 MapSolrParams (org.apache.solr.common.params.MapSolrParams)21 SolrException (org.apache.solr.common.SolrException)18 List (java.util.List)15 LinkedHashMap (java.util.LinkedHashMap)11 SolrParams (org.apache.solr.common.params.SolrParams)10 SearchComponent (org.apache.solr.handler.component.SearchComponent)10 SolrRequestHandler (org.apache.solr.request.SolrRequestHandler)10 AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)10 Map (java.util.Map)9 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)9 IOException (java.io.IOException)8