Lucene_day02_入门

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35537301/article/details/82589476

入门案例

下载jar包

官网

 创建java项目

写入索引库的步骤

  1. 采集数据
  2. 创建文档
  3. 创建域Field
  4. 把数据放在域中,把域放在文档中
  5. 把文档放在文档集合中
  6. 创建分词器对象
  7. 创建指定索引库地址的流对象
  8. 创建写入索引库的配置对象
  9. 创建写入索引库的对象,需要7-8步的对象
  10. 遍历文档集合写入索引库
  11. 释放资源
package com.itheima.test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;

import com.itheima.dao.BookDao;
import com.itheima.dao.BookDaoImpl;
import com.itheima.pojo.Book;

public class IndexLucene {

	@Test
	public void test() throws Exception {
		// 1.采集数据
		BookDao bookDao = new BookDaoImpl();
		List<Book> bookList = bookDao.findBookList();

		// 创建文档集合
		List<Document> docList = new ArrayList<Document>();

		for (Book book : bookList) {

			// 2.创建文档对象
			Document doc = new Document();

			// 3.创建域对象
			// 参数说明:1.域名 2.域值 3.是否存储
			Field idField = new TextField("id", book.getId() + "", Store.YES);
			Field nameField = new TextField("name", book.getName(), Store.YES);
			Field priceField = new TextField("price", book.getPrice() + "", Store.YES);
			Field picField = new TextField("pic", book.getPic() + "", Store.YES);
			Field descField = new TextField("desc", book.getDesc() + "", Store.YES);

			// 4.把数据放在域中,把域放在文档中
			doc.add(idField);
			doc.add(nameField);
			doc.add(priceField);
			doc.add(picField);
			doc.add(descField);

			// 5.把文档放在集合中
			docList.add(doc);
		}
		// 6.创建分词器对象:lucene中默认的分词器对象:StandardAnalyzer(英文分词器对象)
		StandardAnalyzer analyzer = new StandardAnalyzer();
		
		// 7.创建指定索引库地址的流对象
		FSDirectory directory = FSDirectory.open(new File("D:/log"));
		
		// 8.创建索引库配置对象
		// new IndexWriterConfig(版本号, 分词器);
		IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);
		
		// 9.创建写入索引库的对象(1、指定索引库地址的流;2、指定索引库配置对象)
		IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
		
		// 10.把数据写入索引库
		for (Document document : docList) {
			indexWriter.addDocument(document);
		}
		
		// 11.释放资源
		indexWriter.close();
	}

}

搜索的步骤

  1. 创建分词器对象
  2. 创建搜索解析器对象
  3. 创建查询条件对象
  4. 创建指定索引库地址流的对象
  5. 创建读取流的对象
  6. 根据这个读对象创建搜索对象IndexSearch
  7. 指定查询,返回结果集
  8. 根据这个结果集获取坐标
  9. 遍历坐标
  10. 根据这个坐标获取文档ID
  11. 根据这个文档ID查询文档数据
  12. 打印文档数据(封装数据)
  13. 释放资源
package com.itheima.test;

import java.io.File;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;

public class SearchLucene {

	@Test
	public void test() throws Exception {
		// 1.创建分词器对象
		StandardAnalyzer analyzer = new StandardAnalyzer();

		// 2.创建一个搜索解析器对象QueryParser解析器对象(1、默认查询的域名;2、搜索的分词器对象)
		QueryParser parser = new QueryParser("name", analyzer);

		// 3.创建查询有条件对象Query;
		// 参数说明:1.存放的是查询条件;2.可以自定义查询语法
		// 当参数中只有查询的关键字的时候,那么就根据默认查询域来搜索;parser.parse("java");
		// 当参数中自定义查询域名的时候,那么就根据自定义的查询域来搜索,会覆盖默认查询域
		Query query = parser.parse("desc:java");

		// 4.创建一个索引库地址的流
		FSDirectory directory = FSDirectory.open(new File("D:/log"));

		// 5.创建读取索引库对象(根据这个索引库地址流)[文档,索引]
		IndexReader indexReader = DirectoryReader.open(directory);

		// 6.创建一个搜索的对象IndexSearch(根据读取流对象)[文档,索引]
		IndexSearcher indexSearcher = new IndexSearcher(indexReader);

		// 7.根据条件对象执行查询,查询的是索引库中索引;返回结果集
		// 参数说明:1、查询的条件对象;2、查询返回的记录数
		TopDocs topDocs = indexSearcher.search(query, 2);
		System.out.println("查询的总记录数:"+topDocs.totalHits);
		
		// 8.根据结果集获取坐标:数组形式的
		ScoreDoc[] docs = topDocs.scoreDocs;
		// 9.遍历数组
		for (ScoreDoc scoreDoc : docs) {
			
			// 10.根据每一个坐标中获取文档ID
			int docId = scoreDoc.doc;
			
			// 11.在IndexSearch中根据文档ID查询文档数据
			Document doc = indexSearcher.doc(docId);
			
			// 12.打印文档数据
			System.err.println("=======id======" + doc.get("id"));
			System.err.println("=======name======" + doc.get("name"));
			System.err.println("=======price======" + doc.get("price"));
			System.err.println("=======pic======" + doc.get("pic"));
		}
		// 13.释放资源
		indexReader.close();
	}

}

分词器

什么要用分词器

