File: testlucene.py

package info (click to toggle)
python-jpype 0.6.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,560 kB
  • sloc: cpp: 11,957; python: 3,844; java: 986; ansic: 875; makefile: 149; xml: 76; sh: 62
file content (33 lines) | stat: -rw-r--r-- 1,271 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from os import path
import shutil
import tempfile
from jpype import *

lucene_jar = path.join("..", "build", "lucene-1.4.3.jar")
if not path.isfile(lucene_jar):
    raise IOError, "Please provide %s" % path.abspath(lucene_jar)
startJVM(getDefaultJVMPath(),'-Djava.class.path=%s' % lucene_jar)

QueryParser = JClass("org.apache.lucene.queryParser.QueryParser")
IndexSearcher = JClass("org.apache.lucene.search.IndexSearcher")
IndexReader = JClass("org.apache.lucene.index.IndexReader")
StandardAnalyzer = JClass("org.apache.lucene.analysis.standard.StandardAnalyzer")
FSDirectory = JClass("org.apache.lucene.store.FSDirectory")
IndexWriter = JClass("org.apache.lucene.index.IndexWriter")
SimpleAnalyzer = JClass("org.apache.lucene.analysis.SimpleAnalyzer")

tmppath = tempfile.mkdtemp()
IndexWriter(tmppath, SimpleAnalyzer(), True).close()

directory = FSDirectory.getDirectory(tmppath, False)
reader = IndexReader.open(directory)
searcher = IndexSearcher(reader)
queryparser = QueryParser.parse("wenger","contents",StandardAnalyzer())
print queryparser.rewrite
print queryparser.rewrite.matchReport(reader)
qp = queryparser.rewrite(reader)
print qp
print searcher.search.matchReport(qp)
hits = searcher.search(qp)

shutil.rmtree(tmppath)