lucene的代码示范

来源:互联网 发布:笑傲江湖决赛知乎 编辑:程序博客网 时间:2024/06/08 15:33
import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.nio.file.FileSystems;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;import org.apache.lucene.document.TextField;import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.index.Term;import org.apache.lucene.queryparser.classic.ParseException;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.TermQuery;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 IndexFile {        String indexFile = null;        String sourceDir = null;        public IndexFile(String sourceDir, String indexFile){            this.sourceDir = sourceDir;            this.indexFile = indexFile;        }    public  void index() throws Exception {    //  RAMDirectory ramdirectory = new RAMDirectory(); //创建内存索引对象        Directory directory = FSDirectory.open(FileSystems.getDefault().getPath(this.indexFile)); //创建磁盘目录对象        Analyzer analyzer = new StandardAnalyzer();  //创建分词器        IndexWriterConfig writerConfig = new IndexWriterConfig(analyzer);  //创建索引写入对象控制        IndexWriter writer = new IndexWriter(directory, writerConfig); //创建写入对象        this.writeDocuments(writer); //将文档写入对象//      directory.close();    }    public void writeDocuments(IndexWriter writer) throws Exception{        File directory = new File(sourceDir);        File[] fileList = directory.listFiles();        for(int i = 0 ; i < fileList.length; i++){            String title = fileList[i].getName();            String content= this.readFile(fileList[i]);            System.out.println(content);            System.out.println(title);            Document doc = new Document();            doc.add(new Field("name", title, TextField.TYPE_STORED));            doc.add(new Field("address", this.indexFile, TextField.TYPE_STORED));            doc.add(new Field("thing", content, TextField.TYPE_STORED));            writer.addDocument(doc);            System.out.println("add one document");        }        writer.close();    }    public String readFile(File file) throws Exception{        BufferedReader br = null;        String content = null;        try{            br = new BufferedReader(new FileReader(file));            String line ;            while ( (line = br.readLine()) != null){                content += line;            }        }        finally{            if(br != null ){                br.close();            }        }        return content;    }    public void query(String queryStr) throws IOException, ParseException{        Directory directory = FSDirectory.open(FileSystems.getDefault().getPath(this.indexFile));        DirectoryReader directoryReader = DirectoryReader.open(directory);        IndexSearcher indexSearcher = new IndexSearcher(directoryReader);        Analyzer analyzer = new StandardAnalyzer();        QueryParser qp = new QueryParser("thing",  analyzer);        qp.setDefaultOperator(QueryParser.AND_OPERATOR);        Query query = qp.parse(queryStr);//      Term term = new Term("name", queryStr);//      Query query = new TermQuery(term);        TopDocs topDocs = indexSearcher.search(query, 10);        ScoreDoc[] scoreDocs = topDocs.scoreDocs;        System.out.println( "检索到" + topDocs.totalHits + "条记录。");        for( ScoreDoc scoreDoc : scoreDocs){            Document document = indexSearcher.doc(scoreDoc.doc);            System.out.println("title:" + document.getField("name").stringValue());            System.out.println("content:" + document.getField("thing").stringValue());        }        directory.close();    }    public static void main(String[] agrs) throws Exception{        String indexFileName  =  "/home/quincy1994/QA/LuceneIndex/data";        String sourceDir =  "./MovieList";        IndexFile indexFile = new IndexFile(sourceDir, indexFileName);//      indexFile.index();        System.out.println("nice!");        String queryStr = "大城小事讲的是什么";        indexFile.query(queryStr);    }}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 瓶盖打不开怎么办 瓶子是玻璃的 杯子口关错位了怎么办 大玻璃瓶的玻璃瓶盖拧不开怎么办 按压式洗发水瓶盖打不开怎么办 向下按压的瓶盖打不开怎么办 玻璃罐头开过了打不开怎么办 玻璃瓶的塑料盖子打不开怎么办 泡酒玻璃瓶盖子打不开怎么办 罐头的塑料瓶盖打不开怎么办 塑料水杯盖紧了怎么办 拧不开矿泉水瓶盖怎么办 新暖壶盖吸住了怎么办 暖瓶盖被吸住了怎么办 做面包和面粘手怎么办 面包面和稀了怎么办 鱼缸氧气泵声音大怎么办 中班安全遇到火灾怎么办反思 汤洒了怎么办活动反思 下水道被塑料盖堵了怎么办 卫生间地漏盖子掉到下水道怎么办 洗手池下水道翻盖打不开了怎么办 培乐多彩泥吃了怎么办 超轻橡皮泥干了怎么办 脑梗脾气大怎么办好啊 牙齿喝饮料烂了怎么办 大门牙缝里黑了怎么办 椰汁拧不开瓶盖怎么办 装蜂蜜的玻璃罐打不开怎么办 蚂蚱没有草吃了怎么办 笔记本电源已接通未充电怎么办 电源已接通未充电怎么办 遮盖纹身好了颜色淡了怎么办 致炫方向盘变重怎么办 xp音频图标没了怎么办 狙击精英3没子弹怎么办 干活干的手腕疼怎么办 干了活不给钱怎么办 干了活要不到钱怎么办 活干完了钱不给怎么办 微信语音聊天音量很小怎么办 一手软件崩溃钱卡住了怎么办