Lucene開源全文檢索引擎快速入門
Lucene是一個用Java開發的開源全文檢索引擎,官網是:http://lucene.apache.org/ ,Lucene不是一個完整的全文索引應用(與之對應的是solr),而是是一個用Java寫的全文索引引擎工具包,它可以方便的嵌入到各種應用中實現針對應用的全文索引/檢索功能,更多介紹大家自行搜尋。
下載
也就是這幾個:
其中IKAnalyzer2012_FF.jar是一個國人寫的中文分詞工具,Lucene自帶的分詞對中文支援不好。注意,這個jar包網上比較亂,隨便從網上下載的話可能不相容,因為跟具體的Lucene版本有關,初學者建議直接用我demo裡面整理好的jar包:
工具類
package com.test.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
/**
* 檔案操作工具類
*
*/
public class FileUtil
{
private static Logger2 log = LoggerFactory2.getLogger(FileUtil.class);
/**
* 獲取檔案的字尾,包括前面的點,如“.jpg”,並自動轉小寫
* @param path
* @return
*/
public static String getExt(String path)
{
int idx = path.lastIndexOf(".");
if (idx < 0)
return "";
return path.substring(idx).toLowerCase();
}
/**
* 遞迴刪除檔案或資料夾
* @param file
* @return 返回是否刪除成功,如果檔案本身不存在,也返回true
*/
public static boolean delete(File file)
{
boolean flag = true;// 如果檔案本來就不存在那麼預設就是true
if (file.isFile())
flag &= file.delete();
else if (file.isDirectory())
{
for (File f : file.listFiles())
flag &= delete(f);
flag &= file.delete();
}
return flag;
}
/**
* 複製單個檔案
* @param src 待複製的檔名
* @param dest 目標檔名
* @param override 如果目標檔案存在,是否覆蓋
* @return 如果複製成功返回true,否則返回false
*/
public static boolean copy(String src, String dest, boolean override)
{
File srcFile = new File(src);
if (!srcFile.exists()) // 判斷原始檔是否存在
{
log.error("原始檔不存在:{}", src);
return false;
}
else if (!srcFile.isFile())
{
log.error("原始檔不是一個檔案:{}", src);
return false;
}
File destFile = new File(dest); // 判斷目標檔案是否存在
if (destFile.exists())
{
if (override) // 如果目標檔案存在並允許覆蓋
destFile.delete();// 刪除已經存在的目標檔案,無論目標檔案是目錄還是單個檔案
else
{
log.error("已存在同名檔案且不允許覆蓋:{}", dest);
return false;
}
}
else
{
if (!destFile.getParentFile().exists()) // 如果目標檔案所在目錄不存在,則建立目錄
{
if (!destFile.getParentFile().mkdirs())
{
log.error("輸出資料夾不存在且自動建立失敗:{}", dest);
return false;// 複製檔案失敗:建立目標檔案所在目錄失敗
}
}
}
// 複製檔案
int byteread = 0; // 讀取的位元組數
InputStream in = null;
OutputStream out = null;
try
{
in = new FileInputStream(srcFile);
out = new FileOutputStream(destFile);
byte[] buffer = new byte[1024];
while ((byteread = in.read(buffer)) != -1)
out.write(buffer, 0, byteread);
return true;
}
catch (Exception e)
{
log.error("複製檔案失敗:", e);
return false;
}
finally
{
try
{
if (out != null)
out.close();
if (in != null)
in.close();
}
catch (Exception e)
{
log.error("嘗試關閉流時失敗:", e);
e.printStackTrace();
}
}
}
/**
* 複製單個檔案,存在則覆蓋
* @param src
* @param dest
* @return
*/
public static boolean copy(String src, String dest)
{
return copy(src, dest, true);
}
/**
* 剪下檔案
* @param src
* @param dest
* @return
*/
public static boolean cut(String src, String dest)
{
return new File(src).renameTo(new File(dest));
}
/**
* 重新命名檔案
* @param src
* @param newName
* @return
*/
public static boolean rename(File src, String newName)
{
return src.renameTo(new File(src.getParent() + "\\" + newName));
}
/**
* 重新命名檔案
* @param src
* @param newName
* @return
*/
public static boolean rename(String src, String newName)
{
File file = new File(src);
return rename(file, newName);
}
/**
* 從檔案中讀取內容
* @param filePath
* @return
*/
public static String readFile(String filePath, String encoding)
{
log.debug("開始讀取檔案:{}", filePath);
try
{
FileInputStream fis = new FileInputStream(filePath);
BufferedReader br = new BufferedReader(new InputStreamReader(fis, encoding));
String s = "";
StringBuffer sb = new StringBuffer();
while ((s = br.readLine()) != null)
sb.append(s+"\n");
br.close();
return sb.toString();
}
catch (Exception e)
{
log.error("讀取檔案失敗:", e);
return null;
}
}
/**
* 以預設的utf-8編碼讀取檔案
* @param filePath
* @return
*/
public static String readFile(String filePath)
{
return readFile(filePath, "utf-8");
}
/**
* 寫入檔案
* @param filePath
* @param text
* @param encoding
* @return
*/
public static boolean writeFile(String filePath, String text, String encoding)
{
try
{
FileOutputStream fos = new FileOutputStream(filePath);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos, encoding));
bw.append(text);
bw.close();
return true;
}
catch (Exception e)
{
log.error("寫入檔案失敗:", e);
return false;
}
}
/**
* 寫入檔案,預設utf-8編碼
* @param filePath
* @param text
* @return
*/
public static boolean writeFile(String filePath, String text)
{
return writeFile(filePath, text, "utf-8");
}
/**
* 遍歷執行者
* @author LXA
*/
public interface TraverseExecuter
{
/**
* 遍歷某個資料夾時需要內部需要執行的方法
* @param filePath 檔案路徑
* @param fileName 檔名
*/
public void execute(String filePath, String fileName);
}
/**
* 遍歷某個資料夾執行一段操作
* @param filePath 資料夾路徑
* @param fileNameFilter 篩選器,為null時不篩選
* @param executer 需要呼叫的方法
*/
public static void traverse(String filePath, FilenameFilter fileNameFilter, TraverseExecuter executer)
{
try
{
File root = new File(filePath);
for(File file : root.listFiles(fileNameFilter))
{
String path = file.getAbsolutePath();
if(file.isFile())
executer.execute(path, file.getName());
else if(file.isDirectory())
traverse(path, fileNameFilter, executer);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 列出某個路徑下的所有檔案,包括子資料夾,如果本身就是檔案,那麼返回自身
* @param filePath 需要遍歷的檔案路徑
* @param fileNameFilter 檔名過濾器
* @return 檔案集合
*/
public static List<File> listAllFiles(String filePath, FilenameFilter fileNameFilter)
{
List<File> files= new ArrayList<File>();
try
{
File root = new File(filePath);
if(!root.exists()) return files;
if(root.isFile()) files.add(root);
else
{
for(File file : root.listFiles(fileNameFilter))
{
if(file.isFile()) files.add(file);
else if(file.isDirectory())
{
files.addAll(listAllFiles(file.getAbsolutePath(), fileNameFilter));
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return files;
}
/**
* 列出某個路徑下的所有檔案,包括子資料夾,如果本身就是檔案,那麼返回自身
* @param filePath 需要遍歷的檔案路徑
* @return 檔案集合
*/
public static List<File> listAllFiles(String filePath)
{
return listAllFiles(filePath, null);
}
/**
* 遍歷某個資料夾執行一段操作
* @param filePath 資料夾路徑
* @param executer 需要呼叫的方法
*/
public static void traverse(String filePath, TraverseExecuter executer)
{
traverse(filePath, null, executer);
}
/**
* 遍歷某個路徑,刪除其中的空資料夾
* @param filePath
*/
public static void deleteEmptyFolder(File file)
{
File[] files = file.listFiles();
if(files == null || files.length == 0)
{
delete(file);
log.info("以下資料夾為空,已刪除:"+file.getAbsolutePath());
}
for(File f : files)
{
if(f.isDirectory())
deleteEmptyFolder(f);
}
}
/**
* 遍歷某個路徑,刪除其中的空資料夾
* @param filePath
*/
public static void deleteEmptyFolder(String filePath)
{
deleteEmptyFolder(new File(filePath));
}
/**
* 從一個輸入流寫入到輸出流
* @param is 輸入流
* @param os 輸出流
* @param closeInput 是否關閉輸入流
* @param closeOutput 是否關閉輸出流
* @throws IOException
*/
public static void writeIO(InputStream is, OutputStream os, Boolean closeInput, Boolean closeOutput) throws IOException
{
byte[] buf = new byte[1024];
int len = -1;
while ((len = is.read(buf)) != -1)
os.write(buf, 0, len);
if(closeInput)
{
if(is != null )
is.close();
}
if(closeOutput)
{
if( os != null )
{
os.flush();
os.close();
}
}
}
/**
* 從一個輸入流寫入到輸出流
* @param is 輸入流
* @param os 輸出流
* @throws IOException
*/
public static void writeIO(InputStream is, OutputStream os) throws IOException
{
writeIO(is, os, false, false);
}
public static void exportHtmlTest(String filePath,String text) throws IOException
{
FileOutputStream fos = new FileOutputStream(filePath);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
bw.append("<!DOCTYPE html><html><head><title>Title of the document</title></head><body>");
bw.append(text);
bw.append("</body></html>");
bw.close();
}
}
/**
* 為了避免過多jar包引用以及某些不需要日記記錄的場合,內部寫一個最簡單的log工具類<br>
* 如果複製到其他地方的時候不想帶著幾個log4j的jar包,可以放開本段註釋<br>
* 然後把頭部的Logger和LoggerFactory都加上一個2<br>
* 注意同一包下Logger2和LoggerFactory2不能重複<br>
* @author LXA
*/
class Logger2
{
public void debug(String info, Object... args)
{
for(int i=0; i<args.length; i++)
info = info.replaceFirst("\\{\\}", args[i].toString());
System.out.println(info);
}
public void error(String info, Throwable throwable)
{
System.err.println(info);
throwable.printStackTrace();
}
public void error(String info, String aaa)
{
System.err.println(info);
}
public void error(String info)
{
System.err.println(info);
}
public void info(String info)
{
System.out.println(info);
}
}
class LoggerFactory2
{
public static <T> Logger2 getLogger(Class<T> cls)
{
return new Logger2();
}
}
demo
package com.test;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
相關推薦
Lucene開源全文檢索引擎快速入門
Lucene是一個用Java開發的開源全文檢索引擎,官網是:http://lucene.apache.org/ ,Lucene不是一個完整的全文索引應用(與之對應的是solr),而是是一個用Java寫的全文索引引擎工具包,它可以方便的嵌入到各種應用中實現針對應用的全文索引/檢索功能,更多
Slickflow.NET 開源工作流引擎快速入門之三: 簡單或分支流程程式碼編寫示例
前言:對於急切想了解引擎功能的開發人員,在下載版本後,就想嘗試編寫程式碼,完成一個流程的開發和測試。本文試圖從請假流程,或分支模式來快速瞭解引擎程式碼的編寫。
1. 建立或分支流程圖形
或分支流程是常見的決策類的流程,用於處理不同決策場景出現的業務處理,
Slickflow.Graph 開源工作流引擎快速入門之四: 圖形編碼建模工具使用手冊
前言: 業務人員繪製流程時,通常使用圖形GUI介面互動操作來完成,然而對於需要頻繁操作或者管理較多流程的系統管理使用者,就需要一款輔助工具,來幫助他們快速完成流程的建立和編輯更新。Slickflow.Graph 圖形編碼建模工具通過命令列直接編寫程式碼建立圖形,實現了流程圖形繪製效率的快速提升。
申
【Lucene】Apache Lucene全文檢索引擎架構之入門實戰
Lucene是一套用於全文檢索和搜尋的開源程式庫,由Apache軟體基金會支援和提供。Lucene提供了一個簡單卻強大的應用程式介面,能夠做全文索引和搜尋。在Java開發環境裡Lucene是一個成熟的免費開源工具。就其本身而言,Lucene是當前以
Lucene全文檢索引擎
getname 通過 nal dem 檢索 數據庫 project cep 關閉
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSch
javaEE Lucene,全文檢索,站內搜尋,入門程式。索引庫的新增
注意:搜尋使用的分析器(分詞器)要和建立索引時使用的分析器一致。
Field類(域物件):
Test.java(入門程式 測試類):
package com.xxx.lucene;
import static org.junit.Assert.*;
im
全文檢索引擎Lucene優點
Lucene最初是由Doug Cutting開發的,在SourceForge的網站上提供下載。在2001年9月做為高質量的免費、開源Java產品加入到Apache軟體基金會的Jakarta家族中。Lucene是一個全文檢索引擎的架構,提供了完整的查詢引擎和索引引擎。它也是一個
全文檢索引擎Lucene優點 .
Lucene最初是由Doug Cutting開發的,在SourceForge的網站上提供下載。在2001年9月做為高質量的免費、開源Java產品加入到Apache軟體基金會的Jakarta家族中。Lucene是一個全文檢索引擎的架構,提供了完整的查詢引擎和索引引擎。它也是一個高效能、可伸縮的資訊搜尋(IR
【Lucene】Apache Lucene全文檢索引擎架構之搜尋功能
上一節主要總結了一下Lucene是如何構建索引的,這一節簡單總結一下Lucene中的搜尋功能。主要分為幾個部分,對特定項的搜尋;查詢表示式QueryParser的使用;指定數字範圍內搜尋;指定字串開
Lucene全文檢索引擎工具包使用方法總結
Lucene是apache軟體基金會4 jakarta專案組的一個子專案,是一個開放原始碼的全文檢索引擎工具包,但它不是一個完整的全文檢索引擎,而是一個全文檢索引擎的架構,提供了完整的查詢引擎和索引引擎,部分文字分析引擎(英文與德文兩種西方語言)。Lucene的目的是為軟體
Lucene,MG4J ,Sphinx全文檢索引擎的優點
Lucene作為一個全文檢索引擎,其具有如下突出的優點:
(1)索引檔案格式獨立於應用平臺。Lucene定義了一套以8位位元組為基礎的索引檔案格式,使得相容系統或者不同平臺的應用能夠共享建立的索引檔案。
(2)在傳統全文檢索引擎的倒排索引的基礎上,實現了分
Lucene:基於Java的全文檢索引擎簡介
轉載自:http://www.chedong.com/tech/lucene.html
Lucene是一個基於Java的全文索引工具包。
另外,如果是在選擇全文引擎,現在也許是試試Sphinx的時候了:相比Lucene速度更快,有中文分詞的支援,而且
開放原始碼的全文檢索引擎Lucene(轉載)
面對已經存在的商業全文檢索引擎,Lucene也具有相當的優勢。首先,它的開發原始碼發行方式(遵守Apache Software License[12]),在此基礎上程式設計師不僅僅可以充分的利用Lucene所提供的強大功能,而且可以深入細緻的學習到全文檢索引擎製作技術和麵相物件程式設計的實踐,進而在此基礎上根
lucene教程--全文檢索技術
bss bsp 詳細 .cn 總結 bbs 實例demo 技術 .net 1 Lucene 示例代碼 https://blog.csdn.net/qzqanzc/article/details/80916430
2 Lucene 4.7 學習及實例
Lucene(全文檢索)
介紹 Lucene是一個開放原始碼的全文檢索引擎工具包,但它不是一個完整的全文檢索引擎,而是一個全文檢索引擎的架構,提供了完整的查詢引擎和索引引擎,部分文字分析引擎。
A)什麼是索引庫 索引庫是Lucene的一個重要的儲存結構,它包括二部份:原始記錄表(value),詞彙/關
《虛幻4引擎快速入門》視訊教程
本想寫一系列虛幻4引擎入門的部落格,發現呢文字+圖片的方式表達上還是有些不方便,於是開始錄製這一系列視訊教程,釋出在CSDN學院,求圍觀,求好評。下面是課程的目錄:
Unreal Engine 4開發快速入門
專案程式碼和資源下載:https://github.com/neil3d/Unre
大資料spark計算引擎快速入門
spark快速入門 spark框架是用scala寫的,執行在Java虛擬機器(JVM)上。支援Python、Java、Scala或R多種語言編寫客戶端應用。 下載Spark 訪問http://spark.apache.org/downloads.html 選擇預編譯的版本進行
lucene框架全文檢索搜尋引擎方案
搜尋引擎技術方案
搜尋引擎方案
功能需求背景:
----有搜尋引擎需求
功能需求
提高查詢效率,關鍵詞全文檢索。
不需要訪問多次資料庫,只能一次資料庫查詢。
準確關鍵詞全文檢索。
由於查詢功能效
Lucene實現全文檢索
1.配置開發壞境
1.1.下載Lucene(http://lucene.apache.org/ )
jdk要求 1.8 以上
1.2.匯入jar包
下載完之後解壓裡面有所需Jar包 2. 編寫入門案列
2.1建立索引
2.2 查詢索引
2.3 中文分析
分散式全文檢索引擎之ElasticSearch
一 什麼是 ElasticSearch
Elasticsearch 是一個分散式可擴充套件的實時搜尋和分析引擎,一個建立在全文搜尋引擎 Apache Lucene(TM) 基礎上的搜尋引擎.當然 Elasticsearch 並不僅僅是 Lucene 那麼簡單,它不僅包括了全文搜尋功能,還可以進行以下工作: