热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

lucene为数据库数据创建索引并查询索引库

1.1packagecom.home.utils;23importjava.io.IOException;4importjava.sql.Connecti

1.

  1 package com.home.utils;
  2 
  3 import java.io.IOException;
  4 import java.sql.Connection;
  5 import java.sql.DriverManager;
  6 import java.sql.ResultSet;
  7 import java.sql.SQLException;
  8 import java.sql.Statement;
  9 import java.util.ArrayList;
 10 import java.util.List;
 11 
 12 import org.apache.lucene.analysis.Analyzer;
 13 import org.apache.lucene.analysis.standard.StandardAnalyzer;
 14 import org.apache.lucene.document.Document;
 15 import org.apache.lucene.document.Field;
 16 import org.apache.lucene.document.Field.Store;
 17 import org.apache.lucene.index.IndexWriter;
 18 import org.apache.lucene.index.IndexWriter.MaxFieldLength;
 19 import org.apache.lucene.queryParser.QueryParser;
 20 import org.apache.lucene.search.IndexSearcher;
 21 import org.apache.lucene.search.Query;
 22 import org.apache.lucene.search.ScoreDoc;
 23 import org.apache.lucene.search.TopDocs;
 24 import org.apache.lucene.util.Version;
 25 
 26 public class TestLucene {
 27 
 28     static {
 29         try {
 30             // 加载MySql的驱动类
 31             Class.forName("com.mysql.jdbc.Driver");
 32         } catch (ClassNotFoundException e) {
 33             System.out.println("找不到驱动程序类 ,加载驱动失败!");
 34             e.printStackTrace();
 35         }
 36     }
 37 
 38     public static Connection createConnection() {
 39 
 40         String url = "jdbc:mysql://localhost:3306/cool_files";
 41         String username = "root";
 42         String password = "123456";
 43         Connection con = null;
 44         try {
 45             con = DriverManager.getConnection(url, username, password);
 46         } catch (SQLException se) {
 47             System.out.println("数据库连接失败!");
 48             se.printStackTrace();
 49         }
 50 
 51         return con;
 52     }
 53 
 54     public static ResultSet getResult(String sql, Connection conn) {
 55         try {
 56             Statement stmt = conn.createStatement();
 57             ResultSet rs = stmt.executeQuery(sql);
 58             return rs;
 59         } catch (SQLException e) {
 60             System.out.println(e);
 61         }
 62         return null;
 63     }
 64 
 65     public void close(ResultSet rs, Statement stmt, Connection conn) {
 66 
 67         if (rs != null) { // 关闭记录集
 68             try {
 69                 rs.close();
 70             } catch (SQLException e) {
 71                 e.printStackTrace();
 72             }
 73         }
 74         if (stmt != null) { // 关闭声明
 75             try {
 76                 stmt.close();
 77             } catch (SQLException e) {
 78                 e.printStackTrace();
 79             }
 80         }
 81         if (conn != null) { // 关闭连接对象
 82             try {
 83                 conn.close();
 84             } catch (SQLException e) {
 85                 e.printStackTrace();
 86             }
 87         }
 88     }
 89 
 90     public static void Index(ResultSet rs) {
 91         try {
 92             IndexWriter indexWriter = new IndexWriter(LuceneUtils.directory,LuceneUtils.analyzer,MaxFieldLength.LIMITED);
 93             while (rs.next()) {
 94                 Document doc = new Document();
 95                 doc.add(new Field("Au_id", rs.getString(1), Store.YES,
 96                         org.apache.lucene.document.Field.Index.ANALYZED));
 97                 doc.add(new Field("Au_name", rs.getString(2), Store.YES,
 98                         org.apache.lucene.document.Field.Index.ANALYZED));
 99                 doc.add(new Field("Phone", rs.getString(3), Store.YES,
100                         org.apache.lucene.document.Field.Index.ANALYZED));
101                 doc.add(new Field("Address", rs.getString(4), Store.YES,
102                         org.apache.lucene.document.Field.Index.ANALYZED));
103                 doc.add(new Field("City", rs.getString(5), Store.YES,
104                         org.apache.lucene.document.Field.Index.ANALYZED));
105                 doc.add(new Field("State", rs.getString(6), Store.YES,
106                         org.apache.lucene.document.Field.Index.ANALYZED));
107                 doc.add(new Field("Zip", rs.getString(7), Store.YES,
108                         org.apache.lucene.document.Field.Index.ANALYZED));
109                 doc.add(new Field("contract", rs.getString(8), Store.YES,
110                         org.apache.lucene.document.Field.Index.ANALYZED));
111                 indexWriter.addDocument(doc);
112             }
113             indexWriter.optimize();
114             indexWriter.close();
115         } catch (IOException e) {
116             System.out.println(e);
117         } catch (SQLException e) {
118             System.out.println(e);
119         }
120     }
121 
122     public static Analyzer getAnalyzer() {
123         return new StandardAnalyzer(Version.LUCENE_30);
124     }
125 
126     public static List seacher(String queryString) {
127         List authorsList = null;
128         try {
129             IndexSearcher is = new IndexSearcher(LuceneUtils.directory);
130             QueryParser parser = new QueryParser(Version.LUCENE_30, "Au_name",
131                     LuceneUtils.analyzer);
132             Query query = parser.parse(queryString);
133             TopDocs docs = is.search(query, 10);
134             ScoreDoc[] scoreDocs = docs.scoreDocs;
135             authorsList = new ArrayList();
136 
137             for (ScoreDoc scoreDoc : scoreDocs) {
138                 int num = scoreDoc.doc;
139                 Document document = is.doc(num);
140                 Authors article = DocumentUtils.document2Authors(document);
141                 authorsList.add(article);
142             }
143 
144         } catch (Exception e) {
145             System.out.print(e);
146         }
147         return authorsList;
148     }
149 }

 

 

2.

 1 package com.home.controller;
 2 
 3 import java.sql.Connection;
 4 import java.sql.ResultSet;
 5 import java.util.List;
 6 
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 import org.apache.lucene.analysis.Analyzer;
11 import org.springframework.stereotype.Controller;
12 import org.springframework.web.bind.annotation.RequestMapping;
13 import org.springframework.web.bind.annotation.RequestParam;
14 import org.springframework.web.bind.annotation.ResponseBody;
15 
16 import com.home.utils.Authors;
17 import com.home.utils.TestLucene;
18 
19 @Controller
20 @RequestMapping("/lucene")
21 public class LuceneController {
22     
23     @ResponseBody
24     @RequestMapping("/search")
25     public List getAuthors(HttpServletRequest request,HttpServletResponse response,@RequestParam String name){
26         
27         Connection cOnn= TestLucene.createConnection();
28         Analyzer analyzer = TestLucene.getAnalyzer();
29         System.out.println(name);
30         ResultSet rs = TestLucene.getResult("select * from Authors where 1=1 and Au_name='"+name+"'", conn);
31         TestLucene.Index(rs);
32         List authorsList = TestLucene.seacher(name);
33         
34         return authorsList;
35     }
36     
37     @ResponseBody
38     @RequestMapping("/searchOne")
39     public List getAuthorsOne(HttpServletRequest request,HttpServletResponse response,@RequestParam String name){
40         
41         List authorsList = TestLucene.seacher(name);
42         
43         return authorsList;
44     }
45 }

 

3、

 1 
 2 <%@ page language="java" cOntentType="text/html; charset=UTF-8"
 3     pageEncoding="UTF-8"%>
 4 
 5 <%
 6     String path = request.getContextPath();
 7     String basePath = request.getScheme() + "://"
 8             + request.getServerName() + ":" + request.getServerPort()
 9             + path + "/";
10 %>
11 
12 
13 
14     
15              16                 value="搜索" />
17     
18 
19 

 


推荐阅读
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • SpringBoot uri统一权限管理的实现方法及步骤详解
    本文详细介绍了SpringBoot中实现uri统一权限管理的方法,包括表结构定义、自动统计URI并自动删除脏数据、程序启动加载等步骤。通过该方法可以提高系统的安全性,实现对系统任意接口的权限拦截验证。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
author-avatar
萌萌蚂蚁
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有