summaryrefslogtreecommitdiff
path: root/src/main/java/org/dyndns/andreasbaumann/LuceneAnalyzer.java
blob: 670146047bb4b6f0522648f3ca41431ea79381e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 *   LuceneAnalyzer - Lucene Index Analyzer
 *
 *   Copyright (C) 2006  Andreas Baumann
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
package org.dyndns.andreasbaumann;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import jargs.gnu.CmdLineParser;
import jargs.gnu.CmdLineParser.Option;
import jargs.gnu.CmdLineParser.OptionException;

import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexReader.FieldOption;
import org.apache.lucene.index.TermEnum;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.index.TermPositions;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;

import java.util.logging.LogManager;
import java.util.logging.Logger;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.core.CoreDescriptor;
import org.apache.solr.core.SolrResourceLoader;
import org.apache.solr.core.SolrConfig;
import org.apache.solr.core.SolrCore;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.search.SolrIndexSearcher;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.apache.solr.schema.SchemaField;
import org.apache.solr.schema.FieldType;

/**
 * Lucene index analyzer. Works for file system indexes only (not
 * for indexes fully in RAM or in different persistence systems as
 * a JDBCDirectory.
 *
 * Note: requires at least lucene 3.0!
 *
 * @author Andreas Baumann, <abaumann@yahoo.com>
 * @version $Id$
 */

public class LuceneAnalyzer
{
	private static final String programName = "lucenanalyzer";
	private static final String versionString = "0.0.4";
	
	static {
		LogManager.getLogManager( ).reset( );
		Logger globalLogger = Logger.getLogger( "" /* java.util.logging.Logger.GLOBAL_LOGGER_NAME */ );
		globalLogger.setLevel( java.util.logging.Level.OFF );  
	}

	private static void printGlobalInfo( IndexReader indexReader, boolean printHeaders,
	                                     boolean isSolr, SolrIndexSearcher solrSearch ) throws IOException
	{
		if( printHeaders ) {
			System.out.println( "Global Information:" );
			System.out.println( "===================" );
		}

		System.out.println( "\tnumber of documents: " + indexReader.numDocs( ) );

		// we should get the number of features differently, this is inefficient, but Lucene
		// has no notion of global statistics (because the default weighting schema doesn't
		// make use of it!)
		int nofFeatures = 0;
		int nofTokens = 0;
		TermEnum terms = indexReader.terms( );
		while( terms.next( ) ) {
		   Term term = terms.term( );
			int df = terms.docFreq( );
			nofFeatures++;
			nofTokens += df;
		}
		System.out.println( "\ttotal number of features: " + nofFeatures );
		System.out.println( "\ttotal number of tokens: " + nofTokens );

		System.out.println( "\tversion: " + indexReader.getVersion( ) );
		System.out.println( "\tstill current: " + indexReader.isCurrent( ) );
		
		//TODO: we don't get segment information!
		//System.out.println( "is optimized:" + segmentInfos.size( ) == 1 && !indexReader.hasDeletions( ) );
		System.out.println( "\tmaximal document number: " + indexReader.maxDoc( ) );
		System.out.println( "\thas deletions: " + indexReader.hasDeletions( ) );

		if( isSolr ) {
			System.out.println( "\tSolr version: " + solrSearch.getVersion( ) );
		}

		System.out.println( "" );
	}
	
	private static void printFieldInfoPerFieldOption( IndexReader indexReader, IndexReader.FieldOption fieldOption )
	{
		System.out.println( "Fields of type '" + fieldOption + "':" );
		Collection fields = indexReader.getFieldNames( fieldOption );
		Iterator fieldIterator = fields.iterator( );
		while( fieldIterator.hasNext( ) ) {
			String field = (String)fieldIterator.next( );
			if( field != null && !field.equals( "" ) ) {
				// TODO: define data type here!
				System.out.println( "\t" + field.toString( ) );
			}
		}
	}
	