Lucene中自带的分词器是英文分词不会根据中文的语句分词

IKAnalyzer

下载地址

https://code.google.com/p/ik-analyzer/

如何使用中文分词器

  1. 导入IKAnalyzer核心jar
  2. 导入配置文件(IKAnalyzer.cfg.xml)和两个词典:扩展词典(ext.dic),停用词典(stopword.dic)
  3. 在代码中直接new IKAnalyzer分词器对象

注意

词典的编码必须是UTF-8无BOM格式

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">  
<properties>  
	<comment>IK Analyzer 扩展配置</comment>
	<!--用户可以在这里配置自己的扩展字典 -->
	<entry key="ext_dict">ext.dic;</entry> 
	
	<!--用户可以在这里配置自己的扩展停止词字典-->
	<entry key="ext_stopwords">stopword.dic;</entry> 
	
</properties>

扩展词典

编程思想
传智播客
java工程师

停用词词典

a
an
and
are
as
at
be
but
by
for
if
in
into
is
it
no
not
of
on
or
such
that
the
their
then
there
these
they
this
to
was
will
with
地
的
得
啊
package com.itheima.test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import com.itheima.dao.BookDao;
import com.itheima.dao.BookDaoImpl;
import com.itheima.pojo.Book;

public class IndexLucene {

	@Test
	public void test() throws Exception {
		// 1.采集数据
		BookDao bookDao = new BookDaoImpl();
		List<Book> bookList = bookDao.findBookList();

		// 创建文档集合
		List<Document> docList = new ArrayList<Document>();

		for (Book book : bookList) {

			// 2.创建文档对象
			Document doc = new Document();

			// 3.创建域对象
			// 参数说明:1.域名 2.域值 3.是否存储
			Field idField = new TextField("id", book.getId() + "", Store.YES);
			Field nameField = new TextField("name", book.getName(), Store.YES);
			Field priceField = new TextField("price", book.getPrice() + "", Store.YES);
			Field picField = new TextField("pic", book.getPic() + "", Store.YES);
			Field descField = new TextField("desc", book.getDesc() + "", Store.YES);

			// 4.把数据放在域中,把域放在文档中
			doc.add(idField);
			doc.add(nameField);
			doc.add(priceField);
			doc.add(picField);
			doc.add(descField);

			// 5.把文档放在集合中
			docList.add(doc);
		}
		// 6.创建分词器对象:lucene中默认的分词器对象:StandardAnalyzer(英文分词器对象)
		Analyzer analyzer = new IKAnalyzer();

		// 7.创建指定索引库地址的流对象
		FSDirectory directory = FSDirectory.open(new File("D:/log"));

		// 8.创建索引库配置对象
		// new IndexWriterConfig(版本号, 分词器);
		IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);

		// 9.创建写入索引库的对象(1、指定索引库地址的流;2、指定索引库配置对象)
		IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);

		// 10.把数据写入索引库
		for (Document document : docList) {
			indexWriter.addDocument(document);
		}

		// 11.释放资源
		indexWriter.close();
	}

}
package com.itheima.test;

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.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

public class SearchLucene {

	@Test
	public void test() throws Exception {
		// 1.创建分词器对象
		Analyzer analyzer = new IKAnalyzer();

		// 2.创建一个搜索解析器对象QueryParser解析器对象(1、默认查询的域名;2、搜索的分词器对象)
		QueryParser parser = new QueryParser("name", analyzer);

		// 3.创建查询有条件对象Query;
		// 参数说明:1.存放的是查询条件;2.可以自定义查询语法
		// 当参数中只有查询的关键字的时候,那么就根据默认查询域来搜索;parser.parse("java");
		// 当参数中自定义查询域名的时候,那么就根据自定义的查询域来搜索,会覆盖默认查询域
		Query query = parser.parse("desc:java");

		// 4.创建一个索引库地址的流
		FSDirectory directory = FSDirectory.open(new File("D:/log"));

		// 5.创建读取索引库对象(根据这个索引库地址流)[文档,索引]
		IndexReader indexReader = DirectoryReader.open(directory);

		// 6.创建一个搜索的对象IndexSearch(根据读取流对象)[文档,索引]
		IndexSearcher indexSearcher = new IndexSearcher(indexReader);

		// 7.根据条件对象执行查询,查询的是索引库中索引;返回结果集
		// 参数说明:1、查询的条件对象;2、查询返回的记录数
		TopDocs topDocs = indexSearcher.search(query, 2);
		System.out.println("查询的总记录数:"+topDocs.totalHits);
		
		// 8.根据结果集获取坐标:数组形式的
		ScoreDoc[] docs = topDocs.scoreDocs;
		// 9.遍历数组
		for (ScoreDoc scoreDoc : docs) {
			
			// 10.根据每一个坐标中获取文档ID
			int docId = scoreDoc.doc;
			
			// 11.在IndexSearch中根据文档ID查询文档数据
			Document doc = indexSearcher.doc(docId);
			
			// 12.打印文档数据
			System.err.println("=======id======" + doc.get("id"));
			System.err.println("=======name======" + doc.get("name"));
			System.err.println("=======price======" + doc.get("price"));
			System.err.println("=======pic======" + doc.get("pic"));
		}
		// 13.释放资源
		indexReader.close();
	}

}

猜你喜欢

转载自blog.csdn.net/qq_35537301/article/details/82589476
今日推荐