当前位置: 首页 > 工具软件 > PyLucene > 使用案例 >

Pylucene 7.6.0 demo 查询

年文柏
2023-12-01

Pylucene 7.6.0 demo 查询

import sys
import lucene
from java.io import File
from java.nio.file import Paths
from org.apache.lucene.analysis.miscellaneous import LimitTokenCountAnalyzer
from org.apache.lucene.analysis.core import WhitespaceAnalyzer
from org.apache.lucene.analysis.standard import StandardAnalyzer
from org.apache.lucene.document import Document, Field, FieldType,TextField
from org.apache.lucene.index import FieldInfo, IndexWriter, IndexWriterConfig,IndexReader,DirectoryReader
from org.apache.lucene.store import SimpleFSDirectory
from org.apache.lucene.util import Version
from org.apache.lucene.analysis.cjk import CJKAnalyzer
from org.apache.lucene.search import IndexSearcher
from org.apache.lucene.queryparser.classic import QueryParser
"""
PyLucene retriver simple example
"""

INDEXDIR = "indexs"
def init():
    lucene.initVM()

init()
indir = Paths.get(INDEXDIR)
indir = SimpleFSDirectory(indir)
indir = DirectoryReader.open(indir)
lucene_analyzer= CJKAnalyzer()
lucene_searcher= IndexSearcher(indir)

def Query(query):
    my_query= QueryParser(query,lucene_analyzer).parse(query)
    MAX= 1000
    total_hits =lucene_searcher.search(my_query,MAX)
    return process(lucene_searcher,total_hits)
    #print("Hits: ",total_hits.totalHits)
    #for hit in total_hits.scoreDocs:
    #    print("Hit Score: ",hit.score, "Hit Doc:",hit.doc, "HitString:",hit.toString())
    #    doc= lucene_searcher.doc(hit.doc)
    #    print(doc.get("text"))

def process(searcher,hits):
    return {hit.doc:searcher.doc(hit.doc).get("text") for hit in hits.scoreDocs}


if __name__ == '__main__':
    l = Query("text:哈哈")
    print(l)


 类似资料: