package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.UUID; public class ConnectionUtil {
private static String driver="com.mysql.cj.jdbc.Driver";
private static String url="jdbc:mysql://localhost:3306/solrdb?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
private static String user="root";
private static String password="123456"; public static Connection getConn() {
try {
Class.forName(driver);
Connection conn=DriverManager.getConnection(url, user, password);
return conn;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
} public void CreateData() {
Connection conn=getConn();
try {
PreparedStatement ps=conn.prepareStatement
("insert into paper (id,name,title,author,content) values (?,?,?,?,?)");
for(int i=0;i<100;i++) {
ps.setString(1,UUID.randomUUID().toString());
ps.setString(2,"专家233"+i);
ps.setString(3,"杭州九次人大会议"+i);
ps.setString(4,"记者");
ps.setString(5, "北京时间2019-2-13日,杭州市举行了人大代表会议,会议决定现在...."+i);
ps.executeUpdate();
}
System.out.println("执行完毕");
} catch (SQLException e) { e.printStackTrace();
}
} public static void main(String[] args) {
ConnectionUtil util=new ConnectionUtil();
util.CreateData();
}
} //本类只为造数据
package jdbc;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
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.FieldType;
import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory; /**
* Lucene 创建索引
* @author moonxy
*
*/
public class CreateIndexForMysql { public static List<Paper> CreateData() {
Connection conn=ConnectionUtil.getConn();
List<Paper>list=new ArrayList<Paper>();
try {
PreparedStatement ps=conn.prepareStatement
("select * from paper ");
ResultSet rs=ps.executeQuery();
//4.处理数据库的返回结果(使用ResultSet类)
while(rs.next()){
Paper paper=new Paper(rs.getString("id"),
rs.getString("name"), rs.getString("title"),
rs.getString("author"),rs.getString("content"));
list.add(paper);
}
System.out.println("执行完毕");
return list;
} catch (SQLException e) { e.printStackTrace();
}
return null;
} public static void main(String[] args) { // 创建3个News对象 // 开始时间
Date start = new Date();
System.out.println("**********开始创建索引**********");
// 创建IK分词器
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig icw = new IndexWriterConfig(analyzer);
// CREATE 表示先清空索引再重新创建
icw.setOpenMode(OpenMode.CREATE);
Directory dir = null;
IndexWriter inWriter = null;
// 存储索引的目录
Path indexPath = Paths.get("indexdir"); try {
if (!Files.isReadable(indexPath)) {
System.out.println("索引目录 '" + indexPath.toAbsolutePath() + "' 不存在或者不可读,请检查");
System.exit(1);
}
dir = FSDirectory.open(indexPath);
inWriter = new IndexWriter(dir, icw); //自己设置字段索引类型
// 设置ID索引并存储
FieldType idType = new FieldType();
idType.setIndexOptions(IndexOptions.DOCS);
idType.setStored(true); // 设置标题索引文档、词项频率、位移信息和偏移量,存储并词条化
FieldType titleType = new FieldType();
titleType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
titleType.setStored(true);
titleType.setTokenized(true); FieldType contentType = new FieldType();
contentType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
contentType.setStored(true);
contentType.setTokenized(true);
contentType.setStoreTermVectors(true);
contentType.setStoreTermVectorPositions(true);
contentType.setStoreTermVectorOffsets(true);
List<Paper>list=CreateData(); for(int i=0;i<list.size();i++) {
Document doc = new Document();
doc.add(new Field("id", list.get(i).getId(), idType));
doc.add(new Field("title", list.get(i).getTitle(), titleType));
inWriter.addDocument(doc);
} //
// doc1.add(new Field("id", String.valueOf(news1.getId()), idType));
// doc1.add(new Field("title", news1.getTitle(), titleType));
// doc1.add(new Field("content", news1.getContent(), contentType));
// doc1.add(new IntPoint("reply", news1.getReply()));
// doc1.add(new StoredField("reply_display", news1.getReply()));
//
// Document doc2 = new Document();
// doc2.add(new Field("id", String.valueOf(news2.getId()), idType));
// doc2.add(new Field("title", news2.getTitle(), titleType));
// doc2.add(new Field("content", news2.getContent(), contentType));
// doc2.add(new IntPoint("reply", news2.getReply()));
// doc2.add(new StoredField("reply_display", news2.getReply()));
//
// Document doc3 = new Document();
// doc3.add(new Field("id", String.valueOf(news3.getId()), idType));
// doc3.add(new Field("title", news3.getTitle(), titleType));
// doc3.add(new Field("content", news3.getContent(), contentType));
// doc3.add(new IntPoint("reply", news3.getReply()));
// doc3.add(new StoredField("reply_display", news3.getReply())); // inWriter.addDocument(doc1);
// inWriter.addDocument(doc2);
// inWriter.addDocument(doc3); inWriter.commit(); inWriter.close();
dir.close(); } catch (IOException e) {
e.printStackTrace();
}
Date end = new Date();
System.out.println("索引文档用时:" + (end.getTime() - start.getTime()) + " milliseconds");
System.out.println("**********索引创建完成**********");
}
}

  //以上为构建索引代码

///查询代码

package query;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.queryparser.classic.QueryParser.Operator;
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; /**
* 单域搜索
* @author moonxy
*
*/
public class QueryParseTest { public static void main(String[] args) throws ParseException, IOException {
// 搜索单个字段
String field = "title";
// 搜索多个字段时使用数组
//String[] fields = { "title", "content" }; Path indexPath = Paths.get("indexdir");
Directory dir = FSDirectory.open(indexPath);
IndexReader reader = DirectoryReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer();//最细粒度分词
QueryParser parser = new QueryParser(field, analyzer); // 多域搜索
//MultiFieldQueryParser multiParser = new MultiFieldQueryParser(fields, analyzer); // 关键字同时成立使用 AND, 默认是 OR
parser.setDefaultOperator(Operator.AND);
// 查询语句
// Query query = parser.parse("农村学生");//查询关键词
Query query = parser.parse("次人");//查询关键词
System.out.println("Query:" + query.toString()); // 返回前10条
TopDocs tds = searcher.search(query, 100);
for (ScoreDoc sd : tds.scoreDocs) {
// Explanation explanation = searcher.explain(query, sd.doc);
// System.out.println("explain:" + explanation + "\n");
Document doc = searcher.doc(sd.doc);
// System.out.println("DocID:" + sd.doc);
// System.out.println("id:" + doc.get("id"));
System.out.println("title:" + doc.get("title"));
// System.out.println("content:" + doc.get("content"));
// System.out.println("文档评分:" + sd.score);
}
dir.close();
reader.close();
}
}

  

 

 注意我用的是MySQL   8.0   其他版本的自行更改对应的driverClass url

lucene 结合数据库做搜索的更多相关文章

  1. Lucene.net站内搜索—6、站内搜索第二版

    目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...

  2. Lucene.net站内搜索—4、搜索引擎第一版技术储备(简单介绍Log4Net、生产者消费者模式)

    目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...

  3. Lucene.net站内搜索—3、最简单搜索引擎代码

    目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...

  4. Lucene.net站内搜索—2、Lucene.Net简介和分词

    目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...

  5. Lucene.net站内搜索—1、SEO优化

    目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...

  6. 使用Lucene.net提升网站搜索速度整合记录

    1.随着网站数据量达到500万条的时候,发现SQL数据库如果使用LIKE语句来查询,总是占用CPU很忙,不管怎么优化,速度还是上不来; 2.经过网上收集资料,HUBBLE.net目前虽然做得不错,但需 ...

  7. Lucene5.5.4入门以及基于Lucene实现博客搜索功能

    前言 一直以来个人博客的搜索功能很蹩脚,只是自己简单用数据库的like %keyword%来实现的,所以导致经常搜不到想要找的内容,而且高亮显示.摘要截取等也不好实现,所以决定采用Lucene改写博客 ...

  8. Lucene.net站内搜索—5、搜索引擎第一版实现

    目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...

  9. Lucene.Net 站内搜索

    Lucene.Net 站内搜索 一  全文检索: like查询是全表扫描(为性能杀手)Lucene.Net搜索引擎,开源,而sql搜索引擎是收费的Lucene.Net只是一个全文检索开发包(只是帮我们 ...

随机推荐

  1. GitLab CI/CD 进行持续集成

    简介 从 GitLab 8.0 开始,GitLab CI 就已经集成在 GitLab 中,我们只要在项目中添加一个 .gitlab-ci.yml 文件,然后添加一个 Runner,即可进行持续集成. ...

  2. 20175226 2018-2019-2《java程序设计》结对编程-四则运算(第一周-阶段总结)

    结对编程-四则运算(第一周-阶段总结) 需求分析 实现一个四则运算程序,要求: 自动随机生成小学四则运算题目(加,减,乘,除) 支持整数.真分数且支持多项式 能够利用栈的思想,将中缀转换为后缀表达式 ...

  3. BeanUtils.copyProperties缓解代码压力,释放双手

    简单描述:之前在写代码的时候,经常把表单提交到后台的对象的参数,通过getter方法取出来,然后,再通过setter方法传递给需要的对象,代码中写了很多get set这种方法,后来听同事说,sprin ...

  4. 微信小程序rich-text 文本首行缩进和图片居中

    微信小程序开发使用rich-text组件渲染html格式的代码,常常因为不能自定义css导致文本不能缩进,以及图片不能居中等问题,这里可以考虑使用js的replace方法,替换字符串,然后在渲染的同时 ...

  5. TLS1.3&TLS1.2形式化分析

    本博客是对下面博客连接上的原文进行梳理+自己在其他方面资料做个整理 https://blog.csdn.net/andylau00j/article/details/79269499 https:// ...

  6. UNIX哲理名言(中英文对照)

    UNIX 的特点: Everything (including hardware) is a file.所有的事物(甚至硬件本身)都是一个的文件. Configuration data stored ...

  7. Celery初识及简单实例

    Celery是一个“自带电池”的任务队列.易于使用,可以轻易入门,它遵照最佳实践设计,使产品可以扩展,或与其他语言集成,并且它自带了在生产环境中运行这样一个系统所需的工具和支持.本文介绍基础部分: 选 ...

  8. 自己动手造拖拉机轮子系列 -(react-loadable)

    最新消息:react官方已支持懒加载https://reactjs.org/docs/code-splitting.html#reactlazy 文章webpack分片chunk加载原理中深入探究了异 ...

  9. linux系统优化配置

    # 2 millions system-wide sysctl -w fs.file-max=2097152 sysctl -w fs.nr_open=2097152 echo 2097152 > ...

  10. 构建npm包出现"找不到node_modules"的问题

    目录结构 解决方案 先在微信开发者工具->详细->使用npm 1.cd到\WeChatProject\miniprogram文件夹 2.npm init 3.npm install 4.n ...