luence全文检索(数据库检索)
注解:从数据库中查询所有数据然后放入luence中,然后在luence来检索
package com.zhu.demo; import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; 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.TextField;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.queryparser.classic.ParseException;
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.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
/**
*
* Lucene与数据库结合使用
*
* @author YipFun
*/
public class LuceneDemo06 {
private static final String driverClassName="com.mysql.jdbc.Driver";
private static final String url="??characterEncoding=utf-8";
private static final String username="?";
private static final String password="?"; private static final Version version = Version.LUCENE_4_9;
private Directory directory = null;
private DirectoryReader ireader = null;
private IndexWriter iwriter = null;
//private IKAnalyzer analyzer;
private Analyzer analyzer;
private Connection conn; public LuceneDemo06() {
directory = new RAMDirectory();
}
public IndexSearcher getSearcher(){
try {
if(ireader==null) {
ireader = DirectoryReader.open(directory);
} else {
DirectoryReader tr = DirectoryReader.openIfChanged(ireader) ;
if(tr!=null) {
ireader.close();
ireader = tr;
}
}
return new IndexSearcher(ireader);
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
} public Connection getConnection(){
if(this.conn == null){
try {
Class.forName(driverClassName);
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
return conn;
}
private Analyzer getAnalyzer(){
if(analyzer == null){
return new StandardAnalyzer();
}else{
return analyzer;
}
} public void createIndex(){
Connection conn = getConnection();
ResultSet rs = null;
PreparedStatement pstmt = null;
if(conn == null){
System.out.println("get the connection error...");
return ;
}
String sql = "select * from es_goods";
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
IndexWriterConfig iwConfig=new IndexWriterConfig(getAnalyzer());
iwConfig.setOpenMode(OpenMode.CREATE);
iwriter = new IndexWriter(directory,iwConfig);
while(rs.next()){
Integer goods_id = rs.getInt(1);
String name = rs.getString(2);
String sn= rs.getString(3);
int band_id=rs.getInt(4);
int cat_id = rs.getInt(5);
int type_id= rs.getInt(6);
String goods_type = rs.getString(7);
String unit = rs.getString(8);
Double weight = rs.getDouble(9);
Integer market_enable = rs.getInt(10);
String brief = rs.getString(11);
Long intro = rs.getLong(12);
Double price = rs.getDouble(13);
Double cost = rs.getDouble(14);
Double mktprice = rs.getDouble(15);
String params = rs.getString(16);
String specs = rs.getString(17);
Integer have_spec = rs.getInt(18);
Long adjuncts = rs.getLong(19);
Integer create_time = rs.getInt(20);
Integer last_modify = rs.getInt(21);
Integer view_count = rs.getInt(22);
Integer buy_count = rs.getInt(23);
Integer disabled = rs.getInt(24);
Integer store = rs.getInt(25);
Integer enable_store = rs.getInt(26);
Integer point = rs.getInt(27);
String page_title = rs.getString(28);
String meta_keywords = rs.getString(29);
String meta_description = rs.getString(30);
String p20 = rs.getString(31);
String p19 = rs.getString(32);
String p18= rs.getString(33);
String p17= rs.getString(34);
String p16= rs.getString(35);
String p15= rs.getString(36);
String p14= rs.getString(37);
String p13= rs.getString(38);
String p12 = rs.getString(39);
String p11= rs.getString(40);
String p10= rs.getString(41);
String p9= rs.getString(42);
String p8= rs.getString(43);
String p7= rs.getString(44);
String p6= rs.getString(45);
String p5 = rs.getString(46);
String p4= rs.getString(47);
Integer p3= rs.getInt(48);
String p2= rs.getString(49);
String p1 = rs.getString(50);
Integer sord= rs.getInt(51);
int have_field= rs.getInt(52);
int grade = rs.getInt(53);
Long goods_comment = rs.getLong(54);
Integer is_pack= rs.getInt(55);
String thumbnail= rs.getString(56);
String big= rs.getString(57);
String small= rs.getString(58);
String original= rs.getString(59);
String p21= rs.getString(60);
String p22= rs.getString(61);
String p23= rs.getString(62); Document doc = new Document(); doc.add(new TextField("goods_id", goods_id+"",Field.Store.YES));//
doc.add(new TextField("name", name+"",Field.Store.YES));//
doc.add(new TextField("sn", sn+"",Field.Store.YES));//
doc.add(new TextField("band_id", band_id+"",Field.Store.YES));//
doc.add(new TextField("cat_id", cat_id+"",Field.Store.YES));//
doc.add(new TextField("type_id", type_id+"",Field.Store.YES));//
doc.add(new TextField("goods_type", goods_type+"",Field.Store.YES));//
doc.add(new TextField("unit", unit+"",Field.Store.YES));//
doc.add(new TextField("weight", weight+"",Field.Store.YES));//
doc.add(new TextField("market_enable", market_enable+"",Field.Store.YES));//
doc.add(new TextField("brief", brief+"",Field.Store.YES));//
doc.add(new TextField("intro", intro+"",Field.Store.YES));//
doc.add(new TextField("price", price+"",Field.Store.YES));//
doc.add(new TextField("cost", cost+"",Field.Store.YES));//
doc.add(new TextField("mktprice", mktprice+"",Field.Store.YES));//
doc.add(new TextField("params", params+"",Field.Store.YES));//
doc.add(new TextField("specs", specs+"",Field.Store.YES));//
doc.add(new TextField("have_spec", have_spec+"",Field.Store.YES));//
doc.add(new TextField("adjuncts", adjuncts+"",Field.Store.YES));//
doc.add(new TextField("create_time", create_time+"",Field.Store.YES));//
doc.add(new TextField("last_modify", last_modify+"",Field.Store.YES));//
doc.add(new TextField("view_count", view_count+"",Field.Store.YES));//
doc.add(new TextField("buy_count", buy_count+"",Field.Store.YES));//
doc.add(new TextField("disabled", disabled+"",Field.Store.YES));//
doc.add(new TextField("store", store+"",Field.Store.YES));//
doc.add(new TextField("enable_store", enable_store+"",Field.Store.YES));//
doc.add(new TextField("point", point+"",Field.Store.YES));//
doc.add(new TextField("page_title", page_title+"",Field.Store.YES));//
doc.add(new TextField("meta_keywords", meta_keywords+"",Field.Store.YES));//
doc.add(new TextField("meta_description", meta_description+"",Field.Store.YES));//
doc.add(new TextField("p20", p20+"",Field.Store.YES));//
doc.add(new TextField("p19", p19+"",Field.Store.YES));//
doc.add(new TextField("p18", p18+"",Field.Store.YES));//
doc.add(new TextField("p17", p17+"",Field.Store.YES));// doc.add(new TextField("p16", p16+"",Field.Store.YES));//
doc.add(new TextField("p15", p15+"",Field.Store.YES));//
doc.add(new TextField("p14", p14+"",Field.Store.YES));//
doc.add(new TextField("p13", p13+"",Field.Store.YES));//
doc.add(new TextField("p12", p12+"",Field.Store.YES));//
doc.add(new TextField("p11", p11+"",Field.Store.YES));//
doc.add(new TextField("p10", p10+"",Field.Store.YES));//
doc.add(new TextField("p9", p9+"",Field.Store.YES));//
doc.add(new TextField("p8", p8+"",Field.Store.YES));//
doc.add(new TextField("p7", p7+"",Field.Store.YES));//
doc.add(new TextField("p6", p6+"",Field.Store.YES));//
doc.add(new TextField("p5", p5+"",Field.Store.YES));//
doc.add(new TextField("p4", p4+"",Field.Store.YES));//
doc.add(new TextField("p3", p3+"",Field.Store.YES));//
doc.add(new TextField("p2", p2+"",Field.Store.YES));//
doc.add(new TextField("p1", p1+"",Field.Store.YES));//
doc.add(new TextField("sord", sord+"",Field.Store.YES));//
doc.add(new TextField("have_field", have_field+"",Field.Store.YES));//
doc.add(new TextField("grade", grade+"",Field.Store.YES));//
doc.add(new TextField("goods_comment", goods_comment+"",Field.Store.YES));//
doc.add(new TextField("is_pack", is_pack+"",Field.Store.YES));//
doc.add(new TextField("thumbnail", thumbnail+"",Field.Store.YES));//
doc.add(new TextField("big", big+"",Field.Store.YES));//
doc.add(new TextField("small", small+"",Field.Store.YES));//
doc.add(new TextField("original", original+"",Field.Store.YES));//
doc.add(new TextField("p21", p21+"",Field.Store.YES));//
doc.add(new TextField("p22", p22+"",Field.Store.YES));//
doc.add(new TextField("p23", p23+"",Field.Store.YES));//64
iwriter.addDocument(doc);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(iwriter != null)
iwriter.close();
rs.close();
pstmt.close();
if(!conn.isClosed()){
conn.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} public void searchByTerm(String field,String keyword,int num) throws Exception{
IndexSearcher isearcher = getSearcher();
Analyzer analyzer = getAnalyzer();
//使用QueryParser查询分析器构造Query对象
QueryParser qp = new QueryParser(field,analyzer);
//这句所起效果?
// qp.setDefaultOperator(QueryParser.OR_OPERATOR);
qp.setDefaultOperator(QueryParser.AND_OPERATOR);
try {
Query query = qp.parse(keyword);
ScoreDoc[] hits; //注意searcher的几个方法
hits = isearcher.search(query, null, num).scoreDocs;
// assertEquals(1, hits.length);
System.out.println("the names is =");
for (int i = 0; i < hits.length; i++) {
Document doc = isearcher.doc(hits[i].doc);
System.out.print(doc.get("goods_id")+" ");
System.out.println(doc.get("name")+" ");
}
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
} public static void main(String[] args) throws Exception {
LuceneDemo06 ld = new LuceneDemo06();
ld.createIndex();
ld.searchByTerm("name", "平安", 100);
}
}
luence全文检索(数据库检索)的更多相关文章
- Sybase:数据库检索的日期格式
Sybase:数据库检索的日期格式 示例代码: --1,字符转日期 ' as date ),'yyyy/mm/dd'); ---结果:2018/03/09 --2,一年内第几天 ' as date ) ...
- luence全文检索(简介)
刚开始做全文检索也是找了很多资料但是网上的都不是很齐全luence是个很不多的工具 Lucene4.0的官网文档:http://lucene.apache.org/core/4_0_0/core/ov ...
- 【转载】HBase 数据库检索性能优化策略
转自:http://www.ibm.com/developerworks/cn/java/j-lo-HBase/index.html 高性能 HBase 数据库 本文首先介绍了 HBase 数据库基本 ...
- HBase 数据库检索性能优化策略--转
https://www.ibm.com/developerworks/cn/java/j-lo-HBase/index.html HBase 数据表介绍 HBase 数据库是一个基于分布式的.面向列的 ...
- BUPT复试专题—数据库检索(2014软院)
题目描述 在数据库的操作过程中,我们进场会遇到检索操作.这个题目的任务是完成一些特定格式的检索,并输出符合条件的数据库中的所有结果. 我们现在有一个数据库,维护了学生的姓名(Name),性别(Sex) ...
- HBase 数据库检索性能优化策略
HBase 数据表介绍 HBase 数据库是一个基于分布式的.面向列的.主要用于非结构化数据存储用途的开源数据库.其设计思路来源于 Google 的非开源数据库"BigTable" ...
- 下载STRING数据库检索互作关系结果为空,但是在STRING网站却能检索出互作关系,为什么呢???关键词用的是蛋白ID(ENSP开头)
首先介绍下两种方法: 一.本地分析 1.在STRING数据库下载人的互作文件,如下图,第一个文件 https://string-db.org/cgi/download.pl?sessionId=HGr ...
- mysql,实现数据库检索结果添加自增的序号
select t2.rowno from( select (@rownum:=@rownum+1) as rowno, t1.id from news t1 ,(select (@rownum ...
- Lucene.net 全文检索数据库
#define Search using Lucene.Net.Analysis; using Lucene.Net.Analysis.Tokenattributes; using Lucene.Ne ...
随机推荐
- STM32F407 新建基于固件库的项目模板
1.新建文件夹如图: 2.新建项目在USER文件夹中,选cpu如图: 若再弹出窗口, 直接点cancel 3.删了这俩文件夹: 4.复制文件到fwlib: src 存放的是固件库的.c 文件, inc ...
- 添物不花钱学javaEE--CSS
CSS是什么? CSS: Cascading Style Sheets CSS官方网址: https://www.w3.org/TR/2011/REC-CSS2-20110607/ 其实仔细研究这个就 ...
- 2015 湘潭大学程序设计比赛(Internet)部分题解,其中有一个题与NYOJ1057很像,贪心过~~
仙剑奇侠传 祝玩的开心 ...
- [1143] [CTSC2008]祭祀river(最大独立集 || 偏序集最大反链)
传送门 网上说这是偏序集最大反链,然而我实在不理解. 所以我换了一个思路,先用floyd,根据点的连通性连边, 问题就转换成了找出最多的点,使任意两个点之间不连边,也就是最大独立集. ——代码 #in ...
- 关于PHP include文件时的文件查找顺序
常常被include文件的路径搞晕. 看来是要理一理的时候了. PHP官方文档关于include搜索路径的解释是:先查找工作目录下相对于include_path设置所对应的路径,然后再搜索执行文件所在 ...
- 洛谷P1615 西游记公司
题目背景 一道极其无厘头的题目 题目描述 事情是这样的:西游记中的孙沙猪(孙杀猪)三徒弟在西天取经之后开始进入厦门大学经贸系学习经济,在1个小时的学习后,他们用暴力手段毕业了.然后,他们创办了三个公司 ...
- 【IntelliJ 】IntelliJ IDEA 15 创建maven项目
说明 创建Maven项目的方式:手工创建 好处:参考IntelliJ IDEA 14 创建maven项目二(此文章描述了用此方式创建Maven项目的好处)及idea14使用maven创建web工程(此 ...
- CentOS虚拟机与本机同步时间
接着之前的任务,还是为了在VMWare上搭建分布式hadoop集群.搭着搭着注意到虚拟机上的时间和本机是不同步的,而且可以说是乱七八糟,3台虚拟机时间都与本机差了8个小时以上.首先确认不是时区的问题, ...
- HDU 5876 补图 单源 最短路
---恢复内容开始--- Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (J ...
- Servlet表单数据处理
以下内容引用自http://wiki.jikexueyuan.com/project/servlet/form-data.html: 当需要从浏览器到Web服务器传递一些信息并最终传回到后台程序时,一 ...