项目作者: brainnit

项目描述 :
Adonis Scout provides a driver based solution for searching your Lucid models, just like Laravel Scout.
高级语言: JavaScript
项目地址: git://github.com/brainnit/adonisjs-scout.git
创建时间: 2019-02-09T19:16:13Z
项目社区:https://github.com/brainnit/adonisjs-scout

开源协议:MIT License

下载


AdonisJs Scout

Adonis Scout provides a driver based solution for searching your Lucid models, heavily inspired by Laravel Scout and Scout Elasticsearch Driver.

Instalation

Use npm or yarn to install the package:

  1. npm -i @brainnit/adonisjs-scout
  2. # or
  3. yarn add @brainnit/adonisjs-scout

Add Scout to the list of service providers at start/app.js:

  1. const providers = [
  2. // ...
  3. '@brainnit/adonisjs-scout/providers/ScoutProvider',
  4. '@brainnit/adonisjs-scout/providers/IndexKeeperProvider'
  5. ];

Setup

Copy config/index.js to your app config folder and name it scout.js. Don’t forget to setup your environment variables.

You may also need to install extra dependencies depending on the search engine driver you will be using. For instance, to use Elasticsearch you will need:

  1. npm i --save elasticsearch bodybuilder
  2. # or
  3. yarn add elasticsearch bodybuilder

Usage

Add @provider:Searchable trait to your models and define only the methods you want to override to change default behaviour:

  1. /** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
  2. const Model = use('Model');
  3. class User extends Model {
  4. static get traits () {
  5. return ['@provider:Searchable']
  6. }
  7. }
  8. module.exports = Users

Afterwards, create your first IndexKeeper and run the following commands to create/delete indexes on your application:

  1. # if you want to create your indexes
  2. adonis scout:up
  3. # or this if you want to drop indexes
  4. adonis scout:down

Search Rules

A search rule is a class that describes how a search query will be executed and allow you to build complex and reusable searches with the support of the Builder instance.

To create a search rule use the following command:

  1. adonis make:searchableRule MySearchRule

In the file app/Models/SearchableRules/MySearchRule.js you will find a class definition:

  1. 'use strict'
  2. /** @type {typeof import('@brainnit/adonisjs-scout/src/SearchRule')} */
  3. const SearchRule = use('Scout/SearchRule')
  4. class MySearchRule extends SearchRule {
  5. buildQuery () {
  6. return {
  7. 'must': {
  8. 'match': {
  9. 'name': this.builder.query
  10. }
  11. }
  12. }
  13. }
  14. }
  15. module.exports = MySearchRule

To tell Scout about what search rules your model supports, just add the following method:

  1. /**
  2. * Specify what search rules the model supports.
  3. *
  4. * The return value(s) must always class namespaces that will be
  5. * resolved by IoC Container.
  6. *
  7. * @static
  8. *
  9. * @method searchableRules
  10. *
  11. * @return {Array|String} ES6 Class
  12. */
  13. static searchableRules () {
  14. return ['App/Models/SearchRules/MySearchRule']
  15. }

Backlog

  • Move index create/update operations off from indexing methods (Elasticsearch)
  • Add commands (make:scout:searchableModel, make:scout:searchRule, make:scout:indexKeeper)
  • Document all error codes
  • Add setup instructions
  • Add badges for npm version, build status, coverals
  • Add license scan
  • Add better wiki/docs

What else? Please open an Issue for suggestions.