1.¶àÏß³ÌË÷Òý£¬¹²Ïíͬһ¸öIndexWriter¶ÔÏó
ÕâÖÖ·½Ê½Ð§ÂʺÜÂý£¬Ö÷ÒªÔÒòÊÇÒòΪ£º
- public void addDocument(Document doc, Analyzer analyzer) throws IOException {
- SegmentInfo newSegmentInfo = buildSingleDocSegment(doc, analyzer);
- synchronized (this) {
- ramSegmentInfos.addElement(newSegmentInfo);//Õâ¾äºÜÕ¼ÓÃЧÂÊ
- maybeFlushRamSegments();
- }
- }
2 ¶àÏß³ÌË÷Òý£¬ ÏÈдµ½RAMDirectory£¬ÔÙÒ»´ÎÐÔдµ½FSDirectory
¹¦ÄÜ£ºÊ×ÏÈÏòRAMDirectoryÀïд£¬µ±´ïµ½1000¸öDocumentáᣬÔÙÏòFSDirectoryÀïд¡£
µ±¶àÏß³ÌÖ´ÐÐʱ£¬»á´óÁ¿±¨java.lang.NullPointerException
×Ô¼ºÐ´µÄ¶àÏß³ÌË÷ÒýµÄÀàΪ£¨IndexWriterServer£¬¸Ã¶ÔÏóÖ»ÔÚServerÆô¶¯Ê±³õʼ»¯Ò»´Î£©£º
- public class IndexWriterServer{
- private static IndexWriter indexWriter = null;
- //private String indexDir ;//Ë÷ÒýĿ¼£»
- private static CJKAnalyzer analyzer = null;
- private static RAMDirectory ramDir = new RAMDirectory();
- private static IndexWriter ramWriter = null;
- private static int diskFactor = 0;//ÄÚ´æÖÐÏÖÔÚÓжàÉÙDocument
- private static long ramToDistTime = 0;//ÄÚ´æÏòÓ²ÅÌдÐèÒª¶àÉÙʱ¼ä
- private int initValue = 1000;//ÄÚ´æÖдﵽ¶àÉÙDocument£¬²ÅÏòÓ²ÅÌд
- private static IndexItem []indexItems = null;
- public IndexWriterServer(String indexDir){
- initIndexWriter(indexDir);
- }
- public void initIndexWriter(String indexDir){
- boolean create = false;//ÊÇ·ñ´´½¨ÐµÄ
- analyzer = new CJKAnalyzer();
- Directory directory = this.getDirectory(indexDir);
- //ÅжÏÊÇ·ñΪË÷ÒýĿ¼
- if(!IndexReader.indexExists(indexDir)){
- create = true;
- }
- indexWriter = getIndexWriter(directory,create);
- try{
- ramWriter = new IndexWriter(ramDir, analyzer, true);
- }catch(Exception e){
- logger.info(e);
- }
- indexItems = new IndexItem[initValue+2];
- }
- /**
- * Éú³Éµ¥¸öItemË÷Òý
- */
- public boolean generatorItemIndex(IndexItem item, Current __current) throws DatabaseError, RuntimeError{
- boolean isSuccess = true;//ÊÇ·ñË÷Òý³É¹¦
- try{
- Document doc = getItemDocument(item);
- ramWriter.addDocument(doc);//¹Ø¼ü´úÂ룬´íÎó¾ÍÊÇ´ÓÕâÀﱨ³öÀ´µÄ
- indexItems[diskFactor] = item;//ΪÊý¾ÝÍÚ¾òʹÓÃ
- diskFactor ++;
- if((diskFactor % initValue) == 0){
- ramToDisk(ramDir,ramWriter,indexWriter);
- //ramWriter = new IndexWriter(ramDir, analyzer, true);
- diskFactor = 0;
- //Êý¾ÝÍÚ¾ò
- isSuccess = MiningData();
- }
- doc = null;
- logger.info("generator index item link:" + item.itemLink +" success");
- }catch(Exception e){
- logger.info(e);
- e.printStackTrace();
- logger.info("generator index item link:" + item.itemLink +" faiture");
- isSuccess = false;
- }finally{
- item = null;
- }
- return isSuccess;
- }
- public void ramToDisk(RAMDirectory ramDir, IndexWriter ramWriter,IndexWriter writer){
- try{
- ramWriter.close();//¹Ø¼ü´úÂ룬°ÑfileMap¸³ÖµÎªnullÁË
- ramWriter = new IndexWriter(ramDir, analyzer, true);//ÖØÐ¹¹½¨Ò»¸öramWriter¶ÔÏó¡£ÒòΪËüµÄfileMapΪnullÁË,µ«ÊǺÃÏñ²¢Ã»ÓÐÌ«´ó×÷ÓÃ
- Directory ramDirArray[] = new Directory[1];
- ramDirArray[0] = ramDir;
- mergeDirs(writer, ramDirArray);
- }catch(Exception e){
- logger.info(e);
- }
- }
- /**
- * ½«ÄÚ´æÀïµÄË÷ÒýÐÅϢдµ½Ó²ÅÌÀï
- * @param writer
- * @param ramDirArray
- */
- public void mergeDirs(IndexWriter writer,Directory[] ramDirArray){
- try {
- writer.addIndexes(ramDirArray);
- //optimize();
- } catch (IOException e) {
- logger.info(e);
- }
- }
- }
Ö÷ÒªÔÒò´ó¸ÅÊÇÒòΪ£ºÔÚµ÷ÓÃramWriter.close();ʱ£¬Lucene2.1ÀïRAMDirectory µÄclose()·½·¨
- public final void close() {
- fileMap = null;
- }
°ÑfileMap ¸øÖÃnullÁË£¬µ±¶àÏß³ÌÖ´ÐÐramWriter.addDocument(doc);ʱ£¬×îÖÕÖ´ÐÐRAMDirectory µÄ·½·¨£º
- public IndexOutput createOutput(String name) {
- RAMFile file = new RAMFile(this);
- synchronized (this) {
- RAMFile existing = (RAMFile)fileMap.get(name);//fileMapΪnull£¬ËùÒÔ±¨£ºNullPointerException£¬
- if (existing!=null) {
- sizeInBytes -= existing.sizeInBytes;
- existing.directory = null;
- }
- fileMap.put(name, file);
- }
- return new RAMOutputStream(file);
- }
Ìáʾ£ºÔÚÍøÉÏËÑË÷ÁËһϣ¬ºÃÏñÕâ¸öÊÇluceneµÄÒ»¸öbug£¨http://www.opensubscriber.com/message/java-user@lucene.apache.org/6227647.html£©£¬µ«ÊǺÃÏñ²¢Ã»Óиø³ö½â¾ö·½°¸¡£
3.¶àÏß³ÌË÷Òý£¬Ã¿¸öÏß³ÌÒ»¸öIndexWriter¶ÔÏó£¬Ã¿¸öIndexWriter °ó¶¨Ò»¸öFSDirectory¶ÔÏó¡£Ã¿¸öFSDirectory°ó¶¨Ò»¸ö±¾µØµÄ´ÅÅÌĿ¼£¨Î¨Ò»µÄ£©¡£µ¥¶À¿ª±ÙÒ»¸öÏ̳߳öÀ´¼à¿ØÕâЩË÷ÒýỊ̈߳¨¼à¿ØỊ̈߳©£¬Ò²¾ÍÊÇ˵¸ºÔðË÷ÒýµÄÏß³ÌË÷ÒýÍêÁËÒԺ󣬸øÕâ¸ö¼à¿ØÏ̵߳ÄqueueÀï·¢ËÍÒ»¸ö¶ÔÏó£ºqueue.add(directory);,Õâ¸ö¼à¿ØÏֳɵÄqueue¶ÔÏóÊǸöÈ«¾ÖµÄ¡£µ±Õâ¸öqueueµÄsize() > 20 ʱ£¬¼à¿ØÏß³Ì °ÑÕâ20¸öË÷ÒýĿ¼ºÏ²¢(merge):indexWriter.addIndexes(dirs);//ºÏ²¢Ë÷Òý,ºÏ²¢µ½ÕæÕýµÄË÷ÒýĿ¼Àï¡£,ºÏ²¢ÍêÁËÒÔºó£¬È»ºóɾ³ýµôÕâЩÒѾºÏ²¢Á˵ÄĿ¼¡£
µ«ÊÇÕâÑùÒ²Óм¸¸öbug:
a. ºÏ²¢Ï̵߳ÄËÙ¶È Ð¡ÓÚ Ë÷ÒýÏ̵߳ÄËÙ¶È¡£µ¼Ö Ŀ¼ԽÀ´Ô½¶à
b.¾³£»á±¨Ò»¸öÀàËÆÕâÑùµÄ´íÎó£º
2007-06-08 10:49:18 INFO [Thread-2] (IndexWriter.java:1070) - java.io.FileNotFoundException: /home/spider/luceneserver/merge/item_d28686afe01f365c5669e1f19a2492c8/_1.cfs (No such file or directory)
4.µ¥Ïß³ÌË÷Òý£¬µ÷¼¸¸ö²ÎÊýáᣬЧÂÊÒ²·Ç³£¿ì(Ë÷ÒýÒ»ÌõÐÅÏ¢´ó¸ÅÔÚ6-30 msÖ®¼ä)¡£¸Ð¾õÒ»°ãµÄÐèÇóµ¥Ï߳̾͹»ÓÃÁË¡£ÕâЩ²ÎÊýÈçÏÂ:
private int mergeFactor = 100;//´ÅÅÌÀï´ïµ½¶àÉÙáá»á×Ô¶¯ºÏ²¢
private int maxMergeDocs = 1000;//ÄÚ´æÖдﵽ¶àÉÙ»áÏò´ÅÅÌдÈë
private int minMergeDocs = 1000;//lucene2.0ÒѾȡÏûÁË
private int maxFieldLength = 2000;//Ë÷ÒýµÄ×î´óÎÄÕ³¤¶È
private int maxBufferedDocs = 10000;//Õâ¸ö²ÎÊý²»ÄÜÒª£¬Òª²»È»²»»á×Ô¶¯ºÏ²¢ÁË
µÃ³öµÄ½áÂÛÊÇ:LuceneµÄ¶àÏß³ÌË÷Òý»áÓÐЩÎÊÌ⣬Èç¹ûûÓÐÌØÊâÐèÇ󣬵¥Ï̵߳ÄЧÂʼ¸ºõ¾ÍÄÜÂú×ãÐèÇó.
Èç¹ûµ¥Ï̵߳ÄËÙ¶ÈÂú×ã²»ÁËÄãµÄÐèÇó£¬Äã¿ÉÒԶ࿪¼¸¸öÓ¦Óá£Ã¿¸öÓ¦Óö¼°ó¶¨Ò»¸öFSDirectory,È»ºóͨ¹ýsearchʱͨ¹ýRMIÈ¥ÕâЩË÷ÒýĿ¼½øÐÐËÑË÷¡£
RMI Server¶Ë,¹Ø¼üÐÔ´úÂë:
- private void initRMI(){
- //µÚÒ»°²È«ÅäÖÃ
- if (System.getSecurityManager() == null) {
- System.setSecurityManager( new RMISecurityManager() );
- }
- //×¢²á
- startRMIRegistry(serverUrl);
- SearcherWork searcherWork = new SearcherWork("//" + serverUrl + "/" + bindName, directory);
- searcherWork.run();
- }
- public class SearcherWork {
- // Logger
- private static Logger logger = Logger.getLogger(SearcherWork.class);
- private String serverUrl =null;
- private Directory directory =null;
- public SearcherWork(){
- }
- public SearcherWork(String serverUrl, Directory directory){
- this.serverUrl = serverUrl;
- this.directory = directory;
- }
- public void run(){
- try{
- Searchable searcher = new IndexSearcher(directory);
- SearchService service = new SearchService(searcher);
- Naming.rebind(serverUrl, service);
- logger.info("RMI Server bind " + serverUrl + " success");
- }catch(Exception e){
- logger.info(e);
- System.out.println(e);
- }
- }
- }
- public class SearchService extends RemoteSearchable implements Searchable {
- public SearchService (Searchable local) throws RemoteException {
- super(local);
- }
- }
¿Í»§¶Ë¹Ø¼üÐÔ´úÂë:
- RemoteLuceneConnector rlc= new RemoteLuceneConnector();
- RemoteSearchable[] rs= rlc.getRemoteSearchers();
- MultiSearcher multi = new MultiSearcher(rs);
- Hits hits = multi.search(new TermQuery(new Term("content","Öйú")));
°²»ÕлªµçÄÔѧУרҵְҵ¹æ»®Ê¦ÎªÄãÌṩ¸ü¶à°ïÖú¡¾ÔÚÏß×Éѯ¡¿