lucene 创建索引 查询实例

package com.jr.demo;

import java.io.File;
import java.io.IOException;
import java.net.URI;


import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;

import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;

import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;


import com.jr.util.Util;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;

public class Demo1 {
    public static void main(String[] args) throws Exception {
        //index("d://index");

        QueryContent("搜");
    }



    /**
     * 创建索引
     * @param strindex
     */
    public  static void index(String strindex){


        Path ps=Paths.get("d://");
        File f=ps.toFile();
        File []files=f.listFiles();

        try {
/*          Path p=Paths.get(strindex);
            Directory directory=FSDirectory.open(p);*/
            Util.OpenDirectory("d://index");

            IndexWriterConfig indexWriterConfig=new IndexWriterConfig(new StandardAnalyzer());

            IndexWriter indexWriter=new IndexWriter(Util.directory, indexWriterConfig);
            for (File file:files) {
                Document document=new Document();
                String filename=file.getName();
                String prefixpathname=file.getCanonicalPath();
                String path=file.getPath();
                document.add(new TextField("filename", filename, Field.Store.YES));
                document.add(new TextField("prefixpathname", prefixpathname, Field.Store.YES));
                document.add(new TextField("path", path, Field.Store.YES));
                indexWriter.addDocument(document);
                // System.out.println(filename);
            }


            indexWriter.commit();
            indexWriter.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public  static List QueryContent(String s){
        Util.OpenDirectory("d://index");
        try {
            IndexReader indexReader=DirectoryReader.open(Util.directory);
            IndexSearcher searcher=new IndexSearcher(indexReader);

            QueryParser parser=new QueryParser("filename", new StandardAnalyzer());
            Query query=parser.parse(s);

            TopDocs docs=searcher.search(query, 10000);
            ScoreDoc[] docs2=docs.scoreDocs;

            for (ScoreDoc doc:docs2) {
                Document document=searcher.doc(doc.doc);
                System.out.println("filename:"+document.get("filename"));
                System.out.println("prefixpathname:"+document.get("prefixpathname"));
                System.out.println("path:"+document.get("path"));
            }
            indexReader.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}

工具类:

package com.jr.util;

import java.io.IOException;
import java.nio.file.Paths;

import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
/**
 * 创建Directory工具类
 * 
 *
 */
public class Util {
public  static Directory directory;

    public static boolean OpenDirectory(String path){
        try {
            directory=FSDirectory.open(Paths.get(path));
            return true;
        } catch (IOException e) {

            e.printStackTrace();
            return false;
        }


    }




}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Lucene是一个开源的全文搜索引擎库,它提供了丰富的API和功能来创建、更新和搜索索引。要更新Lucene索引,首先需要获取一个IndexWriter实例,然后通过该实例来执行索引更新操作。 更新索引的过程通常包括以下几个步骤: 1. 创建或获取一个IndexWriter实例:IndexWriter负责索引的写入和更新操作,需要确保只有一个线程能够同时操作一个IndexWriter实例。 2. 创建或获取文档对象:更新索引需要构建文档对象,可以通过Document类来创建一个文档对象,并向其添加字段。 3. 执行更新操作:可以通过IndexWriter的addDocument、updateDocument或deleteDocuments方法来执行索引的添加、更新或删除操作。 4. 提交更新:在更新索引操作完成后,需要调用IndexWriter的commit或flush方法来提交更新,使更新操作生效。 更新索引的频率取决于应用的需求,可以是实时更新,也可以是批量更新。总之,更新索引是一个重要的操作,在使用Lucene构建搜索引擎或全文检索功能时,需要根据具体的业务需求来合理地更新索引。同时,为了保证索引更新的性能和可靠性,需要注意合理地管理IndexWriter实例,确保其在适当的时候被关闭或重新打开。 总之,通过Lucene的API和功能,可以方便地进行索引的更新操作,从而保证应用的搜索功能能够及时、准确地返回符合用户需求的搜索结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值