How to dynamically loading data for solr ?

深藏阁楼爱情的钟 2022-04-13 10:16 254阅读 0赞

[b][color=green][size=large]Our application will often face all kinds of demand. Among the them , the most taboo is to the restart the server. In the application of some large, restart the service may bring some serious impact.

So, in most of the time. we should try to avoid to restart .

Then , In Solr ,how should we dynamic load some data ? such as : The dynamic load our index or Dynamic update our modified schemal and solrconfig.xml.
[/size][/color][/b]
[b][size=large]if you need, the CoreAdminRequest class is a good choice.

Example Code is given blow :
[/size][/b]

  1. /**
  2. * dynamic adding indexes
  3. *
  4. * Search technology group : 324714439
  5. *
  6. *
  7. * */
  8. public static void reloadIndex()throws Exception{
  9. CoreAdminRequest admin=new CoreAdminRequest();
  10. //some index need to be added
  11. String []indexDirs={"D:\\mysolr\\webapps\\solr\\solr_home\\collections\\collection1\\data\\index"};
  12. // String []srcCores={"collection2","collection1"};
  13. String []srcCores={};
  14. server.setBaseURL("http://localhost:9001/solr/collection2");
  15. server.deleteByQuery("*:*");//clear old index
  16. server.setBaseURL("http://localhost:9001/solr/");
  17. admin.mergeIndexes("collection2", indexDirs, srcCores, server);//copy new index into target
  18. admin.reloadCore("collection2", server);
  19. server.setBaseURL("http://localhost:9001/solr/collection2");
  20. server.commit();
  21. System.out.println("execute successful !");
  22. }
  23. /*
  24. * Dynamic update SolrCore
  25. *
  26. * avoid to the restart service
  27. *
  28. * **/
  29. public static void reloadCore()throws Exception{
  30. HttpSolrServer s=new HttpSolrServer("http://localhost:9003/solr");
  31. CoreAdminRequest core=new CoreAdminRequest();
  32. core.reloadCore("collection1", s);
  33. System.out.println("reload success!");
  34. s.commit();
  35. }

[b][size=large][color=olive]
Some is marked on the images below part, can be easy dynamically loaded .
[/color][/size][/b]
[img]http://dl2.iteye.com/upload/attachment/0096/8299/7fbdb7f9-5301-3f85-a65f-e9f9576e8365.jpg\[/img\]

  1. /**
  2. * Method 2
  3. *
  4. * some new method
  5. * you don't reload core again .
  6. *
  7. * **/
  8. public static void reloadIndex3()throws Exception{
  9. HttpSolrServer server=new HttpSolrServer("http://localhost:9004/solr/collection2");
  10. CoreAdminRequest admin=new CoreAdminRequest();
  11. //some index need to be added
  12. String []indexDirs={"C:\\Users\\qindongliang\\Desktop\\test\\mysolr\\webapps\\solr\\solr_home\\collections\\collection1\\data\\index"};
  13. String []srcCores={};
  14. server.deleteByQuery("*:*");//clear old index
  15. admin.mergeIndexes("collection2", indexDirs, srcCores, new HttpSolrServer("http://localhost:9004/solr"));//copy new index into target
  16. server.commit();
  17. System.out.println("execute successful !");
  18. }

[b][color=red][size=x-large]
Restart the service is bad , You need dynamic loading !
[/size][/color][/b]

发表评论

表情:
评论列表 (有 0 条评论,254人围观)

还没有评论,来说两句吧...

相关阅读