close
Amazon CloudSearch
Info
-
AWS 的雲端搜尋服務
Installation
-
Create a New Domain
-
NAME YOUR DOMAIN
-
Search Domain Name, 設定名稱
-
Desired Instance Type, 設定機器等級,預設 small
-
Desired Replication Count, 設定 Replication 數量,預設 1
-
-
CONFIGURE INDEX
-
Manual configuration, 手動設定
-
-
REVIEW INDEX CONFIGURATION
-
這邊可以設定 index,除了 Name 以外,必須決定欄位的 Type
-
一般文字設定 text 即可,如果有多個單詞時(ex. 分類名稱)可以設定 text-array
-
另外 literal 也是一種文字類型,但是大小寫敏感,而且之後可以 group
-
其他類型請參考文件
-
之後可以透過程式建立,所以也可以略過不建立
-
-
SETUP ACCESS POLICIES
-
設定權限,建議選擇 “Allow access to all services from specific IP(s)“,設定允許對外的 IP,多個 IP 時以逗號分隔
-
-
CONFIRM
-
最後確認並完成建立,建立大概會需要 10 分鐘
-
Usage
-
實際操作需要透過 AWS SDK,PHP 請參考「初探 Amazon CloudSearch (使用當 PHP 範例)」
Limit
-
使用 Amazon CloudSearch 有一些限制,請參考說明文件
-
參數 size, start 分頁時常使用,要注意的是 start 最多不可超過 10000;也就是透過這二個參數取得的資料最多只有前 10000 筆
-
要取得 10000 筆以後的資料必須透過 cursor 取得
Laravel Package
Info
-
整合 Laravel 與 Amazon CloudSearch,安裝流程請參考 GitHub 說明
Settings
-
.env 設定參數
- /project/.env
-
AWS_KEY=xxx AWS_SECRET=xxx # 之前建立的 Domain Name CLOUDSEARCH_DOMAIN=xxx # Dashboard 顯示的 Document Endpoint,必須包含 http CLOUDSEARCH_ENDPOINT=http://doc-xxx-xxx-xxx.us-west-2.cloudsearch.amazonaws.com # 不太清楚這個設定是什麼,我是填 AWS 區域 CLOUDSEARCH_REGION=us-west-2
-
config/cloud-search.php 設定
- /project/config/cloud-search.php
-
// 修改從 .env 取得 'domain_name' => env('CLOUDSEARCH_DOMAIN'), // 設定 index 欄位 Name 與 Type 'fields' => [ 'title' => 'text', 'author' => 'text-array', 'type' => 'literal', ], // 設定 Eloquent 所在位置,ex. App/Models/Book.php 'model_namespace' => '\\App\\Models',
-
透過 Command 建立 index
php artisan search:fields
-
設定 Eloquent
- /project/app/Models/Book.php
-
<?php namespace App\Models; use LaravelCloudSearch\Eloquent\Searchable; class Book extends Model { // 使用 Searchable use Searchable; // 必須實作這個方法,回傳 index 的值 public function getSearchDocument() { return [ 'title' => $this->title, 'author' => $this->authors->pluck('name')->toArray(), 'type' => $this->type, ]; } }
-
建立 index
php artisan search:index Book
Searching
-
最基本的搜尋
// search "Kitten fluff"
$books = App\Models\Book::search('Kitten fluff')->get();
要留意的是基本搜尋只會找 Type 是 text, text-array 的欄位,如果需要尋找其他 Type 的欄位必須自建 searchBuilder
-
新增額外設定,其他參數請參考文件
// search "Kitten fluff", default operator "or", add fields and weight
$books = App\Models\Book::searchBuilder()
->options('defaultOperator', 'or')
->options('fields', [
'type^10',
'title^5',
'author',
])->term('Kitten fluff')->get();
Reference
文章標籤
全站熱搜