lucene的简单的项目实现ik分词和高亮显示

本项目使用springboot实现简单的ik分词和高亮显示

一.配置pom文件

[html]  view plain  copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.et</groupId>  
  5.     <artifactId>lucene_myproject</artifactId>  
  6.     <version>0.0.1-SNAPSHOT</version>  
  7.     <!-- springboot所继承的jar包 -->  
  8.     <parent>  
  9.         <groupId>org.springframework.boot</groupId>  
  10.         <artifactId>spring-boot-starter-parent</artifactId>  
  11.         <version>1.5.9.RELEASE</version>  
  12.     </parent>  
  13.     <dependencies>  
  14.         <dependency>  
  15.             <groupId>org.springframework.boot</groupId>  
  16.             <artifactId>spring-boot-starter-web</artifactId>  
  17.         </dependency>  
  18.         <!-- springboot连接数据库依赖的jar包 -->  
  19.         <dependency>  
  20.             <groupId>org.springframework.boot</groupId>  
  21.             <artifactId>spring-boot-starter-data-jpa</artifactId>  
  22.         </dependency>  
  23.         <!-- mysql驱动包 -->  
  24.         <dependency>  
  25.             <groupId>mysql</groupId>  
  26.             <artifactId>mysql-connector-java</artifactId>  
  27.             <version>5.1.44</version>  
  28.         </dependency>  
  29.         <!--ik分词器  -->  
  30.         <dependency>  
  31.             <groupId>com.janeluo</groupId>  
  32.             <artifactId>ikanalyzer</artifactId>  
  33.             <version>2012_u6</version>  
  34.         </dependency>  
  35.         <!-- ik高亮显示所需到jar包 -->  
  36.         <dependency>  
  37.             <groupId>org.apache.lucene</groupId>  
  38.             <artifactId>lucene-highlighter</artifactId>  
  39.             <version>4.7.2</version>  
  40.         </dependency>  
  41.     </dependencies>  
  42. </project>  

二.编辑application.properties文件链接数据库

[html]  view plain  copy
  1. spring.datasource.url=jdbc:mysql://localhost/food  
  2. spring.datasource.username=root  
  3. spring.datasource.password=123  
  4. spring.datasource.driver-class-name=com.mysql.jdbc.Driver  

三.编写分词工具类实现分词

[html]  view plain  copy
  1. package com.et.food.utils;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.ArrayList;  
  6. import java.util.HashMap;  
  7. import java.util.List;  
  8. import java.util.Map;  
  9.   
  10. import org.apache.lucene.analysis.Analyzer;  
  11. import org.apache.lucene.analysis.TokenStream;  
  12. import org.apache.lucene.document.Document;  
  13. import org.apache.lucene.index.DirectoryReader;  
  14. import org.apache.lucene.index.IndexWriter;  
  15. import org.apache.lucene.index.IndexWriterConfig;  
  16. import org.apache.lucene.queryparser.classic.ParseException;  
  17. import org.apache.lucene.queryparser.classic.QueryParser;  
  18. import org.apache.lucene.search.IndexSearcher;  
  19. import org.apache.lucene.search.Query;  
  20. import org.apache.lucene.search.ScoreDoc;  
  21. import org.apache.lucene.search.highlight.Highlighter;  
  22. import org.apache.lucene.search.highlight.QueryScorer;  
  23. import org.apache.lucene.search.highlight.SimpleHTMLFormatter;  
  24. import org.apache.lucene.search.highlight.TextFragment;  
  25. import org.apache.lucene.search.highlight.TokenSources;  
  26. import org.apache.lucene.store.Directory;  
  27. import org.apache.lucene.store.FSDirectory;  
  28. import org.apache.lucene.util.Version;  
  29. import org.wltea.analyzer.lucene.IKAnalyzer;  
  30.   
  31. public class LuceneUtils {  
  32.     static String dir = "F:\\index";  
  33.     static Analyzer analyzer = new IKAnalyzer();  
  34.     /**  
  35.      * 搜索  
  36.      * @throws ParseException   
  37.      * @throws IOException   
  38.      */  
  39.     public static List<Map> search(String field,String value) throws Exception{  
  40.         Directory directory = FSDirectory.open(new File(dir));  
  41.         //读取索引库的存储目录  
  42.         DirectoryReader ireader = DirectoryReader.open(directory);  
  43.         //搜索类  
  44.         IndexSearcher isearcher = new IndexSearcher(ireader);  
  45.         //lucence查询解析 用于指定查询的属性名和分词器  
  46.         QueryParser parser = new QueryParser(Version.LUCENE_47, field, analyzer);  
  47.         //开始搜索  
  48.         Query query = parser.parse(value);  
  49.         //最终結果被分詞后添加前綴和后綴的处理类  <B></B>  
  50.         SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter("<font color=red>","</font>");  
  51.         //將高亮搜索的詞 添加到高亮处理器中  
  52.         Highlighter highlighter = new Highlighter(htmlFormatter, new QueryScorer(query));  
  53.           
  54.         //获取搜索的结果 指定返回的docuemnt个数  
  55.          ScoreDoc[] hits = isearcher.search(query, null, 10).scoreDocs;  
  56.          List<Map> list=new ArrayList<Map>();  
  57.         for (int i = 0; i < hits.length; i++) {  
  58.           int id = hits[i].doc;  
  59.           Document hitDoc = isearcher.doc(hits[i].doc);  
  60.           Map map=new HashMap();  
  61.           map.put("foodid", hitDoc.get("foodid"));  
  62.           String foodname=hitDoc.get("foodname");  
  63.           //將查詢的結果和搜索詞匹配 匹配到添加前綴和后綴高亮   
  64.           TokenStream tokenStream = TokenSources.getAnyTokenStream(isearcher.getIndexReader(), id, "foodname", analyzer);  
  65.           //传入的第二個參数是查詢的值   
  66.           TextFragment[] frag = highlighter.getBestTextFragments(tokenStream, foodname, false, 200);  
  67.           String foodnameHign="";  
  68.           for (int j = 0; j < frag.length; j++) {  
  69.             if ((frag[j] != null) && (frag[j].getScore() > 0)) {  
  70.                 foodnameHign=((frag[j].toString()));  
  71.             }  
  72.           }  
  73.           map.put("foodname",foodnameHign);  
  74.           map.put("price",  hitDoc.get("price"));  
  75.           map.put("imagepath",  hitDoc.get("imagepath"));  
  76.           list.add(map);  
  77.         }  
  78.         ireader.close();  
  79.         directory.close();  
  80.         return list;  
  81.     }  
  82.     /**  
  83.      * 创建索引库  
  84.      *   
  85.      * select * from table where userdesc like '%永州%'  
  86.      *    
  87.      * @throws IOException   
  88.      */  
  89.     public static void write(Document doc) throws IOException{  
  90.         //索引库的存储目录  
  91.         Directory directory = FSDirectory.open(new File(dir));  
  92.         //关联lucence版本和当前分词器  
  93.         IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_47, analyzer);  
  94.         //传入 目录和分词器  
  95.         IndexWriter iwriter = new IndexWriter(directory, config);  
  96.           
  97.         iwriter.addDocument(doc);  
  98.         iwriter.commit();  
  99.         iwriter.close();  
  100.     }  
  101. }  

四.dao层的编写

[html]  view plain  copy
  1. package com.et.food.dao.impl;  
  2.   
  3. import java.util.List;  
  4. import java.util.Map;  
  5.   
  6. import org.springframework.beans.factory.annotation.Autowired;  
  7. import org.springframework.jdbc.core.JdbcTemplate;  
  8. import org.springframework.stereotype.Repository;  
  9.   
  10. @Repository  
  11. public class FoodDaoImpl {  
  12.     @Autowired  
  13.     private JdbcTemplate jdbc;  
  14.     /**  
  15.      * 获取总行数  
  16.      */  
  17.     public int queryFoodCount(){  
  18.         String sql="select count(*) as foodCount from food";  
  19.         return Integer.parseInt(jdbc.queryForList(sql).get(0).get("foodCount").toString());  
  20.           
  21.     }  
  22.     /**  
  23.      * 分页获取数据  
  24.      * 数据库存放很多数据进行一部分分词  
  25.      * @param start开始位置  
  26.      * @param rows获取总行数  
  27.      * @return  
  28.      */  
  29.     public List<Map<String,Object>> queryFood(int start,int rows){  
  30.         String sql = "select * from food limit "+start+","+rows;  
  31.         return jdbc.queryForList(sql);  
  32.     }  
  33. }  

五.controller的编写

