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 ...
随机推荐
- HashMap、ConcurrentHashMap以及HashTable(面试向)
---->HashMap 在java1.7中,hashmap的数据结构是基于数组+链表的结构,即我们比较熟悉的Entry数组,其包含的(key-value)键值对的形式.在多线程环境下,Hash ...
- 2017 计蒜之道 初赛 第一场 A 阿里的新游戏
题链:https://nanti.jisuanke.com/t/15499 这题观察图纸可知成三线段上的相邻点之间的距离有1,2,3三种情况的,同时要成线段必然是同横坐标或者纵坐标,然后我们排除掉穿过 ...
- 全文搜索(A)-相关性
文章:搜索相关性 文章:推荐系统中相似度算法介绍及效果测试 文章:常用的相似度计算方法原理及实现 文章:推荐系统用户相似度计算方法研究
- HASH的应用(负数下标用偏移量解决)
Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个处于区间[-500000,500000]的整数. Output 对每组测试数据按从大到小的 ...
- BBS+Blog项目流程及补充知识点
项目流程: 1. 产品需求 (1)基于用户认证组件和Ajax实现登陆验证(图片验证码) (2)基于forms组件和Ajax实现注册功能 (3)设计系统首页(文章列表渲染) (4)设计个人站点页面 (5 ...
- 【BZOJ4591】超能粒子炮·改(Lucas定理,组合计数)
题意: 曾经发明了脑洞治疗仪&超能粒子炮的发明家SHTSC又公开了他的新发明:超能粒子炮·改--一种可以发射威力更加 强大的粒子流的神秘装置.超能粒子炮·改相比超能粒子炮,在威力上有了本质的提 ...
- 「CodePlus 2017 11 月赛」大吉大利,晚上吃鸡!
n<=50000,m<=50000的图,给s和t,问有多少点对$(a,b)$满足 嗯. 不会. 首先最短路DAG造出来,然后两个条件转述一下:条件一,$N_a$表示从s到t经过a的路径,$ ...
- .net 程序集保护与破解
一.保护方法(强签名.混淆.加壳) 强名称是由程序集的标识加上公钥和数字签名组成的.其中,程序集的标识包括简单文本名称.版本号.区域性信息(如果提供的话).语言文化信息.处理器架构信息.强名称是使用相 ...
- Ubuntu 16.04中XMind 8导致Java内存溢出的问题解决(硬盘卡死,桌面卡死)
XMind使用的是Java进行开发,如果出现内存溢出的问题,那么一定是桌面快捷方式的问题,解决方法是直接修改快捷方式里面的内容,修改如下: [Desktop Entry] Encoding=UTF-8 ...
- Chains (链 )
Indy 中的工作队列系统,调度器,和链引擎都被叫做链. 当使用链的时候,一个基于链的 IOHandler 存储工作项目到有关的工作队列中.在一个工作单元被完成以前,执行这个工作单元的纤程是无法做其它 ...