You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by nitin gopi <ni...@gmail.com> on 2009/03/07 15:21:34 UTC

doubt in adding a field in document

hi all, i am having error in my code. the line giving error is bold in the
code.the error is cannot find symbol.

thank you
nitin


import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.Date;

// import org.apache.lucene;
  import org.apache.lucene.analysis.Analyzer;
  import org.apache.lucene.analysis.standard.StandardAnalyzer;
  import org.apache.lucene.document.Document;
  import org.apache.lucene.document.Field;
  import org.apache.lucene.index.IndexWriter;

/**
 * This class demonstrates the process of creating an index with Lucene
 * for text files in a directory.
 */
public class TextFileIndexer {
 public static void main(String[] args) throws Exception{
   //fileDir is the directory that contains the text files to be indexed
   File   fileDir  = new File("C:\\files_to_index ");

   //indexDir is the directory that hosts Lucene's index files
   File   indexDir = new File("C:\\luceneIndex");
   Analyzer luceneAnalyzer = new StandardAnalyzer();
   IndexWriter indexWriter = new IndexWriter(indexDir,luceneAnalyzer,true);
   File[] textFiles  = fileDir.listFiles();
   long startTime = new Date().getTime();

   //Add documents to the index
   for(int i = 0; i < textFiles.length; i++){
     if(textFiles[i].isFile() && textFiles[i].getName().endsWith(".txt")){
       System.out.println("File " + textFiles[i].getCanonicalPath()
              + " is being indexed");
       Reader textReader = new FileReader(textFiles[i]);
       Document document = new Document();
       Field field1 = new Field("content",textReader);
       document.add(field1);
      * Field field2 = new Field("path",textFiles[i].getPath());*
       document.add(field2);
     // document.add(new Field("path",textFiles[i].getPath()));
       indexWriter.addDocument(document);
     }
   }

   indexWriter.optimize();
   indexWriter.close();
   long endTime = new Date().getTime();

   System.out.println("It took " + (endTime - startTime)
              + " milliseconds to create an index for the files in the
directory "
              + fileDir.getPath());
  }
}

Re: doubt in adding a field in document

Posted by Erick Erickson <er...@gmail.com>.
Didn't you post this already? Have you really looked
at the Field documentation as was suggested last time?

The short form is that there is no Field constructor like
new Field("path",textFiles[i].getPath());

For plain strings, use the form:
*Field<http://lucene.apache.org/java/2_4_0/api/org/apache/lucene/document/Field.html#Field%28java.lang.String,%20java.lang.String,%20org.apache.lucene.document.Field.Store,%20org.apache.lucene.document.Field.Index%29>
*(String<http://java.sun.com/j2se/1.4/docs/api/java/lang/String.html?is-external=true>
name,
String<http://java.sun.com/j2se/1.4/docs/api/java/lang/String.html?is-external=true>
value,
Field.Store<http://lucene.apache.org/java/2_4_0/api/org/apache/lucene/document/Field.Store.html>
store,
Field.Index<http://lucene.apache.org/java/2_4_0/api/org/apache/lucene/document/Field.Index.html>
 index)

In the future you'd get better help if you posted the actual
error showing the symbol that couldn't be found. Providing
this little information doesn't help us help you very well.

Erick

On Sat, Mar 7, 2009 at 9:21 AM, nitin gopi <ni...@gmail.com> wrote:

> hi all, i am having error in my code. the line giving error is bold in the
> code.the error is cannot find symbol.
>
> thank you
> nitin
>
>
> import java.io.File;
> import java.io.FileReader;
> import java.io.Reader;
> import java.util.Date;
>
> // import org.apache.lucene;
>  import org.apache.lucene.analysis.Analyzer;
>  import org.apache.lucene.analysis.standard.StandardAnalyzer;
>  import org.apache.lucene.document.Document;
>  import org.apache.lucene.document.Field;
>  import org.apache.lucene.index.IndexWriter;
>
> /**
>  * This class demonstrates the process of creating an index with Lucene
>  * for text files in a directory.
>  */
> public class TextFileIndexer {
>  public static void main(String[] args) throws Exception{
>   //fileDir is the directory that contains the text files to be indexed
>   File   fileDir  = new File("C:\\files_to_index ");
>
>   //indexDir is the directory that hosts Lucene's index files
>   File   indexDir = new File("C:\\luceneIndex");
>   Analyzer luceneAnalyzer = new StandardAnalyzer();
>   IndexWriter indexWriter = new IndexWriter(indexDir,luceneAnalyzer,true);
>   File[] textFiles  = fileDir.listFiles();
>   long startTime = new Date().getTime();
>
>   //Add documents to the index
>   for(int i = 0; i < textFiles.length; i++){
>     if(textFiles[i].isFile() && textFiles[i].getName().endsWith(".txt")){
>       System.out.println("File " + textFiles[i].getCanonicalPath()
>              + " is being indexed");
>       Reader textReader = new FileReader(textFiles[i]);
>       Document document = new Document();
>       Field field1 = new Field("content",textReader);
>       document.add(field1);
>      * Field field2 = new Field("path",textFiles[i].getPath());*
>       document.add(field2);
>     // document.add(new Field("path",textFiles[i].getPath()));
>       indexWriter.addDocument(document);
>     }
>   }
>
>   indexWriter.optimize();
>   indexWriter.close();
>   long endTime = new Date().getTime();
>
>   System.out.println("It took " + (endTime - startTime)
>              + " milliseconds to create an index for the files in the
> directory "
>              + fileDir.getPath());
>  }
> }
>