J'intègre une API de recherche lucene 3.6 dans une application de bureau java. Le système lucene utilise un répertoire de système de fichiers pour stocker l'index. Le code pour créer le répertoire d'index, l'indexeur et ajouter des documents à l'index.Remplissage du répertoire de fichiers de Lucene 3.6 Index
Les données pour l'index sont collectées à partir d'une base de données Derby. Les champs de la table de base de données sont ajoutés en tant que champs au document lucene. Ainsi, chaque ligne de la table de base de données est représentée sous la forme d'un seul document lucene.
Ma question est, est-il un moyen de vérifier le répertoire de l'index et si il n'est pas rempli avec des documents lucene puis le peupler. Ou pour ignorer en repeuplant l'index lorsqu'il est déjà en population.
Code de création du fichier d'index.
public File createIndexDir() throws IOException, SQLException
{
//Check if directory exist
if(!userDir.exists())
{ userDir.mkdir();
System.out.println(" Index directory created at " + userDir.getAbsolutePath());
}
return userDir.getAbsoluteFile();
}
Code
pour créer écrivain index
public void createIndexWriter() throws IOException, SQLException
{
indexDir = createIndexDir();
if(iw == null)
{
try {
// create some index
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
IndexWriterConfig IWConfig = new IndexWriterConfig(Version.LUCENE_36, analyzer);
iw = new IndexWriter(FSDirectory.open(indexDir), IWConfig);
}
catch (CorruptIndexException ex) {
Logger.getLogger(Indexer.class.getName()).log(Level.SEVERE, null, ex);
} catch (LockObtainFailedException ex) {
Logger.getLogger(Indexer.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Indexer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
c'est le code qui rempli le fichier d'index avec des données à partir d'une base de données
public void buildIndex() throws SQLException, CorruptIndexException, IOException
{
/* Connecting to the database */
Connection con = DriverManager.getConnection(host, uName, uPass);
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sql = "SELECT * FROM APP.REGISTRY";
ResultSet rs = stmt.executeQuery(sql);
rs.beforeFirst(); //set poinyrt to begining of result set
while(rs.next())
{
Document doc = new Document();
doc.add(new Field("id",rs.getString("ID"),Field.Store.YES,Field.Index.NO));
if(rs.getString("SUBJECT")== null)
{ doc.add(new Field("subject","",Field.Store.YES,Field.Index.ANALYZED)); }
else {
doc.add(new Field("subject",rs.getString("SUBJECT"),Field.Store.YES,Field.Index.ANALYZED));
}
if(rs.getString("LETTER_FROM")== null)
{ doc.add(new Field("letter_from"," ",Field.Store.YES,Field.Index.ANALYZED)); }
else {
doc.add(new Field("letter_from",rs.getString("LETTER_FROM"),Field.Store.YES,Field.Index.ANALYZED));
}
doc.add(new Field("date_of_letter",DateTools.dateToString(rs.getDate("DATE_OF_LETTER"),
DateTools.Resolution.DAY),Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("date_received",DateTools.dateToString(rs.getDate("DATE_RECEIVED"),
DateTools.Resolution.DAY),Field.Store.YES,Field.Index.NO));
if(rs.getString("REMARKS")== null)
{ doc.add(new Field("remarks"," ",Field.Store.YES,Field.Index.ANALYZED)); }
else {
doc.add(new Field("remarks",rs.getString("REMARKS"),Field.Store.YES,Field.Index.ANALYZED)); }
if(rs.getDate("DATE_DISPATCHED")== null)
{ doc.add(new Field("date_dispatched",DateTools.dateToString(new Date(0L),DateTools.Resolution.DAY),Field.Store.YES,Field.Index.ANALYZED)); }
else {
doc.add(new Field("date_dispatched",DateTools.dateToString(rs.getDate("DATE_DISPATCHED"),
DateTools.Resolution.MINUTE),Field.Store.YES,Field.Index.ANALYZED));
}
if(rs.getString("OFFICE_DISPATCHED_TO")== null)
{ doc.add(new Field("office_dispatched_to"," ",Field.Store.YES,Field.Index.ANALYZED));}
else {
doc.add(new Field("office_dispatched_to",rs.getString("OFFICE_DISPATCHED_TO"),Field.Store.YES,Field.Index.ANALYZED));
}
iw.addDocument(doc);
}
iw.commit();
closeIndexWriter();
stmt.close();
rs.close();
con.close();
}
Toute idée d'une solution. applaudit à tous.
Veuillez définir la population. Comment voulez-vous gérer un scénario à demi-peuplé? – phani
désolé en peuplant je veux dire créer les champs et les documents et les ajouter à l'index lucene. – CodeAngel