Lucene學習篇之修改/刪除索引
阿新 • • 發佈:2018-12-15
前言:
修改示例:
import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.StringField; import org.apache.lucene.document.TextField; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.Term; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; import org.wltea.analyzer.lucene.IKAnalyzer; import java.io.File; import java.io.IOException; /** * 索引修改 */ public class LunceneUpdate { public static void main(String[] args) throws IOException { /** * 更新索引本質是先根據條件查詢索引在刪除索引,再建立索引 */ FSDirectory fsDirectory = FSDirectory.open(new File("E://test")); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LATEST, new IKAnalyzer()); IndexWriter indexWriter = new IndexWriter(fsDirectory, indexWriterConfig); Document document = new Document(); document.add(new StringField("id","112", Field.Store.YES)); document.add(new TextField("content", "隨便寫的枸杞日誌內容軟體", Field.Store.YES)); //執行更新 indexWriter.updateDocument(new Term("id","112"),document); indexWriter.commit(); indexWriter.close(); } }
刪除示例:
import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.StringField; import org.apache.lucene.document.TextField; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.Term; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; import org.wltea.analyzer.lucene.IKAnalyzer; import java.io.File; import java.io.IOException; /** * 刪除索引 */ public class LunceneDelete { public static void main(String[] args) throws IOException { /** * 刪除索引本質是先根據條件查詢索引在刪除索引,再建立索引 */ FSDirectory fsDirectory = FSDirectory.open(new File("E://test")); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LATEST, new IKAnalyzer()); IndexWriter indexWriter = new IndexWriter(fsDirectory, indexWriterConfig); Document document = new Document(); document.add(new StringField("id","113", Field.Store.YES)); document.add(new TextField("content", "無所謂", Field.Store.YES)); indexWriter.addDocument(document);//寫入索引 //執行刪除 //1.條件刪除 // indexWriter.deleteDocuments(new Term("id","113")); // indexWriter.deleteDocuments(NumericRangeQuery.newIntRange("id",112,140,false,false)); //2.刪除所有 indexWriter.deleteAll(); indexWriter.commit(); indexWriter.close(); } }