0
저는 python lucene으로 URL 페이지를 색인하고 있습니다.파이썬 lucene 함수가 필드 내용을 문서에 추가하지 않습니다.
문서에 필드를 추가하는 중 오류가 발생했습니다. 왜 그런지 모르겠습니다. 오류는 말한다 :
JavaError :> 자바 스택 트레이스 : java.lang.IllegalArgumentException가 : 그것은 인덱스도 org.apache.lucene.document에 저장도 아닌 필드가 이해가되지 않습니다. 필드 (Field.java:249)
내가 어디에 두 줄을 :. doc.add (필드 ("내용", 텍스트, T2))
내가 사용하는 파이썬 코드는 다음과 같습니다
def IndexerForUrl(start, number, domain):
lucene.initVM()
# join base dir and index dir
path = os.path.abspath("paths")
directory = SimpleFSDirectory(Paths.get(path)) # the index
analyzer = StandardAnalyzer()
writerConfig = IndexWriterConfig(analyzer)
writerConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE)
writer = IndexWriter(directory, writerConfig)
print "reading lines from sys.std..."
# hashtable dictionary
D = {}
D[start] = [start]
numVisited = 0
wordBool = False
n = start
queue = [start]
visited = set()
t1 = FieldType()
t1.setStored(True)
t1.setTokenized(False)
t2 = FieldType()
t2.setStored(False)
t2.setTokenized(True)
while numVisited < number and queue and not wordBool:
pg = queue.pop(0)
if pg not in visited:
visited.add(pg)
htmlwebpg = urllib2.urlopen(pg).read()
# robot exclusion standard
rp = robotparser.RobotFileParser()
rp.set_url(pg)
rp.read() # read robots.txt url and feeds to parser
soup = BeautifulSoup(htmlwebpg, 'html.parser')
for script in soup(["script","style"]):
script.extract()
text = soup.get_text()
lines = (line.strip() for line in text.splitlines())
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
text = '\n'.join(chunk for chunk in chunks if chunk)
print text
doc = Document()
doc.add(Field("urlpath", pg, t2))
if len(text)> 0:
doc.add(Field("contents", text, t2))
else:
print "warning: no content in %s " % pgv
writer.addDocument(doc)
numVisited = numVisited+1
linkset = set()
# add to list
for link in soup.findAll('a', attrs={'href':re.compile("^http://")}):
#links.append(link.get('href'))
if rp.can_fetch(link.get('href')):
linkset.add(link.get('href'))
D[pg] = linkset
queue.extend(D[pg] - visited)
writer.commit()
writer.close()
directory.close() #close the index
return writer
감사합니다. 나는 그것을 시도 할 것이다. –