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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

3.5 实例讲解Lucene索引的结构设计

發(fā)布時間:2025/3/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 3.5 实例讲解Lucene索引的结构设计 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

3.2節(jié)我們已經(jīng)運(yùn)行了一個Lucene建立索引的小程序,這一節(jié)我們就以這個小程序?yàn)槔v解一下Lucene建立索引的過程。

1 import java.nio.charset.StandardCharsets; 2 import java.nio.file.Files; 3 import java.nio.file.Paths; 4 import java.io.*; 5 6 import org.apache.lucene.analysis.standard.StandardAnalyzer; 7 import org.apache.lucene.document.Document; 8 import org.apache.lucene.document.Field; 9 import org.apache.lucene.document.StringField; 10 import org.apache.lucene.document.TextField; 11 import org.apache.lucene.index.IndexWriter; 12 import org.apache.lucene.index.IndexWriterConfig; 13 import org.apache.lucene.store.Directory; 14 import org.apache.lucene.store.FSDirectory; 15 import org.apache.lucene.util.Version; 16 17 /** 18 * @author csl 19 * @description: 20 * 依賴jar:Lucene-core,lucene-analyzers-common,lucene-queryparser 21 * 作用:簡單的索引建立 22 */ 23 public class Indexer { 24 public static Version luceneVersion = Version.LATEST; 25 /** 26 * 建立索引 27 */ 28 public static void createIndex(){ 29 IndexWriter writer = null; 30 try{ 31 //1、創(chuàng)建Directory 32 //Directory directory = new RAMDirectory();//創(chuàng)建內(nèi)存directory 33 Directory directory = FSDirectory.open(Paths.get("index"));//在硬盤上生成Directory00 34 //2、創(chuàng)建IndexWriter 35 IndexWriterConfig iwConfig = new IndexWriterConfig( new StandardAnalyzer()); 36 writer = new IndexWriter(directory, iwConfig); 37 //3、創(chuàng)建document對象 38 Document document = null; 39 //4、為document添加field對象 40 File f = new File("raw");//索引源文件位置 41 for (File file:f.listFiles()){ 42 document = new Document(); 43 document.add(new StringField("path", f.getName(),Field.Store.YES)); 44 System.out.println(file.getName()); 45 document.add(new StringField("name", file.getName(),Field.Store.YES)); 46 InputStream stream = Files.newInputStream(Paths.get(file.toString())); 47 document.add(new TextField("content", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))));//textField內(nèi)容會進(jìn)行分詞 48 //document.add(new TextField("content", new FileReader(file))); 如果不用utf-8編碼的話直接用這個就可以了 49 writer.addDocument(document); 50 } 51 }catch(Exception e){ 52 e.printStackTrace(); 53 }finally{ 54 //6、使用完成后需要將writer進(jìn)行關(guān)閉 55 try { 56 writer.close(); 57 } catch (IOException e) { 58 e.printStackTrace(); 59 } 60 } 61 } 62 public static void main(String[] args) throws IOException 63 { 64 createIndex(); 65 } 66 } View Code

創(chuàng)建索引共六步:

1.創(chuàng)建索引目錄。

Directory directory = new RAMDirectory(); Directory directory = FSDirectory.open(Paths.get("index")); View Code

創(chuàng)建索引目錄有兩種方式:

  • RAMDirectory類:創(chuàng)建一個內(nèi)存目錄,優(yōu)點(diǎn)是速度快,缺點(diǎn)是程序退出后索引目錄數(shù)據(jù)就會丟失。
  • FSDirectory類: ?創(chuàng)建一個文件目錄,該方式創(chuàng)建的索引數(shù)據(jù)保存在磁盤上,不會因?yàn)槌绦虻耐顺龆А?/li>

下文針對FSDirectory方式來講解Lucene的基本使用。

2.創(chuàng)建IndexWriter。

1 IndexWriterConfig iwConfig = new IndexWriterConfig( new StandardAnalyzer()); 2 IndexWriter writer = new IndexWriter(directory, iwConfig); View Code

通過IndexWriter對象來創(chuàng)建和維護(hù)索引。

IndexWriterConfig對象用來對IndexWriter進(jìn)行初始配置:配置分詞器;配置索引維護(hù)的方式;配置用來緩沖文檔的RAM大小等。

具體可參照IndexWriterrConfig文檔根據(jù)需求進(jìn)行個性化配置。

3. 創(chuàng)建Document。

1 Document doc=new Document(); View Code

Document是Lucene建立索引的基本單元,相當(dāng)于數(shù)據(jù)庫的關(guān)系表。

4. 添加Field。

1 document = new Document(); 2 document.add(new StringField("path", f.getName(),Field.Store.YES)); 3 System.out.println(file.getName()); 4 document.add(new StringField("name", file.getName(),Field.Store.YES)); 5 InputStream stream = Files.newInputStream(Paths.get(file.toString())); 6 document.add(new TextField("content", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))));//textField內(nèi)容會進(jìn)行分詞 7 //document.add(new TextField("content", new FileReader(file))); 如果不用utf-8編碼的話直接用這個就可以了 View Code

Field是Lucene建立索引的最小單元,相當(dāng)于關(guān)系表中的屬性。一個Document可以包含多個Field。Document添加Field只需調(diào)用Add()方法。

Lucene為我們提供了多種類型的Field,比如IntField, LongField, StringField, TextField等。程序?qū)嵗?#xff0c;我們用到了StringField和TextField。我們有必要來了解一下這兩種Field的區(qū)別,因?yàn)檫@關(guān)系到倒排表的建立:

  • StringField:對域進(jìn)行索引,但不進(jìn)行分詞,將域值作為單一的語匯單元,適用于索引那些不能被分解的域值,如URL,文件路徑,電話號碼等。參考StringField文檔。
  • TextField: 對域既索引又分詞,Lucene會對這個域進(jìn)行分詞并建立倒排表。參考TextField文檔。

5.添加Document。

對IndexWriter對象調(diào)用addDocument方法將文檔添加到索引庫中。

6.關(guān)閉IndexWriter對象。

把所有的文檔都添加到索引庫中后,關(guān)閉Indexwriter對象。

ps:這篇博客以文集為例形象生動地說明了IndexWriter,Document和Field的關(guān)系,大家不妨看一看:例子

關(guān)于Lucene的具體索引步驟就介紹到這里~~

.

?

轉(zhuǎn)載于:https://www.cnblogs.com/itcsl/p/6828652.html

總結(jié)

以上是生活随笔為你收集整理的3.5 实例讲解Lucene索引的结构设计的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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