Thứ Tư, 16 tháng 11, 2011

PHP Zend search lucene tiếng việt Unicode

PHP zend search lucene tiếng Việt Unicode

I/ Search lucene ?

Mô hình search giống như dữ liệu chính xác (có thể gần giống như google) hay giống như full-text search. Search lucene đã có những phiên bản hỗ trợ với nhiều ngôn ngữ khác nhau. Java, .Net, .PHP các bạn có thể thao khảo thêm trên http://lucene.apache.org hoặc trên wiki http://vi.wikipedia.org/wiki/Lucene,…vv


II/ PHP search lucene

Ở đây tôi dùng thư viện zend_search_lucene, đã hỗ trợ Unicode tiếng Việt khá tốt.
Cũng như các ngôn ngữ khác zend search lucene gỗm có 2 phần
1/ Phần index (công việc phân tích đánh dấu chỉ mục tài liệu)
2/ Phần search (công việc tìm kiếm trên các file dữ liệu mà bạn đã phân tích ở phần 1)

III/ Hướng dẫn cách sử dụng zend search lucene

Các bạn download thư viện zend trên trang của zend, hiện tại tôi đang dùng zend framework version 1.10.5 http://framework.zend.com/download/webservices

Nếu project của bạn sử dụng zend framework thì không cần bàn thêm, ngược lại nếu bạn sử dụng framework khác thì bạn tìm cách load thư viện zend phù hợp với framework mà bạn đang dùng, và bạn cũng có thể bỏ bớt một số libraries không cần thiết (Acl, Amf, Cloud, CodeGenerator, Console, ..vv). Nhưng nhớ phải giữ lại các libraries liên quan zend_search_luene (Application, Controller, Date, Feed, File, Loader, Locale, Memory, Mime, Search, Server, Service, Soap, Text) Còn lại có xóa bỏ bớt cho cho nhẹ host.

1/ Phần 1 index
Ví dụ:


$arr_data = array() // là dữ liệu bạn cần index để sử dụng cho phần 2 ‘searching’. Bạn có thể lấy từ CSDL MySQL.


$lucene_index_path = ‘c:\xampp\htdocs\my_project\lucene_index'; (Window & Xampp)
//$lucene_index_path = ‘/var/home/my_project/lucene_index'; (Host linux)


$index = new Zend_Search_Lucene($lucene_index_path, true);
Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());


foreach($arr_data as $test) {
//create an cache index doc
$doc = new Zend_Search_Lucene_Document();
$doc->addField(Zend_Search_Lucene_Field::Keyword('prId', $test->prId, 'UTF-8'));
$doc->addField(Zend_Search_Lucene_Field::Text('products', $test->products, 'UTF-8'));
$doc->addField(Zend_Search_Lucene_Field::Text('product_desc', $test->product_desc, 'UTF-8'));
$doc->addField(Zend_Search_Lucene_Field::Keyword('lastUpdate', $test->lastUpdate, 'UTF-8'));


$index->addDocument($doc);
}


$index->commit();
$index->optimize();
echo $index->count().' Documents indexed.';

Tại sao lại dùng Keywor, Text, … các bạn tham khảo table bên dưới:

Field Type
Stored
Indexed
Tokenized
Binary
Keyword
yes
yes
no
no

UnIndexed
yes
no
no
no

Binary
yes
no
no
yes

Text
yes
yes
yes
no

UnStored
no
yes
yes
no


Hoặc xem ở thêm tại Link sau: http://darksleep.com/lucene/,

- $test->lastUpdate: là dự liệu kiểu số tôi tính toán từ cơ sở dữ liệu MySQL
// SELECT DATEDIFF(CURRENT_DATE(), product_date_add) AS lastUpdate FROM products,
Tôi thêm lastUpdate để làm ví dụ sort cho zend lucene search ở phần 2 (searching)

2/ Phần 2 searching
Ví dụ:

$lucene_index_path = ‘c:\xampp\htdocs\my_project\lucene_index'; (Window & Xampp)
//$lucene_index_path = ‘/var/home/my_project/lucene_index'; (Host linux)


Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8Num_CaseInsensitive());
Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('UTF-8');
$index = Zend_Search_Lucene::open($lucene_index_path);


$query = ‘products: Sản phẩm’;


$hits = $index->find($query, 'lastUpdate', SORT_NUMERIC, SORT_ASC);
$totals = count($hits); //total rows


foreach ($hits as $hit){
print_r($hit);
//echo $hit->prId;
//$hit->products;
//$hit->product_desc;
//$hit->lateUpdate;
}

Có một vấn đề không biết có phải host share bị giới hạn execute_time. Nếu dữ liệu tìm thấy quá lớn thì zend_search_lucene không thể chạy. Trường hợp này bạn phải sử dụng thêm chức năng giới hạn.

setResultSetLimit // đặt set limit ngay sau khi lệnh Open

Zend_Search_Lucene::open(‘’);
Zend_Search_Lucene::setResultSetLimit(350);


nhantam
PHP Developer



tag: php search lucene, zend lucene search, thuat toan tim kiem

Không có nhận xét nào: