欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
用java實(shí)現全文檢索
ZhangLiHai.Com Blog
用java實(shí)現全文檢索
張利海 于 2004年11月24日 12:45 發(fā)表
關(guān)鍵詞 : 全文檢索 lucene
在本文我又提到lucene了,在java業(yè)界,提到全文檢索,幾乎沒(méi)有什么人不知道它。
用google搜索一下,滿(mǎn)世界都是有關(guān)資料。具有代表性的就是車(chē)東的“基于Java的全文索引引擎Lucene簡(jiǎn)介”,
我要寫(xiě)的也就只有最簡(jiǎn)單的三板斧,再加上支持中文的ChineseAnalyzer以及按照時(shí)間排序的搜索結果排序方法。
這些都可以在其他地方找到相關(guān)資料,我只是把他們提出來(lái),作為lucence應用中經(jīng)常遇到的麻煩解決辦法。
去年MSN上面有個(gè)朋友跟我提到希望用lucene構建個(gè)網(wǎng)站的全文檢索,我當時(shí)就覺(jué)得很簡(jiǎn)單,直說(shuō)沒(méi)問(wèn)題沒(méi)問(wèn)題,
不過(guò)他提到一個(gè)要求就是搜索結果要安裝時(shí)間排序,我查閱了些資料,發(fā)現lucene并不提供用戶(hù)自定義排序方式,
而只能按照自己相關(guān)性算法排序。后來(lái)我在車(chē)東的weblucene項目找到了IndexOrderSearcher。
解決了結果排序常規需求。
IndexOrderSearcher跟一般IndexSearch使用差不多,僅僅在構建對象的時(shí)候多加一個(gè)參數IndexOrderSearcher.ORDER_BY_DOCID_DESC
IndexOrderSearcher indexsearcher = new IndexOrderSearcher("/home/lucenetest/index",IndexOrderSearcher.ORDER_BY_DOCID_DESC);
新版本的lucene還提供了一個(gè)MultiFieldQueryParser,可以同時(shí)檢索多個(gè)字段,以前QueryParser比較麻煩。
private static ChineseAnalyzer chineseAnalyzer = new ChineseAnalyzer();
public Hits search(String queryText){
if (queryText == null){
return null;
}
Query query;
try{
query = MultiFieldQueryParser.parse(queryText, new String[]{"title"},chineseAnalyzer);
return indexsearcher.search(query);
}catch(Exception e){
return null;
}
}
下面是構建索引,定時(shí)從數據庫取出數據索引,做完記錄完成時(shí)間,我是把時(shí)間寫(xiě)入一個(gè)txt文件。
package com.test.search;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.*;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import java.io.*;
import java.sql.*;
import java.util.Date;
import com.test.db.*;
import com.test.utility.*;
/**
* Title: SearchIndexer
* Description: 全文索引
* Copyright:   Copyright (c) 2001
* Company: test
* @author Sean
* @version 1.0
*/
public class SearchIndexer {
private String indexPath = null;
protected Analyzer analyzer = new ChineseAnalyzer();
public SearchIndexer(String s) {
this.indexPath = s;
}
/**
* 索引某日期以前的所有文檔
* @param fromdate
* @return
*/
public final void updateIndex(String fromdate) {
Connection conn = DbUtil.getCon();
IndexWriter indexWriter = null;
try {
indexWriter = getWriter(false);
//索引發(fā)布系統內部文件
PreparedStatement pstm = conn.prepareStatement(
"select title,body,creationtime from document where creationtime > ‘" + fromdate +
"‘ order by creationtime");
ResultSet rs = pstm.executeQuery();
while (rs.next()) {
String creationtime = rs.getString("creationtime");
String title = rs.getString("title");
String body = rs.getString("body");
if (title == null || body == null) {
continue;
}
try {
addDocsToIndex(title,body, creationtime,indexWriter);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
indexWriter.optimize();
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
try {
indexWriter.close();
conn.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 檢查索引文件是否存在
* @param s
* @return 索引是否存在
*/
private boolean indexExists(String s) {
File file = new File(s + File.separator + "segments");
return file.exists();
}
/**
* 增加一組索引
* @param title
* @param body
* @param creationtime
* @param indexwriter
* @return
*/
private final void addNewsToIndex(String docid, String url,String title, String body,
String ptime, IndexWriter indexwriter) throws
IOException {
if (indexwriter == null) {
return;
}
else {
try {
Document document = new Document();
document.add(Field.Text("title", title));
document.add(Field.Text("body", body));
document.add(new Field("creationtime", creationtime, true, true, false));
indexwriter.addDocument(document);
}
catch (Exception ex) {
ex.printStackTrace();
}
return;
}
}
/**
* 取得IndexWriter
* @param flag 是否新建索引
* @return IndexWriter
*/
private IndexWriter getWriter(boolean flag) throws IOException {
String s = indexPath;
if (s == null) {
throw new IOException("索引文件路徑設置錯誤.");
}
indexPath = s + File.separator + "search";
IndexWriter indexwriter = null;
if (flag) {
try {
indexwriter = new IndexWriter(indexPath, analyzer, true);
}
catch (Exception exception) {
System.err.println("ERROR: Failed to create a new index writer.");
exception.printStackTrace();
}
}
else {
if (indexExists(indexPath)) {
try {
indexwriter = new IndexWriter(indexPath, analyzer, false);
}
catch (Exception exception1) {
System.err.println("ERROR: Failed to open an index writer.");
exception1.printStackTrace();
}
}
else {
try {
indexwriter = new IndexWriter(indexPath, analyzer, true);
}
catch (Exception exception2) {
System.err.println("ERROR: Failed to create a new index writer.");
exception2.printStackTrace();
}
}
}
return indexwriter;
}
public static void main(String[] args) {
String lastUpdate = "/home/lucenetest/lastUpdate.txt";
SearchIndexer searchIndexer = new SearchIndexer("/home/lucenetest/index");
//取出上次更新時(shí)間
String str = Util.readTxtFile(lastUpdate);
if(str==null || str.length()==0){
str = new java.util.Date().toString();
}
searchIndexer.updateIndex(str);
//寫(xiě)入當前時(shí)間
Util.writeTxtFile(lastUpdate,new java.util.Date(),false);
}
}
寫(xiě)個(gè)cmd或者sh在相應操作系統下面定時(shí)執行SearchIndexer就可以了。
新版本Blog中有更多內容
Copyright (C)2002-2005 All Rights Reserved Powered By:ZhangLiHai.Com
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Net Core使用Lucene.Net和盤(pán)古分詞器 實(shí)現全文檢索
現狀及初步應用
lucene.net 3.0.3、結合盤(pán)古分詞進(jìn)行搜索的小例子(分頁(yè)功能)
Lucene5學(xué)習之LuceneUtils工具類(lèi)簡(jiǎn)單封裝
Lucene 3.0 入門(mén)實(shí)例
Lucene版Hello world(世界,你好)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久