Sometimes you have more than one Solr index and you want to merge them together into a single index.
CoreAdminHandler now supports merging one or more indexes into another index (since Solr 1.4).
http://localhost:8983/solr/admin/cores?action=mergeindexes&core=core0&indexDir=/opt/solr/core1/data/index&indexDir=/opt/solr/core2/data/index |
The above command will merge the indexes of core1 and core2 into core0. The path for this command is the 'adminPath' defined in solr.xml (default is /admin/cores).
Before executing this command, one must make sure to call commit on core1 and core2 (in order to close IndexWriter) and no writes should happen on core1 and core2 until the merge command completes. Failure to do so may corrupt the core0 index. Once the merge is completed, a commit should be called on core0 to make the changes visible to searchers.
NOTE: In this example core0 must exist and have a compatible schema with core1 and core2. The 'mergeindexes' command will not create a new core named 'core0' if it does not already exist.
Solr3.3 CoreAdminHandler also supports merging one or more cores into another core (since Solr 3.3) through a "srcCore" parameter.
http://localhost:8983/solr/admin/cores?action=mergeindexes&core=core0&srcCore=core1&srcCore=core2 |
The differences between using "srcCore" parameter and "indexDir" parameter are that:
Another way is to use the IndexMergeTool that comes as part of lucene-misc. In order to do this:
java -cp /path/to/lucene-core-VERSION.jar:/path/to/lucene-misc-VERSION.jar org.apache.lucene.misc.IndexMergeTool /path/to/newindex /path/to/index1 /path/to/index2
This will create a new index at /path/to/newindex
that contains both index1 and index2. Copy this new directory to the location of your application's solr index (move the old one aside first, of course) and start solr.
The command below assumes that the files lucene-core-3.4.0.jar
and lucene-misc-3.4.0.jar
are in the current directory:
java -cp lucene-core-3.4.0.jar:lucene-misc-3.4.0.jar org.apache.lucene.misc.IndexMergeTool ./newindex ./app1/solr/data/index ./app2/solr/data/index |
Caution:
Lucene MergeTool and Solr CoreAdmin both will give result in an index with duplicate docs as a result of merge; if there are 2 docs with the same uniqueKey in 2 shards to be merged.