Lucene学习.doc_第1页
Lucene学习.doc_第2页
Lucene学习.doc_第3页
Lucene学习.doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Lucene的例子lucene爬数据库中的数据无非也是查询数据。所有我们用lucene搜索数据主要有下面几个步骤:(代码紧供参考) 一 ,从数据库中查数据 =爬数据-1public ArrayList getDate(String sql) throws SQLException ArrayList item = new ArrayList();ConnBase dataConn = new ConnBase();/数据库连接conn = dataConn.DBconn();ps = conn.prepareStatement(sql);rs = ps.executeQuery();/ jdbcTemplate.execute(sql);while (rs.next() BaseItem i = new BaseItem();i.setTitle(rs.getString(title); / 对应你的Blog表里的titlei.setContent(rs.getString(content); / 取表里的博客内容i.setUr(SingleArticle_lucene.action?id= + rs.getInt(blogId); / 如 a. action ?id=8item.add(i); / 把数据库里的数据取出来return item; 二 ,/ 建立索引存放的位置 -本方法是创建 在C盘-2public void CreateFileIndex(String dir) try /* 这里放索引文件的位置 */File indexDir = new File(c: + dir); / 存放 检索文件的路径if (!indexDir.exists() indexDir.mkdirs();/ 创建标准文本分析器, 标准的是可以支持的中文的Analyzer luceneAnalyzer = new StandardAnalyzer();indexWriter = new IndexWriter(indexDir, luceneAnalyzer, true);/ 可以说是创建一个新的写入工具/ 第一个参数是要索引建立在哪个目录里/ 第二个参数是新建一个文本分析器,这里用的是标准的大家也可以自己写一个/ 第三个参数如果是true,在建立索引之前先将c: index目录清空indexWriter.setMaxFieldLength(100000);indexWriter.optimize(); catch (IOException e) System.out.println(建立索引失败!);e.printStackTrace();三 , / 添加数据到索引里去-3public String createIndex(String title, String url, String content) try / 增加document到索引去/ document对象,相当于数据库中一条记录Document document = new Document();/ Field对象,相当于数据库中字段Field FiledTitle = new Field(title, title, Field.Store.YES,Field.Index.ANALYZED);/ Field.Index.ANALYZED 这就能进行索引了, 如果设置为NO的话就不能检索Field FiledContent = new Field(content, content, Field.Store.YES,Field.Index.ANALYZED);Field FieldBody = new Field(url, url, Field.Store.YES,Field.Index.NO); document.add(FieldBody);document.add(FiledContent);document.add(FiledTitle);indexWriter.addDocument(document); catch (IOException e) e.printStackTrace();return 建立索引失败!;return 建立索引成功!;四 , / 关闭索引= 4public void close() throws IOException this.indexWriter.close(); /这里非常的重要,不关闭直接导致你的索引创建不成功五 , / 查询索引的方法 =5public ArrayList getQueryDate(String info)throws CorruptIndexException, IOException,org.apache.lucene.queryParser.ParseException ArrayList doc = new ArrayList();String queryString = info;/ Hits hits = null;/ Query query = null;/ QueryParser qp = null;/ String dir = c:hujiong; / 一定要跟你建索引的位置 一致/ / 建立索引检索对象/ IndexSearcher searcher = new IndexSearcher(dir);/ / 分词器/ Analyzer analyzer = new StandardAnalyzer();/ qp = new QueryParser(content, analyzer);/ 这里上面只写了一个按Content查找./ 下面添加的是title, 查找/ query = qp.parse(queryString);/ if (searcher != null) / hits = searcher.search(query);/ doc = new ArrayList();/ for (int i = 0; i 0) for (int i = 0; i hits1.length; i+) Document doc1 = searcher.doc(hits1i.doc); / 这是从这个返回的数组里面迭代每一个数据, 它的值是Documentdoc.add(doc1);System.out.println(doc1.get(title) + -title);System.out.println(doc1.get(content) + -content); else System.out.println(没有数据);return doc;/上面注释的一段代码是紧对单个field查询, 下面是支持多个field查询/上面的例子只需要改变第一步,就可以查多个表的数据,你只需在getDate(String sql)中的sql语句改变为你要查询的表的sql语句,/该方法中 while (rs.next() BaseItem i = new BaseItem();i.setTitle(rs.getString(title); / 对应你的Blog表里的titlei.setContent(rs.getString(content); / 取表里的博客内容i.setUr(SingleArticle_lucene.action?id= + rs.getInt(blogId); / 如 a. action ?id=8item.add(i); / 把数据库里的数

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论