Crear Indice con Lucene

package com.codemonkey.clases; import java.io.File; import java.io.FileReader; import java.io.Reader; 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.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriter.MaxFieldLength; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.SimpleFSDirectory; import org.apache.lucene.util.Version; public class LuceneExample { public static final String files = "C:\\Users\\USUARIO\\Desktop\\Programacion\\testJava\\directorio"; public static final String index = "C:\\Users\\USUARIO\\Desktop\\Programacion\\testJava\\directorio\\index"; public static void createIndex() { System.out.println("Crando indices...."); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30); try { // Store the index in file Directory directory = new SimpleFSDirectory(new File(index)); IndexWriter iwriter = new IndexWriter(directory, analyzer, true, MaxFieldLength.UNLIMITED); File dir = new File(files); File[] files = dir.listFiles(); for (File file : files) { System.out.println(file.getPath()); Document doc = new Document(); doc.add(new Field("path", file.getPath(), Field.Store.YES, Field.Index.ANALYZED)); Reader reader = new FileReader(file.getCanonicalPath()); doc.add(new Field("contents", reader)); iwriter.addDocument(doc); } iwriter.optimize(); iwriter.close(); } catch (Exception e) { e.printStackTrace(); } } public static void searchIndex(String searchString) { System.out.println("Buscando.... '" + searchString + "'"); try { IndexReader reader = IndexReader.open(FSDirectory.open(new File( index)), true); IndexSearcher searcher = new IndexSearcher(reader); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);// construct our usual analyzer QueryParser qp = new QueryParser(Version.LUCENE_30, "contents", analyzer); Query query = qp.parse(searchString); // parse the query and construct the Query object TopDocs hits = searcher.search(query, 100); // run the query if (hits.totalHits == 0) { System.out.println("No se ha encontrado."); } else { for (int i = 0; i < hits.totalHits; i++) { Document doc = searcher.doc(hits.scoreDocs[i].doc); // get the next document String url = doc.get("path"); // get its path field System.out.println("Encontrado en :: " + url); } } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { createIndex(); searchIndex("Hola"); } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.