greenstone.org greenstone wiki greenstone trac planet greenstone

Changeset 16429

Show
Ignore:
Timestamp:
2008-07-16 15:58:24 (6 months ago)
Author:
mdewsnip
Message:

Changed to use Lucene's IndexWriter? class instead of GS2IndexModifier, since IndexModifier? is deprecated in Lucene 2.3.2. Also fixed up some nasty formatting.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • indexers/trunk/lucene-gs/src/org/greenstone/LuceneWrapper/GS2LuceneDelete.java

    r13686 r16429  
    3333 
    3434import java.io.IOException; 
    35 import java.util.Arrays; 
    36 import java.util.Enumeration; 
    37 import java.util.Vector; 
    38  
    39 import org.apache.lucene.analysis.Analyzer; 
    4035import org.apache.lucene.analysis.standard.StandardAnalyzer; 
    41 import org.apache.lucene.document.Document
    42 import org.apache.lucene.document.Field
     36import org.apache.lucene.index.IndexWriter
     37import org.apache.lucene.index.Term
    4338 
    4439 
     
    6156    { 
    6257        // Parse arguments 
     58        String index_path = ""; 
    6359        int node_id = -1; 
    64         String index_path = ""; 
    6560 
    6661        for (int i = 0; i < args.length; i += 2) 
    67             { 
    68                 if (args[i].equals("--index")) 
    69                     { 
    70                         index_path = args[i + 1]; 
    71                     } 
    72                 else if (args[i].equals("--nodeid")) 
    73                     { 
    74                         String temp = args[i + 1]; 
    75                         node_id = Integer.parseInt(temp); 
    76                         temp = null; // Off to the gc with you! 
    77                     } 
    78                 else  
    79                     { 
    80                         System.out.println("Error! Unknown argument: " + args[i]); 
    81                         GS2LuceneDelete.printUsage(); 
    82                     } 
    83             } 
     62        { 
     63            if (args[i].equals("--index")) 
     64            { 
     65                index_path = args[i + 1]; 
     66            } 
     67            else if (args[i].equals("--nodeid")) 
     68            { 
     69                node_id = Integer.parseInt(args[i + 1]); 
     70            } 
     71            else  
     72            { 
     73                System.out.println("Error! Unknown argument: " + args[i]); 
     74                GS2LuceneDelete.printUsage(); 
     75                System.exit(0); 
     76            } 
     77        } 
    8478 
    8579        // Check arguments 
    86         if(index_path.equals("")) 
    87             { 
    88                 System.out.println("Error! Missing index path"); 
    89                 GS2LuceneDelete.printUsage(); 
    90             } 
    91         if(node_id == -1) 
    92             { 
    93                 System.out.println("Error! Missing or invalid Node ID"); 
    94                 GS2LuceneDelete.printUsage(); 
    95             } 
    96  
     80        if (index_path.equals("")) 
     81        { 
     82            System.out.println("Error! Missing index path"); 
     83            GS2LuceneDelete.printUsage(); 
     84            System.exit(0); 
     85        } 
     86        if (node_id == -1) 
     87        { 
     88            System.out.println("Error! Missing or invalid Node ID"); 
     89            GS2LuceneDelete.printUsage(); 
     90            System.exit(0); 
     91        } 
    9792 
    9893        // Instantiate deletor, and perform the delete 
     
    10297        deletor = null; 
    10398    } 
    104     /** main() **/ 
    10599 
    106     /** **/ 
    107     private boolean debug = true; 
    108  
    109     /** **/ 
    110     private GS2IndexModifier index_modifier; 
    111  
    112     /** Constructor which takes the path to the Lucene index to be edited. 
    113      * 
    114      *  @param  index_path The full path to the index directory as a String 
    115      */ 
    116     public GS2LuceneDelete(String index_path) 
    117         throws IOException 
    118     { 
    119         Analyzer analyzer = new StandardAnalyzer(); 
    120         index_modifier = new GS2IndexModifier(index_path, analyzer); 
    121     } 
    122     /** GS2LuceneDelete **/ 
    123  
    124     /** When called prints a debug message but only if debugging is enabled. 
    125      */ 
    126     public void debug(String message) 
    127     { 
    128         if(debug) 
    129             { 
    130                 System.err.println(message); 
    131             } 
    132     } 
    133     /** debug() **/ 
    134  
    135     /** Destructor which unallocates connection to Lucene. 
    136      */ 
    137     public void destroy() 
    138         throws IOException 
    139     { 
    140         index_modifier.close(); 
    141         index_modifier = null; 
    142     } 
    143  
    144     /** Delete the indicated document from the Lucene index. This process is 
    145      *  very similar to the initial step of index editing. 
    146      * 
    147      *  @param  node_id   The unique identifier of a Lucene document as an 
    148      *                    integer 
    149      */ 
    150     public void deleteDocument(int node_id) 
    151         throws IOException 
    152     { 
    153         debug("GS2LuceneDelete.deleteDocument(" + node_id + ")"); 
    154         debug("- Initial number of documents in index: " + index_modifier.docCount()); 
    155         // Retrieve the document requested 
    156         int doc_num = index_modifier.getDocNumByNodeID(node_id); 
    157         if (doc_num != -1) 
    158             { 
    159                 debug("* Found document #" + doc_num); 
    160                 // Retrieve the actual document 
    161                 Document document = index_modifier.document(doc_num); 
    162                 // Remove the document from the index before modifying 
    163                 index_modifier.deleteDocument(doc_num); 
    164                 debug("* Removed document from index"); 
    165             } 
    166         else 
    167             { 
    168                 debug("- No such document!"); 
    169             } 
    170         debug("- Final number of documents in index: " + index_modifier.docCount()); 
    171     } 
    172     /** editIndex() **/ 
    173100 
    174101    /** Display program usage message. 
     
    184111        System.out.println("             same as the docnum in the GDBM"); 
    185112        System.out.println(""); 
    186         System.exit(0); 
    187113    } 
    188     /** printUsage() **/ 
    189114 
     115 
     116    /** **/ 
     117    private boolean debug = true; 
     118 
     119    /** **/ 
     120    private IndexWriter index_writer = null; 
     121 
     122 
     123    /** Constructor which takes the path to the Lucene index to be edited. 
     124     * 
     125     *  @param  index_path The full path to the index directory as a String 
     126     */ 
     127    public GS2LuceneDelete(String index_path) 
     128        throws IOException 
     129    { 
     130        index_writer = new IndexWriter(index_path, new StandardAnalyzer()); 
     131    } 
     132 
     133 
     134    /** When called prints a debug message but only if debugging is enabled. 
     135     */ 
     136    public void debug(String message) 
     137    { 
     138        if (debug) 
     139        { 
     140            System.err.println(message); 
     141        } 
     142    } 
     143 
     144 
     145    /** Destructor which unallocates connection to Lucene. 
     146     */ 
     147    public void destroy() 
     148        throws IOException 
     149    { 
     150        index_writer.close(); 
     151        index_writer = null; 
     152    } 
     153 
     154 
     155    /** Delete the indicated document from the Lucene index. This process is 
     156     *  very similar to the initial step of index editing. 
     157     * 
     158     *  @param  node_id   The unique identifier of a Lucene document as an 
     159     *                    integer 
     160     */ 
     161    public void deleteDocument(int node_id) 
     162        throws IOException 
     163    { 
     164        debug("GS2LuceneDelete.deleteDocument(" + node_id + ")"); 
     165        debug("- Initial number of documents in index: " + index_writer.docCount()); 
     166        index_writer.deleteDocuments(new Term("nodeid", "" + node_id)); 
     167        debug("- Final number of documents in index: " + index_writer.docCount()); 
     168    } 
    190169} 
    191 /** class GS2LuceneDelete **/