关于lucene学习(一)
1.luecene 的本地创建 索引库
首先我创建的是maven项目,需要引入maven坐标(代码引入可用),仅仅作为学习的一个参考,其实一个功能的实现是,方法有很多,具体提还需要自己去体会
相关得到依赖
<!--Lunece 相关的maven依赖-->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<version>5.5.0</version>
</dependency>
2、关于 本地读取本地文件,写入索引库,创建索引的代码
@Test
public void TestLunce() throws IOException {
//初始化索引库对象
FSDirectory directory = FSDirectory.open(Paths.get("F:\\Luncene\\index1"));
//初始化索引写入器对象
StandardAnalyzer standardAnalyzer = new StandardAnalyzer();
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(standardAnalyzer);
IndexWriter indexWriter = new IndexWriter(directory,indexWriterConfig);
//初始化文档对象
Document document = new Document();
document.add(new TextField("content","学好技术,至少是改变现在生活的关键", Field.Store.YES));
indexWriter.addDocument(document);
indexWriter.commit();
indexWriter.close();
}
//读取索引库信息并进行查找
@Test
public void TestSerch() throws IOException {
//准备索引的关键词
String keyWord="学";
//初始化化索引库的存储信息
FSDirectory directory = FSDirectory.open(Paths.get("F:\\Luncene\\index2"));
//初始化索引库读取器
DirectoryReader directoryReader = DirectoryReader.open(directory);
//初始化索引的检索器
IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
Term t = new Term("content",keyWord);
//创建了一个基于词源的查询对象
TermQuery termQuery = new TermQuery(t);
//参数1代表的是查询条件,参数二代表的是匹配的n条记录
//查询前十条数据
TopDocs topDocs = indexSearcher.search(termQuery, 10);
System.out.println("命中结果得到数量"+topDocs.totalHits);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
//这个是得分,也就是权重值,决定着排名
float score = scoreDoc.score;
System.out.println("文档的得分"+score);
int doc = scoreDoc.doc;
System.out.println(doc);
Document document = directoryReader.document(doc);
String content = document.get("content");
System.out.println(content);
}
}
/**
* 读取外部文件获取索引库
* @throws IOException
*/
@Test
public void Test3() throws IOException {
//初始化索引库对象
FSDirectory directory = FSDirectory.open(Paths.get("F:\\Luncene\\index2"));
//初始化索引写入器对象
StandardAnalyzer standardAnalyzer = new StandardAnalyzer();
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(standardAnalyzer);
IndexWriter indexWriter = new IndexWriter(directory,indexWriterConfig);
File file = new File("E:\\test");
File[] files = file.listFiles();
Document document=null;
for (int i = 0; i < files.length; i++) {
document = new Document();
//获取索引库的编号
document.add(new IntField("id",i, Field.Store.YES));
//获取索文件的名字
document.add(new StringField("name",files[i].getName(), Field.Store.YES));
//获取文件的内容
document.add(new TextField("content",new FileReader(files[i])));
}
//初始化文档对象
indexWriter.commit();
indexWriter.close();
}
//读取信息
Test
public void TestUpdate() throws IOException {
//初始化索引库对象
FSDirectory directory = FSDirectory.open(Paths.get("F:\\Luncene\\index2"));
//初始化索引写入器对象
StandardAnalyzer standardAnalyzer = new StandardAnalyzer();
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(standardAnalyzer);
IndexWriter indexWriter = new IndexWriter(directory,indexWriterConfig);
Document document = new Document();
document.add(new TextField("content","woaishanghai", Field.Store.YES));
Term term = new Term("content", "中");
indexWriter.updateDocument(term,document);
}
运行以上的代码,仔细阅读,你就会发现其实luecenede 学习还是很简单的,还是那句话,学习技术很简单,难的是如何进行应用,明天更关于高亮显示的相关代码,另外上面的代码很简单,注解写的也算是比较清晰,想学习,可以慢慢看一下