lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 7912] - Field.isIndexed() returns false for UnStored fields
Date Fri, 26 Apr 2002 18:27:08 GMT
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=7912>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=7912

Field.isIndexed() returns false for UnStored fields





------- Additional Comments From fix@idiom.com  2002-04-26 18:27 -------
Hi.  I wrote a class that'll check a lucene index and tell you all the
searchable fields in it.  The code is pasted below.  Save it as
'CheckIsIndexed.java'

Run the class using java from the command line and pass it one argument, the
name of an index to check.  If you the index does not exist, a one-document
index will
be created in the current directory with 4 fields, and the analysis will be
based on that.

In my experience, UnStored fields are not returned.  It may be that I'm doing
something wrong, in which case I hope to learn something.

The getSearchableFields(Directory directory) method may also be of interest to
other lucene users.

best, eric

-------------------- CODE BEGINS AFTER THIS LINE ----------------
/** CheckIsIndexed.java
	Test to see if isIndexed() works
	April 2002
	Eric Fixler <fix@idiom.com>
	
*/

import java.util.*;
import java.io.*;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.*;
import org.apache.lucene.document.*;
import org.apache.lucene.store.*;

public class CheckIsIndexed {
	private Directory directory = null;
	private int documentCount = 0;
	public static final boolean DEBUG = true;
	
	public boolean printAllDocuments = false;
	
	
	public CheckIsIndexed (String indexName) throws IOException {
		boolean makeIt = this.initializeIndex(indexName);
		if (makeIt) this.makeIndex();
	}
	
	private boolean initializeIndex(String indexName) throws IOException {
		File idir = new File(indexName);
		boolean isNew = ! idir.exists();
		if (isNew) idir.mkdirs();
		if (! idir.isDirectory()) throw new IOException("Directory " + indexName + " does
not exist (may be a file?)");
		System.out.println("Getting an index at "  + idir.getAbsolutePath());
		if (isNew) System.out.println("(created it)");
		this.directory = FSDirectory.getDirectory(idir, false);
		return isNew;
	}
	
	public void makeIndex() throws IOException {
		System.out.println("Opening index...");
		IndexWriter writer = new IndexWriter(directory,new StandardAnalyzer(), true);
		this.addDocument(writer);
		writer.optimize();
		writer.close();
		directory.close();
		System.out.println("Done, index closed.\n");
	}
		
	private void addDocument(IndexWriter writer) throws IOException {
		System.out.println("Adding document " + this.documentCount + "...");
		Document doc = new Document();
		doc.add(Field.UnIndexed("unindexed", "UnIndexed Field"));
		doc.add(Field.UnStored("unstored", "Unstored Field: should, however, be indexed,
no?"));
		doc.add(Field.Text("text", "Text field: should return true for isIndexed()"));
		doc.add(Field.Text("keyword", "Keyword field: should return true for isIndexed()"));
		writer.addDocument(doc);
		this.documentCount++;
	}
	
	public void checkIndex() throws IOException {
		String[] fields = this.getSearchableFields(this.directory);
		System.out.println("Searchable field names in directory: ");
		for (int i = 0; i < fields.length; i++) System.out.println("\t" + fields[i]);
		System.out.print("\n");
	}
	
	//I suspect there's probably a better way to iterate over the index, but I'm not
sure how...
	/** This method looks at every document in the index and compiles all the searchable
		fields it finds. */
	public String[] getSearchableFields(Directory dir) throws IOException {
		IndexReader reader = IndexReader.open(dir);
		int count = 0;
		Set fieldNames = new HashSet();
		for (int i = 0; i < reader.maxDoc(); i++) {
			try { 
				if (DEBUG && (reader.isDeleted(i))) { System.out.println("deleted doc " + i);

continue; }
				Document doc = reader.document(i);
				if (DEBUG && (doc == null)) { System.out.println("null doc " + i);  continue;
}
				if (this.printAllDocuments) System.out.println("Analyzing document " + i + "...");
				Enumeration en = doc.fields();
				while (en.hasMoreElements()) {
					Field field = (Field) en.nextElement();
					boolean indexed = field.isIndexed();
					if (this.printAllDocuments)  System.out.println("\t" + field.name() + ", isIndexed? :
" + indexed);
					if (indexed) fieldNames.add(field.name());	
				}
				if (this.printAllDocuments)  System.out.println("----------------------------------------");
	
			} catch (Exception e) {
				System.err.println("Error reading documents for field info: " + e + "\n\t" +
e.getMessage());
				e.printStackTrace(System.err);
				reader.close();
				if (e instanceof IOException) 		throw (IOException) 		e;
				if (e instanceof RuntimeException)	throw (RuntimeException)	e;
				return new String[0];
			}
			
		}
		reader.close();
		if (DEBUG) System.out.println("Done checking index");
		String[] rval = (String[]) fieldNames.toArray(new String[fieldNames.size()]);
		Arrays.sort(rval);
		return rval;
	}
		
	public static void main (String[] args) throws Exception {
		CheckIsIndexed ces = new CheckIsIndexed(args[0]);
		ces.checkIndex();
		
	}	
	
}

// _END_

--
To unsubscribe, e-mail:   <mailto:lucene-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:lucene-dev-help@jakarta.apache.org>


Mime
View raw message