java建立索引文件检索_java学习:全文检索引擎Lucene(2)创建索引库,查询索引,分析器,Field种类...

使用java操作lucene

使用lucene创建索引库

package cn.figo.lucene;

import org.apache.commons.io.FileUtils;

import org.apache.lucene.document.*;

import org.apache.lucene.index.*;

import org.apache.lucene.search.*;

import org.apache.lucene.store.Directory;

import org.apache.lucene.store.FSDirectory;

import org.junit.Test;

import java.io.File;

/*** @Author Figo* @Date 2019/12/19 0:12*/

public class LuceneFirst {

public void createIndex() throws Exception {

//1、创建一个Director对象,指定索引库保存的位置。//把索引库保存在内存中//Directory directory = new RAMDirectory();//把索引库保存在磁盘Directory directory = FSDirectory.open(new File("D:\\1_Code\\java\\lucene\\temp\\index").toPath());

//2、基于Directory对象创建一个IndexWriter对象// IndexWriterConfig config = new IndexWriterConfig(new IKAnalyzer());IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig());

//3、读取磁盘上的文件,对应每个文件创建一个文档对象。File dir = new File("D:\\1_Code\\java\\lucene\\searchsource");

File[] files = dir.listFiles();

for (File f : files) {

//取文件名String fileName = f.getName();

//文件的路径String filePath = f.getPath();

//文件的内容String fileContent = FileUtils.readFileToString(f, "utf-8");

//文件的大小long fileSize = FileUtils.sizeOf(f);

//创建Field//参数1:域的名称,参数2:域的内容,参数3:是否存储Field fieldName = new TextField("name", fileName, Field.Store.YES);

Field fieldPath = new TextField("path", filePath, Field.Store.YES);

// Field fieldPath = new StoredField("path", filePath);Field fieldContent = new TextField("content", fileContent, Field.Store.YES);

Field fieldSize = new TextField("size", fileSize + "", Field.Store.YES);

// Field fieldSizeValue = new LongPoint("size", fileSize);// Field fieldSizeStore = new StoredField("size", fileSize);//创建文档对象Document document = new Document();

//向文档对象中添加域document.add(fieldName);

document.add(fieldPath);

document.add(fieldContent);

document.add(fieldSize);

// document.add(fieldSizeValue);// document.add(fieldSizeStore);//5、把文档对象写入索引库indexWriter.addDocument(document);

}

//6、关闭indexwriter对象indexWriter.close();

}

}

原始文档

生成的索引库

生成的索引库是二进制文件,可以通过 luke工具查看

关键词列表

每个文档的内容

查询保存的索引

public void searchIndex() throws Exception {

//1、创建一个Director对象,指定索引库的位置Directory directory = FSDirectory.open(new File("D:\\1_Code\\java\\lucene\\temp\\index").toPath());

//2、创建一个IndexReader对象IndexReader indexReader = DirectoryReader.open(directory);

//3、创建一个IndexSearcher对象,构造方法中的参数indexReader对象。IndexSearcher indexSearcher = new IndexSearcher(indexReader);

//4、创建一个Query对象,TermQueryQuery query = new TermQuery(new Term("name", "spring"));

//5、执行查询,得到一个TopDocs对象//参数1:查询对象 参数2:查询结果返回的最大记录数TopDocs topDocs = indexSearcher.search(query, 10);

//6、取查询结果的总记录数System.out.println("查询总记录数:" + topDocs.totalHits);

//7、取文档列表ScoreDoc[] scoreDocs = topDocs.scoreDocs;

//8、打印文档中的内容for (ScoreDoc doc :

scoreDocs) {

//取文档idint docId = doc.doc;

//根据id取文档对象Document document = indexSearcher.doc(docId);

System.out.println(document.get("name"));

System.out.println(document.get("path"));

System.out.println(document.get("size"));

//System.out.println(document.get("content"));System.out.println("-----------------寂寞的分割线");

}

//9、关闭IndexReader对象indexReader.close();

}

查询结果如下

分析器

关于分析器

IndexWriterConfig 默认使用标准分析器 StandardAnalyzer

public IndexWriterConfig() {

this(new StandardAnalyzer());

}

查看标准分析器的分析效果

public void testTokenStream() throws Exception {

//1)创建一个Analyzer对象,StandardAnalyzer对象Analyzer analyzer = new StandardAnalyzer();

//2)使用分析器对象的tokenStream方法获得一个TokenStream对象TokenStream tokenStream = analyzer.tokenStream("", "The Spring Framework provides a comprehensive programming and configuration model.");

//3)向TokenStream对象中设置一个引用,相当于数一个指针CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);

//4)调用TokenStream对象的rest方法,把指针调到最前。如果不调用抛异常tokenStream.reset();

//5)使用while循环遍历TokenStream对象while(tokenStream.incrementToken()) {

System.out.println(charTermAttribute.toString());

}

//6)关闭TokenStream对象tokenStream.close();

}

分析效果

标准分析器对中文支持不太好,可以使用第三方 IKAnalyzer 分析器

使用 IKAnalyzer 需要将配置文件和扩展词典添加到工程的classpath下

扩展词典严禁使用win记事本编辑,需要保证 utf-8格式

扩展词典可以添加一些新词

停用词词典可以是一些无意义的词或者是敏感词

Analyzer analyzer = new IKAnalyzer();

可以自行添加扩展词典和停用词词典

使用新的分析器重新创建索引库

IndexWriterConfig config = new IndexWriterConfig(new IKAnalyzer());

IndexWriter indexWriter = new IndexWriter(directory, config);

Field的选择

1 StringField(FieldName, FieldValue,Store.YES))

数据类型 字符串,一般不分析,可以索引和储存

2 LongPoint(String name, long... point)

数据类型 Long型,一般分析和索引,不储存

3 StoredField(FieldName, FieldValue)

一般只储存,不分析和索引

4 TextField(FieldName, FieldValue, Store.NO) 或 TextField(FieldName, reader)

数据类型 字符串或流,一般分析、索引且储存

之前的Field可以改为

Field fieldName = new TextField("name", fileName, Field.Store.YES);

Field fieldPath = new StoredField("path", filePath);

Field fieldContent = new TextField("content", fileContent, Field.Store.YES);

// 不储存Field fieldSizeValue = new LongPoint("size", fileSize);

// 只储存Field fieldSizeStore = new StoredField("size", fileSize);

这时path和size两个Field中的term为空,不参与分析

但由于我们使用StoredField储存了这两个值,所以是可以取出来的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值