lucene3.6.1 经典案例 入门教程 (包含从文件中读取content)
转载http://liqita.iteye.com/blog/1676664
第一步:下载lucene的核心包
lucene-core-3.6.1-javadoc.jar (3.5 MB)
lucene-core-3.6.1.jar (1.5 MB)
拷贝到项目的lib 文件夹里
第二步:
在C盘下建立source文件夹 (C:\source)
source文件夹存放待索引的文件,例如,建立两个文件,名称为 test1.txt test2.txt 。
test1.txt文件内容为:欢迎来到绝对秋香的博客。
test2.txt文件内容为:绝对秋香引领你走向潮流。
在C盘下再建立index文件夹,存放索引文件 (C:\index)
第三步,建立索引类 TextFileIndexer ,并运行主函数
- package com.newtouchone.lucene;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Date;
- 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.index.IndexWriter;
- import org.apache.lucene.index.IndexWriterConfig;
- import org.apache.lucene.index.IndexWriterConfig.OpenMode;
- import org.apache.lucene.store.Directory;
- import org.apache.lucene.store.FSDirectory;
- import org.apache.lucene.util.Version;
- public class TextFileIndexer {
- public static void main(String[] args) throws Exception {
- /* 指明要索引文件夹的位置,这里是C盘的source文件夹下 */
- File fileDir = new File("C:\\source");
- /* 这里放索引文件的位置 */
- File indexDir = new File("C:\\index");
- Directory dir = FSDirectory.open(indexDir);
- Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_36);
- IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36,luceneAnalyzer);
- iwc.setOpenMode(OpenMode.CREATE);
- IndexWriter indexWriter = new IndexWriter(dir,iwc);
- File[] textFiles = fileDir.listFiles();
- long startTime = new Date().getTime();
- //增加document到索引去
- for (int i = 0; i < textFiles.length; i++) {
- if (textFiles[i].isFile()
- && textFiles[i].getName().endsWith(".txt")) {
- System.out.println("File " + textFiles[i].getCanonicalPath()
- + "正在被索引....");
- String temp = FileReaderAll(textFiles[i].getCanonicalPath(),
- "GBK");
- System.out.println(temp);
- Document document = new Document();
- Field FieldPath = new Field("path", textFiles[i].getPath(),
- Field.Store.YES, Field.Index.NO);
- Field FieldBody = new Field("body", temp, Field.Store.YES,
- Field.Index.ANALYZED,
- Field.TermVector.WITH_POSITIONS_OFFSETS);
- document.add(FieldPath);
- document.add(FieldBody);
- indexWriter.addDocument(document);
- }
- }
- indexWriter.close();
- //测试一下索引的时间
- long endTime = new Date().getTime();
- System.out
- .println("这花费了"
- + (endTime - startTime)
- + " 毫秒来把文档增加到索引里面去!"
- + fileDir.getPath());
- }
- public static String FileReaderAll(String FileName, String charset)
- throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- new FileInputStream(FileName), charset));
- String line = new String();
- String temp = new String();
- while ((line = reader.readLine()) != null) {
- temp += line;
- }
- reader.close();
- return temp;
- }
- }
输出结果为:
- File C:\source\test1.txt正在被索引....
- 欢迎来到绝对秋香的博客。
- File C:\source\test2.txt正在被索引....
- 绝对秋香引领你走向潮流。
- 这花费了641 毫秒来把文档增加到索引里面去!C:\source
第四步,建立测试类TestQuery,并运行主函数,输出测试结果
- package com.newtouchone.lucene;
- import java.io.File;
- import java.io.IOException;
- import org.apache.lucene.analysis.Analyzer;
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.index.IndexReader;
- import org.apache.lucene.queryParser.ParseException;
- import org.apache.lucene.queryParser.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.FSDirectory;
- import org.apache.lucene.util.Version;
- public class TestQuery {
- public static void main(String[] args) throws IOException, ParseException {
- String index = "C:\\index"; //搜索的索引路径
- IndexReader reader = IndexReader.open(FSDirectory.open(new File(index)));
- IndexSearcher searcher = new IndexSearcher(reader);
- ScoreDoc[] hits = null;
- String queryString = "绝对秋香"; //搜索的关键词
- Query query = null;
- Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
- try {
- QueryParser qp = new QueryParser(Version.LUCENE_36,"body", analyzer);
- query = qp.parse(queryString);
- } catch (ParseException e) {
- }
- if (searcher != null) {
- TopDocs results = searcher.search(query,10); //返回最多为10条记录
- hits = results.scoreDocs;
- if (hits.length > 0) {
- System.out.println("找到:" + hits.length + " 个结果!");
- }
- searcher.close();
- }
- }
- }
测试输出结果为:
- 找到:2 个结果!
附件homework.rar为项目文件,解压部署则可运行该lucene案例
- lucene-core-3.6.1.jar (1.5 MB)
- 下载次数: 605
- lucene-core-3.6.1-javadoc.jar (3.5 MB)
- 下载次数: 674
- homework.rar (4.5 MB)
- 下载次数: 500
lucene3.6.1 经典案例 入门教程 (包含从文件中读取content)的更多相关文章
- lucene3.6.0 经典案例 入门教程
第一步:下载并导入lucene的核心包(注意版本问题): 例如Lucene3.6版本:将lucene-core-3.6.0.jar拷贝到项目的libs 文件夹里. 例如Lucene4.6版本:将l ...
- Entity Framework入门教程(3)---EF中的上下文简介
1.DbContext(上下文类) 在DbFirst模式中,我们添加一个EDM(Entity Data Model)后会自动生成一个.edmx文件,这个文件中包含一个继承DbContext类的上下文实 ...
- DotNetBrowser入门教程(更新完善中)
DotNetBrowser 希望实现的目标:桌面软件可以完美运行Html5,内置支持MVC与WebSocket的微型服务器. 基于.Net 4.0开发.开发环境:VS2017,运行环境支持Window ...
- linux入门教程(六) Linux文件与目录管理
在linux中什么是一个文件的路径呢,说白了就是这个文件存在的地方,例如在上一章提到的/root/.ssh/authorized_keys 这就是一个文件的路径.如果你告诉系统这个文件的路径,那么系统 ...
- flask的模板引擎jinja入门教程 包含一个通过网络实时传输Video视频流的示例
本文首发于个人博客https://kezunlin.me/post/1e37a6/,欢迎阅读最新内容! tutorial to use python flask jinja templates and ...
- Entity Framework入门教程(4)---EF中的实体关系
这一节将总结EF是怎么管理实体之间的关系.EF与数据库一样支持三种关系类型:①一对一 ,②一对多,③多对多. 下边是一个SchoolDB数据库的实体数据模型,图中包含所有的实体和各个实体间的关系.通过 ...
- JavaScript 入门教程二 在HTML中使用 JavaScript
一.使用 <script> 元素的方式有两种:直接在页面中嵌入 JavaScript 代码和引用外部 JavaScript 文件. 二.使用内嵌方式,一般写法为: <script t ...
- Entity Framework入门教程(5)---EF中的持久化场景
EF中的持久性场景 使用EF实现实体持久化(保存)到数据库有两种情况:在线场景和离线场景. 1.在线场景 在线场景中,context是同一个上下文实例(从DbContext派生),检索和保存实体都通过 ...
- Entity Framework入门教程(7)--- EF中的查询方法
这里主要介绍两种查询方法 Linq to entity(L2E)和Sql 1.L2E查询 L2E查询时可以使用linq query语法,或者lambda表达式,默认返回的类型是IQueryable,( ...
随机推荐
- LeetCode OJ 153. Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- 1.编写一个Java应用程序,该程序中有3个类:Ladder、Circle和主类A。具体要求如下:Ladder类具有类型为double的上底、下底、高、面积属性,具有返回面积的功能,包括一个构造方法对上底、下底、高进行初始化。Circle类具有类型为double的半径、周长和面积属性,具有返回周长、面积的功能,包括一个构造方法对半径进行初始化。主类A用来测试类Ladder和类Circle的功能。
Ladder package com.hanqi.test; public class Ladder { //属性 double shangdi,xiadi,gao,mianji; //构造方法 La ...
- slam相关知识
Kinect视觉SLAM技术介绍 http://www.open-open.com/news/view/ce76e2 本文介绍SLAM的历史.理论以及实现的方式,且主要介绍基于视觉(Kinect)的实 ...
- C/C++语言的标准库函数malloc/free与运算符new/delete的区别
概括地说 1.malloc与free是C++/C的标准库函数,new/delete是C++的运算符,它们都可用于申请动态内存和释放内存. 2.对于非内部数据类型的对象而言,只用malloc/free无 ...
- Python基础篇-day2
主要内容: for循环 while循环 格式化输出(2) 数据统计及记录 ############################################################# 1 ...
- mysql、sqlserver数据库常见数据类型对应java中的的类型探究
由于本次测试表的结构不涉及到主键的自增长,所以mysql.sqlserver建表语句相同: CREATE TABLE testType ( id INT NOT NULL DEFAULT 0, gen ...
- JavaScript(4)——闭包与this对象以及window对象
闭包与this对象以及window对象 这次写的是这三个内容.其实在写之前,会觉得这三个内容很多,但是写了之后会发现,内容确实很多,但是可以写出来的也并不是很多.可能是我总结能力太差.但是这些内容我觉 ...
- 外部VBS的调用
一.QTP调用外部VBS的方法 加到QTP的Resource中 在QTP菜单中设置, 菜单FileàSettingsàResource,将要加载的VB脚本添加进来. 举例: 步骤1:在D盘下新建一个V ...
- LeetCode OJ 100. Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- OpenCV ——双线性插值(Bilinear interpolation)
1,原理 在图像的仿射变换中,很多地方需要用到插值运算,常见的插值运算包括最邻近插值,双线性插值,双三次插值,兰索思插值等方法,OpenCV提供了很多方法,其中,双线性插值由于折中的插值效果和运算速度 ...