Advertisement
Guest User

Indexación

a guest
Jun 8th, 2011
893
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. public class CreaIndice {
  2.     public static void main(String[] args) throws Exception {
  3.  
  4.         File directorioGuardarIndice = new File("c:/Temp/indice");
  5.         File directorioCorpus = new File("c:/Temp/documentos");
  6.  
  7.         Directory RecorreDirectorio = FSDirectory.open(directorioGuardarIndice);
  8.  
  9.         Analyzer analizador = new StandardAnalyzer(Version.LUCENE_31);
  10.  
  11.         IndexWriterConfig configuracionIndice = new IndexWriterConfig(
  12.                 Version.LUCENE_31, analizador);
  13.  
  14.         IndexWriter Indice = new IndexWriter(RecorreDirectorio, configuracionIndice);
  15.  
  16.         File[] archivos = directorioCorpus.listFiles();
  17.         for (int i = 0; i < archivos.length; i++) {
  18.             File f = archivos[i];
  19.             if (!f.isDirectory() && !f.isHidden() && f.exists() && f.canRead()
  20.                     && (f.getName().endsWith(".txt"))) {
  21.                 System.out.println("Indexing " + f.getCanonicalPath());
  22.                 Document doc = new Document();
  23.                 // Campo nombre archivo
  24.                 Field camponombre = new Field("rutaArchivo", f.getName(),
  25.                         Field.Store.YES, Field.Index.ANALYZED_NO_NORMS);
  26.                 doc.add(camponombre);
  27.                 // Campo contenido del archivo
  28.                 Field campocontenido = new Field("contenido", new                                    FileReader(f));
  29.                 doc.add(campocontenido);
  30.                
  31.                 Indice.addDocument(doc);
  32.             }
  33.         }
  34.         Indice.optimize();
  35.         Indice.close();
  36.         System.out.println("el numero de documentos indexados es "
  37.                 + Indice.numDocs());
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement