博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
lucene简单入门demo
阅读量:6533 次
发布时间:2019-06-24

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

hot3.png

package com.pdsu.lucene;/** * 类说明: *  * @author 作者: LiuJunGuang * @version 创建时间:2011-12-29 下午04:40:39 */public class Article {	private int id;	private String title;	private String content;	public int getId() {		return id;	}	public void setId(int 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;	}	public Article(int id, String title, String content) {		super();		this.id = id;		this.title = title;		this.content = content;	}	public Article() {		super();	}	@Override	public String toString() {		return "Article [id=" + id + ", title=" + title + ", content="				+ content + "]";	}}

lucene简单应用

package com.pdsu.lucene;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;/** * 类说明: *  * @author 作者: LiuJunGuang * @version 创建时间:2011-12-29 下午04:42:21 */public class Lucene {	@Test	public void insert() throws IOException {		Article article = new Article(1, "lucene is very good!",				"lucene very good!,lucene very good!");		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);// 分词器		Directory dir = FSDirectory.open(new File("./store")); // 数据存储目录		IndexWriter index = new IndexWriter(dir, analyzer,				MaxFieldLength.LIMITED);		Document document = new Document();// 文档对象		Field idField = new Field("id", String.valueOf(article.getId()), Store.YES, Index.NOT_ANALYZED);		Field titleField = new Field("title", article.getTitle(), Store.YES, Index.ANALYZED);		Field contentField = new Field("content", article.getContent(), Store.YES, Index.NOT_ANALYZED);		document.add(idField);		document.add(titleField);		document.add(contentField);		index.addDocument(document);// 添加文档对象		index.close();	}		@Test	public  void query() throws Exception{		Directory dir = FSDirectory.open(new File("./store")); // 数据存储目录		IndexSearcher indexSearcher = new IndexSearcher(dir);				Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);// 分词器		QueryParser qp = new QueryParser(Version.LUCENE_30, "title", analyzer);		Query query = qp.parse("lucene");		TopDocs tops = indexSearcher.search(query, 10);		//获得记录总条数		int count = tops.totalHits;				ScoreDoc []scoreDocs =  	tops.scoreDocs;//得到目录		List
articles = new ArrayList
(); for (int i = 0; i < scoreDocs.length; i++) { float score = scoreDocs[i].score;//相关度得分 int doc = scoreDocs[i].doc; Document document = indexSearcher.doc(doc); Article article = new Article(Integer.parseInt(document.get("id")), document.get("title"),document.get("content")); articles.add(article); } System.out.println(articles); }}

转载于:https://my.oschina.net/java2010/blog/356454

你可能感兴趣的文章
Comet:基于 HTTP 长连接的“服务器推”技术
查看>>
BZOJ 2733: [HNOI2012]永无乡 启发式合并treap
查看>>
四种方法校验数组中是否包含某个指定的字符串
查看>>
29、Java并发性和多线程-非阻塞算法
查看>>
安装OpenResty开发环境
查看>>
第0课 从0开始
查看>>
python class和class(object)用法区别
查看>>
hadoop无法启动DataNode问题
查看>>
java泛型中<?>和<T>区别
查看>>
这里是指推送通知跟NSNotification有区别:
查看>>
Linux中断(interrupt)子系统之一:中断系统基本原理【转】
查看>>
用户ID的代码生成
查看>>
win7经常出现“关闭xxxx前您必须关闭所有会话框”
查看>>
SNMP安全配置的两种方法(也可同一时候兼顾配置两种方法)
查看>>
react-native 常见操作 及 git 补充
查看>>
MongoDB 自己定义函数
查看>>
Summary Day30
查看>>
逆向输出回环数组
查看>>
自己动手,实现“你的名字”滤镜
查看>>
想要快速搭建开发测试环境?这么做就可以!
查看>>