日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java微服务篇3——Lucene

發(fā)布時間:2025/3/12 java 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java微服务篇3——Lucene 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Java微服務(wù)篇3——Lucene

1、數(shù)據(jù)分類

1.1、結(jié)構(gòu)化數(shù)據(jù)

具有固定格式或有限長度的數(shù)據(jù),如數(shù)據(jù)庫,元數(shù)據(jù)等

常見的結(jié)構(gòu)化數(shù)據(jù)也就是數(shù)據(jù)庫中的數(shù)據(jù),在數(shù)據(jù)庫中搜索很容易實現(xiàn),通常都是使用 sql語句進行查詢,而且能很快的得到查詢結(jié)果

數(shù)據(jù)庫中的數(shù)據(jù)存儲是有規(guī)律的,有行有列而且數(shù)據(jù)格式、數(shù)據(jù)長度都是固定的,所以搜索很容易

1.2、非結(jié)構(gòu)化數(shù)據(jù)

不定長或無固定格式的數(shù)據(jù),如郵件,word 文檔等磁盤上的文件

1.2.1、順序掃描

順序掃描,比如要找內(nèi)容包含某一個字符串的文件,就是一個文檔一個文檔的看,對于每一個文 檔,從頭看到尾,如果此文檔包含此字符串,則此文檔為我們要找的文件,接著看下一個文件,直到掃 描完所有的文件。如利用 windows 的搜索也可以搜索文件內(nèi)容,只是相當?shù)穆?/p>

1.2.2、全文檢索

全文檢索是指計算機索引程序通過掃描文章中的每一個詞,對每一個詞建立一個索引,指明該詞在 文章中出現(xiàn)的次數(shù)和位置,當用戶查詢時,檢索程序就根據(jù)事先建立的索引進行查找,并將查找的結(jié)果 反饋給用戶的檢索方法。這個過程類似于通過字典的目錄查字的過程

2、全文檢索(Lucene)

Lucene 是 apache 下的一個開放源代碼的全文檢索引擎工具包。提 供了完整的查詢引擎和索引引擎,部分文本分析引擎(英文與德文兩種西方語言),Lucene 的目的是為軟件開發(fā)人員提供一個簡單易用的工具包,以方便的在目標系統(tǒng)中實現(xiàn)全文檢索的功能。

2.1、Lucene優(yōu)點

穩(wěn)定、索引性能高

  • 每小時能夠索引150GB以上的數(shù)據(jù)
  • 對內(nèi)存的要求小,只需要1MB的堆內(nèi)存
  • 增量索引和批量索引一樣快
  • 索引的大小約為索引文本大小的20%~30%

高效、準確、高性能的搜索算法

  • 良好的搜索排序
  • 強大的查詢方式支持:短語查詢、通配符查詢、臨近查詢、范圍查詢等
  • 支持字段搜索(如標題、作者、內(nèi)容) 可根據(jù)任意字段排序
  • 支持多個索引查詢結(jié)果合并
  • 支持更新操作和查詢操作同時進行
  • 支持高亮、join、分組結(jié)果功能
  • 速度快
  • 可擴展排序模塊,內(nèi)置包含向量空間模型、BM25模型可選
  • 可配置存儲引擎

跨平臺

  • 純java編寫
  • 作為Apache開源許可下的開源項目,你可以在商業(yè)或開源項目中使用
  • Lucene有多種語言實現(xiàn)版(如C,C++、Python等),不僅僅是JAVA

2.2、架構(gòu)圖


2.3、Lucene實現(xiàn)全文檢索流程

2.4、應(yīng)用場景

單機軟件的搜索:word、markdown

站內(nèi)搜索:京東、淘寶、拉勾,索引源是數(shù)據(jù)庫

搜索引擎:百度、Google,索引源是爬蟲程序抓取的數(shù)據(jù)

3、Lucene實戰(zhàn)

3.1、項目搭建

job_info.sql文件 百度云:https://pan.baidu.com/s/1Iw7Hfd4kHSVptDKdQ2bmaQ提取碼:m27x

導(dǎo)入依賴

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core --><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-core</artifactId><version>4.10.3</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-analyzers-common --><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-analyzers-common</artifactId><version>4.10.3</version></dependency></dependencies>

實體類

public class JobInfo {private Long id;private String company_name;private String company_addr;private String company_info;private String job_name;private String job_addr;private String job_info;private int salary_min;private int salary_max;private String url;private String time; }

mapper

@Mapper public interface JobInfoMapper {@Select("select * from job_info")public List<JobInfo> selectJobInfo(); }

service

public interface JobInfoService {public List<JobInfo> selectJobInfo(); } @Service public class JobInfoServiceImpl implements JobInfoService {@AutowiredJobInfoMapper jobInfoMapper;@Overridepublic List<JobInfo> selectJobInfo() {return jobInfoMapper.selectJobInfo();} }

controller

@RestController public class JobInfoController {@AutowiredJobInfoServiceImpl jobInfoService;@RequestMapping("/")public String hello(){return "hello,lucene!";}@RequestMapping("/selectJobInfo")public List<JobInfo> selectJobInfo(){return jobInfoService.selectJobInfo();} }

application.yaml

