Lucene笔记(2)

来源:互联网 发布:unity 特效贴图优化 编辑:程序博客网 时间:2024/04/30 14:35

关于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);    }}
0 0
原创粉丝点击