Lunce(全文检索入门)

全文检索的目的:

为了替换高级查询,使查询的速度更快,应用各种中小型的应用程序中。

特点:

(1)根据相关度来进行排序,相关度高的排在最前面

(2)只关注文本,不考虑语意

(3)不支持分布式

Lunce的核心:创建索引搜索索引

①创建索引

将现实世界中所有的结构化和非结构化数据提取信息,创建索引的过程。

图片加载失败

在①处分别为3个句子加上编号,然后进行分词,把被一个单词分解出来与编号对应放在②处;在搜索的过程总,对于搜索的过程中大写和小写指的都是同一个单词,在这就没有区分的必要,按规则统一变为小写放在③处;要加快搜索速度,就必须保证这些单词的排列时有一定规则,这里按照字母顺序排列后放在④处;最后再简化索引,合并相同的单词,就得到⑤的结果。

②搜索索引

得到用户的查询请求,搜索创建的索引,然后返回结果的过程

Lunce入门

Lucene中的索引维护使用IndexWriter,由这个类提供添删改相关的操作;

创建索引就是在本地创建一个索引库,这个库的结构可以理解为(非官方理解,这样只是便于理解):Document(行)及IndexableField(列)

索引的搜索则是使用IndexSearcher进行索引的搜索;

使用导入三个jar包:lucene-analyzers-common-5.5.0.jar,lucene-core-5.5.0.jar,lucene-queryparser-5.5.0.jar

①创建索引

步骤:

        1、 把文本内容转换为Document对象

                      文本是作为Document对象的一个字段而存在

        2、准备IndexWriter(索引写入器)

        3 、通过IndexWriter,把Document添加到缓冲区并提交:addDocument,commit,close

//创建索引的数据 现在写死,以后根据实际应用场景
	String doc1 = "hello world";
	String doc2 = "hello java world";
	String doc3 = "hello lucene world";
        //生成的索引库的位置
	private String path ="F:/eclipse/workspace/lucene/index/hello";
@Test
public void testCreate() {
    try {
	//2、准备IndexWriter(索引写入器)
	//索引库的位置 FS fileSystem
	Directory d = FSDirectory.open(Paths.get(path ));
	//分词器
	Analyzer analyzer = new SimpleAnalyzer();
	//索引写入器的配置对象
	IndexWriterConfig conf = new IndexWriterConfig(analyzer);
	IndexWriter indexWriter = new IndexWriter(d, conf);
	System.out.println(indexWriter);
			
	//1、 把文本内容转换为Document对象
	//把文本转换为document对象
	Document document1 = new Document();
	//标题字段
	document1.add(new TextField("title", "doc1", Store.YES));
	document1.add(new TextField("content", doc1, Store.YES));
	//添加document到缓冲区
	indexWriter.addDocument(document1);
	Document document2 = new Document();
	//标题字段
	document2.add(new TextField("title", "doc2", Store.YES));
	document2.add(new TextField("content", doc2, Store.YES));
	//添加document到缓冲区
	indexWriter.addDocument(document2);
	Document document3 = new Document();
	//标题字段
	document3.add(new TextField("title", "doc3", Store.YES));
	document3.add(new TextField("content", doc3, Store.YES));
			
	//3 、通过IndexWriter,把Document添加到缓冲区并提交
	//添加document到缓冲区
	indexWriter.addDocument(document3);
	indexWriter.commit();
	indexWriter.close();
			
    } catch (Exception e) {
	e.printStackTrace();
	}
}

②搜索索引

步骤:

         1 封装查询提交为查询对象

         2 准备IndexSearcher

         3 使用IndexSearcher传入查询对象做查询-----查询出来只是文档编号DocID

         4 通过IndexSearcher传入DocID获取文档

         5 把文档转换为前台需要的对象 Docment---->  Article

@Test
public void testSearch() {
    String keyWord = "lucene";
    try {
        // * 1 封装查询提交为查询对象
        //通过查询解析器解析一个字符串为查询对象
        String f = "content"; //查询的默认字段名,
        Analyzer a = new SimpleAnalyzer();//查询关键字要分词,所有需要分词器
        QueryParser parser = new QueryParser(f, a);
        Query query = parser.parse("content:"+keyWord);

        // * 2 准备IndexSearcher
        Directory d = FSDirectory.open(Paths.get(path ));
	IndexReader r = DirectoryReader.open(d);
	IndexSearcher searcher = new IndexSearcher(r);

        // * 3 使用IndexSearcher传入查询对象做查询-----查询出来只是文档编号DocID
	TopDocs topDocs = searcher.search(query, 1000);//查询ton条记录 前多少条记录
	System.out.println("总命中数:"+topDocs.totalHits);
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;//命中的所有的文档的封装(docId)

	// * 4 通过IndexSearcher传入DocID获取文档
	for (ScoreDoc scoreDoc : scoreDocs) {
	    int docId = scoreDoc.doc;
	    Document document = searcher.doc(docId);
	    // * 5 把文档转换为前台需要的对象 Docment---->Article 
 System.out.println("title:"+document.get("title")++",content:"+document.get("content"));	
	}
    } catch (Exception e) {
	e.printStackTrace();
    }
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值