Pylucene7.6创建倒排索引

 

import os,sys,glob
import lucene
import pandas as pd
import sys, os, lucene, threading, time
from java.io import File
from java.nio.file import Paths
from org.apache.lucene.analysis.miscellaneous import LimitTokenCountAnalyzer
from org.apache.lucene.analysis.standard import StandardAnalyzer
from org.apache.lucene.analysis.cjk import CJKAnalyzer
from org.apache.lucene.document import Document, Field, FieldType,StringField, TextField
from org.apache.lucene.index import FieldInfo, IndexWriter, IndexWriterConfig
from org.apache.lucene.store import SimpleFSDirectory
from org.apache.lucene.analysis.core import SimpleAnalyzer
from org.apache.lucene.util import Version

df=pd.read_json('/Users/...',orient='records',encoding='UTF-8')

lucene.initVM()
indir=Paths.get("/Users/shiyufei/Desktop/index") 
INDEXIDR= indir
indexdir= SimpleFSDirectory(indir) #实例化一个SimpleFSDirectory对象,将索引保存至本地文件之中,保存的路径为自定义的路径“INDEXIDR”

analyzer= CJKAnalyzer() #实例化一个CJKAnalyzer分析器,该分析器对中文支持较好,适用于中文内容的文本处理
config = IndexWriterConfig(analyzer)
writer = IndexWriter(indexdir, config) #实例化一个IndexWriter对象,索引写入类 

title=df['title']  
url=df['current_url']
text=df['news_text']
date=df['news_date']

for i in range(len(df)):
    document=Document()
    document.add(Field("title", "".join(df['title'][i]), TextField.TYPE_STORED)) 
    document.add(Field("url", "".join(df['current_url'][i]), StringField.TYPE_STORED))
    document.add(Field("text", "".join(df['news_text'][i]), TextField.TYPE_STORED))
    document.add(Field("date","".join(df['news_date'][i]), StringField.TYPE_STORED))
    writer.addDocument(document) #实例化IndexWriter对象,将文档document写入索引文件

writer.commit()
writer.close()

 通过luke查看构建的索引

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值