rapidwebltd / search
搜索功能允许您轻松将智能搜索引擎添加到您的网站或Web应用程序。它可以配置为搜索任何数据库表。
v2.0.2
2018-01-30 15:49 UTC
Requires
- illuminate/support: ^5.1
- psr/cache: ^1.0
- rapidwebltd/uxdm: ^1.17.4
This package is auto-updated.
Last update: 2024-09-06 09:24:24 UTC
README
搜索功能允许您轻松将智能搜索引擎添加到您的网站或Web应用程序。它可以配置为搜索任何数据库表。
安装
您可以使用Composer安装此包。
composer require rapidwebltd/search
使用方法
使用Search非常简单。请看以下示例。
use \RapidWeb\Search\Search; // Setup your database connection. // If you already have a connection setup, you can skip this step. $pdo = new PDO('mysql:dbname=database_name;host=127.0.0.1', 'username', 'password'); // Create a new Search object $search = new Search; // Configure the Search object $search->setDatabaseConnection($pdo) ->setTable('products') ->setPrimaryKey('product_groupid') ->setFieldsToSearch(['product_name', 'product_description', 'product_seokeywords']) ->setConditions(['product_live' => 1]); // Perform a search for 'test product', limited to top 10 results $results = $search->query('test product', 10); // Output results var_dump($results);
结果以SearchResults
对象返回,如下所示,包含一系列SearchResult
对象。此SearchResults
对象还包含各种统计数据,例如最高、最低和平均相关性,以及搜索所需的时间。
数组中的每个SearchResult
对象都提供了主键id
和其relevance
。relevance
是一个简单的数字,对于更相关的结果更高。数组按相关性降序排序。
object(RapidWeb\Search\SearchResults)#731 (5) { ["results"]=> array(10) { [0]=> object(RapidWeb\Search\SearchResult)#588 (2) { ["id"]=> int(80) ["relevance"]=> float(637.80198499153) } /** ... snipped ... */ [9]=> object(RapidWeb\Search\SearchResult)#597 (2) { ["id"]=> int(18469) ["relevance"]=> float(121.65783596237) } } ["highestRelevance"]=> float(637.80198499153) ["lowestRelevance"]=> float(121.65783596237) ["averageRelevance"]=> float(336.74613218217) ["time"]=> float(0.33661985397339) }
缓存源数据
为了加速搜索,您可以使用任何PSR-6兼容的缓存池来缓存源数据。以下是一个示例。
// Create cache pool $filesystemAdapter = new Local(storage_path().'/search-cache/'); $filesystem = new Filesystem($filesystemAdapter); $cacheItemPool = new FilesystemCachePool($filesystem); // Set cache expiry time $cacheExpiryInSeconds = 300; // Create a new Search object $search = new Search; // Configure the Search object $search->setDatabaseConnection($pdo) ->setTable('products') ->setPrimaryKey('product_groupid') ->setFieldsToSearch(['product_name']) ->setCache($cacheItemPool, $cacheExpiryInSeconds); // Setup cache