您的位置:首页 > 其它

lucene 学习日记(一) 源码的导入,及第一个demo的开始

2016-03-24 16:18 483 查看
 第一次写博客写得不好大家随便吐槽就好了。只是把自己学习的lucene 记录下来方便日后自己回忆以及希望能帮助大家。
闲话不说了。往下看

学习工具,eclipse,lucene code 4.0.0 《下载地址》(我是用了4.0.0 如果不希望看我的博客出现版本问题建议和我一样), ant , lvy。
这里简单介绍一下 ant,lvy 
Apache Ant,是一个将软件编译、测试、部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发。
Apache Ivy 是Apache Ant 下的一个子项目。Apache Ivy是一个优秀的管理(记录、跟踪、解析和报告)项目依赖的工具,提供了强大的依赖管理功能,可与Apache Ant紧密集成。
由于 lucene 源码是使用这两个工具进行部署,所以把这两个下载下来,把 F:\apache-ant-1.9.6\bin目录配置到系统环境变量 path,把lvy 的jar包copy 到
F:\apache-ant-1.9.6\lib目录中同时把 F:\apache-ant-1.9.6\lib 配置到系统环境变量classpath 中。到这里,你可以cmd 运行 ant -v  这里出现缺少 build.xml 的信息或者版本信息,这个工具就配置好了。

我通过svn 把代码copy 下来之后  在 lucene-code-4.0.0 的目录下 进入cmd ,运行  ant eclipse    
命令  ,这里你会看到ant 会通过lvy 在服务器上下载相关的jar包,所以lvy还是蛮好用的(我第一次接触,感觉里面的配置都相对比较简单。不像maven那么复杂)。然后一直等到 successful的出现。看你网速了。

现在可以把代码导进来了。eclipse中选择new java project 去掉 use default location 的勾。选择 lucene-code-4.0.0/lucene 目录 点击导入。完成后你也许会看到目录里面有编译错误。不用担心,把项目的编码格式从 gbk  改成 utf-8 就好了。

这里你的源码导入已经完成了。

下面
下面
下面。。。。
第一个简单的lucene 相关的demo 代码 ()

在项目新建一个自己的包,然后放入下面两个类。
第一个类 IndexFile新建 索引文件的
package com.edu;
import java.io.File;

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.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;
public class IndexFile {

protected String[] ids={"1", "2"};

protected String[] content={"Amsterdam has lost of add  cancals", "i love  add this girl"};

protected String[] city={"Amsterdam", "Venice"};

private Directory dir;

/**
* 初始添加文档
* @throws Exception
*/
public void init() throws Exception {
String pathFile="D://lucene/index";
dir=FSDirectory.open(new File(pathFile).toPath());
IndexWriter writer=getWriter();
for(int i=0; i < ids.length; i++) {
Document doc=new Document();
doc.add(new StringField("id", ids[i], Store.YES));
doc.add(new TextField("content", content[i], Store.YES));
doc.add(new StringField("city", city[i], Store.YES));
writer.addDocument(doc);
}
System.out.println("init ok?");
writer.close();
}

/**
* 获得IndexWriter对象
* @return
* @throws Exception
*/
public IndexWriter getWriter() throws Exception {
Analyzer analyzer=new StandardAnalyzer();
IndexWriterConfig iwc=new IndexWriterConfig(analyzer);
return new IndexWriter(dir, iwc);
}

public static void main(String[] args) throws Exception {
new IndexFile().init();
}

}


第二个文件是通过索引获得文件

package com.edu;

import java.io.File;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
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;

public class IndexSearch {

/**
* 查询
* @throws Exception
*/
public void search() throws Exception {
String filePath="D://lucene/index";
Directory dir=FSDirectory.open(new File(filePath).toPath());
IndexReader reader=DirectoryReader.open(dir);
IndexSearcher searcher=new IndexSearcher(reader);
Term term=new Term("content", "add");
TermQuery query=new TermQuery(term);
TopDocs topdocs=searcher.search(query, 5);
ScoreDoc[] scoreDocs=topdocs.scoreDocs;
System.out.println("查询结果总数---" + topdocs.totalHits+"最大的评分--"+topdocs.getMaxScore());
for(int i=0; i < scoreDocs.length; i++) {
int doc = scoreDocs[i].doc;
Document document = searcher.doc(doc);
System.out.println("content===="+document.get("content"));
System.out.println("id--" + scoreDocs[i].doc + "---scors--" + scoreDocs[i].score+"---index--"+scoreDocs[i].shardIndex);
}
reader.close();
}

public static void main(String[] args) throws Exception {
new IndexSearch().search();
}
}


结束。 跑一下就知道效果了。我自己还在看着两段代码。有心得的放到评论下给我看看。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lucene