Giter Club home page Giter Club logo

laravel-scout-settings's Introduction

DEPRECATED: Use of this repository is deprecated. Please use Scout Extended - https://github.com/algolia/scout-extended instead.

Laravel Scout Settings

Import/Export Algolia settings, synonyms and query rules into your Laravel Scout project.

The easiest way to manage your settings is usually to go to your Algolia dashboard because it has a nice UI and you can test the relevancy directly there.

Once you fine tuned your configuration, you may want to add it to your project.

This package adds two Laravel commands to your project:

  • one to save your settings, synonyms and query rules into JSON files
  • one to push everything back to Algolia

This has 3 major advantages:

  1. You can version your configuration with your VCS
  2. You can set up a new environment or restore backups easily
  3. It lets you customize your settings in JSON format before pushing them

Install

Install this package with composer

composer require algolia/laravel-scout-settings

Laravel 5.5

If you use Laravel 5.5, this package will take advantage of the Package Auto-Discovery feature. Nothing more to do to register the commands.

Laravel 5.4 and prior

If you use an older version of Laravel, you will have to add the Service Provider to the providers array in config/app.php

Algolia\Settings\ServiceProvider::class,

Usage

You will now get two new commands available in artisan. They both take a model's fully qualified class name, just like Laravel Scout does to import/flush data.

The following example assume you have an App\Contact class, which uses the Searchable trait.

Note: Scout allows you to customize the index name with the searchableAs() method. This package will follow this naming convention.

Backing up settings (Project ⬅️ Algolia)

The following command will export all the settings and synonyms from the App\Contact's index into the following files:

  • Settings: resources/algolia-settings/index_name.json
  • Synonyms: resources/algolia-settings/index_name-synonyms.json
  • Query Rules: resources/algolia-settings/index_name-rules
php artisan algolia:settings:backup "App\Contact"

Note that if you want to add the prefix to your file names (which was the default behavior in v1), you can pass the --prefix option.

php artisan algolia:settings:backup "App\Contact" --prefix

Pushing settings (Project ➡️ Algolia)

The following command will read all the settings, synonyms and query rules from the files in resources/algolia-settings/ and import them into Algolia's index.

php artisan algolia:settings:push "App\Contact"

You can also pass the --prefix option, just like the backup command.

Customizing directory

By default, settings, rules and synonyms are saved into the resources/algolia-settings. The directory can be customized by the defining an environment variable named ALGOLIA_SETTINGS_FOLDER. For example, the following command will save all the index resources into resources/indexmeta.

ALGOLIA_SETTINGS_FOLDER=indexmeta php artisan algolia:settings:backup

Testing

composer test

Need help?

Feel free to open a thread on our Community forum

Contribute

Contributions are welcome!

laravel-scout-settings's People

Contributors

christhompsontldr avatar julienbourdeau avatar nullthoughts avatar nunomaduro avatar rayrutjes avatar skylerrichter avatar taylornotwell avatar tomcoonen avatar weotch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-scout-settings's Issues

Question: rationale behind using the prefix in file names?

I would like to pose this as a question, since I am not sure if my use case makes sense or not.

Background
I am migrating a Laravel 5.1 + algolia/algoliasearch-laravel application to Laravel 5.5 + Laravel Scout. This application is worked on by multiple developers, and the application is deployed on DTAP-style environments. In the current setup all index settings are properties of respective models. The $perEnvironment setting is used as well. Using this we have achieved a index per environment, with each developer having his own index (because he/she names the environment "jdoe_dev").

Current situation
The model contains the index settings. On a deploy (on any of the DTAP environments), the settings as they are at that moment in git history are pushed to Algolia with a custom Artisan command that hooks into the algoliasearch-laravel API.

Laravel Scout Settings
Since we're using multiple indexes in the same Algolia app, i.e. for test, acceptance and production, I would have to commit the setting-json files in triple. But I would have to manually update those files, since on my dev environment, the backup command would create another set of json files. Also, if at some point the environment name would change (which we did in the past, when we replaced "staging" with "test" and "acceptance"), it would require some more changes to the setting files as well.

I see no benefit to this. The way the plugin works, it would always only use one set of json files.

Suggested change
I think it would be possible to cater to my use case (which I don't think is uncommon/illogical, but correct me if I'm wrong... 😅) whilst keeping the default behavior the same. Instead of directly using the index name to concat the filename, use a mediating function in between, something like AlgoliaCommand::getBaseFileName($indexName). By default this would then just return $indexName, but if set, an alternative callback could be executed to get the base file name to be used.

If you want, I could try and make a PR for this?

forwardToReplicas cannot be set

I cannot set "forwardToReplicas":true in my json settings and there is no way to set it from the dashboard. Is there a way to achieve this?

Prefix use case

With version 2.0.0 this package now has the capability to save the settings-files without the prefix defined by the env value SCOUT_PREFIX and used in Laravel with config('scout.prefix').

This change was, I think, initiated by my earlier issue #14 . It is however still not enough to support my use case. This mostly relates to how replicas and primaries are defined in the settings.

Example background:

  • Table is called "posts"
  • Scout prefix is "test_"
  • Two indices; primary "test_posts" and replica "test_posts_newest"

When the backup command is run, without prefix, files named "posts.json" and "posts_newest.json" will be created. Which is great, since their not tied to a specific environment. But the JSON in those files have properties that are environment specific, like "replicas": ["test_posts_newest"] and "primary": "test_posts".

I am embarrassed that this slipped my attention when checking my PR and making the testcases. I am creating this issue mostly to:

  • Check again my use case is valid
  • Discuss the direction of implementation of a PR (which I would be happy to make)

My suggestion would be:

  • On backup manipulate settings to remove the prefix from both the primary and replicas, similar to how it is removed from the filenames.
  • On push manipulate settings to add back the prefix. This is actually a bit tricky to do in a fail-proof manner, since the searchableAs() methodology of Scout offers a lot of freedom in naming an index. Perhaps the indexname as know to Algolia should be kept on the repository too, just like the usePrefix-setting.

Incorrect examples in Algolia.com documentation

Description

The documentation on Algolia.com for laravel-scout-settings contains incorrect examples that will fail, originally corrected in PR #7

Steps To Reproduce

Visit https://www.algolia.com/doc/api-client/laravel/managing-settings/?language=laravel#how-to-use

Without the use of quotes around the classname, the console command is striping characters from $this->argument('model') (App\ClassName becomes AppClassName).

Possible Fixes

  • As per @ChrisThompsonTLDR, update documentation to include quotes around the class name: "App\ClassName"
  • Or change documentation to escape backslash in command: App\\ClassName

Not supporting Scout v5.0.1

It seems this package is not supporting Laravel/Scout 5.0.+. I'd appreciate it if anyone can help me with this.

Check if model is searchable

Hello!

We have a custom trait that uses the Laravel Scout classes but from the models, we use our trait (which is basically a wrapper).

We are getting the error while running the artisan commands "... does not use the [Laravel\Scout\Searchable] trait". (But we do in our custom wrapper.

We commented the lines of code that do the check on the BackupCommand.php and PushCommand.php and the rest works perfect.

So we were thinking that maybe you could have an extra setting like you do with the folder customization, to allow to skip the check of "isClassSearchable". Maybe like: ALGOLIA_SETTINGS_SKIP_CHECK=true.

If not we can do a fork and maintain a parallel package... We can do a PR if you like!

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.