您的位置:首页 > 其它

Lucene4.3开发之第一步小试牛刀

2013-11-05 17:37 246 查看
首先,本篇适合对于刚学lucene的朋友们,在这之前笔者还是喜欢啰嗦几句,想要学好一门技术,首先就得从思想层次上全面了解这种技术的作用,适用范围,以及优缺点,对于这些理论,大家可以先在Google,或百度上深入了解一下,也可以到其官方网站上看文档或者WIKI,只有在思想层面上,大概了解这个技术的总体架构,那么学起来,就可以很快上手,举个例子,先“会当凌绝顶”一下,然后在爬山,肯定会容易一些,笔者想说的就是这个道理。

下面就开始进入主题,本项目采用Maven进行构件,由于以后的lucene学习采用的架包都差不多,所以就新建了lucene-parent项目主要是对架包进行统一管理,该项目的结构如下:



只是一个简单的pom.xml文件,该文件内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>

<groupId>lucene</groupId>
<artifactId>lucene-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Lucene Parent</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<lucene.version>4.3.1</lucene.version>
<junit.version>3.8.1</junit.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>${lucene.version}</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>${lucene.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

然后新建一个项目进行lucene的入门学习,本篇的入门代码,相对简单,主要是先把lucene添加的Demo给搭建起来,后续的修改,删除,查询会在后面的文章中一一补上,笔者觉得学习这东西还是脚踏实地一步一步来比较好,只要真正真正理解每一行代码的意思,我们就算有收获了,有时候学习步伐太快,反而会根基不牢,效果不好。

项目名叫lucene-index,项目的结构如下:



pom.xml文件的配置如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<parent>
<groupId>lucene</groupId>
<artifactId>lucene-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../lucene-parent/pom.xml</relativePath>
</parent>

<artifactId>lucene-index</artifactId>
<name>lucene-index</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
</dependency>
</dependencies>
</project>


CommonLuceneBasic.java的代码如下:

package cn.lichunan.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.StringField;
import org.apache.lucene.document.Field.Store;
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;

/**
* Lucene的演示Demo类
*
*/
public class CommonLuceneBasic
{
/**
* 抽象的父类文件夹
*/
public static Directory directory;

/**
* 返回IndexWriter
* @throws IOException
*/
public static IndexWriter getWriter() throws IOException{
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);//设置标准分词器,默认是一元分词
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_43, analyzer);//设置IndexWriterConfig
return new IndexWriter(directory, iwc);
}

/**
*
* @param indexWriterPath 索引存放路径
*/
public static void add(String indexWriterPath){
IndexWriter writer = null;
try {
directory = FSDirectory.open(new File(indexWriterPath));//打开存放索引的路径
writer = getWriter();
Document doc = new Document();
doc.add(new StringField("id", "1", Store.YES));//存储
doc.add(new StringField("name", "张飞", Store.YES));//存储
doc.add(new StringField("content", "也许放弃,才能靠近你!", Store.YES));//存储
writer.addDocument(doc);//添加写入流里
writer.forceMerge(1);//优化压缩段,大规模添加数据的时候,少使用本方法,会影响性能
writer.commit();//提交数据
System.out.println("添加成功");
} catch (Exception e) {
e.printStackTrace();
} finally{
if(writer != null){
try {
writer.close();//关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

public static void main(String[] args){
String path = "E:\\临时索引";
add(path);//调用添加方法
}

}


添加成功后,我们可以通过Luke工具,进行索引查看,如果不知道Luke工具是什么,或者不知道怎么使用,可以到网上去查查资料。

下面贴一张使用Luke工具查看后的结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: