用 PHP 構建自定義搜尋引擎3

2021-04-15 02:35:26 字數 4210 閱讀 3999

構建和測試索引

您現在已經準備好為 body parts 應用程式構建索引。為此,需要執行以下步驟:

鍵入 $ sudo mkdir -p /var/data/sphinx 建立目錄結構 /var/data/sphinx

假定 mysql 正在執行,使用如下所示的**執行索引器來建立索引。

清單 10. 建立索引

$ sudo /usr/local/bin/indexer --config /usr/local/etc/sphinx.conf --all

sphinx 0.9.7

using config file '/usr/local/etc/sphinx.conf'...

indexing index 'catalog'...

collected 8 docs, 0.0 mb

sorted 0.0 mhits, 82.8% done

total 8 docs, 149 bytes

total 0.010 sec, 14900.00 bytes/sec, 800.00 docs/sec

注: -all 引數將重構 sphinx.conf 中列出的所有索引。如果不需要重構所有索引,您可以使用其他引數只對部分索引進行重構。

您現在可以使用如下所示的**用 search 實用程式測試索引(不必執行 searchd 即可使用 search)。

清單 11. 用 search 測試索引

$ /usr/local/bin/search --config /usr/local/etc/sphinx.conf eng

sphinx 0.9.7

index 'catalog': query 'eng ': returned 2 matches of 2 total in 0.000 sec

displaying matches:

1. document=8, weight=1, assembly=5, model=7

id=8

partno=eng088

descrīption=cylinder head

price=55

2. document=9, weight=1, assembly=5, model=3

id=9

partno=eng976

descrīption=large cylinder head

price=65

words:

1. 'eng': 2 documents, 2 hits

$ /usr/local/bin/search --config /usr/local/etc/sphinx.conf wind

sphinx 0.9.7

index 'catalog': query 'wind ': returned 2 matches of 2 total in 0.000 sec

displaying matches:

1. document=1, weight=1, assembly=3, model=1

id=1

partno=win408

descrīption=portal window

price=423

2. document=5, weight=1, assembly=3, model=1

id=5

partno=win958

descrīption=windshield, front

price=500

words:

1. 'wind': 2 documents, 2 hits

$ /usr/local/bin/search /

--config /usr/local/etc/sphinx.conf --filter model 3 eng

sphinx 0.9.7

index 'catalog': query 'eng ': returned 1 matches of 1 total in 0.000 sec

displaying matches:

1. document=9, weight=1, assembly=5, model=3

id=9

partno=eng976

descrīption=large cylinder head

price=65

words:

1. 'eng': 2 documents, 2 hits

第一條命令 /usr/local/bin/search --config /usr/local/etc/sphinx.conf eng 在零件號中找到了兩個含有 eng 的結果。第二條命令 /usr/local/bin/search --config /usr/local/etc/sphinx.conf wind 在兩個零件描述中找到了子字串 wind。而第三條命令把結果限定為 model 為 3 的條目。

編寫**

最後,您可以編寫 php **來呼叫 sphinx 搜尋引擎。sphinx php api 非常小並且易於掌握。清單 12 是乙個小型 php 應用程式,用於呼叫 searchd 以得到使用上面所示的最後一條命令得到的相同結果(「在屬於型號 3 的名稱中找到含有 『cylinder』 的所有零件」)。

清單 12. 從 php 呼叫 sphinx 搜尋引擎

<?php

include('sphinx-0.9.7/api/sphinxapi.php');

$cl = new sphinxclient();

$cl->setserver( "localhost", 3312 );

$cl->setmatchmode( sph_match_any );

$cl->setfilter( 'model', array( 3 ) );

$result = $cl->query( 'cylinder', 'catalog' );

if ( $result === false )

else

if ( ! empty($result["matches"]) )

print_r( $result );}}

exit;

?>要測試**,需要為 sphinx 建立 log 目錄,啟動 searchd,然後執行 php 應用程式,如下所示:

清單 13. php 應用程式

$ sudo mkdir -p /var/log/searchd

$ sudo /usr/local/bin/searchd --config /usr/local/etc/sphinx.conf

$ php search.php

9array (

[fields] => array (

[0] => partno

[1] => descrīption

)[attrs] => array(

[assembly] => 1

[model] => 1

)[matches] => array(

[9] => array(

[weight] => 1

[attrs] => array(

[assembly] => 5

[model] => 3))

)[total] => 1

[total_found] => 1

[time] => 0.000

[words] => array(

[cylind] => array(

[docs] => 2

[hits] => 2))

)注意事項:total_found 是在索引中找到的匹配總數,而 found 是返回的結果數。這兩者可能不同,因為您可以更改每次返回多少個匹配結果以及要返回哪批匹配結果,哪個結果利於對冗長的結果列表分頁。請檢視 api 呼叫 setlimits()。乙個分頁示例是用 $cl->setlimits( ( $page - 1 ) * span, span ) 呼叫搜尋引擎返回第一批、第二批、第三批(依此類推)span 匹配結果,這取決於顯示哪個頁面。

結束語sphinx 還有更多的功能可以利用。我在這裡僅僅介紹了最淺顯的一部分,但是您現在有乙個可以工作的現實示例作為基石來擴充套件您的技能。

仔細研讀隨發行版附帶的樣例 sphinx 配置檔案 /usr/local/etc/sphinx.conf.dist。該檔案中的注釋將說明每個 sphinx 引數可以實現的功能;展示如何建立分布式冗餘配置;並說明如何繼承基本設定以避免源**及索引中的重複。sphinx readme 檔案還是十分豐富的資訊源,包括如何將 sphinx 直接嵌入 mysql v5 —— 不需要使用守護程式。

PHP搜尋引擎

簡單php搜尋引擎源 需要開啟php的curl擴充套件。功能 對某一 進行檢索,獲取 基本資訊,同時提取 的所有連線。class engine 啟動引擎 public function start 獲取meta內容 public function getmeta content 獲取body內容 p...

案例 如何利用Google自定義搜尋引擎賺錢

網路上到處亂逛,發現乙個叫 food blog search 的英文 其創意之精巧,令人嘆為觀止。原理非常簡單 第一步 利用google custom search engine 自定義搜尋引擎 功能,建立乙個搜尋引擎 第二步 針對特定的領域,尋找優質的內容源,把他們加入該搜尋引擎的索引源 第三,掛...

用C 實現FTP搜尋引擎

晚輩最近用c 寫了乙個教育網ftp搜尋引擎,希望能得到高手的指點。部分 using system using softplib using system.threading using system.collections using system.net using system.net.sock...