Lucene笔记(2)

关于Lucene的增删改查的demo


实体

public class Article {
    private int id;
    private String title;
    private String author;
    private String content;
    private String link;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public String getLink() {
        return link;
    }
    public void setLink(String link) {
        this.link = link;
    }
}

DAO层

public class LuceneDao {
    /*
     * crud都是通过indexwirter完成的
     */
    public void addIndex(Article article) throws Exception{
        IndexWriter indexWriter = LuceneUtils.getIndexWriter();
        Document document = ArticleUtils.articleToDocument(article);
        indexWriter.addDocument(document);
        indexWriter.close();
    }

    public void delIndex(String fieldName,String fieldValue) throws IOException{
        IndexWriter indexWriter = LuceneUtils.getIndexWriter();
        Term term = new Term(fieldName,fieldValue);
        indexWriter.deleteDocuments(term);
        //没有close就没有刷新
        indexWriter.close();
    }

    public void updateIndex(String fieldName,String fieldValue,Article article)throws IOException{
        IndexWriter indexWriter = LuceneUtils.getIndexWriter();

        Term term = new Term(fieldName,fieldValue);

        Document doc = ArticleUtils.articleToDocument(article);

        indexWriter.updateDocument(term, doc);

        indexWriter.close();
    }

    public List<Article> findIndex(String keywords,int start,int rows) throws IOException, ParseException{
        IndexSearcher indexSearcher = LuceneUtils.getIndexSearcher();
        String fields [] ={"title","content"};

        QueryParser queryParser = new MultiFieldQueryParser(LuceneUtils.getMatchVersion(),fields,LuceneUtils.getAnalyzer());
        Query query = queryParser.parse(keywords);

        TopDocs topDocs = indexSearcher.search(query, start+rows);
        System.out.println("=========topDocs"+topDocs.totalHits);
        ScoreDoc scoreDocs [] = topDocs.scoreDocs;

        Article article = null;

        List<Article> articleList = new ArrayList<Article>();

        int endResult = Math.min(scoreDocs.length, start+rows);

        for(int i = start;i<endResult;i++){
            int docId = scoreDocs[i].doc;
            Document document =
            indexSearcher.doc(docId);
            article = new Article();
            article.setId(Integer.parseInt(document.get("id")));
            article.setAuthor(document.get("author"));
            article.setTitle(document.get("title"));
            article.setContent(document.get("content"));
            article.setLink(document.get("link"));
            articleList.add(article);
        }
        return articleList;
    }
}

工具包

public class ArticleUtils {
    public static Document articleToDocument(Article article){
        Document document = new Document();

        IntField idfield = new IntField("id",article.getId(),Store.YES);
        StringField authorfield = new StringField("author",article.getAuthor(),Store.YES);
        StringField urlfield = new StringField("link",article.getLink(),Store.YES);
        TextField title = new TextField("title",article.getTitle(),Store.YES);
        TextField contentfield = new TextField("content",article.getContent(),Store.YES);

        document.add(idfield);
        document.add(authorfield);
        document.add(urlfield);
        document.add(title);
        document.add(contentfield);

        return document;
    }
}

public class LuceneUtils {

    private static Directory directory = null;
    private static IndexWriterConfig config = null;
    private static Version matchVersion = null;
    private static Analyzer analyzer = null;

    static{
        try {
            directory = FSDirectory.open(new File(Contains.INDEXURL));
            matchVersion = Version.LUCENE_44;
            analyzer = new StandardAnalyzer(matchVersion);
            config = new IndexWriterConfig(matchVersion,analyzer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static IndexWriter getIndexWriter() throws IOException{
        IndexWriter indexWriter = new IndexWriter(directory,config);
        return indexWriter;
    }

    public static IndexSearcher getIndexSearcher() throws IOException{
        IndexReader indexReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        return indexSearcher;
    }

    public static Version getMatchVersion() {
        return matchVersion;
    }

    public static void setMatchVersion(Version matchVersion) {
        LuceneUtils.matchVersion = matchVersion;
    }

    public static Analyzer getAnalyzer() {
        return analyzer;
    }

    public static void setAnalyzer(Analyzer analyzer) {
        LuceneUtils.analyzer = analyzer;
    }
}


public class Contains {
    public static final String INDEXURL = "d://index/news";
}

测试类

public class TestDemo_1 {
    private LuceneDao luceneDao = new LuceneDao();

    @Test
    public void addIndex() throws Exception{
        for(int i=25;i<=25;i++){
            Article article = new Article();
            article.setId(i);
            article.setTitle("你好");
            article.setContent("安是的理解方式打开都是学");
            article.setLink("www.baidu.com");
            article.setAuthor("dqf");
            luceneDao.addIndex(article);
        }
    }

    @Test
    public void testSearcher() throws IOException, ParseException{
        String keyWords = "你好";
        List<Article> listArticles = luceneDao.findIndex(keyWords,0,10);
        for(Article article :listArticles){
            System.out.println(article.getId());
            System.out.println(article.getAuthor());
            System.out.println(article.getLink());
            System.out.println(article.getContent());
            System.out.println(article.getTitle());
        }
    }

    @Test
    public void testDelete() throws IOException{
        luceneDao.delIndex("title", "你");
    }

    @Test
    public void testUpdate() throws IOException{
        String fieldName = "title";
        String fieldValue = "你";
        Article article = new Article();
        article.setId(123465);
        article.setAuthor("王照厅");
        article.setContent("王照厅");
        article.setLink("www.dqf.com");
        article.setTitle("王照厅");
        luceneDao.updateIndex(fieldName, fieldValue, article);
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值