Search in sources :

Example 61 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 62 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 63 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)

Example 64 with LocalSolrQueryRequest

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

the class DirectUpdateHandlerTest method testDeleteRollback.

@Test
public void testDeleteRollback() throws Exception {
    // re-init the core
    deleteCore();
    initCore("solrconfig.xml", "schema12.xml");
    assertU(adoc("id", "A"));
    assertU(adoc("id", "B"));
    // commit "A", "B"
    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(2, duh2.addCommands.longValue());
    assertEquals(2, duh2.addCommandsCumulative.getCount());
    assertEquals(0, duh2.commitCommands.getCount());
    updater.commit(cmtCmd);
    assertEquals(0, duh2.addCommands.longValue());
    assertEquals(2, duh2.addCommandsCumulative.getCount());
    assertEquals(1, duh2.commitCommands.getCount());
    ureq.close();
    // search - "A","B" should 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("\"A\" and \"B\" should be found.", req, "//*[@numFound='2']", "//result/doc[1]/str[@name='id'][.='A']", "//result/doc[2]/str[@name='id'][.='B']");
    // delete "B"
    assertU(delI("B"));
    // search - "A","B" should be found.
    assertQ("\"A\" and \"B\" should be found.", req, "//*[@numFound='2']", "//result/doc[1]/str[@name='id'][.='A']", "//result/doc[2]/str[@name='id'][.='B']");
    // rollback "B"
    ureq = req();
    RollbackUpdateCommand rbkCmd = new RollbackUpdateCommand(ureq);
    assertEquals(1, duh2.deleteByIdCommands.longValue());
    assertEquals(1, duh2.deleteByIdCommandsCumulative.getCount());
    assertEquals(0, duh2.rollbackCommands.getCount());
    updater.rollback(rbkCmd);
    ureq.close();
    assertEquals(0, duh2.deleteByIdCommands.longValue());
    assertEquals(0, duh2.deleteByIdCommandsCumulative.getCount());
    assertEquals(1, duh2.rollbackCommands.getCount());
    // search - "B" should be found.
    assertQ("\"B\" should be found.", req, "//*[@numFound='2']", "//result/doc[1]/str[@name='id'][.='A']", "//result/doc[2]/str[@name='id'][.='B']");
    // 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 65 with LocalSolrQueryRequest

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

the class MoreLikeThisHandlerTest method testInterface.

@Test
public void testInterface() throws Exception {
    SolrCore core = h.getCore();
    MoreLikeThisHandler mlt = new MoreLikeThisHandler();
    ModifiableSolrParams params = new ModifiableSolrParams();
    SolrQueryRequestBase req = new SolrQueryRequestBase(core, params) {
    };
    // requires 'q' or single content stream
    try {
        mlt.handleRequestBody(req, new SolrQueryResponse());
    }// expected
     catch (Exception ex) {
    }
    // requires 'q' or single content stream
    try {
        ArrayList<ContentStream> streams = new ArrayList<>(2);
        streams.add(new ContentStreamBase.StringStream("hello"));
        streams.add(new ContentStreamBase.StringStream("there"));
        req.setContentStreams(streams);
        mlt.handleRequestBody(req, new SolrQueryResponse());
    }// expected
     catch (Exception ex) {
    } finally {
        req.close();
    }
    assertU(adoc("id", "42", "name", "Tom Cruise", "subword", "Top Gun", "subword", "Risky Business", "subword", "The Color of Money", "subword", "Minority Report", "subword", "Days of Thunder", "subword", "Eyes Wide Shut", "subword", "Far and Away", "foo_ti", "10"));
    assertU(adoc("id", "43", "name", "Tom Hanks", "subword", "The Green Mile", "subword", "Forest Gump", "subword", "Philadelphia Story", "subword", "Big", "subword", "Cast Away", "foo_ti", "10"));
    assertU(adoc("id", "44", "name", "Harrison Ford", "subword", "Star Wars", "subword", "Indiana Jones", "subword", "Patriot Games", "subword", "Regarding Henry"));
    assertU(adoc("id", "45", "name", "George Harrison", "subword", "Yellow Submarine", "subword", "Help", "subword", "Magical Mystery Tour", "subword", "Sgt. Peppers Lonley Hearts Club Band"));
    assertU(adoc("id", "46", "name", "Nicole Kidman", "subword", "Batman", "subword", "Days of Thunder", "subword", "Eyes Wide Shut", "subword", "Far and Away"));
    assertU(commit());
    params.set(CommonParams.Q, "id:42");
    params.set(MoreLikeThisParams.MLT, "true");
    params.set(MoreLikeThisParams.SIMILARITY_FIELDS, "name,subword");
    params.set(MoreLikeThisParams.INTERESTING_TERMS, "details");
    params.set(MoreLikeThisParams.MIN_TERM_FREQ, "1");
    params.set(MoreLikeThisParams.MIN_DOC_FREQ, "1");
    params.set("indent", "true");
    SolrQueryRequest mltreq = new LocalSolrQueryRequest(core, params);
    assertQ("morelikethis - tom cruise", mltreq, "//result/doc[1]/int[@name='id'][.='46']", "//result/doc[2]/int[@name='id'][.='43']");
    params.set(MoreLikeThisParams.BOOST, "true");
    mltreq.close();
    mltreq = new LocalSolrQueryRequest(core, params);
    assertQ("morelikethis - tom cruise", mltreq, "//result/doc[1]/int[@name='id'][.='46']", "//result/doc[2]/int[@name='id'][.='43']");
    params.set(CommonParams.Q, "id:44");
    mltreq.close();
    mltreq = new LocalSolrQueryRequest(h.getCore(), params);
    assertQ("morelike this - harrison ford", mltreq, "//result/doc[1]/int[@name='id'][.='45']");
    // test MoreLikeThis debug
    params.set(CommonParams.DEBUG_QUERY, "true");
    mltreq.close();
    mltreq = new LocalSolrQueryRequest(h.getCore(), params);
    assertQ("morelike this - harrison ford", mltreq, "//lst[@name='debug']/lst[@name='moreLikeThis']/lst[@name='44']/str[@name='rawMLTQuery']", "//lst[@name='debug']/lst[@name='moreLikeThis']/lst[@name='44']/str[@name='boostedMLTQuery']", "//lst[@name='debug']/lst[@name='moreLikeThis']/lst[@name='44']/str[@name='realMLTQuery']", "//lst[@name='debug']/lst[@name='moreLikeThis']/lst[@name='44']/lst[@name='explain']/str[@name='45']");
    // test that qparser plugins work
    params.remove(CommonParams.DEBUG_QUERY);
    params.set(CommonParams.Q, "{!field f=id}44");
    mltreq.close();
    mltreq = new LocalSolrQueryRequest(h.getCore(), params);
    assertQ(mltreq, "//result/doc[1]/int[@name='id'][.='45']");
    params.set(CommonParams.Q, "id:42");
    params.set(MoreLikeThisParams.QF, "name^5.0 subword^0.1");
    mltreq.close();
    mltreq = new LocalSolrQueryRequest(h.getCore(), params);
    assertQ("morelikethis with weights", mltreq, "//result/doc[1]/int[@name='id'][.='43']", "//result/doc[2]/int[@name='id'][.='46']");
    // test that qparser plugins work w/ the MoreLikeThisHandler
    params.set(CommonParams.QT, "/mlt");
    params.set(CommonParams.Q, "{!field f=id}44");
    mltreq.close();
    mltreq = new LocalSolrQueryRequest(h.getCore(), params);
    assertQ(mltreq, "//result/doc[1]/int[@name='id'][.='45']");
    // test that debugging works (test for MoreLikeThis*Handler*)
    params.set(CommonParams.QT, "/mlt");
    params.set(CommonParams.DEBUG_QUERY, "true");
    mltreq.close();
    mltreq = new LocalSolrQueryRequest(h.getCore(), params);
    assertQ(mltreq, "//result/doc[1]/int[@name='id'][.='45']", "//lst[@name='debug']/lst[@name='explain']");
    // params.put(MoreLikeThisParams.QF,new String[]{"foo_ti"});
    // String response = h.query(mltreq);
    // System.out.println(response);
    mltreq.close();
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrCore(org.apache.solr.core.SolrCore) ArrayList(java.util.ArrayList) SolrQueryRequestBase(org.apache.solr.request.SolrQueryRequestBase) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) ContentStream(org.apache.solr.common.util.ContentStream) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase) 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