003 Lucene读取文档创建索引、与查询

根据文档创建索引案例:

public void createIndex() throws Exception {
    //创建IndexWriter对象
    Directory directory = FSDirectory.open(Paths.get("D:\\lucene-7.6.0\\BlackTv_demo", new String[0]));
    Analyzer analyzer = new StandardAnalyzer();//StandardAnalyzer是官方推荐的,所以对中文肯定没支持,中文支持的后面再说
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
    IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);

    File folder = new File("D:\\lucene-7.6.0\\BlackTv_demo\\demo");//获取文件夹
    File[] fileList = folder.listFiles();//获取该文件夹下文件列表

    for (File file : fileList) {
        //创建field域,这里创建4个:文件名称、文件大小、文件路径、文件名称,只分析文件名和内容,大小和路径分析了也没意义
        Field fileNameField = new TextField("fileName", file.getName(), Field.Store.YES);
        long fileSize = FileUtils.sizeOf(file);//文件大小的field做不了,lucene 7.6.0找不到LongField
        Field filePathField = new StoredField("filePath", file.getPath());
        Field fileContentField = new TextField("fileContent", FileUtils.readFileToString(file), Field.Store.YES);
        //创建Document对象,将field添加到文档里,一个文件一个文档
        Document document = new Document();
        document.add(fileNameField);
        document.add(filePathField);
        document.add(fileContentField);
        //将文档对象存储到索引库
        indexWriter.addDocument(document);
    }
    indexWriter.close();//关闭indexWriter流
}

生成索引后,在索引目录里会有索引文件,可以通过Luck这个工具来查看https://github.com/DmitryKey/luke/releases #下载和Lucene相同版本的、

有IndexSearcher的方法看出可以写分页查询,补充一个IndexSearcher的方法

int maxDoc()	#返回比最大可能的文档数目多一个

查询案例,查询关键字为apache:

public void Search() throws Exception {
    //创建Directory对象,打开索引库的位置
    Directory directory = FSDirectory.open(Paths.get("D:\\lucene-7.6.0\\BlackTv_demo", new String[0]));
    //创建IndexReader对象,打开索引库
    IndexReader indexReader = DirectoryReader.open(directory);
    //创建IndexSearcher对象
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    //创建查询对象,查询域为文件内容,关键字为apache
    Query query = new TermQuery(new Term("fileContent", "apache"));
    //执行查询,查询term出现频率最高的前3
    TopDocs topDocs = indexSearcher.search(query, 3);
    //获取查询结果,遍历打印
    ScoreDoc[] scoreDocs = topDocs.scoreDocs;
    for (ScoreDoc scoreDoc : scoreDocs) {
        System.out.println("文档id:" + scoreDoc.doc);
        //根据文档id获取文档对象
        Document document = indexSearcher.doc(scoreDoc.doc);
        System.out.println("文件名:" + document.get("fileName"));
        System.out.println("文件路径:" + document.get("filePath"));
//            System.out.println("文件内容:" + document.get("fileContent"));//内容太多就不打印出来了
        System.out.println("----------------------------------------------------------------------------------");
        System.out.println("----------------------------------------------------------------------------------");
    }
    //关闭流
    indexReader.close();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C# Lucene.Net创建索引的步骤: 1.添加Lucene.Net库的引用 在Visual Studio中,右键单击项目并选择“管理NuGet程序包”。在搜索框中搜索“Lucene.Net”,然后安装Lucene.Net库。 2.创建索引 ```csharp using Lucene.Net.Analysis.Standard; using Lucene.Net.Documents; using Lucene.Net.Index; using Lucene.Net.Store; using System.IO; // 创建索引 public void CreateIndex(string indexPath, string dataPath) { // 创建分析器 var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); // 创建索引存储目录 var directory = FSDirectory.Open(new DirectoryInfo(indexPath)); // 创建索引写入器 var writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED); // 读取数据文件 var lines = File.ReadAllLines(dataPath); // 遍历数据文件中的每一行 foreach (var line in lines) { // 创建文档 var doc = new Document(); // 添加字段 doc.Add(new Field("content", line, Field.Store.YES, Field.Index.ANALYZED)); // 将文档写入索引 writer.AddDocument(doc); } // 关闭索引写入器 writer.Dispose(); } ``` 3.使用索引 ```csharp using Lucene.Net.Analysis.Standard; using Lucene.Net.QueryParsers; using Lucene.Net.Search; using Lucene.Net.Store; using System.IO; // 使用索引 public void SearchIndex(string indexPath, string queryStr) { // 创建分析器 var analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); // 创建索引存储目录 var directory = FSDirectory.Open(new DirectoryInfo(indexPath)); // 创建索引搜索器 var searcher = new IndexSearcher(directory, true); // 创建查询解析器 var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "content", analyzer); // 解析查询字符串 var query = parser.Parse(queryStr); // 执行查询 var hits = searcher.Search(query, null, 10, Sort.RELEVANCE).ScoreDocs; // 遍历查询结果 foreach (var hit in hits) { // 获取文档 var doc = searcher.Doc(hit.Doc); // 输出文档内容 Console.WriteLine(doc.Get("content")); } // 关闭索引搜索器 searcher.Dispose(); } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值