The main features of SolrCloud are:

Key features include powerful full-text search, click display, face search, dynamic clustering, database integration, rich document (such as Word, PDF) processing, and spatial search, and it has a high degree of scalability, providing fault tolerant distributed search and indexing.

1) Centralized configuration information 2) automatic fault tolerance 3) near real-time search 4) automatic load balancing during queries

Figure 1. Rough structure diagram of Collection

! [osjp-vertx-fig1-thumb-100159570-orig](http://7xvmgi.com1.z0.glb.clouddn.com/1.png)<br>
1, 2, download, download https://zookeeper.apache.org/ http://lucene.apache.org/solr/ 3, download at http://tomcat.apache.org/

Here’s where I start my learning journey:

Start by copying three tomcats such as tomcat-server_1 port: 8080 tomcat-server_2 port: 8090 tomcat-server_3 port: 8100

One, in the reproduction solr – 4.7.0 respectively. The following example/webapps solr war decompression is copied to the tomcat server_1, tomcat server_2, tomcat – server_3 webapp for solr below, Create three folders (solr_home_1, solr_home_2, and solr_home_3) for storing solr data. Solr_home_1, solr_home_2, solr_home_3, solr_home_3 Figure 2:

! [osjp-vertx-fig1-thumb-100159570-orig](http://7xvmgi.com1.z0.glb.clouddn.com/2.png)<br>

Solr_home_1, solr_home_2, solr_home_3, solr_home_3

<solr> < solrCloud > < STR name="host">${host:}</ STR > <int name="hostPort">${jetty.port:8080}</int>// Tomcat port < STR name="hostContext">${hostContext:solr}</str> <int name="zkClientTimeout">${zkClientTimeout:30000}</int> <bool name="genericCoreNodeNames">${genericCoreNodeNames:true}</bool> </solrcloud> <shardHandlerFactory name="shardHandlerFactory" class="HttpShardHandlerFactory"> <int name="socketTimeout">${socketTimeout:0}</int> <int Name ="connTimeout">${connTimeout:0}</int> </shardHandlerFactory> </solr>

Three, respectively into the tomcat – server_1, tomcat server_2, tomcat server_3 webapp below the solr below, and then go to the WEB – a WEB under the INF. XML, respectively, the following list:

<env-entry> <env-entry-name>solr/home</env-entry-name> <env-entry-value>D:/solrCloud/solr_home_1</env-entry-value>// <env-entry-type>java.lang.String</env-entry-type> </env-entry> <env-entry> <env-entry-name>solr/home</env-entry-name> <env-entry-value>D:/solrCloud/solr_home_2</env-entry-value>// Env-entry-value >// <env-entry-type>java.lang.String</env-entry-type> </env-entry> <env-entry> <env-entry-name>solr/home</env-entry-name> <env-entry-value>D:/solrCloud/solr_home_3</env-entry-value>// Define the address of the solr index <env-entry-type>java.lang.String</env-entry-type> </env-entry>

Catalina. bat in bin of tomcat-server_1, tomcat-server_2, tomcat-server_3

set java_opts=-Dsolr.home=d:/solrCloud/solr_home_1 -Dbootstrap_confdir=D:/solrCloud/solr_home_1/collection1/conf Set jAVA_OPTS = -dSolr. Home = D :/solrCloud/solr_home_2 - DzkHost = 127.0.0.1:2181 set JAVA_OPTS = - Dsolr. Home = d: / solrCloud/solr_home_3 - DzkHost = 127.0.0.1:2181

5. Duplicate three ZOO for example:

ZK-server_1 port: 2181 ZK-server_2 port: 2182 ZK-server_3 port: 2182 ZK-server_3 port: 2181 ZK-server_2 port: 2182 ZK-server_3 port: Create two folders under zk-server_1, zk-server_2 and zk-server_3 as data and logs, respectively, to store data and logs. "Myid" = "myid"; "myid" = "myid"; D:\solrCloud\zk-server_1\conf\zoo.cfg: dataDir=D:/solrCloud/zk-server_1/data dataLogDir=D:/solrCloud/zk-server_1/logs clientPort=2181 Server.1 = 127.0.1:2888:3888 Server.2 =127.0.0.1:2889:3889 Server.3 =127.0.0.1:2890:3890 The other two configurations are as above.

Figure 3:

! [osjp-vertx-fig1-thumb-100159570-orig](http://7xvmgi.com1.z0.glb.clouddn.com/3.png)<br> Let's start zk_server1, zk_server2, zk_server3. The diagram below:

Figure 4:

! [osjp vertx - fig1 - thumb - 100159570 - orig] (http://7xvmgi.com1.z0.glb.clouddn.com/4.jpeg) < br > starts up, the first error is because can't find the other two zk, complains. This is not important, three start good will not report an error. < br > here we start tomcat server_1, tomcat server_2, tomcat - server_3. <br>

Figure 5:

! [osjp-vertx-fig1-thumb-100159570-orig](http://7xvmgi.com1.z0.glb.clouddn.com/5.jpeg)

Figure 6:

! [osjp-vertx-fig1-thumb-100159570-orig](http://7xvmgi.com1.z0.glb.clouddn.com/6.png)

create

http://127.0.0.1:8080/solr/admin/collections?action=CREATE&name=collection1&numShards=3&replicationFactor=2&maxShardsPer Node=2&collection.configName=myconf

Reload interface

http://127.0.0.1:8080/solr/admin/collections?action=RELOAD&name=collection1

delete

http://127.0.0.1:8080s/solr/admin/collections?action=DELETE&name=collection1

Listing 1. SolrCloud. Java

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Map;
    import org.apache.solr.client.solrj.SolrQuery;
    import org.apache.solr.client.solrj.SolrServer;
    import org.apache.solr.client.solrj.SolrServerException;
    import org.apache.solr.client.solrj.impl.CloudSolrServer;
    import org.apache.solr.client.solrj.response.QueryResponse;
    import org.apache.solr.common.SolrDocument;
    import org.apache.solr.common.SolrDocumentList;
    import org.apache.solr.common.SolrInputDocument;
    public class SolrCloud {
            private static CloudSolrServer cloudSolrServer;
            private static synchronized CloudSolrServer getCloudSolrServer(final String zkHost) {
            if (cloudSolrServer == null) {
                try {
                        cloudSolrServer = new CloudSolrServer(zkHost);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
            }

        return cloudSolrServer;
    }
    //添加索引
    private void addIndex(SolrServer solrServer) {
        try {
            SolrInputDocument doc1 = new SolrInputDocument();
            doc1.addField("id", "421245251215121452521251");
            doc1.addField("title", "张三");
            SolrInputDocument doc2 = new SolrInputDocument();
            doc2.addField("id", "4224558524254245848524243");
            doc2.addField("title", "李四");

            SolrInputDocument doc3 = new SolrInputDocument();
            doc3.addField("id", "4543543458643541324153453");
            doc3.addField("title", "王五");

            Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
            docs.add(doc1);
            docs.add(doc2);
            docs.add(doc3);

            solrServer.add(docs);
            solrServer.commit();

        } catch (SolrServerException e) {
            System.out.println("Add docs Exception !!!");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("Unknowned Exception!!!!!");
            e.printStackTrace();
        }

    }
   // 搜索
    public void search(SolrServer solrServer, String String) {
        SolrQuery query = new SolrQuery();
        query.setQuery(String);
        try {
            QueryResponse response = solrServer.query(query);
            SolrDocumentList docs = response.getResults();
            for (SolrDocument doc : docs) {
                for (Map.Entry<String, Object> entry : doc) {
                    System.out.println(entry.getKey()+"="+entry.getValue());
                } 
            }
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("Unknowned Exception!!!!");
            e.printStackTrace();
        }
    }
    //删除
    public void deleteAllIndex(SolrServer solrServer) {
        try {
            solrServer.deleteByQuery("*:*");// delete everything!
            solrServer.commit();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("Unknowned Exception !!!!");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        final String zkHost = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183";
        final String defaultCollection = "collection1";
        final int zkClientTimeout = 20000;
        final int zkConnectTimeout = 1000;

        CloudSolrServer cloudSolrServer = getCloudSolrServer(zkHost);
        System.out.println("The Cloud SolrServer Instance has benn created!");
        cloudSolrServer.setDefaultCollection(defaultCollection);
        cloudSolrServer.setZkClientTimeout(zkClientTimeout);
        cloudSolrServer.setZkConnectTimeout(zkConnectTimeout);
        cloudSolrServer.connect();
        System.out.println("The cloud Server has been connected !!!!");
        SolrCloud test = new SolrCloud();
         test.addIndex(cloudSolrServer);
         test.search(cloudSolrServer, "id:*");
         test.deleteAllIndex(cloudSolrServer);
         test.search(cloudSolrServer, "id:*");
         System.out.println("hashCode"+test.hashCode());
         cloudSolrServer.shutdown();
    }
}

reading

Build the Web server and messaging system using VERT.x

Application of RabbitMQ in Distributed System


The author information (the force spectrum SuYun LeapCloud _ cloud services research and development team members: JinYang Zhang starting address: https://blog.maxleap.cn/archives/977

Welcome to WeChat Subscription Number: From Mobile to the Cloud (MaxLeap – Making Mobile Development Faster)

Welcome to join our LEAPCloud activity QQ group: 555973817, we will do technology sharing activities from time to time. If you need to reprint, please pay attention to bring the author’s information column and this We Media official number: LEAP-LEAPCLOUD, respect the original author’s labor achievements ~ thanks for your cooperation ~