mybatis:type-aliases-package: cn.winkto.beanmapper-locations: classpath:mapper/*.xml spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: blingbling123.url: jdbc:mysql://localhost:3306/job?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghaiapplication:name: product server:port: 8099

啟動類

@SpringBootApplication @MapperScan("cn.winkto.mapper") public class LuceneApplication {public static void main(String[] args) {SpringApplication.run(LuceneApplication.class, args);}}

3.2、Filed類型

Field類型數(shù)據(jù)類型是否分詞是否索引是否存儲說明
StringField(FieldName, FieldValue, Store.YES)字符串NYY/N字符串類型Field, 不分詞, 作為一個整體進行索引(如: 身份證號, 訂單編號), 是否需要存儲由Store.YES或Store.NO決定
StoredField(FieldName, FieldValue)重載方法, 支持多種類型NNY構(gòu)建不同類型的Field, 不分詞, 不索引, 要存儲. (如: 商品圖片路徑)
TextField(FieldName, FieldValue, Store.NO)文本類型YYY/N文本類型Field, 分詞并且索引, 是否需要存儲由Store.YES或Store.NO決定

3.3、索引創(chuàng)建

@SpringBootTest class LuceneApplicationTests {@AutowiredJobInfoServiceImpl jobInfoService;@Testvoid contextLoads() throws IOException {// 索引文件存儲的位置 D:\indexDirectory directory= FSDirectory.open(Paths.get("D:\\index"));// 分詞器StandardAnalyzer standardAnalyzer = new StandardAnalyzer();// 索引創(chuàng)建配置對象IndexWriterConfig indexWriterConfig = new IndexWriterConfig(standardAnalyzer);// 索引創(chuàng)建對象IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);// 刪除已有索引indexWriter.deleteAll();// 元數(shù)據(jù)查詢List<JobInfo> jobInfos = jobInfoService.selectJobInfo();for (JobInfo jobInfo : jobInfos) {// 文檔對象 import org.apache.lucene.document.*;Document indexableFields = new Document();// 添加元數(shù)據(jù)indexableFields.add(new StringField("id", String.valueOf(jobInfo.getId()), Field.Store.YES));indexableFields.add(new TextField("companyName", jobInfo.getCompany_name(), Field.Store.YES));indexableFields.add(new TextField("companyAddr", jobInfo.getCompany_addr(), Field.Store.YES));// 添加文檔indexWriter.addDocument(indexableFields);}indexWriter.close();} }

3.4、索引查詢

@Test void contextLoads1() throws IOException {// 索引文件存儲的位置 D:\indexDirectory directory= FSDirectory.open(Paths.get("D:\\index"));DirectoryReader reader = DirectoryReader.open(directory);IndexSearcher indexSearcher = new IndexSearcher(reader);TermQuery termQuery = new TermQuery(new Term("companyName", "北"));TopDocs search = indexSearcher.search(termQuery, 100);System.out.println(search.totalHits);ScoreDoc[] scoreDocs = search.scoreDocs;for (ScoreDoc scoreDoc : scoreDocs) {int id=scoreDoc.doc;Document doc = indexSearcher.doc(id);System.out.println(doc.get("companyName"));System.out.println("========================");} }

3.5、中文分詞器

導(dǎo)入依賴

<dependency><groupId>com.janeluo</groupId><artifactId>ikanalyzer</artifactId><version>2012_u6</version> </dependency>

測試類

@SpringBootTest class LuceneApplicationTests {@AutowiredJobInfoServiceImpl jobInfoService;@Testvoid contextLoads() throws IOException {// 索引文件存儲的位置 D:\indexDirectory directory= FSDirectory.open(new File("D:\\index"));// 分詞器// StandardAnalyzer standardAnalyzer = new StandardAnalyzer();IKAnalyzer standardAnalyzer = new IKAnalyzer();// 索引創(chuàng)建配置對象IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LATEST,standardAnalyzer);// 索引創(chuàng)建對象IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);// 刪除已有索引indexWriter.deleteAll();// 元數(shù)據(jù)查詢List<JobInfo> jobInfos = jobInfoService.selectJobInfo();for (JobInfo jobInfo : jobInfos) {// 文檔對象 import org.apache.lucene.document.*;Document indexableFields = new Document();// 添加元數(shù)據(jù)indexableFields.add(new StringField("id", String.valueOf(jobInfo.getId()), Field.Store.YES));indexableFields.add(new TextField("companyName", jobInfo.getCompany_name(), Field.Store.YES));indexableFields.add(new TextField("companyAddr", jobInfo.getCompany_addr(), Field.Store.YES));// 添加文檔indexWriter.addDocument(indexableFields);}indexWriter.close();}@Testvoid contextLoads1() throws IOException {// 索引文件存儲的位置 D:\indexDirectory directory= FSDirectory.open(new File("D:\\index"));DirectoryReader reader = DirectoryReader.open(directory);IndexSearcher indexSearcher = new IndexSearcher(reader);TermQuery termQuery = new TermQuery(new Term("companyName", "瓜子"));TopDocs search = indexSearcher.search(termQuery, 100);System.out.println(search.totalHits);ScoreDoc[] scoreDocs = search.scoreDocs;for (ScoreDoc scoreDoc : scoreDocs) {int id=scoreDoc.doc;Document doc = indexSearcher.doc(id);System.out.println(doc.get("companyName"));System.out.println("========================");}} } 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的Java微服务篇3——Lucene的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。