Lucene Dao
来源:互联网 发布:python环境变量配置 编辑:程序博客网 时间:2024/06/06 20:32
1.LuceneDao层
/** * * 使用lucenedao来操作索引库... * @author Administrator * */public class LuceneDao {/** * 创建索引.. * @throws IOException */public void creatIndex(Article article) throws IOException{IndexWriter indexWriter=LuceneUtils.getIndexWriter();Document document=ArticleToDocumentUtils.articleToDocument(article);indexWriter.addDocument(document);indexWriter.close();}/** * 删除document * @param fieldName 字段对应的名称 * @param fieldValue 字段对应的值value * @throws IOException */public void deleteIndex(String fieldName,String fieldValue) throws IOException{IndexWriter indexWriter=LuceneUtils.getIndexWriter();Term term=new Term(fieldName,fieldValue);//author : 习大大indexWriter.deleteDocuments(term);indexWriter.close();}/** * 更新... * @throws IOException * * 先删除,再创建.... * */public void updateIndex(String fieldName,String fieldValue,Article article) throws IOException{IndexWriter indexWriter=LuceneUtils.getIndexWriter();/** * update document set article where term * * 1:条件 * 2:需要更新的值... * * 1:先删除符合term 的记录,再创建一条document 的记录... * */Term term=new Term(fieldName, fieldValue);Document document=ArticleToDocumentUtils.articleToDocument(article);indexWriter.updateDocument(term, document);indexWriter.close();}/** * * 分页。。。 * * mysql limit * * 0,10 * 10,10 * 20,10 * * @param keywords * @return * @throws IOException * @throws ParseException */public List<Article> getArticleByKeywords(String keywords,int firstResult,int maxResult) throws IOException, ParseException{List<Article> articles=new ArrayList<Article>();IndexSearcher indexSearcher=LuceneUtils.getIndexSearcher();//根据多个字段进行搜索.../** * 1:matchVersion * * 2:需要根据那些字段进行搜索,数组 * * 3:分词器... */String [] fields ={"title","content","author"};QueryParser queryParser=new MultiFieldQueryParser(LuceneUtils.getCurrentVersion(),fields,LuceneUtils.getAnalyzer());Query query=queryParser.parse(keywords);//假设是第一页,搜索前面10,0+10 //假设是第二页 搜索从10,10 10+10TopDocs topdocs=indexSearcher.search(query, firstResult+maxResult);//返回匹配到的总记录数Article article=null;ScoreDoc scoreDocs []=topdocs.scoreDocs;/** * 这个是根据条件查询出来的实际长度。。 * */int socreLength=scoreDocs.length;int endResult=firstResult+maxResult;/** * 比较两个int 类型的最小值... * */int limitResult=Math.min(socreLength, endResult);for(int i=firstResult;i<limitResult;i++){int docID=scoreDocs[i].doc;Document document=indexSearcher.doc(docID);article=ArticleToDocumentUtils.documentToArticle(document);articles.add(article);}return articles;}}
2.Lucene测试类
/** * * 测试lucene 的dao * @author Administrator * */public class JunitTest {LuceneDao luceneDao=new LuceneDao();@Testpublic void testadd() throws IOException{for(int i=1;i<29;i++){Article article=new Article();article.setId(i);article.setTitle("你不问,你不问你不问你不问你不问你不问我不说,这就是距离");article.setContent("表哥临时工女同学lucene");article.setAuthor("新浪微博");luceneDao.creatIndex(article);}}@Testpublic void testsearcher() throws IOException, ParseException{//article.setTitle("事业单位养老金并轨落地 教授称找钱成难题"); textfield 分词 单字分词器//article.setContent("国务院近日发布的《事业单位人事管理条例》将于7月1日起正式执行"); 分词//article.setAuthor("习大大"); StringFiled 被分词 author:习大大List<Article> articles=luceneDao.getArticleByKeywords("我不说",0,10);System.out.println("size==="+articles.size());for(Article article:articles){System.out.println(article.getId());System.out.println(article.getTitle());System.out.println(article.getAuthor());}}@Testpublic void testdelete() throws IOException{String fieldName="author";String fieldValue="习大大";luceneDao.deleteIndex(fieldName, fieldValue);}/** * 测试更新 * @throws IOException * */@Testpublic void testUpdate() throws IOException{String fieldName="author";String fieldValue="新浪微博";Article article=new Article();article.setId(9527);article.setTitle("屌丝逆袭高富帅..");article.setContent("这可能吗");article.setAuthor("老于");luceneDao.updateIndex(fieldName, fieldValue, article);}}
0 0
- Lucene Dao
- DAO
- DAO
- DAO
- DAO
- DAO
- DAO
- DAO
- Dao
- dao
- dao
- dao
- DAO
- DAO
- DAO
- DAO
- DAO
- DAO
- Makefile中使用 for 控制结构编译多个目标文件
- 网络流 dicnic sap 2种算法详细解释
- 360百度CDN加速与nat123网络共享加速
- 开发板中烧写应用程序之前必须烧写内核、根文件吗?
- oj比较大小
- Lucene Dao
- 落网音乐下载工具
- Software architecture vs code
- XSS与HTTP-only Cookie 脚本获取JSESSIONID的方法
- 如何在一周内摸清一个行业
- Apache CXF实战之一 Hello World Web Service
- 关于textarea和window.clipboardData.setData遇到的小小问题
- Ubuntu中更好地使用Source Insight
- oj成绩处理