give u to test embeded solr:

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.core.CoreContainer;

public class EmbededSolrTest {
        
        private static int commitNum = 5000;
        
        private static String path =
"/home/solr/Rollin/solr-4.1.0/embeddedExample";
        
        /**
         * @param args
         * @throws Exception
         */
        public static void main(String[] args) throws Exception {
                if(args != null) {
                        if(args.length > 0) {
                                path = args[0].trim();
                        }
                        if(args.length > 1) {
                                commitNum = Integer.parseInt(args[1].trim());
                        }
                }
                //path = "D:\\program\\solr\\41embededtest";
                System.setProperty("solr.solr.home", path);
                CoreContainer.Initializer initializer = new 
CoreContainer.Initializer();
                CoreContainer coreContainer = initializer.initialize();
                EmbeddedSolrServer server = new 
EmbeddedSolrServer(coreContainer, "");
                addIndex(server);
                //query(server);
                //deleteAllDoc(server);
        }
        
        public static void query(SolrServer server) throws Exception {
                try {
                        SolrQuery q = new SolrQuery();
                        q.setQuery("*:*");
                        q.setStart(0);
                        q.setRows(20);
                        SolrDocumentList list = server.query(q).getResults();
                        System.out.println(list.getNumFound());
                } catch(Exception e) {
                        e.printStackTrace();
                } finally {
                        server.shutdown();
                }
        }
        
        public static void deleteAllDoc(SolrServer server) throws Exception {
                try {
                        server.deleteByQuery("*:*");
                        server.commit();
                        query(server);
                } catch(Exception e) {
                        e.printStackTrace();
                } finally {
                        server.shutdown();
                }
        }
        
        public static void addIndex(SolrServer solrServer) throws IOException,
ParseException {
                
                String path = "index";
                Analyzer analyzer = new SimpleAnalyzer(Version.LUCENE_35);
                //Analyzer analyzer = new SimpleAnalyzer();
                Directory directonry = FSDirectory.open(new File(path));
                IndexReader ireader = IndexReader.open(directonry);
                IndexSearcher isearcher = new IndexSearcher(ireader);
                QueryParser parser = new QueryParser(Version.LUCENE_35, "", 
analyzer);
                Query query = parser.parse("*:*");
                TopDocs hits = isearcher.search(query, null, 1000000);
                System.out.println("find size: " + hits.totalHits);
                java.net.InetAddress addr = java.net.InetAddress.getLocalHost();
                String computerName = addr.getHostName();
                //insert2Solr(solrServer, isearcher, hits);
                long beginTime = System.currentTimeMillis();
                long totalTime = 0;
                System.out.println("begin time: " + beginTime);
                try {
                        Collection<SolrInputDocument> docs = new 
ArrayList<SolrInputDocument>();
                        for(int i = 0; i < hits.scoreDocs.length; i ++ ) {
                                SolrInputDocument doc = new SolrInputDocument();
                                Document hitDoc = 
isearcher.doc(hits.scoreDocs[i].doc);
                                doc.addField("id", i + "a" + computerName +
Thread.currentThread().getId());
                                
                                
                                doc.addField("text", hitDoc.get("text"));
                                docs.add(doc);
                                if((i + 1) % commitNum == 0) {
                                        long t = System.currentTimeMillis();
                                        solrServer.add(docs);
                                        solrServer.commit();
                                        System.err.println(computerName + " " + 
Thread.currentThread().getId()
+ " : " + (System.currentTimeMillis() - t));
                                        totalTime = System.currentTimeMillis() 
- t + totalTime;
                                        docs.clear();
                                }
                        }
                        if(docs.size() > 0) {
                                long t = System.currentTimeMillis();
                                solrServer.add(docs);
                                solrServer.commit();
                                System.err.println(computerName + " " + 
Thread.currentThread().getId() +
" : " + (System.currentTimeMillis() - t));
                                totalTime = System.currentTimeMillis() - t + 
totalTime;
                        }
                } catch(MalformedURLException e) {
                        e.printStackTrace();
                } catch(Exception e) {
                        e.printStackTrace();
                } finally {
                }
                System.out.println("end time: " + 
Thread.currentThread().getId() + " \t "
+ (System.currentTimeMillis() - beginTime));
                System.out.println("end time total: " + 
Thread.currentThread().getId() + "
t \t" + totalTime);
                solrServer.shutdown();
        }



--
View this message in context: 
http://lucene.472066.n3.nabble.com/Embedded-Solr-tp481608p4047562.html
Sent from the Solr - User mailing list archive at Nabble.com.

Reply via email to