[html]  view plain  copy
  1. package com.et.food.dao.impl;  
  2.   
  3.   
  4. import java.io.IOException;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import org.apache.lucene.document.Document;  
  9. import org.apache.lucene.document.Field;  
  10. import org.apache.lucene.document.TextField;  
  11. import org.springframework.beans.factory.annotation.Autowired;  
  12. import org.springframework.web.bind.annotation.GetMapping;  
  13. import org.springframework.web.bind.annotation.RestController;  
  14.   
  15. import com.et.food.utils.LuceneUtils;  
  16.   
  17.   
  18. @RestController  
  19. public class FoodController {  
  20.     @Autowired  
  21.     FoodDaoImpl dao;  
  22.     @GetMapping("/searchFood")  
  23.     public List<Map> getFood(String keyWord) throws Exception {  
  24.         return LuceneUtils.search("foodname", keyWord);  
  25.     }  
  26.     @GetMapping("/createIndex")  
  27.     public String createIndex(){  
  28.         try {  
  29.             //数据库查询数据 查询数据批量查询  
  30.             int queryFoodCount = dao.queryFoodCount();  
  31.             //第一次拉取 0,1000  
  32.             //第二次拉取 1001,2000  
  33.             int startIndex = 0;  
  34.             int rows = 5;  
  35.             while (startIndex <= queryFoodCount) {  
  36.                 //每次拉取数据  
  37.                 List<Map<String, Object>> queryFood = dao.queryFood(startIndex, rows);  
  38.                 for (int i = 0; i < queryFood.size(); i++) {  
  39.                     Map<String, Object> mso = queryFood.get(i);  
  40.                     Document doc = new Document();  
  41.                     Field field1 = new Field("foodid", mso.get("foodid").toString(), TextField.TYPE_STORED);  
  42.                     Field field2 = new Field("foodname", mso.get("foodname").toString(), TextField.TYPE_STORED);  
  43.                     Field field3 = new Field("price", mso.get("price").toString(), TextField.TYPE_STORED);  
  44.                     doc.add(field1);  
  45.                     doc.add(field2);  
  46.                     doc.add(field3);  
  47.                     LuceneUtils.write(doc);  
  48.   
  49.                 }  
  50.                 //传入lucene索引  
  51.                 startIndex += 1 + rows;  
  52.             }   
  53.         } catch (IOException e) {  
  54.             e.printStackTrace();  
  55.             return "0";  
  56.         }  
  57.         return "1";  
  58.     }  
  59. }  

六.html的编写


[html]  view plain  copy
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta charset="UTF-8">  
  5. <title>Insert title here</title>  
  6. <script type="text/javascript"src="jquery-3.1.0.min.js"></script>  
  7. <script type="text/javascript">  
  8.     $(function(){  
  9.         $("#searchBtn").click(function(){  
  10.             $.ajax({  
  11.                 url:'searchFood',  
  12.                 data:'keyWord='+$("input[name='keyWord']").val(),  
  13.                 dataType:'json',  
  14.                 success:function(jd){  
  15.                     $("div[name='c']").remove()  
  16.                     for(var i=0;i<jd.length;i++){  
  17.                         var html = "<div name='c'>"+  
  18.                         "<h3>"+jd[i].foodname+"</h3>"+  
  19.                         "<span>"+jd[i].foodname+"价格是"+jd[i].price+"</span>"+  
  20.                         "<hr>"+  
  21.                         "</div>";  
  22.                         $("#foodDiv").append(html);  
  23.                     }  
  24.                 }  
  25.                       
  26.             })  
  27.         })  
  28.     })  
  29. </script>  
  30. </head>  
  31. <body>  
  32.     <div>  
  33.         <img src="bd_logo1.png" width="280px"><br/>  
  34.          <input type="text" name="keyWord"  style="border:1px solid grey;width:200px;height:25px">  
  35.          <input id="searchBtn" type="button" value="搜索一下"  style="background-color: rgb(51,133,255);color: white;border:0px;height:28px ">  
  36.          <div id="foodDiv">  
  37.            
  38.          </div>  
  39.     </div>  
  40. </body>  
  41. </html>  

七.运行项目

[html]  view plain  copy
  1. package com.et.food.dao.impl;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5.   
  6. @SpringBootApplication  
  7. public class FoodMain {  
  8.   
  9.     public static void main(String[] args) throws Exception {  
  10.         SpringApplication.run(FoodMain.class, args);  
  11.     }  
  12. }  

猜你喜欢

转载自blog.csdn.net/qq_37928350/article/details/79071069
今日推荐