用lucene+庖丁解牛主要是要完成对数据库比如某张表单的某些字段的搜索,由于lucene本身对中文支持非常不好,像StandardAnalyzer基本都是一个字一个字匹配的,而用了庖丁解牛分词之后主要是它有一个dic字典,进行分词,效率很高。
首先我封装了一个lucene类,为了简单一点,对数据库操作直接用jdbc进行数据库操作
package lucene;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import net.paoding.analysis.analyzer.PaodingAnalyzer;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import com.model.searchResult;
public class lucene {
private String searchString;
private List<searchResult> list = new ArrayList<searchResult>();;
public List<searchResult> getList() {
return list;
}
public void setList(List<searchResult> list) {
this.list = list;
}
public lucene() {
}
public lucene(String searchString) {
this.setSearchString(searchString);
}
public void setSearchString(String searchString) {
this.searchString = searchString;
}
public String getSearchString() {
return searchString;
}
// 对于name属性进行全文搜索,返回hits值
public Hits seacher(String queryString) {
Hits hits = null;
try {
File indexFile = new File("d:/index/");
IndexReader reader = IndexReader.open(indexFile);
Analyzer analyzer = new PaodingAnalyzer();
QueryParser parser = new QueryParser("name", analyzer);
IndexSearcher searcher = new IndexSearcher(reader);
Query query = parser.parse(queryString);
hits = searcher.search(query);
} catch (Exception e) {
System.out.print(e);
}
return hits;
}
//创建索引
public void indexCreateUtil() {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/rent";
String user = "root";
String password = "111111";
//查询所有房屋信息放在index索引中
String query = "select a.house_id,a.house_name,b.house_type_name,house_rent_type_name,a.single_day_price from t_house a,t_house_type b,t_house_rent_type c where a.house_type_id=b.house_type_id and a.house_rent_type_id=c.house_rent_type_id";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if (!conn.isClosed())
System.out.println("数据库连接成功!");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
Analyzer analyzer = new PaodingAnalyzer();
try {
// 使用索引文件夹,庖丁解牛分词器创建IndexWriter
IndexWriter indexWriter = new IndexWriter("d:/index/",
analyzer, true);
while (rs.next()) {
Document doc = new Document();
doc.add(new Field("id", rs.getString("house_id"),
Field.Store.YES, Field.Index.TOKENIZED,
Field.TermVector.NO));
doc.add(new Field("name", rs.getString("house_name"),
Field.Store.YES, Field.Index.TOKENIZED,
Field.TermVector.NO));
doc.add(new Field("type_name", rs
.getString("house_type_name"), Field.Store.YES,
Field.Index.TOKENIZED, Field.TermVector.NO));
doc.add(new Field("rent_name", rs
.getString("house_rent_type_name"),
Field.Store.YES, Field.Index.TOKENIZED,
Field.TermVector.NO));
doc.add(new Field("price",
rs.getString("single_day_price"), Field.Store.YES,
Field.Index.TOKENIZED, Field.TermVector.NO));
indexWriter.addDocument(doc);
}
//优化
indexWriter.optimize();
indexWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("找不到驱动程序");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
//用索引搜索,并加入list
public List<searchResult> indexSearchUtil(String search) {
Hits hits = seacher(search);
for (int i = 0; i < hits.length(); i++) {
try {
Document doc = hits.doc(i);
int id = Integer.parseInt(doc.get("id"));
String name = doc.get("name");
String type_name = doc.get("type_name");
String rent_name = doc.get("rent_name");
int price = Integer.parseInt(doc.get("price"));
// System.out.println(id + "" + name + "" + type_name + ""
// + rent_name + "" + price);
// List<searchResult> list = new ArrayList<searchResult>();
searchResult sr = new searchResult();
sr.setHouse_id(id);
sr.setHouse_name(name);
sr.setHouse_rent_name(rent_name);
sr.setHouse_type_name(type_name);
sr.setSingle_day_price(price);
list.add(sr);
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
}
然后在luceneSearchAction.java中调用
package action;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import lucene.lucene;
public class luceneSearchAction implements action {
private String searchContent;
private List house_list;
public String getSearchContent() {
return searchContent;
}
public void setSearchContent(String searchContent) {
this.searchContent = searchContent;
}
public List getHouse_list() {
return house_list;
}
public void setHouse_list(List house_list) {
this.house_list = house_list;
}
public String execute() throws Exception {
lucene lucene = new lucene();
lucene.indexCreateUtil();
lucene.indexSearchUtil(searchContent);
ActionContext.getContext().getSession()
.put("house_list", lucene.getList());
return SUCCESS;
}
}
最后在页面上调用
<c:forEach items="${house_list}" var="orderItem">
${orderItem.house_id}
${orderItem.house_name}
...
...
</c:forEach>
分享到:
相关推荐
由于庖丁官方目前提供可下载尚不支持Lucene 3.0以上版本。因此作者对paoding进行重新编译,使其与最新Lucene 3.0.1版本适用。 Latest paoding 3.0.1 for lucene 3.0.1 使用说明: 先下载2.0.4的版本(h t t p : / ...
支持lucene3的庖丁解牛分词器和字典,可直接调用
庖丁解牛(很好的分词效率) 在做站内全文检索时创建索引时比较快,而且感觉效果比JE要好一些。。
SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...
支持中文的庖丁解牛,庖丁分词,找了好久才找到的希望对你有帮助。
可以适用于lucene3.5的庖丁解牛分词器jar包
Lucene加庖丁解牛测试类Lucene加庖丁解牛测试类
Lucene 庖丁解牛分词法 , 能够使用它解决中文分词问题。
SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...
实例是一个java实例,可直接导入到MyEclipse中...其中是lucene3.0整合了庖丁解牛分词法,添加了高亮显示。因为lucene3.0无法整合paoding-analysis.jar 所以我已经把paoding-analysis中的源码整合进来了避免无法整合问题
SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...
SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...
SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...
SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...
Paoding's Knives 中文分词具有极 高效率 和 高扩展性 。引入隐喻,采用完全的面向对象设计,构思先进。 高效率:在PIII 1G内存个人机器上,1秒 可准确分词 100万 汉字。 采用基于 不限制个数 的词典文件对文章...
NULL 博文链接:https://qpshenggui.iteye.com/blog/1157999
lucene3.5 + ik中文分词器例子,可从网页抓取输出流,并通过关键字查询相关文件 java ,亲自测试可以使用
全文检索MVC+Lucene+PanGu分词源码(含数据库)
compass2.1.4包+所用lucene包+中文分词器所用包
庖丁解牛 源码 for Lucene 2.4