本章主要使用 lucene 创建索引。
实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class News {
private Integer id;
private String title;
private String content;
private Integer reply;
}
创建索引
@Test
public void testCreateIndex() throws IOException {
List<News> news = initNews();
List<Document> documents = initDocuments(news);
//索引保存的目录
Path path = Paths.get("D:\\engine");
Directory directory = FSDirectory.open(path);
//默认使用的是 StandardAnalyzer 解析器。
IndexWriterConfig config = new IndexWriterConfig();
IndexWriter writer = new IndexWriter(directory, config);
for (Document document : documents) {
writer.addDocument(document);
}
//提交后,会创建索引
writer.commit();
//是流,所以关闭
writer.close();
}
private List<News> initNews() {
List<News> newsList = new ArrayList<>();
News news1 = new News();
news1.setId(1);
news1.setTitle("create web page");
news1.setContent("Learn how to create a web page with Spring MVC.");
news1.setReply(672);
newsList.add(news1);
News news2 = new News();
news2.setId(2);
news2.setTitle("Serving Web Content");
news2.setContent("Serving Web Content with Spring MVC");
news2.setReply(995);
newsList.add(news2);
News news3 = new News();
news3.setId(3);
news3.setTitle("apache lucene");
news3.setContent("To review the documentation, read the main documentation page, located at:\n" +
"`docs/index.html`To build Lucene or its documentation for a source distribution, see BUILD.txt");
news3.setReply(1872);
newsList.add(news3);
return newsList;
}
private List<Document> initDocuments(List<News> news) {
List<Document> documents = new ArrayList<>();
Document document = null;
int size = news.size();
for (int i = 0; i < size; i++) {
News ne = news.get(i);
document = new Document();
TextField idField = new TextField("id", ne.getId().toString(), Field.Store.YES);
TextField titleField = new TextField("title", ne.getTitle(), Field.Store.YES);
TextField contentField = new TextField("content", ne.getContent(), Field.Store.YES);
TextField replyField = new TextField("reply", ne.getReply().toString(), Field.Store.YES);
document.add(idField);
document.add(titleField);
document.add(contentField);
document.add(replyField);
documents.add(document);
}
return documents;
}
删除索引
Path path = Paths.get("D:\\engine");
Directory directory = FSDirectory.open(path);
IndexWriterConfig config = new IndexWriterConfig();
IndexWriter writer = new IndexWriter(directory, config);
Term term = new Term("id","1");
writer.deleteDocuments(term);
writer.commit();
writer.close();
更新索引
该文档如果有,就进行更新,如果没有,就进行添加。
Path path = Paths.get("D:\\engine");
Directory directory = FSDirectory.open(path);
IndexWriterConfig config = new IndexWriterConfig();
IndexWriter writer = new IndexWriter(directory, config);
Term term = new Term("id","1");
Document document = new Document();
News news1 = new News();
news1.setId(5);
news1.setTitle("create web page");
news1.setContent("Learn how to create a web page with Spring MVC.");
news1.setReply(672);
TextField idField = new TextField("id", news1.getId().toString(), Field.Store.YES);
TextField titleField = new TextField("title", news1.getTitle(), Field.Store.YES);
TextField contentField = new TextField("content", news1.getContent(), Field.Store.YES);
TextField replyField = new TextField("reply", news1.getReply().toString(), Field.Store.YES);
document.add(idField);
document.add(titleField);
document.add(contentField);
document.add(replyField);
writer.updateDocument(term, document);
writer.commit();
writer.close();