lucene使用3.0.3_使用Apache Lucene 4.3轻松进行搜索
lucene使用3.0.3
Lucene是用Java編寫的全文搜索引擎,可以為任何應用程序提供強大的搜索功能。 Lucene的核心是基于文件的全文本索引。 Lucene提供API創建該索引,然后向該索引添加和刪除內容。 此外,它允許使用功能強大的搜索算法從該索引中搜索和檢索信息。 可以從不同的來源(如數據庫,文件系統以及網站)中提取存儲的數據。 在開始之前,讓我們先思考一下。
倒排索引
倒排索引是一種數據結構,用于存儲內容的映射以及包含該內容的對象的位置。 為了更加清楚,這里有一些示例
多面搜索
任何對象都可以具有多個屬性,每個屬性是該對象的一個方面 。 分面搜索使我們可以基于多個分面來搜索對象的集合。 多面搜索也稱為多面導航或多面瀏覽 ,它使我們可以搜索根據多面組織結構組織的信息 。
考慮一個購物車中的商品示例。 商品可以具有多個類別,例如類別,標題,價格,顏色,重量等。現在,商品搜索可以讓我們搜索花園類別中所有具有紅色且價格在30盧比到Rs之間的商品.40。
Lucene為我們提供了一個API
所有以上這些使Lucene成為超快速的搜索引擎,它返回超相關的搜索結果。
Lucene功能
有關完整列表,請訪問此處: http : //lucene.apache.org/core/features.html
Lucene概念和術語
教程目標
本教程的代碼已提交給SVN。 可以從以下位置檢出: https : //www.assembla.com/code/weblog4j/subversion/nodes/24/SpringDemos/trunk
這是一個擴展項目,包含更多教程。 lucene類位于com.aranin.spring.lucene包中
逐步演練
1. 依賴關系 –依賴關系可以通過maven添加
<dependency><artifactId>lucene-core</artifactId><groupId>org.apache.lucene</groupId><type>jar</type><version>${lucene-version}</version></dependency><dependency><artifactId>lucene-queries</artifactId><groupId>org.apache.lucene</groupId><type>jar</type><version>${lucene-version}</version></dependency><dependency><artifactId>lucene-queryparser</artifactId><groupId>org.apache.lucene</groupId><type>jar</type><version>${lucene-version}</version></dependency><dependency><artifactId>lucene-analyzers-common</artifactId><groupId>org.apache.lucene</groupId><type>jar</type><version>${lucene-version}</version></dependency><dependency><artifactId>lucene-facet</artifactId><groupId>org.apache.lucene</groupId><type>jar</type><version>${lucene-version}</version></dependency>2. 創建索引 –可以通過在創建模式下創建IndexWriter來創建索引。
public void createIndex() throws Exception {boolean create = true;File indexDirFile = new File(this.indexDir);if (indexDirFile.exists() && indexDirFile.isDirectory()) {create = false;}Directory dir = FSDirectory.open(indexDirFile);Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_43, analyzer);if (create) {// Create a new index in the directory, removing any// previously indexed documents:iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE);}IndexWriter writer = new IndexWriter(dir, iwc);writer.commit();writer.close(true);}- indexDir是您要在其中創建索引的目錄。
- 目錄是用于存儲索引的文件的平面列表。 它可以是RAMDirectory,FSDirectory或基于DB的目錄。
- FSDirectory實現目錄并將索引保??存在文件系統中的文件中。
- IndexWriterConfig.Open模式在create或create_append或appned模式下創建編寫器。 如果創建模式不存在或覆蓋現有索引,則創建模式會創建一個新索引。 為了創建目的,我們創建一個現有的。
- 調用上述方法將創建一個空索引。
3. 寫入索引 –創建索引后,我們可以向其中寫入文檔。 這可以通過以下方式完成。
public void createIndexWriter() throws Exception {boolean create = true;File indexDirFile = new File(this.indexDir);Directory dir = FSDirectory.open(indexDirFile);Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43); <span style="color: #222222; font-family: 'Courier 10 Pitch', Courier, monospace; line-height: 21px;">IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_43, analyzer);</span>iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);this.writer = new IndexWriter(dir, iwc);}上面的方法在create_append模式下創建一個writer。 在這種模式下,如果創建了索引,則不會覆蓋它。 您可以注意到,此方法不會關閉編寫器。 它只是創建并返回它。 創建IndexWriter是一項昂貴的操作。 因此,我們不應該在每次必須將文檔寫入索引時都創建作者。 相反,我們應該創建一個IndexWriter池,并使用線程系統從池中將寫入器寫入索引,然后將寫入器返回到池中。
public void addBookToIndex(BookVO bookVO) throws Exception {Document document = new Document();document.add(new StringField("title", bookVO.getBook_name(), Field.Store.YES));document.add(new StringField("author", bookVO.getBook_author(), Field.Store.YES));document.add(new StringField("category", bookVO.getCategory(), Field.Store.YES));document.add(new IntField("numpage", bookVO.getNumpages(), Field.Store.YES));document.add(new FloatField("price", bookVO.getPrice(), Field.Store.YES));IndexWriter writer = this.luceneUtil.getIndexWriter();writer.addDocument(document);writer.commit();}插入時,我們不會在代碼中創建編寫器。 取而代之的是,我們使用了一個預先創建的writer,它被存儲為實例變量。
4. 搜索索引 –這又分兩個步驟完成:1.創建IndexSearcher 2.創建查詢并進行搜索。
public void createIndexSearcher(){IndexReader indexReader = null;IndexSearcher indexSearcher = null;try{File indexDirFile = new File(this.indexDir);Directory dir = FSDirectory.open(indexDirFile);indexReader = DirectoryReader.open(dir);indexSearcher = new IndexSearcher(indexReader);}catch(IOException ioe){ioe.printStackTrace();}this.indexSearcher = indexSearcher;}注–搜索器中使用的分析器應與用于創建編寫器的分析器相同,因為分析器負責將數據存儲在索引中的方式。 再次創建IndexSearcher是一項昂貴的操作,因此預創建IndexSearcher池并以與IndexWriter類似的方式使用它是有意義的。
public List<BookVO> getBooksByField(String value, String field, IndexSearcher indexSearcher){List<BookVO> bookList = new ArrayList<BookVO>();Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);QueryParser parser = new QueryParser(Version.LUCENE_43, field, analyzer);try {BooleanQuery query = new BooleanQuery();query.add(new TermQuery(new Term(field, value)), BooleanClause.Occur.MUST);//Query query = parser.Query(value);int numResults = 100;ScoreDoc[] hits = indexSearcher.search(query,numResults).scoreDocs;for (int i = 0; i < hits.length; i++) {Document doc = indexSearcher.doc(hits[i].doc);bookList.add(getBookVO(doc));}} catch (IOException e) {e.printStackTrace(); }return bookList; }已預先創建IndexSearcher并將其傳遞給該方法。 搜索的主要部分是查詢形成。 Lucene支持許多不同種類的查詢器。
您可以為搜索選擇適當的查詢。 可以從此處了解查詢語言的語法: http : //lucene.apache.org/core/old_versioned_docs/versions/2_9_1/queryparsersyntax.pdf
資源資源
摘要
搜索仍然是任何內容驅動的應用程序的骨干。 傳統的數據庫驅動的搜索功能不是很強大,還有很多不足之處。 因此,需要一種快速,準確且功能強大的搜索解決方案,該解決方案可以輕松地并入應用程序代碼中。 Lucene很好地填補了這一空白,它使搜索變得輕而易舉,并得到強大的搜索算法陣列的支持,例如相關性排名,詞組,通配符,接近度和范圍搜索。 它還具有空間和內存效率。 難怪在Lucene之上構建了如此多的應用程序。 本文旨在提供一個基礎教程,以幫助親愛的讀者使用Lucene入門工具。 還有很多要說的,但是那您不想自己探索嗎?
翻譯自: https://www.javacodegeeks.com/2013/06/searching-made-easy-with-apache-lucene-4-3.html
lucene使用3.0.3
總結
以上是生活随笔為你收集整理的lucene使用3.0.3_使用Apache Lucene 4.3轻松进行搜索的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新游q1怎么连接电脑(新游n1连接电脑)
- 下一篇: Neo4j:Cypher – Neo.C