实际程序调用IndexWriter* writer = NULL
最近由于項目需要,原先用MySQL實現的全文檢索功能已不能滿足需求,于是乎想到了利用開源的全文檢索項目,經人介紹看看了Lucene,Lucene使用Java實現的,目前是Apache Jakarta的項目之一,已經有一套完整和成熟的庫,可惜本人對Java懂之甚少,便找了個C++ porting的版本,看了看里面的demo,動手體驗了一下,感覺用“短小、強悍”形容再恰當不過了,十幾行代碼便能完成建立索引和進行搜索:
建立索引:
IndexWriter* writer = NULL;
lucene::analysis::standard::StandardAnalyzer an;
// 其中target是要建立的索引名
char* target = "index_name";
writer =?new IndexWriter( target ,&an, true);
Document* doc =?new Document();
doc->add( *Field::Text(_T("author"), _T("abc")) );
writer->addDocument( doc );
writer->optimize();
writer->close();
搜索:
char* index = "index_name";
char* searchfor= "abc";
IndexSearcher s(index);
standard::StandardAnalyzer analyzer;
// searchfor 為需要搜索的字符串
Query* q = QueryParser::parse(searchfor, _T("author"), &analyzer);
// 返回匹配的結果集
Hits* h = s.search(q);
// 遍歷結果集
for ( int32_t i=0;i<h->length();i++ ){
//? 毎一條結果都是一個Document對象,引用也很方便
Document* doc = &h->doc(i);
// 結果是根據匹配度排序的:
h->score(i);
}
?
當然,更多的代碼能實現更多的功能,如Filter, Sort等都是Lucene中的功能,對于一般的中小型應用來說,已經足夠了。另外建立索引和搜索的速度,目前還沒有大規模的測試,從已用過的反饋來看都還是不錯的。
CLucene目前的版本還不支持中日韓等Unicode字符集,關于Lucene如何支持Unicode字符集,正在研究中,后面會與大家分享。
下面是關于Lucene的一些資源
Lucene項目主頁:
http://jakarta.apache.org/lucene/
Lucene教程:
http://www.darksleep.com/puff/lucene/lucene.html
Lucene的.NET實現:dotLucene
http://sourceforge.net/projects/dotlucene/
Lucene作者Cutting的幾篇論文和專利
http://lucene.sourceforge.net/publications.html
CLucene API 文檔:
http://clucene.sourceforge.net/doc/html/
轉載于:https://www.cnblogs.com/cy163/archive/2008/06/09/1216424.html
總結
以上是生活随笔為你收集整理的实际程序调用IndexWriter* writer = NULL的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 超级日期控件
- 下一篇: 解决启动Oracle9i的OEM或OMS