PyLucene-学习-01安装及建索引、查询

PyLucene是Java版Lucene的Python版封装。这个工具的目标是让Python使用Lucene的文本索引和搜索能力。它与Java版Lucene的最新版本是兼容的。PyLucene把一个带有JAVA VM的Lucene嵌入到Python进程中。你可以在http://lucene.apache.org/pylucene/网站上找到更多的PyLucene详情。

本文中,我们将描述如何使用PyLucene构建搜索索引和查询一个搜索索引。你可以从先前的文档看到Lucene3.0安装说明。

PyLucene-Win32下的安装包可以从下面的网址中找到:

http://code.google.com/a/apache-extras.org/p/pylucene-extra/downloads/list

注:使用PyLucene必须安装Java SDK

一、 使用PyLucene创建索引

使用下面的代码基于PyLucene来创建索引

#!/usr/bin/env python
 import os,sys,glob
 import lucene
 from lucene import SimpleFSDirectory, System, File, Document, Field, \
 StandardAnalyzer, IndexWriter, Version

 """
 Example of Indexing with PyLucene 3.0
 """

def luceneIndexer(docdir,indir):
         """
         IndexDocuments from a directory
         """

         lucene.initVM()
         DIRTOINDEX= docdir
         INDEXIDR= indir
         indexdir= SimpleFSDirectory(File(INDEXIDR))
         analyzer= StandardAnalyzer(Version.LUCENE_30)
         index_writer= IndexWriter(indexdir,analyzer,True,\
         IndexWriter.MaxFieldLength(512))
         for tfile in glob.glob(os.path.join(DIRTOINDEX,'*.txt')):
                   print"Indexing: ", tfile
                   document= Document()
                   content= open(tfile,'r').read()
                   document.add(Field("text",content,Field.Store.YES,\
                            Field.Index.ANALYZED))
                   index_writer.addDocument(document)
                   print"Done: ", tfile
         index_writer.optimize()
         printindex_writer.numDocs()
         index_writer.close()

 你必须提供两个参数给luceneIndexer()函数。

1)  一个保存被索引文档的目录路径;

2)  一个索引存储的目录路径。

 

二、使用Pylucene查询

下面的代码用于查询Pylucene创建的索引。

#!/usr/bin/env python
 import sys
 import lucene
 from lucene import SimpleFSDirecto
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值