Hola, estoy haciendo un buscador en lucene. ya tengo creado el indice, el indexar y el buscador. todo funciona correctamente.
El buscador me busca correctamente pero me da el directorio donde esta ese archivo indexado y me gustaría que me diera el articulo que concuerde con esa búsqueda para mostrarla al usuario que a consultado ese tema. este es mi código de buscar, no se como hacerlo.
otra consulta como podría acceder a ese contenido para poder darle formato luego a la hora de mostrarlo (parsearlo).
Código HTML:
import org.apache.lucene.document.Document;
import org.apache.lucene.search.*;
import org.apache.lucene.store.*;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import java.io.File;
import java.util.Date;
public class Searcher {
public static void main(String[] args) throws Exception {
String[] argsv=new String[2];
argsv[0]=new String("c:\\indexdir");
argsv[1]=new String("alimentarias");
File indexDir = new File(argsv[0]);
String q = argsv[1];
if (!indexDir.exists() || !indexDir.isDirectory()) {
throw new Exception(indexDir + " no existe o no es undirectorio. ");
}
search(indexDir, q);
}
public static void search(File indexDir, String q)throws Exception {
Directory fsDir = FSDirectory.getDirectory(indexDir, false);
IndexSearcher is = new IndexSearcher(fsDir);
Query query = QueryParser.parse(q, "contents",new StandardAnalyzer());
long start = new Date().getTime();
Hits hits = is.search(query);
long end = new Date().getTime();
System.out.println
("Se han encontrado " + hits.length() +" documento(s) (en " + (end - start) +" millisegundos) relacionados con la palabra '" +q + "' en los directorios: ");
for (int i = 0; i < hits.length(); i++) {
Document doc = hits.doc(i);
System.out.println(doc.get("filename"));
}
}
}
un saludo y gracias