- 浏览: 134809 次
- 性别:
- 来自: 石家庄
文章分类
最新评论
-
shuzheng5201314:
如果不管这样的错误有影响吗?你这样貌似可行!
Proxool连接池在reload web容器时出现HouseKeeper的空指针异常 -
SpreadDiaries:
...
[转]常见数据库字段类型与java.sql.Types的对应 -
kevinhrw:
<bean
class="org.spri ...
用BeanNameAutoProxyCreator自动创建事务代理 -
hilliate:
第一步,把冰箱门打开第二步,把大象装进去第三步,把冰箱门关上呵 ...
Struts2中实现自定义标签很简单,主要分为3步:
Lucene 对索引库的增删改查操作的 API 演示
没什么说的,apache 的 API 一向简单、不难理解。所以直接拷代码过去稍微看一下就差不多了。
/** * "文章" 实体 */ public class Article { private Integer id; private String title; private String content; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } } /** * 描述某一页的检索结果集 */ public class QueryResult { /* 匹配的总记录数 */ private int totalCount; /* 检索到的文章对象集合 */ private List<Article> atrticle; public QueryResult(int totalCount, List<Article> atrticle) { this.totalCount = totalCount; this.atrticle = atrticle; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public List<Article> getAtrticle() { return atrticle; } public void setAtrticle(List<Article> atrticle) { this.atrticle = atrticle; } } /** * 直接操作索引库的 DAO */ public class ArticleIndexDao { /** * 保存索引 * @param article */ public void save(Article article) { Document document = Article2DocumentUtil.article2document(article); IndexWriter indexWriter = null; try { Directory indexDir = FSDirectory.open(new File("./indexDir/")); // 标准分词器,另外 Lucene 还提供了针对多种语言的分词器 Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30); indexWriter = new IndexWriter(indexDir, analyzer, MaxFieldLength.LIMITED); indexWriter.addDocument(document); } catch (IOException e) { throw new RuntimeException(e); } finally { if (indexWriter != null) { try { indexWriter.close(); } catch (IOException e) { throw new RuntimeException(e); } } } } /** * 删除 * * @param article * 删除的 Article 对象 */ public void delete(Article article) { IndexWriter indexWriter = null; try { Directory indexDir = FSDirectory.open(new File("./indexDir/")); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30); indexWriter = new IndexWriter(indexDir, analyzer, MaxFieldLength.LIMITED); /* 意思是将索引库中 id 为 article.getId() 的记录删了 */ Term term = new Term("id", article.getId() + ""); indexWriter.deleteDocuments(term); } catch (IOException e) { throw new RuntimeException(e); } finally { if (indexWriter != null) { try { indexWriter.close(); } catch (IOException e) { throw new RuntimeException(e); } } } } /** * 修改 */ public void update(Article article) { IndexWriter indexWriter = null; try { Directory indexDir = FSDirectory.open(new File("./indexDir/")); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30); indexWriter = new IndexWriter(indexDir, analyzer, MaxFieldLength.LIMITED); /* 意思是将索引库中 id 为 article.getId() 的记录改了 */ Term term = new Term("id", article.getId() + ""); indexWriter.updateDocument(term, Article2DocumentUtil .article2document(article)); /* * updateDocument 等价于: delete(article); save(article); * 在大数据量的时候,采用 "删除再创建" 的效率更高 */ } catch (IOException e) { throw new RuntimeException(e); } finally { if (indexWriter != null) { try { indexWriter.close(); } catch (IOException e) { throw new RuntimeException(e); } } } } /** * 分页搜索 * @param queryStr 搜索条件 * @param firstResult 首条数据位置 * @param maxResults 最多取多少条数据 * @return 一页结果集 */ public QueryResult search(String queryStr, int firstResult, int maxResults) { IndexSearcher indexSearcher = null; List<Article> atrticles = new ArrayList<Article>(); try { /* * 构建 IndexSearcher */ Directory indexDir = FSDirectory.open(new File("./indexDir/")); indexSearcher = new IndexSearcher(indexDir); /* * 构建 Query */ Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30); QueryParser queryParser = new QueryParser(Version.LUCENE_30, "content", analyzer); Query query = queryParser.parse(queryStr); /* * 查询 */ TopDocs topDocs = indexSearcher.search(query, firstResult + maxResults); int totalCount = topDocs.totalHits; ScoreDoc[] scoreDocs = topDocs.scoreDocs; /* 保证循环的次数不超过 scoreDocs 的长度*/ int length = Math.min(firstResult+maxResults, scoreDocs.length); for(int i=firstResult; i<length; i++) { /* * 根据编号取出Document数据 */ Document document = indexSearcher.doc(i); Article article = Article2DocumentUtil.document2article(document); atrticles.add(article); } return new QueryResult(totalCount, atrticles); } catch (Exception e) { throw new RuntimeException(e); } finally { try { if (indexSearcher != null) { indexSearcher.close(); } } catch (IOException e) { throw new RuntimeException(e); } } } } ** * 工具类:Article 对象与 Document 对象的转换 */ public class Article2DocumentUtil { public static Document article2document(Article article) { if(article == null) { return null; } Document document = new Document(); document.add(new Field("id", article.getId()+"", Store.YES, Index.ANALYZED)); document.add(new Field("title", article.getTitle(), Store.YES, Index.ANALYZED)); document.add(new Field("content", article.getContent(), Store.YES, Index.ANALYZED)); return document; } public static Article document2article(Document document) { if(document == null) { return null; } Article article = new Article(); article.setId(Integer.parseInt(document.get("id"))); article.setTitle(document.get("title")); article.setContent(document.get("content")); return article; } } /** * JUnit 测试 */ public class TestArticleIndexDao { /** * 测试保存 * @throws Exception */ @Test public void testSave() throws Exception { ArticleIndexDao dao = new ArticleIndexDao(); Article article = new Article(); article.setId(1); article.setTitle("wjh上天山"); article.setContent("据悉,文建华已于昨日抵达天山。高歌一曲HelloWorld"); dao.save(article); } @Test public void testBatchSave() throws Exception { for(int i=0; i<35; i++) { ArticleIndexDao dao = new ArticleIndexDao(); Article article = new Article(); article.setId(i); article.setTitle("wjh上天山第" + i + "集"); article.setContent("据悉,wjh已于昨日抵达天山。高歌"+ i +"曲 HelloWorld"); dao.save(article); } } @Test public void testUpdate() throws Exception { ArticleIndexDao dao = new ArticleIndexDao(); Article article = new Article(); article.setId(1); article.setTitle("wjh上天山"); article.setContent("wjh已于昨日抵达天山。高歌一曲HelloWorld"); dao.update(article); } @Test public void testDel() throws Exception { ArticleIndexDao dao = new ArticleIndexDao(); Article article = new Article(); article.setId(1); article.setTitle("wjh上天山"); article.setContent("wjh已于昨日抵达天山。高歌一曲HelloWorld"); dao.delete(article); } @Test public void testSearch() throws Exception { ArticleIndexDao dao = new ArticleIndexDao(); QueryResult queryResult = dao.search("HelloWorld", 30, 10); int count = queryResult.getTotalCount(); System.out.println("共匹配了 " + count + " 条记录。"); List<Article> articles = queryResult.getAtrticle(); for (Article article : articles) { System.out.println("id:" + article.getId()); System.out.println("title:" + article.getTitle()); System.out.println("content:" + article.getContent()); System.out.println("----------------"); } } }
发表评论
-
truelicense使用手册
2015-04-25 09:47 39191.生成truelicense的maven项目 mvn a ... -
Java实现ftp上传文件、文件夹
2012-04-06 11:06 841import java.io.File; import ... -
Lucene入门级笔记六 -- 优化 .
2011-10-24 22:54 0Lucene 优化 1. 让程序中只有一个 Inde ... -
Lucene入门级笔记五 -- 分词器,使用中文分词器,扩展词库,停用词 .
2011-10-24 22:53 14061. 常见的中文分词器有 ... -
Lucene(全文检索技术)入门级笔记整之一——第一个Lucene程序 .
2011-10-24 22:50 1241Lucene(全文检索技术)入门级笔记整之一——第一个Lu ... -
Lucene入门级笔记三 -- 给关键词添加高亮效果
2011-10-24 22:43 17221. 使用高亮器。 2. ... -
Lucene入门级笔记四 -- 对检索结果排序 .
2011-10-24 22:40 1553对检索结果排序 1. 某些场合需要我们自定义搜索结果的 ... -
java获取各种日期
2011-07-14 16:47 828package com.cjy.test; impo ... -
tomcat无法运行两个struts2项目。解决方式
2011-06-15 14:22 1028提示异常: 严重: Exception starting f ... -
APACHE 2.2.9+TOMCAT6.0.18配置负载均衡
2010-10-21 23:14 1462目标: 使用 apache 和 tomcat 配置一个可以应 ... -
Request用法
2010-09-08 11:13 1182Request [JSP] JSP中的隐藏对象 -- ... -
div错位/解决IE6、IE7、IE8样式不兼容问题
2010-01-13 10:07 2299IE6里DIV错位的问题 ... -
主题:J2EE常用工具类汇总
2009-10-18 18:24 853J2EE常用工具类汇总 J2EE ... -
dreamweaver cs4 许可证过期的解决办法
2009-09-24 13:43 1854dreamweaver cs4 许可证过期的解决办法 200 ... -
lucene四种索引方式详解
2009-09-16 21:45 11711。今天研究了一下lucene,对于初学者来说,有一个地方以前 ... -
时间处理类
2009-07-24 10:14 767/** * 时间处理类 */ ... -
apache POI 读取 Word
2009-07-24 10:13 1314import java.io.File; import ... -
apache POI 读取 Excel
2009-07-24 10:12 1225import java.io.File; import ja ... -
自动得到汉语拼音
2009-07-24 10:11 960import java.util.Iterator; imp ... -
java MD5 加密
2009-07-24 10:09 776/** * java.security包中的Messa ...
相关推荐
赠送原API文档:lucene-core-7.7.0-javadoc.jar; 赠送源代码:lucene-core-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-7.7.0.pom; 包含翻译后的API文档:lucene-core-7.7.0-javadoc-API文档-中文...
赠送原API文档:lucene-analyzers-smartcn-7.7.0-javadoc.jar; 赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-analyzers-smartcn-7.7.0.pom; 包含翻译后的API文档:...
赠送原API文档:lucene-core-7.2.1-javadoc.jar; 赠送源代码:lucene-core-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-core-7.2.1.pom; 包含翻译后的API文档:lucene-core-7.2.1-javadoc-API文档-中文...
赠送原API文档:lucene-analyzers-common-6.6.0-javadoc.jar; 赠送源代码:lucene-analyzers-common-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-analyzers-common-6.6.0.pom; 包含翻译后的API文档:...
赠送原API文档:lucene-core-6.6.0-javadoc.jar; 赠送源代码:lucene-core-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-6.6.0.pom; 包含翻译后的API文档:lucene-core-6.6.0-javadoc-API文档-中文...
赠送原API文档:lucene-backward-codecs-7.2.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-codecs-7.2.1.pom; 包含翻译后的API文档:lucene-...
赠送原API文档:lucene-backward-codecs-7.3.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-codecs-7.3.1.pom; 包含翻译后的API文档:lucene-...
赠送原API文档:lucene-core-6.6.0-javadoc.jar; 赠送源代码:lucene-core-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-6.6.0.pom; 包含翻译后的API文档:lucene-core-6.6.0-javadoc-API文档-中文...
赠送原API文档:lucene-spatial-extras-7.3.1-javadoc.jar; 赠送源代码:lucene-spatial-extras-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras-7.3.1.pom; 包含翻译后的API文档:lucene-...
赠送原API文档:lucene-analyzers-smartcn-7.7.0-javadoc.jar; 赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-analyzers-smartcn-7.7.0.pom; 包含翻译后的API文档:...
赠送原API文档:lucene-spatial-extras-6.6.0-javadoc.jar; 赠送源代码:lucene-spatial-extras-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras-6.6.0.pom; 包含翻译后的API文档:lucene-...
赠送原API文档:lucene-spatial-extras-7.2.1-javadoc.jar; 赠送源代码:lucene-spatial-extras-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras-7.2.1.pom; 包含翻译后的API文档:lucene-...
赠送原API文档:lucene-backward-codecs-6.6.0-javadoc.jar; 赠送源代码:lucene-backward-codecs-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-codecs-6.6.0.pom; 包含翻译后的API文档:lucene-...
赠送原API文档:lucene-sandbox-7.2.1-javadoc.jar; 赠送源代码:lucene-sandbox-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-sandbox-7.2.1.pom; 包含翻译后的API文档:lucene-sandbox-7.2.1-javadoc-API...
赠送原API文档:lucene-backward-codecs-6.6.0-javadoc.jar; 赠送源代码:lucene-backward-codecs-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-codecs-6.6.0.pom; 包含翻译后的API文档:lucene-...
赠送原API文档:lucene-suggest-6.6.0-javadoc.jar; 赠送源代码:lucene-suggest-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-suggest-6.6.0.pom; 包含翻译后的API文档:lucene-suggest-6.6.0-javadoc-API...
赠送原API文档:lucene-spatial-6.6.0-javadoc.jar; 赠送源代码:lucene-spatial-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-6.6.0.pom; 包含翻译后的API文档:lucene-spatial-6.6.0-javadoc-API...
赠送原API文档:lucene-highlighter-6.6.0-javadoc.jar; 赠送源代码:lucene-highlighter-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-highlighter-6.6.0.pom; 包含翻译后的API文档:lucene-highlighter-...
赠送原API文档:lucene-queryparser-6.6.0-javadoc.jar; 赠送源代码:lucene-queryparser-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-queryparser-6.6.0.pom; 包含翻译后的API文档:lucene-queryparser-...
赠送原API文档:lucene-spatial-extras-7.7.0-javadoc.jar; 赠送源代码:lucene-spatial-extras-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras-7.7.0.pom; 包含翻译后的API文档:lucene-...