	private static void printFieldInfo( IndexReader indexReader, boolean printHeaders,
	                                    boolean isSolr, SolrIndexSearcher solrSearch ) throws IOException
	{
		if( printHeaders ) {
			System.out.println( "Field Information:" );
			System.out.println( "==================" );
		}
		
		// print info per Lucene field type
		printFieldInfoPerFieldOption( indexReader, IndexReader.FieldOption.ALL );
		printFieldInfoPerFieldOption( indexReader, IndexReader.FieldOption.INDEXED );
		printFieldInfoPerFieldOption( indexReader, IndexReader.FieldOption.INDEXED_NO_TERMVECTOR );
		printFieldInfoPerFieldOption( indexReader, IndexReader.FieldOption.INDEXED_WITH_TERMVECTOR );
		printFieldInfoPerFieldOption( indexReader, IndexReader.FieldOption.OMIT_POSITIONS );
		printFieldInfoPerFieldOption( indexReader, IndexReader.FieldOption.OMIT_TERM_FREQ_AND_POSITIONS );
		printFieldInfoPerFieldOption( indexReader, IndexReader.FieldOption.STORES_PAYLOADS );
		printFieldInfoPerFieldOption( indexReader, IndexReader.FieldOption.TERMVECTOR );
		printFieldInfoPerFieldOption( indexReader, IndexReader.FieldOption.TERMVECTOR_WITH_OFFSET );
		printFieldInfoPerFieldOption( indexReader, IndexReader.FieldOption.TERMVECTOR_WITH_POSITION );
		printFieldInfoPerFieldOption( indexReader, IndexReader.FieldOption.TERMVECTOR_WITH_POSITION_OFFSET );
		printFieldInfoPerFieldOption( indexReader, IndexReader.FieldOption.UNINDEXED );

		System.out.println( "" );
	}
	
	private static void printTerms( IndexReader indexReader,
	                                boolean printHeaders,
	                                boolean isSolr, SolrIndexSearcher solrSearch,
	                                boolean printDocNumbers,
                                        boolean printPositions ) throws IOException
	{
		if( printHeaders ) {
			System.out.println( "Terms:" );
			System.out.println( "======" );
		}
		TermEnum terms = indexReader.terms( );
		while( terms.next( ) ) {
		   Term term = terms.term( );
			// the df is stored in the iterator and not in the term, weird...
			int df = terms.docFreq( );
			String field = term.field( );
			String text = term.text( );
			if( isSolr ) {
				IndexSchema schema = solrSearch.getSchema( );
				SchemaField schemaField = schema.getField( field );
				FieldType fieldType = schemaField.getType( );
				text = fieldType.indexedToReadable( text );
			}
			if( !printDocNumbers && !printPositions ) {
				System.out.print( field + "\t" + text + "\t" + df );
			} else {
				System.out.print( field + "\t" + text );
			}

			if( printDocNumbers ) {
				TermDocs termDocs = indexReader.termDocs( term );
				boolean first = true;
				while( termDocs.next( ) ) {
					if( first ) {
						System.out.print( "\t" + termDocs.doc( ) );
						first = false;
					} else {
						System.out.print( "," + termDocs.doc( ) );
					}
				}
				termDocs.close( );
			} else if( printPositions ) {
				TermPositions termPositions = indexReader.termPositions( term );
				boolean first = true;
				while( termPositions.next( ) ) {
					if( first ) {
						System.out.print( "\t" + termPositions.doc( ) );
						first = false;
					} else {
						System.out.print( "," + termPositions.doc( ) );
					}
					
					for( int i = 0; i < termPositions.freq( ); i++ ) {
						int position = termPositions.nextPosition( );
						if( i == 0 ) {
							System.out.print( "[" );
						}
						System.out.print( position );
						if( i < termPositions.freq( ) - 1 ) {
							System.out.print( "," );
						}
						if( i == termPositions.freq( ) - 1 ) {
							System.out.print( "]" );
						}
					}
				}
				termPositions.close( );
			}
			
			System.out.println( "" );
		}
		System.out.println( "" );
	}

 	private static List optionHelpStrings = new ArrayList();
	
	private static Option addHelp( Option option, String helpString )
	{
		if( option.shortForm( ) != null ) {
			optionHelpStrings.add( " -" + option.shortForm( ) + "/--" + option.longForm( ) + ": " + helpString  );
		} else {
			optionHelpStrings.add( " --" + option.longForm( ) + ": " + helpString  );
		}
		return option;
	}

	private static void printUsage()
	{
		System.err.println( "Usage: " + programName + " [options] <lucene index dir>\n" );
		for( Iterator i = optionHelpStrings.iterator( ); i.hasNext( ); ) {
			System.err.println( i.next( ) );
		}
	}
	
	private static void printVersion()
	{
		System.out.println( "Version " + LuceneAnalyzer.class.getName( ) + " " + versionString );
	}
                	
