lucene4入门(1)
欢迎转载http://www.cnblogs.com/shizhongtao/p/3440325.html
lucene你可以理解为一种数据库,他是全文搜索的一种引擎。
1.首先去官网download最新的jar包,我下载的是4.5版本的,当然你也可以使用maven来下载,
2.新建项目,并把lucene-core-4.5.1.jar加入到项目中,其他需要的分词器等jar包,可以用的时候加入就可以。因为是入门创建java project就可以了。
3.lucene中主要分为三部分,分别是索引部分、分词部分、搜索部分。
- 索引部分:可以理解像字典中前面的查找索引
- 分词部分:就是将内容进行拆分,比如“我是好人”,这个词我们怎么去分词。“我”,“好人”,“人”等。
- 搜索部分:就是如何去查找了。
4.创建索引,因为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.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version; /**
* @author bingyulei
*
*/
public class HelloLucene
{
/**
* 建立索引
*/
public void createIndex(String indexWriterPath){
// 创建directory
Directory directory=null;
// 创建indexwriter
Analyzer analyzer=new StandardAnalyzer(Version.LUCENE_45);//设置标准分词器 ,默认是一元分词
IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_45, analyzer);//设置IndexWriterConfig
IndexWriter writer=null; try
{
directory= FSDirectory.open(new File(indexWriterPath));//打开存放索引的路径
writer=new IndexWriter(directory, iwc);
// 创建Document对象
Document doc=new Document();
//为document添加field
doc.add(new StringField("id", "1", Store.YES));//存储
doc.add(new StringField("name", "hello", Store.YES));//存储
doc.add(new StringField("content", "hello world!", Store.YES));//存储
//通过IndexWriter添加文档
writer.addDocument(doc);
writer.commit();//提交数据
System.out.println("添加成功");
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
5.然后测试代码
public class HelloLuceneTest
{
@Test
public void test(){
HelloLucene test=new HelloLucene();
test.createIndex("D:\\lucene\\index");
}
}
6.如果想要把电脑的文件假如索引,简单文档的话可以这样写。下图是文件

java代码:
package com.bing.test; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; 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.FieldType;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version; /**
* @author bingyulei
*
*/
public class HelloLucene
{ Directory directory = null;
Document doc;
IndexWriter writer = null; /**
*
* @param indexWriterPath 索引创建路径
* @param filePath 读取文件路径
*/
public void createIndex(String indexWriterPath, String filePath)
{ // 创建indexwriter
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_45);// 设置标准分词器
// ,默认是一元分词
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_45,
analyzer);// 设置IndexWriterConfig try
{
// 创建directory
//directory=RAMDirectory();//创建在内存中
//创建在硬盘上
directory = FSDirectory.open(new File(indexWriterPath));// 打开存放索引的路径
writer = new IndexWriter(directory, iwc); // 为document添加field
addFile(writer,filePath); System.out.println("添加成功");
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} } private void addFile(IndexWriter writer,String filePath)
{
File f = new File(filePath);
FieldType ft = new FieldType();
ft.setIndexed(true);//索引
ft.setStored(true);//存储,数据量比较大,一般都是不鼓励存储,放在索引文件中会把索引文件撑大
ft.setTokenized(true);
for (File file : f.listFiles())
{
try
{
// 创建Document对象
doc = new Document();
//doc.add(new Field("content", new FileReader(file), ft));
doc.add(new TextField("content",new FileReader(file)));// 这个方法默认的Store的属性是NO
doc.add(new TextField("filename",file.getName(),Store.YES));
doc.add(new StringField("path", file.getPath(), Store.YES));
//添加文档
writer.addDocument(doc);
writer.commit();// 提交数据
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
}
测试代码:
package com.bing.test; import org.junit.Test; public class HelloLuceneTest
{
@Test
public void test(){
HelloLucene test=new HelloLucene();
test.createIndex("D:\\lucene\\index","D:\\lucene\\file");
}
}
lucene4入门(1)的更多相关文章
- lucene4入门(3)琐记
欢迎转载http://www.cnblogs.com/shizhongtao/p/3440486.html <--这个是lucene4.6的api下载地址,格式是chm的.需要的人可以下载htt ...
- lucene4入门(2)搜索
欢迎转载http://www.cnblogs.com/shizhongtao/p/3440479.html 接着上一篇,这里继续搜索,对于搜索和创建一样,首先你要确定搜索位置,然后用规定的类来读取.还 ...
- java课程设计团队博客《基于学院的搜索引擎》
JAVA课程设计 基于学院网站的搜索引擎 对学院网站用爬虫进行抓取.建索(需要中文分词).排序(可选).搜索.数据摘要高亮.分页显示.Web界面. 一.团队介绍 学号 班级 姓名 简介 2016211 ...
- Lucene4.3入门
辞职交接期间无聊看了一下搜索引擎,java社区比较火的当然是Lucene,想写一个简单的小例子,在网上找了些资料,不过都不是4.3的,自己看了一下. 下载地址:http://lucene.apache ...
- [全文检索]Lucene基础入门.
本打算直接来学习Solr, 现在先把Lucene的只是捋一遍. 本文内容: 1. 搜索引擎的发展史 2. Lucene入门 3. Lucene的API详解 4. 索引调优 5. Lucene搜索结果排 ...
- Lucene基础(一)--入门
Lucene介绍 lucene的介绍,这里引用百度百科的介绍Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,即它不是一个完整的全文检索引 ...
- Lucene全文检索入门使用
一. 什么是全文检索 全文检索是计算机程序通过扫描文章中的每一个词,对每一个词建立一个索引,指明该词在文章中出现的次数和位置.当用户查询时根据建立的索引查找,类似于通过字典的检索字表查字的过程 全文检 ...
- ElasticSearch入门介绍之安装部署(二)
散仙,在上篇文章对ElasticSearch整体入门作了个介绍,那么本篇我们来看下,如何安装,部署es,以及如何安装es的几个比较常用的插件. es的安装和部署,是非常简单方便的,至少这一点散仙在es ...
- Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求
上一篇:Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数 感觉这篇不是很好写,因为涉及到网络请求,如果采用真实的网络请求,这个例子大家拿到手估计还要自己写一个web ...
随机推荐
- linux 下网站压力测试工具webbench
一直在用webbench ,这个linux下的网站压力测试工具.整理下. 笔记本装的ubuntu,其他linux系统也差不多. webbench 需要先安装 ctags,一个vim的阅读插件,可以直接 ...
- 优秀js插件收藏
1. 滚动视差效果,类似锤子主页等效果实现 https://github.com/hahnzhu/parallax.js 2. jQuery全屏滚动插件 http://www.dowebok.com/ ...
- C++赋值运算符函数
为类添加赋值运算符函数: 类型定义 class CMyString { public: CMyString(char *pData = NULL); CMyString(const CMyString ...
- 琐碎-关于hadoop2.X那些端口
此文转载http://www.aboutyun.com/thread-7513-1-1.html Hadoop集群的各部分一般都会使用到多个端口,有些是daemon之间进行交互之用,有些是用于RPC访 ...
- How to solve Original Tango programmer”Hardware not Found”?
Original Tango programmer is a new generation of transponder programmer which is developed to cover, ...
- js工作中编程习惯
在前端编程中总结的习惯不管js还是css 还是后端开发这几点都是必须要做到的做好这几点不管去什么公司都是受到别人的尊重 善用变量,尤其是对DOM结构中的ID以及CLASS 多写注释,自己不熟,前面写后 ...
- Ubuntu14.04 Kylin下 GO语言环境搭建
sudo apt-get install golang gccgo安装 gcc -v 查看 --enable-languages=c,c++,objc,obj-c++,java,fortran,ada ...
- Linux vsftpd 无法登录 cannot change directory:xxx priv_sock_get_cmd 问题
配置vsftpd时本地用户无法切换不能登录问题.问题如下: C:\Users\kai>ftp ftp> open 172.24.144.10 连接到 172.24.144.10. (vsF ...
- 【推理,贪心】UVa 1319 - Maximum
看到了大神的代码.理解了好久...真是差距. 题意:给出m, p, a, b,然后xi满足已下两个公式, 求 xp1 + xp2 +...+ xpm 的最大值. 1.-1/sqrt(a) <= ...
- Messenger实现Android IPC
当Service不需要支持并发操作时Messenger会非常有用.Messenger类使用Handler执行每个传入的消息,所有客户端的调用都按顺序运行在同一个线程上,这和AIDL是有区别的,AIDL ...