最近、私のプロジェクトでは、データベースでの重複検索に lucene を使用しましたが、完全に機能しています。しかし、今では lucene インデックスを暗号化する必要があり、外部ライブラリを使用するのではなく、lucene 自体が提供する暗号化機能を探すように求められました。
LUCENE-2228 AES 暗号化ディレクトリを見つけて、小さな POC を作成しました。問題は、インデックスを再作成すると、次のエラーが発生することです。
java.lang.RuntimeException: File already Exists
at org.apache.lucene.util.AESWriter.<init>(AESWriter.java:117)
at org.apache.lucene.store.AESDirectory$AESIndexOutput.<init>
(AESDirectory.java:187)
at org.apache.lucene.store.AESDirectory.createOutput(AESDirectory.java:72)
at org.apache.lucene.index.SegmentInfos.finishCommit(SegmentInfos.java:939)
at org.apache.lucene.index.IndexWriter.finishCommit(IndexWriter.java:3539)
at org.apache.lucene.index.IndexWriter.commitInternal(IndexWriter.java:3529)
at org.apache.lucene.index.IndexWriter.closeInternal(IndexWriter.java:1879)
at org.apache.lucene.index.IndexWriter.close(IndexWriter.java:1822)
at org.apache.lucene.index.IndexWriter.close(IndexWriter.java:1786)
at org.apache.lucene.test.indexing.main(indexing.java:45)
これが私のコードです:
public class indexing
{
private static final byte[] KEY =
new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't',
'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };
public static void main(String[] args) throws Exception
{
Directory INDEX_DIR = new AESDirectory(new File("index1"),KEY);
Connection conn=null;
SnowballAnalyzer analyzer=new SnowballAnalyzer(Version.LUCENE_30,"English");
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql:///lucene", "abcd", "abcd");
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_34, analyzer);
IndexWriter writer = new IndexWriter(INDEX_DIR, config);
writer.deleteAll();
//writer.flush();
System.out.println("Indexing to directory '" + INDEX_DIR + "'...");
long starttime=System.currentTimeMillis();
indexDocs(writer, conn);
writer.optimize();
writer.close();
long endtime=System.currentTimeMillis();
long timetaken=TimeUnit.MILLISECONDS.convert(endtime - starttime,TimeUnit.MILLISECONDS);
System.out.println("Time taken to do indexing is "+timetaken+"ms");
}
catch (Exception e)
{
e.printStackTrace();
}
}
static void indexDocs(IndexWriter writer, Connection conn) throws Exception
{
//String sql = "select qid,question from tblquestions";
String sql = "select qid,question from tblquestions";
Statement stmt = conn.createStatement();
stmt.setFetchSize(Integer.MIN_VALUE);
ResultSet rs = stmt.executeQuery(sql);
Integer count = 0;
while (rs.next())
{
count ++;
Document d = new Document();
d.add(new Field("qid", rs.getString("qid"), Field.Store.YES, Field.Index.NOT_ANALYZED));
d.add(new Field("question", rs.getString("question"), Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(d);
}
System.out.println("count: " + count);
}
}
誰でもこの問題を解決するのを手伝ってくれますか? または lucene インデックスの暗号化について考えてください。