2017-12-06 5 views
0

Lucene을 처음 사용했습니다. 파이썬 3에서 PyLucene 6.5의 샘플 코드를 작성하고 싶습니다. this 버전의 샘플 코드를 변경했습니다. 그러나 몇 가지 문서를 찾을 수 있었고 변경 사항이 정확한지 잘 모르겠습니다.PyLucene 인덱서 및 리트리버 샘플

# indexer.py 
import sys 
import lucene 

from java.io import File 
from org.apache.lucene.analysis.standard import StandardAnalyzer 
from org.apache.lucene.document import Document, Field, StringField, FieldType 
from org.apache.lucene.index import IndexWriter, IndexWriterConfig 
from org.apache.lucene.store import SimpleFSDirectory, FSDirectory 
from org.apache.lucene.util import Version 

if __name__ == "__main__": 
    lucene.initVM() 
    indexPath = File("index/").toPath() 
    indexDir = FSDirectory.open(indexPath) 
    writerConfig = IndexWriterConfig(StandardAnalyzer()) 
    writer = IndexWriter(indexDir, writerConfig) 

    print("%d docs in index" % writer.numDocs()) 
    print("Reading lines from sys.stdin...") 

    tft = FieldType() 
    tft.setStored(True) 
    tft.setTokenized(True) 
    for n, l in enumerate(sys.stdin): 
     doc = Document() 
     doc.add(Field("text", l, tft)) 
     writer.addDocument(doc) 
    print("Indexed %d lines from stdin (%d docs in index)" % (n, writer.numDocs())) 
    print("Closing index of %d docs..." % writer.numDocs()) 
    writer.close() 

이 코드는 입력을 읽고 index 디렉토리에 저장합니다.

# retriever.py 
import sys 
import lucene 

from java.io import File 
from org.apache.lucene.analysis.standard import StandardAnalyzer 
from org.apache.lucene.document import Document, Field 
from org.apache.lucene.search import IndexSearcher 
from org.apache.lucene.index import IndexReader, DirectoryReader 
from org.apache.lucene.queryparser.classic import QueryParser 
from org.apache.lucene.store import SimpleFSDirectory, FSDirectory 
from org.apache.lucene.util import Version 

if __name__ == "__main__": 
    lucene.initVM() 
    analyzer = StandardAnalyzer() 
    indexPath = File("index/").toPath() 
    indexDir = FSDirectory.open(indexPath) 
    reader = DirectoryReader.open(indexDir) 
    searcher = IndexSearcher(reader) 

    query = QueryParser("text", analyzer).parse("hello") 
    MAX = 1000 
    hits = searcher.search(query, MAX) 

    print("Found %d document(s) that matched query '%s':" % (hits.totalHits, query)) 
    for hit in hits.scoreDocs: 
     print(hit.score, hit.doc, hit.toString()) 
     doc = searcher.doc(hit.doc) 
     print(doc.get("text").encode("utf-8")) 

우리는 retriever.py와 (검색)를 검색 할 수 있어야하지만 아무것도 반환하지 않습니다. 그게 뭐가 잘못 됐어?

답변

0
In []: tft.indexOptions() 
Out[]: <IndexOptions: NONE> 

DOCS_AND_FREQS_AND_POSITIONS이 기본값이지만 더 이상 적용되지 않습니다. 이는 TextField의 기본값입니다. a FieldType은 명시 적으로 setIndexOptions이어야합니다.