Lucene(3)—索引操作

本章主要使用 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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值