PyLucene实战

PyLuceneJavaLucenePython版封装。这个工具的目标是让Python使用Lucene的文本索引和搜索能力。它与JavaLucene的最新版本是兼容的。PyLucene把一个带有JAVA VMLucene嵌入到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

1.使用PyLucene创建索引

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

#!/usr/bin/env python

import os,sys,glob

import lucene

fromlucene 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))

fortfile 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) 一个索引存储的目录路径。

2.使用Pylucene查询

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

#!/usr/bin/env python

import sys

import lucene

fromlucene import SimpleFSDirectory, System, File, Document, Field,\

StandardAnalyzer, IndexSearcher, Version,QueryParser

"""

PyLucene retriver simple example

"""

INDEXDIR = "./MyIndex"

def luceneRetriver(query):

lucene.initVM()

indir= SimpleFSDirectory(File(INDEXDIR))

lucene_analyzer= StandardAnalyzer(Version.LUCENE_30)

lucene_searcher= IndexSearcher(indir)

my_query= QueryParser(Version.LUCENE_30,"text",\

lucene_analyzer).parse(query)

MAX= 1000

total_hits =lucene_searcher.search(my_query,MAX)

print"Hits: ",total_hits.totalHits

forhit in total_hits.scoreDocs:

print"Hit Score: ",hit.score, "Hit Doc:",hit.doc, "HitString:",hit.toString()

doc= lucene_searcher.doc(hit.doc)

printdoc.get("text").encode("utf-8")

luceneRetriver("really coolrestaurant")

在代码中,我们认为的指定索引目录为INDEXDIR=./MyIndex,你也可以使用命令行参数(sys.argv)来接收索引目录来替换它。

当使用函数luceneRetriver()时,你必须给一个查询作为参数。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值