課程編程項目2,第2組的學校項目,專業單身漢在Erasmumshogeschool Brussel上進行了“應用”。
搜索引擎申請保險文件。保險公司在其特定領域中存儲有關立法,法學和法律學說的文件。目的是根據Elasticsearch框架的算法為員工提供易於使用的搜索引擎應用程序。
| 成分 | 版本 |
|---|---|
| Linux Ubuntu | |
| fscrawler | |
| Elasticsearch | 6.8 |
| Elasticsearch-PHP | 6.7 |
| php | 7.4.10(CLI) |
| 作曲家 | 2.0.6 |
| Laravel安裝程序 | 4.1.0 |
| mysql |
注意: FSCrawler VXXXX僅與Elasticsearch 6.8兼容。因此,在您的laravel Composer.json文件中需要包裝eLasticsearch-php v6.7
完整的文檔可以在此處找到。文檔存儲在repo /docs /中,因此,如果您看到錯別字或問題,請提交PR修復它!
我們還使用util/GenerateDocExamples.php腳本提供了用於PHP的代碼示例生成器。此命令解析此json規範產生的util/alternative_report.spec.json文件,並生成php示例for each摘要值。這些示例以docs/examples文件夾的形式以Asciidoc格式存儲。
安裝Elasticsearch-PHP的推薦方法是通過作曲家。
將elasticsearch/elasticsearch添加為項目的composer.json文件中的依賴項(例如,更改適合您的Elasticsearch版本的版本,例如ES 7.0):
{
"require" : {
"elasticsearch/elasticsearch" : " ^7.0 "
}
}下載並安裝作曲家:
curl -s http://getcomposer.org/installer | php安裝您的依賴項:
php composer.phar install需要作曲家的自動加載器
作曲家還準備一個自動加載文件,該文件能夠自動加載其下載的任何庫中的所有類。要使用它,只需將以下行添加到您的代碼的引導過程中:
<?php
use Elasticsearch ClientBuilder ;
require ' vendor/autoload.php ' ;
$ client = ClientBuilder:: create ()-> build ();您可以在如何安裝作曲家,配置自動加載以及其他最佳實踐方面找到有關在getComposer.org上定義依賴項的更多信息。
該庫的7.0版至少需要PHP版本7.1。此外,它要求本機JSON擴展名為1.3.7版或更高版本。
| Elasticsearch-PHP分支 | PHP版本 |
|---|---|
| 7.0 | > = 7.1.0 |
| 6.0 | > = 7.0.0 |
| 5.0 | > = 5.6.6 |
| 2.0 | > = 5.4.0 |
| 0.4,1.0 | > = 5.3.9 |
由於InsuraQuest使用Laravel Jetstream,因此包括登錄,註冊,電子郵件驗證,兩因素身份驗證和會話管理。 Jetstream使用Laravel Fortify,這是Laravel的前端不可知論身份驗證後端。
在config/fortify.php配置文件中,您可以自定義不同方面,請選擇要在項目等上實現的方面。
可以根據授權請求執行的邏輯,可以在App Action Fortify中找到和修改。
有關Jetstream的更多信息和文檔,請參見Jetstream網站。
InsuraQuest通過屬性“類型”實現授權,該屬性包含在每個用戶啟示中。有四種類型:來賓,用戶,圖書館員和管理員。類型是級聯。每個新級別具有以下級別的權限 +其他權限。
授權在不同的路線(web.php)上執行。在混合視圖方面,還通過實現本機Laravel @Can和@Cannot來在視圖級上執行。
可以直接在數據庫中或使用AdminActCount簽名時在數據庫中進行調整。
當訪客尚未簽約時,他將被重新安排到登錄屏幕上。默認情況下 - 新用戶註冊時 - 他將分配“訪客”類型。他將能夠看到著陸頁和文檔,但無法查詢任何文件。用戶可以查詢文檔,打開它們並郵寄。圖書館員可以上傳新文件,刪除文件並更改其上的標籤。管理員可以查看所有用戶,他們的信息並調整其類型。
部署設置的描述。
"external" : {
"properties" : {
"title" : {
"type" : " text "
},
"language" : {
"type" : " keyword "
},
"date_published" : {
"type" : " date "
},
"issuer" : {
"type" : " keyword "
},
"category" : {
"type" : " keyword "
},
"tag" : {
"type" : " keyword "
}
}
}被認為比內置的Laravel服務器更健壯。
server {
listen 80 ;
server_name 10.3.50.7 ;
root /var/www/insuraquest_production/insuraquest/public ;
add_header X-Frame-Options " SAMEORIGIN " ;
add_header X-XSS-Protection " 1; mode=block " ;
add_header X-Content-Type-Options " nosniff " ;
index index.php ;
charset utf-8 ;
location / {
try_files $uri $uri / /index.php ? $query_string ;
}
location = /favicon.ico { access_log off ; log_not_found off ; }
location = /robots.txt { access_log off ; log_not_found off ; }
error_page 404 /index.php ;
location ~ . php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock ;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name ;
include fastcgi_params ;
}
location ~ / . ( ? ! well-known). * {
deny all ;
}
}php artisan migrate:fresh --seedgit init --bare /home/student/insuraquest/bare_project.init #! /bin/bash
# check out the files
git --work-tree=/var/www/insuraquest_production --git-dir=/home/student/insuraquest/bare_project.git checkout -f
chmod +x /path/to/bare_project.git/hooks/post-receivegit remote add live ' [email protected]:/home/student/insuraquest/bare_project.git '
git push --set-upstream live maincomposer update chgrp -R www-data insuraquest_productionphp artisan storage:link在Elasticsearch-PHP中,幾乎所有內容都是由關聯數組配置的。休息端點,文檔和可選參數 - 一切都是關聯數組。
要索引文檔,我們需要指定三個信息:索引,ID和文檔正文。這是通過構造密鑰的關聯數組來完成的:值對。請求主體本身是一個關聯陣列,具有密鑰:值對,與文檔中的數據相對應:
$ params = [
' index ' => ' my_index ' ,
' id ' => ' my_id ' ,
' body ' => [ ' testField ' => ' abc ' ]
];
$ response = $ client -> index ( $ params );
print_r ( $ response );您返回的響應指示該文檔是在您指定的索引中創建的。響應是一個關聯陣列,包含Elasticsearch返回的JSON的解碼版本:
Array
(
[_index] => my_index
[_type] => _doc
[_id] => my_id
[_version] => 1
[result] => created
[_shards] => Array
(
[total] => 1
[successful] => 1
[failed] => 0
)
[_seq_no] => 0
[_primary_term] => 1
)讓我們獲取我們剛剛索引的文檔。這只會返回文檔:
$ params = [
' index ' => ' my_index ' ,
' id ' => ' my_id '
];
$ response = $ client -> get ( $ params );
print_r ( $ response );該響應包含一些元數據(索引,版本等)以及_source字段,這是您發送到Elasticsearch的原始文檔。
Array
(
[_index] => my_index
[_type] => _doc
[_id] => my_id
[_version] => 1
[_seq_no] => 0
[_primary_term] => 1
[found] => 1
[_source] => Array
(
[testField] => abc
)
)如果要直接檢索_source字段,則有getSource方法:
$ params = [
' index ' => ' my_index ' ,
' id ' => ' my_id '
];
$ source = $ client -> getSource ( $ params );
print_r ( $ source );響應將只是_source值:
Array
(
[testField] => abc
)搜索是Elasticsearch的標誌,所以讓我們進行搜索。我們將使用匹配查詢作為演示:
$ params = [
' index ' => ' my_index ' ,
' body ' => [
' query ' => [
' match ' => [
' testField ' => ' abc '
]
]
]
];
$ response = $ client -> search ( $ params );
print_r ( $ response );響應與以前的響應有些不同。我們看到了一些元數據( took , timed_out等)和一個名為hits的陣列。這代表您的搜索結果。 hits的內部是另一個名為hits的數組,其中包含單獨的搜索結果:
Array
(
[took] => 33
[timed_out] =>
[_shards] => Array
(
[total] => 1
[successful] => 1
[skipped] => 0
[failed] => 0
)
[hits] => Array
(
[total] => Array
(
[value] => 1
[relation] => eq
)
[max_score] => 0.2876821
[hits] => Array
(
[ 0 ] => Array
(
[_index] => my_index
[_type] => _doc
[_id] => my_id
[_score] => 0.2876821
[_source] => Array
(
[testField] => abc
)
)
)
)
)好吧,讓我們繼續刪除我們之前添加的文檔:
$ params = [
' index ' => ' my_index ' ,
' id ' => ' my_id '
];
$ response = $ client -> delete ( $ params );
print_r ( $ response );您會注意到這與get語法相同。唯一的區別是操作: delete而不是get 。響應將確認該文檔已刪除:
Array
(
[_index] => my_index
[_type] => _doc
[_id] => my_id
[_version] => 2
[result] => deleted
[_shards] => Array
(
[total] => 1
[successful] => 1
[failed] => 0
)
[_seq_no] => 1
[_primary_term] => 1
)由於Elasticsearch的動態性質,我們添加的第一個文檔自動構建了一個帶有一些默認設置的索引。讓我們刪除該索引,因為我們要以後指定自己的設置:
$ deleteParams = [
' index ' => ' my_index '
];
$ response = $ client -> indices ()-> delete ( $ deleteParams );
print_r ( $ response );回應:
Array
(
[acknowledged] => 1
)現在我們開始新鮮(沒有數據或索引),讓我們添加一個新的索引,並使用一些自定義設置:
$ params = [
' index ' => ' my_index ' ,
' body ' => [
' settings ' => [
' number_of_shards ' => 2 ,
' number_of_replicas ' => 0
]
]
];
$ response = $ client -> indices ()-> create ( $ params );
print_r ( $ response );Elasticsearch現在將使用您選擇的設置創建該索引,並返回確認:
Array
(
[acknowledged] => 1
)圖書館員有可能上傳新文件。上傳文檔時,可以將標籤添加到上傳的文檔中。標籤的內容從MySQL表中拉出並添加到表單中。
FileUploadController.php
$ this ->validate( $ request , [
' title ' => ' required ' ,
' language ' => ' required ' ,
' date ' => ' required|date ' ,
' issuer ' => ' required ' ,
' category ' => ' required ' ,
' tag ' => ' required ' ,
' file ' => ' required|mimes:pdf|max:2048 '
]上傳文檔後,文件和標籤將發佈到fscrawler,該文件將在添加到我們的elasticsearch節點之前索引文檔。
FileUploadController.php
$ file = $ request -> file ( ' file ' );
$ pathname = $ file -> store ( ' public ' );
$ fully_qualified_pathname = storage_path ( ' app/ ' . $ pathname );
$ client = new Client ();
try {
$ client -> request ( ' POST ' , ' http://127.0.0.1:8080/fscrawler/_upload ' ,
);
} catch ( GuzzleException $ e ) {
echo $ e ;
}為形式佈局添加了一個插件 - > tailwind.config.js
https://tailwindcss-custom-forms.netlify.app/
plugins: [
require ( ' @tailwindcss/custom-forms ' ),
]用戶獲得所有搜索結果後,他可以查看有關任何結果的更多詳細信息。
在這裡,他有可能編輯,刪除或郵寄顯示的PDF。
修改或創建的郵件功能文件是
命令使用了用於創建電子郵件的Laravel Mail -Mail Markdown類。
php artisan make:mail EmailInsuraquest --markdown=Email.insuraEmail
郵件控制器,從本質上講,我們將定義具有顯示用戶列表的邏輯。運行命令以創建控制器。
php artisan make:controller MailController
測試電子郵件功能的可能性http:// localhost:8000/send -email->將郵件發送到MailTrap(帳戶BART)
TODO:將郵件功能實現到一個搜索結果中
use GuzzleHttp Ring Client MockHandler ;
use Elasticsearch ClientBuilder ;
// The connection class requires 'body' to be a file stream handle
// Depending on what kind of request you do, you may need to set more values here
$ handler = new MockHandler ([
' status ' => 200 ,
' transfer_stats ' => [
' total_time ' => 100
],
' body ' => fopen ( ' somefile.json ' ),
' effective_url ' => ' localhost '
]);
$ builder = ClientBuilder:: create ();
$ builder -> setHosts ([ ' somehost ' ]);
$ builder -> setHandler ( $ handler );
$ client = $ builder -> build ();
// Do a request and you'll get back the 'body' response above那隻是客戶及其語法的崩潰課程概述。如果您熟悉Elasticsearch,您會注意到這些方法的命名就像rebt端點一樣。
您還會注意到,客戶端的配置方式可以促進通過IDE輕鬆發現。所有核心操作均在$client對象(索引,搜索,獲取等)下可用。索引和集群管理分別位於$client->indices()和$client->cluster()對像下。
查看其余文檔,以了解整個客戶端的工作原理。
請注意,此項目可在學校環境中使用。要進一步發展,請聯繫TE
用戶可以選擇他們希望使用的許可證。由於沒有區分可執行文件或分發捆綁包來區分許可,因此用戶應在庫重新分配的情況下在外部記錄其許可證選擇。如果沒有做出明確的選擇,則假設重新分配遵守這兩個許可的規則。