• lucene4.6 索引创建和搜索例子


    本文转自:http://blog.csdn.net/jyf211314/article/details/17503997           点击打开链接


    lucene4.6小例子,Lucene全文检索步骤大体上有两方面:索引过程和搜索过程,具体如下:

    1.索引过程
    <1>创建IndexWriter
    ,它的作用是用来写索引文件,

    可以将IndexWriter看做是一个特定类型的数据库,用来存放各种表,可以将Document看做是一张张的表。

    IndexWriter iw=new IndexWriter(Directory dire, IndexWriterConfig iwc);

    可见,该方法有两个参数,第一个参数为索引存放位置,参数类型为Directory,第二个参数为 IndexWriter的配置类,

    @1.Directory dire=FSDirectory.open(new File(D:\lucenefiles));

    通过Directory的创建指定索引存放位置(D:lucenefiles文件夹)

    @2.Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_46);

    通过Analyzer 的创建指定索引语言词汇的分析器。当前版本Lucene-core-4.6.0

    @3.IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_46, analyzer);

    iwc.setOpenMode(OpenMode.CREATE); //CREATE:清空重建(推荐);

    IndexWriter indexWriter=new IndexWriter(dire, iwc); 

    通过IndexWriterConfig的创建指定索引版本和语言词汇分析器


    <2>创建Document,代表要索引的文档

    可以将Document看做是数据库中的一张张的表,而每个field都是表中的一个colum用来存放各种类型的信息,如标题、作者、时间等等

    @1.File fileDir= new File("D:\lucenefiles"); File[] files = fileDir.listFiles();

    读取文件

    @2.Document doc=new Document();

    通过创建Document指定要索引的文档

    @3.doc.add(new TextField("name", String name=files[i].getName(), Store.YES));

    doc.add(new TextField("content", getFileContent(files[i]), Store.YES));  

    向Document文档中添加Field信息,不同类型的信息用不同类型的Field来表示;

    getFileContent(File file)用于读取文件内容。


    <3>将Document添加到IndexWriter中并且提交

    表(Document)创建好之后,当然要添加到数据库(IndexWriter)中,同时commit

    @1.indexWriter.addDocument(doc); 通过该方法添加

    indexWriter.commit(); 


    2.搜索过程

    <!>首先指定搜索目录

    Directory dire=FSDirectory.open(new File("D:\luceneindex"));

    <2>创建语言词汇解析器,前后版本要一致

    Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_46);

    <3>创建IndexReader将搜索目录读取到内存,

    IndexReader ireader=DirectoryReader.open(dire);

    <4>创建IndexSearcher准备搜索

    IndexSearcher isearcher=new IndexSearcher(ireader);

    <5>创建QueryParser对查询语句进行语法分析,确定搜索的内容 

    QueryParser parser = new QueryParser(Version.LUCENE_46, "path", analyzer);

    <6>创建Query生成查询语法树,

    QueryParser parser = new QueryParser(Version.LUCENE_46, "content", analyzer); 

    通过调用QueryParser 的parse("");方法来生成Query以及查询语法树

    Query query = parser.parse("程序员");

    <7>获取搜索结果

    TopDocs td=is.search(query, 1000);ScoreDoc[] sds =td.scoreDocs;

    <8>对搜索结果遍历操作


    3.以下是个人测试例子

    public class Constants {
    	public final static String INDEX_FILE_PATH = "F:\lucene\test"; 
    //索引的文件的存放路径 测试时可以在本目录下自行建一些文档,内容自行编辑即可
        public final static String INDEX_STORE_PATH = "F:\lucene\index"; 
    //索引的存放位置
    }

    package com.lucene.jl;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    
    import org.apache.lucene.analysis.Analyzer;
    import org.apache.lucene.analysis.standard.StandardAnalyzer;
    import org.apache.lucene.document.Document;
    import org.apache.lucene.document.Field.Store;
    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 org.apache.lucene.store.Directory;
    import org.apache.lucene.store.FSDirectory;
    import org.apache.lucene.util.Version;
    
    public class LuceneIndex {
    	
    	/**
    	 * 创建索引
    	 * @param analyzer
    	 * @throws Exception
    	 */
    	public static void createIndex(Analyzer analyzer) throws Exception{
    		Directory dire=FSDirectory.open(new File(Constants.INDEX_STORE_PATH));
    		IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_46, analyzer);
    		IndexWriter iw=new IndexWriter(dire, iwc);
    		LuceneIndex.addDoc(iw);
    		iw.close();
    	}
    	
    	/**
    	 * 动态添加Document
    	 * @param iw
    	 * @throws Exception
    	 */
    	public static void addDoc(IndexWriter iw)  throws Exception{
    		File[] files=new File(Constants.INDEX_FILE_PATH).listFiles();
    		for (File file : files) {
    			Document doc=new Document();
    			String content=LuceneIndex.getContent(file);
    			String name=file.getName();
    			String path=file.getAbsolutePath();
    			doc.add(new TextField("content", content, Store.YES));
    			doc.add(new TextField("name", name, Store.YES));
    			doc.add(new TextField("path", path,Store.YES));
    			System.out.println(name+"==="+content+"==="+path);
    			iw.addDocument(doc);
    			iw.commit();
    		}
    	}
    	
    	/**
    	 * 获取文本内容
    	 * @param file
    	 * @return
    	 * @throws Exception
    	 */
    	@SuppressWarnings("resource")
    	public static String getContent(File file) throws Exception{
    		FileInputStream fis=new FileInputStream(file);
    		InputStreamReader isr=new InputStreamReader(fis);
    		BufferedReader br=new BufferedReader(isr);
    		StringBuffer sb=new StringBuffer();
    		String line=br.readLine();
    		while(line!=null){
    			sb.append(line+"
    ");
    			line=null;
    		}
    		return sb.toString();
    	}
    	
    	/**
    	 * 搜索
    	 * @param query
    	 * @throws Exception
    	 */
    	private static void search(Query query) throws Exception {
    		Directory dire=FSDirectory.open(new File(Constants.INDEX_STORE_PATH));
    		IndexReader ir=DirectoryReader.open(dire);
    		IndexSearcher is=new IndexSearcher(ir);
    		TopDocs td=is.search(query, 1000);
    		System.out.println("共为您查找到"+td.totalHits+"条结果");
    		ScoreDoc[] sds =td.scoreDocs;
    		for (ScoreDoc sd : sds) { 
    			Document d = is.doc(sd.doc); 
    			System.out.println(d.get("path") + ":["+d.get("path")+"]"); 
    		}
    	}
    	
    	
    	public static void main(String[] args) throws Exception, Exception {
    		Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_46);
    		LuceneIndex.createIndex(analyzer);
    		QueryParser parser = new QueryParser(Version.LUCENE_46, "content", analyzer); 
    		Query query = parser.parse("人");
    		LuceneIndex.search(query);
    	}
    }

    4.磁盘文件检索如上述,对于数据库的检索页应该是差不多,应该也是先从数据库查出数据,然后对查出的数据做索引然后搜索,大致一样吧




  • 相关阅读:
    javascript 模板系统 ejs v2
    三国观后感
    《非诚勿扰》乐嘉老师送给男生女生的话:
    正则学习笔记6
    硬链接和符号链接
    javascript 模板系统 ejs v1
    javascript模板系统 ejs v3
    程序员编程艺术:第八章、从头至尾漫谈虚函数
    程序员编程艺术第十一章:最长公共子序列(LCS)问题
    编程艺术第二十三~四章&十一续:杨氏矩阵查找,倒排索引关键词Hash编码
  • 原文地址:https://www.cnblogs.com/anyuan9/p/6171544.html
Copyright © 2020-2023  润新知