博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lucene的基本应用
阅读量:7020 次
发布时间:2019-06-28

本文共 4027 字,大约阅读时间需要 13 分钟。

import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.List;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;import org.apache.lucene.document.Field.Index;import org.apache.lucene.document.Field.Store;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriter.MaxFieldLength;import org.apache.lucene.queryParser.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.Directory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.Version;import org.junit.Test;import cn.itcast._domain.Article;public class HelloWorld {	private static Directory directory; // 索引库文件夹	private static Analyzer analyzer; // 分词器	static {		try {			directory = FSDirectory.open(new File("./indexDir"));			analyzer = new StandardAnalyzer(Version.LUCENE_30);		} catch (IOException e) {			throw new RuntimeException(e);		}	}	// 建立索引	@Test	public void testCreateIndex() throws Exception {		// 准备数据		Article article = new Article();		article.setId(1);		article.setTitle("准备Lucene的开发环境");		article.setContent("假设信息检索系统在用户发出了检索请求后再去互联网上找答案,根本无法在有限的时间内返回结果。");		// 放到索引库中		// 1, 把Article转为Document		Document doc = new Document();		String idStr = article.getId().toString();		doc.add(new Field("id", idStr, Store.YES, Index.NOT_ANALYZED));		doc.add(new Field("title", article.getTitle(), Store.YES, Index.ANALYZED));		doc.add(new Field("content", article.getContent(), Store.NO, Index.ANALYZED));		// 2, 把Document放到索引库中		IndexWriter indexWriter = new IndexWriter(directory, analyzer,  MaxFieldLength.UNLIMITED);		indexWriter.addDocument(doc);		indexWriter.close();	}	// 搜索	@Test	public void testSearch() throws Exception {		// 准备查询条件		String queryString = "lucene";		// String queryString = "hibernate";		// 运行搜索		List
list = new ArrayList
(); // ========================================================================================== // 1,把查询字符串转为Query对象(默认仅仅从title中查询) QueryParser queryParser = new QueryParser(Version.LUCENE_30, "title", analyzer); Query query = queryParser.parse(queryString); // 2,运行查询,得到中间结果 IndexSearcher indexSearcher = new IndexSearcher(directory); // 指定所用的索引库 TopDocs topDocs = indexSearcher.search(query, 100); // 最多返回前n条结果 int count = topDocs.totalHits; ScoreDoc[] scoreDocs = topDocs.scoreDocs; // 3,处理结果 for (int i = 0; i < scoreDocs.length; i++) { ScoreDoc scoreDoc = scoreDocs[i]; float score = scoreDoc.score; // 相关度得分 int docId = scoreDoc.doc; // Document的内部编号 // 依据编号拿到Document数据 Document doc = indexSearcher.doc(docId); // 把Document转为Article String idStr = doc.getField("id").toString(); //doc.get("id"); String title = doc.get("title"); String content = doc.get("content"); // 等价于 doc.getField("content").stringValue(); Article article = new Article(); article.setId(Integer.parseInt(idStr)); article.setTitle(title); article.setContent(content); list.add(article); } indexSearcher.close(); // ========================================================================================== // 显示结果 System.out.println("总结果数:" + list.size()); for (Article a : list) { System.out.println("------------------------------"); System.out.println("id = " + a.getId()); System.out.println("title = " + a.getTitle()); System.out.println("content = " + a.getContent()); } }}

public class Article {	private Integer id;	private String title;	private String content;	public Integer getId() {		return id;	}	public void setId(Integer id) {		this.id = id;	}	public String getTitle() {		return title;	}	public void setTitle(String title) {		this.title = title;	}	public String getContent() {		return content;	}	public void setContent(String content) {		this.content = content;	}}

转载地址:http://labxl.baihongyu.com/

你可能感兴趣的文章
[git] warning: LF will be replaced by CRLF
查看>>
部署VCAS 6.5时,卡在80%安装RPM的问题解决办法
查看>>
docker简洁用法
查看>>
Linux-其他命令--其它命令:mtools、man、unendcode、uudecode
查看>>
SCCM2012 安装主站点连接到管理中心报错“试图执行未经授权的操作”
查看>>
2012 新的征程开始
查看>>
我的友情链接
查看>>
使用Strongswan搭建IPSec/IKEv2 ***和window、android、ios、mac如何使用***
查看>>
DBVERIFY 工具的使用
查看>>
JQuery正则表达式
查看>>
keepalived+lvs
查看>>
给ZENCART加上类似wordpress弹性搜索框
查看>>
mysql 安装
查看>>
我的友情链接
查看>>
Xenserver HA功能配置文档
查看>>
centOS下memcached的安装
查看>>
和lock一起学beego 博客系统开发为例(七)
查看>>
嵌入式操作系统常识及分类
查看>>
在VSAN实验环境下如何将普通HDD标注成SSD
查看>>
生产场景不同角色linux服务器分区案例分享
查看>>