打开文件夹找到kindeditor-min.js文件,搜索pasteType函数,默认值是2.设置为1即可。
设置粘贴类型,0:禁止粘贴, 1:纯文本粘贴, 2:HTML粘贴。
打开文件夹找到kindeditor-min.js文件,搜索pasteType函数,默认值是2.设置为1即可。
设置粘贴类型,0:禁止粘贴, 1:纯文本粘贴, 2:HTML粘贴。
全文搜索对于允许用户浏览内容丰富的网站至关重要。 在这篇文章中,我将向您展示如何为Laravel应用实现全文搜索。 实际上,我们将使用Laravel Scout库,该库使实现全文搜索变得轻松而有趣。
Laravel Scout到底是什么? 官方文档总结如下:
Laravel Scout提供了一个简单的基于驱动程序的解决方案,用于向Eloquent模型添加全文搜索。 通过使用模型观察器,Scout将自动使您的搜索索引与您的口才记录保持同步。基本上,Laravel Scout是一个库,它在模型数据发生更改时管理索引的操纵。 将为数据建立索引的位置取决于使用Scout库配置的驱动程序。
到目前为止,Scout库支持基于云的搜索引擎API Algolia,这就是我们将在本文中用来演示全文搜索实现的内容。
我们将从安装Scout和Algolia服务器库开始,并且在继续进行过程中,我们将通过一个真实的示例来演示如何索引和搜索数据。
服务器配置
在本节中,我们将安装使Scout库与Laravel一起使用所需的依赖项。 安装后,我们需要进行大量配置,以便Laravel可以检测Scout库。
让我们继续使用Composer安装Scout库。
$composer require laravel/scout
就Scout库安装而言,仅此而已。 现在我们已经安装了Scout库,让我们确保Laravel知道它。
使用Laravel,您可能已经了解了服务提供者的概念,它使您可以在应用程序中配置服务。 因此,每当您想在Laravel应用程序中启用新服务时,只需在
config/app.php
添加相关的服务提供者条目。在我们的例子中,我们只需要将
ScoutServiceProvider
提供程序添加到config/app.php
中的服务提供程序列表config/app.php
,如以下代码片段所示。... ... 'providers' => [ /* * Laravel Framework Service Providers... */ Illuminate\Auth\AuthServiceProvider::class, Illuminate\Broadcasting\BroadcastServiceProvider::class, Illuminate\Bus\BusServiceProvider::class, Illuminate\Cache\CacheServiceProvider::class, Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class, Illuminate\Cookie\CookieServiceProvider::class, Illuminate\Database\DatabaseServiceProvider::class, Illuminate\Encryption\EncryptionServiceProvider::class, Illuminate\Filesystem\FilesystemServiceProvider::class, Illuminate\Foundation\Providers\FoundationServiceProvider::class, Illuminate\Hashing\HashServiceProvider::class, Illuminate\Mail\MailServiceProvider::class, Illuminate\Notifications\NotificationServiceProvider::class, Illuminate\Pagination\PaginationServiceProvider::class, Illuminate\Pipeline\PipelineServiceProvider::class, Illuminate\Queue\QueueServiceProvider::class, Illuminate\Redis\RedisServiceProvider::class, Illuminate\Auth\Passwords\PasswordResetServiceProvider::class, Illuminate\Session\SessionServiceProvider::class, Illuminate\Translation\TranslationServiceProvider::class, Illuminate\Validation\ValidationServiceProvider::class, Illuminate\View\ViewServiceProvider::class, /* * Package Service Providers... */ Laravel\Tinker\TinkerServiceProvider::class, /* * Application Service Providers... */ App\Providers\AppServiceProvider::class, App\Providers\AuthServiceProvider::class, App\Providers\BroadcastServiceProvider::class, App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, Laravel\Scout\ScoutServiceProvider::class, ], ... ...
现在,Laravel知道了
ScoutServiceProvider
服务提供者。 Scout库带有一个配置文件,该文件允许我们设置API凭据。让我们继续使用以下命令发布Scout库提供的资产。
$ php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider" Copied File [/vendor/laravel/scout/config/scout.php] To [/config/scout.php] Publishing complete.
如您所见,它已将
vendor/laravel/scout/config/scout.php
文件复制到config/scout.php
。接下来,继续并在Algolia创建一个帐户,因为我们首先需要API凭据。 获得API信息后,让我们继续在
config/scout.php
文件中配置必要的设置,如以下代码片段所示。<?php return [ /* |-------------------------------------------------------------------------- | Default Search Engine |-------------------------------------------------------------------------- | | This option controls the default search connection that gets used while | using Laravel Scout. This connection is used when syncing all models | to the search service. You should adjust this based on your needs. | | Supported: "algolia", "null" | */ 'driver' => env('SCOUT_DRIVER', 'algolia'), /* |-------------------------------------------------------------------------- | Index Prefix |-------------------------------------------------------------------------- | | Here you may specify a prefix that will be applied to all search index | names used by Scout. This prefix may be useful if you have multiple | "tenants" or applications sharing the same search infrastructure. | */ 'prefix' => env('SCOUT_PREFIX', ''), /* |-------------------------------------------------------------------------- | Queue Data Syncing |-------------------------------------------------------------------------- | | This option allows you to control if the operations that sync your data | with your search engines are queued. When this is set to "true" then | all automatic data syncing will get queued for better performance. | */ 'queue' => env('SCOUT_QUEUE', false), /* |-------------------------------------------------------------------------- | Chunk Sizes |-------------------------------------------------------------------------- | | These options allow you to control the maximum chunk size when you are | mass importing data into the search engine. This allows you to fine | tune each of these chunk sizes based on the power of the servers. | */ 'chunk' => [ 'searchable' => 500, 'unsearchable' => 500, ], /* |-------------------------------------------------------------------------- | Soft Deletes |-------------------------------------------------------------------------- | | This option allows you to control whether to keep soft deleted records in | the search indexes. Maintaining soft deleted records can be useful | if your application still needs to search for the records later. | */ 'soft_delete' => false, /* |-------------------------------------------------------------------------- | Algolia Configuration |-------------------------------------------------------------------------- | | Here you may configure your Algolia settings. Algolia is a cloud hosted | search engine which works great with Scout out of the box. Just plug | in your application ID and admin API key to get started searching. | */ 'algolia' => [ 'id' => env('ALGOLIA_APP_ID', 'STQK4DEGMA'), 'secret' => env('ALGOLIA_SECRET', '6ef572194f70201ed7ad102cc9f90e05'), ], ];
请注意,我们已将
SCOUT_DRIVER
的值设置为algolia
驱动程序。 因此,需要在文件末尾配置Algolia驱动程序的必要设置。 基本上,您只需要设置从Algolia帐户获得的id
和secret
即可。如您所见,我们正在从环境变量中获取值。 因此,请确保在
.env
文件中正确设置以下变量。... ... ALGOLIA_APP_ID=STQK4DEGMA ALGOLIA_SECRET=6ef572194f70201ed7ad102cc9f90e05 ... ...
最后,我们需要安装Algolia PHP SDK,该SDK将用于使用API与Algolia进行交互。 让我们使用以下代码片段所示的作曲器进行安装。
$composer require algolia/algoliasearch-client-php
至此,我们已经安装了所有必要的依赖关系,以便将数据发布和索引到Algolia服务。
使模型可索引和可搜索
在上一节中,我们进行了所有艰苦的工作来设置Scout和Algolia库,以便我们可以使用Algolia搜索服务对数据进行索引和搜索。
在本节中,我们将通过一个示例演示如何对现有数据建立索引并从Algolia检索搜索结果。 我假设您的应用程序中有一个默认的
Post
模型,我们将在示例中使用该模型。我们需要做的第一件事是将
Laravel\Scout\Searchable
特征添加到Post
模型。 这使得Post
模型可以搜索; 每次添加,更新或删除帖子记录时,Laravel都会将帖子记录与Algolia索引同步。<?php namespace App; use Illuminate\Database\Eloquent\Model; use Laravel\Scout\Searchable; class Post extends Model { use Searchable; /** * The attributes that should be mutated to dates. * * @var array */ protected $dates = [ 'created_at', 'updated_at' ]; }
这样,
Post
模型就变得易于搜索!接下来,我们要配置首先应建立索引的字段。 当然,您不想在Algolia中为模型的所有字段建立索引,以使其保持有效且轻巧。 实际上,您通常不需要它。
您可以在模型类中添加
toSearchableArray
,以配置将被索引的字段。/** * Get the indexable data array for the model. * * @return array */ public function toSearchableArray() { $array = $this->toArray(); return array('id' => $array['id'],'name' => $array['name']); }
现在,我们准备将现有的
Post
记录导入并建立索引到Algolia中。 实际上,Scout库通过提供以下工匠命令使此操作变得容易。$php artisan scout:import "App\Post"
那应该一次性导入
Post
模型的所有记录! 它们在导入后就立即建立索引,因此我们已经准备好查询记录。 继续浏览Algolia仪表板,以查看导入的记录和其他实用程序。总共如何运作
在本节中,我们将创建一个示例,演示如何执行与Algolia索引实时同步的搜索和CRUD操作。
继续并使用以下内容创建
app/Http/Controllers/SearchController.php
文件。<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use App\Post; class SearchController extends Controller { public function query() { // queries to Algolia search index and returns matched records as Eloquent Models $posts = Post::search('title')->get(); // do the usual stuff here foreach ($posts as $post) { // ... } } public function add() { // this post should be indexed at Algolia right away! $post = new Post; $post->setAttribute('name', 'Another Post'); $post->setAttribute('user_id', '1'); $post->save(); } public function delete() { // this post should be removed from the index at Algolia right away! $post = Post::find(1); $post->delete(); } }
当然,我们还需要添加关联的路由。
Route::get('search/query', 'SearchController@query'); Route::get('search/add', 'SearchController@add'); Route::get('search/delete', 'SearchController@delete');
让我们看一下
query
方法,看看如何在Algolia中执行搜索。public function query() { // queries to Algolia search index and returns matched records as Eloquent Models $posts = Post::search('title')->get(); // do the usual stuff here foreach ($posts as $post) { // ... } }
回想一下,我们通过添加
Searchable
特征使Post
模型可搜索。 因此,Post
模型可以使用search
方法从Algolia索引中检索记录。 在上面的示例中,我们试图获取与title
关键字匹配的记录。接下来,有一个
add
方法模仿添加新帖子记录的工作流程。public function add() { // this post should be indexed at Algolia right away! $post = new Post; $post->setAttribute('name', 'Another Post'); $post->setAttribute('user_id', '1'); $post->save(); }
上面的代码没什么花哨的; 它只是使用
Post
模型创建一个新的post记录。 但是Post
模型实现了Searchable
特性,因此Laravel这次通过索引在Algolia中新创建的记录来做一些额外的工作。 如您所见,索引是实时完成的。最后是
delete
方法。 让我们也经历一下。public function delete() { // this post should be removed from the index at Algolia right away! $post = Post::find(1); $post->delete(); }
如您所料,一旦从数据库中删除该记录,就会立即从Algolia索引中删除该记录。
基本上,如果您想使现有模型可搜索,则无需您费力。 一切都由Scout库使用模型观察器处理。
这也将我们带到了本文的结尾!
结论
今天,我们讨论了如何使用Laravel Scout库在Laravel中实现全文搜索。 在此过程中,我们通过必要的安装和一个实际示例进行了演示。
请使用下面的评论供稿随意询问您是否有任何疑问或疑问!
在日常工作中,经常会需要编辑文字内容,相信很多朋友都和我一样,喜欢用txt来编辑,因为这样编辑的文字是不带任何格式的,在其它地方编辑起来也会方便许多。之前使用Windows电脑时,只要右键就能新建txt文档文件,刚换Mac时,还真找不到新建txt文档文件的入口,本篇文章就为大家介绍如何设置新建txt文档文件的快捷键。
第一种方法:
通常可以直接用Mac的搜索工具Spotlight或Alfred搜索TextEdit,打开文本编辑应用程序即可创建txt文档,若打开文本编辑时出现下方界面,可以通过修改格式来转为txt文件。
修改入口在顶部菜单栏的格式下拉框中,选择“制作纯文本”选项即可。
第二种方法:
首先打开Mac的“自动操作”应用程序,可以用搜索工具Spotlight或Alfred直接搜索Automator。
文稿类型选择“快速操作”后,点击“选取”按钮。
工作流程收到选项选择“没有输入”,位置选项选择“访达.app”。
然后在左侧列表中选择 “实用工具” > “运行AppleScript”,将其拖到右侧空白处。
将里面的代码全部删掉,粘贴下方的代码后,点击黑色三角形运行,然后关闭“自动操作”应用程序,按指示保存文件即可。
on run {input, parameters}
tell application "Finder"
set selection to make new file at (get insertion location)
end tell
return input
end run
设置完成后,就可以在顶部菜单栏的访达下拉框中的服务选项下找到新建txt的入口了。
最后打开系统偏好设置,在键盘设置中设置好新建txt的快捷键,就完成所有的操作了。
有时我们打开一个PDF文档后,由于页面较多,为了快速查看某一内容,使用查找关键字快速定位是比较方便的,那么应该如何操作才能进行查找呢?
首先用极速PDF编辑器打开文档后,直接使用快捷键“Ctrl+F”就能打开查找设置窗口。
在“查找”输入框输入需要查找的关键字即可;点击右侧“字符格式化”会弹出“查找格式”设置窗口,可根据需求进行字体、字号、填充等更精确的设定来定位查找。
点击在“当前文档所有页面”蓝色字体,可修改搜索的页面范围,根据需求进行相应设置后,点击“确认”。(默认是搜索整个文档所有页面)
另外还可根据需求选择“全词匹配”/”区分大小写”/”使用通配符”等进行更精确的条件查找,可同时勾选多项,设置完成后点击键盘“enter”开始查找,或设置窗口右侧“查找下一个”或“查找全部”均可。页面会自动跳转,并将查找的关键字黑色高亮显示。