	public static void main( String[] args ) throws IOException
	{
		CmdLineParser parser = new CmdLineParser( );
		
		// default options, well-known, should always be around
		Option verbose = addHelp( parser.addBooleanOption( 'v', "verbose" ),
		                          "print extra verbosity information" );
		Option help =    addHelp( parser.addBooleanOption( 'h', "help" ),
		                          "print this help message" );
		Option version = addHelp( parser.addBooleanOption( "version" ),
		                          "print version information" );
		Option globals = addHelp( parser.addBooleanOption( 'g', "globals" ),
		                          "print global statistics" );
		Option fields =  addHelp( parser.addBooleanOption( 'f', "fields" ),
		                          "print field information" );
		Option terms =   addHelp( parser.addBooleanOption( 't', "terms" ),
		                          "print statistics per term" );
		Option headers = addHelp( parser.addBooleanOption( 'H', "headers" ),
		                          "print headers for sections" );
		Option solr   =  addHelp( parser.addBooleanOption( 's', "solr" ),
		                          "treat index as a Solr index, indexDir is the Solr base dir" );

		// read the command line options
		try {
			parser.parse( args );
		} catch( OptionException e ) {
			System.err.println( e.getMessage( ) );
			printUsage( );
			System.exit( 1 );
		}
		
		if( (Boolean)parser.getOptionValue( help, Boolean.FALSE ) ) {
			printUsage( );
			System.exit( 0 );
		}
		
		if( (Boolean)parser.getOptionValue( version, Boolean.FALSE ) ) {
			printVersion( );
			System.exit( 0 );
		}

		// verbosity as a level, increased with -vvv
		int verbosity = 0;
		while( true ) {
			Boolean verboseValue = (Boolean)parser.getOptionValue( verbose );
			if( verboseValue == null ) {
				break;
			} else {
				verbosity++;
			}
		}

		boolean printHeaders = false;
		if( (Boolean)parser.getOptionValue( headers, Boolean.FALSE ) ) {
			printHeaders = true;
		}

		boolean isSolr = false;
		if( (Boolean)parser.getOptionValue( solr, Boolean.FALSE ) ) {
			isSolr = true;
		}
		
		// read command line arguments
		String[] otherArgs = parser.getRemainingArgs( );
		
		if( otherArgs.length != 1 ) {
			System.err.println( "Missing a lucene index directory as first argument" );
			printUsage( );
			System.exit( 1 );
		}
		
		String basePath = otherArgs[0];
		String indexPath = otherArgs[0];
		if( isSolr ) {
			indexPath += "/data/index";
		}
		File indexDir = new File( indexPath );
		if( !indexDir.exists( ) ) {
			System.err.println( indexPath + " doesn't exist" );
			System.exit( 1 );
		}
		if( !indexDir.isDirectory( ) ) {
			System.err.println( indexPath + " is not a directory" );
			System.exit( 1 );
		}

		SolrIndexSearcher solrSearcher = null;
		Directory luceneDirectory = new SimpleFSDirectory( indexDir );
		IndexReader indexReader = IndexReader.open( luceneDirectory );
		if( isSolr ) {
			try {
				Properties p = System.getProperties( );
				p.setProperty( "solr.solr.home", basePath );

				CoreContainer cores = new CoreContainer( new SolrResourceLoader( basePath ) );
				SolrConfig solrConfig = new SolrConfig( basePath, SolrConfig.DEFAULT_CONF_FILE, null );
				CoreDescriptor descrCore = new CoreDescriptor( cores, "", solrConfig.getResourceLoader( ).getInstanceDir( ) );
				IndexSchema solrSchema = new IndexSchema( solrConfig, basePath + "/conf/schema.xml", null );
				SolrCore solrCore = new SolrCore( basePath, solrSchema );
				solrSearcher = new SolrIndexSearcher( solrCore, solrSchema, "test",
					luceneDirectory, true, false );
			} catch( javax.xml.parsers.ParserConfigurationException e ) {
				System.err.println( "Illegal Solr configuration: " + e );
				System.exit( 1 );
			} catch( org.xml.sax.SAXException e ) {
				System.err.println( "Illegal Solr configuration: " + e );
				System.exit( 1 );
			}
		}

		if( (Boolean)parser.getOptionValue( globals, Boolean.FALSE ) ) {
			printGlobalInfo( indexReader, printHeaders, isSolr, solrSearcher );
		}
		if( (Boolean)parser.getOptionValue( fields, Boolean.FALSE ) ) {
			printFieldInfo( indexReader, printHeaders, isSolr, solrSearcher );
		}
		if( (Boolean)parser.getOptionValue( terms, Boolean.FALSE ) ) {
			printTerms( indexReader, printHeaders, isSolr, solrSearcher, verbosity == 1, verbosity >= 2 );
		}
		
		indexReader.close( );
		
		System.exit( 0 );
	}
}