Giter Club home page Giter Club logo

meilisearch's Introduction

Statamic Meilisearch Driver

This addon provides a Meilisearch search driver for Statamic sites.

Requirements

  • PHP 8.1+
  • Laravel 10+
  • Statamic 5
  • Meilisearch 1.0+

Installation

composer require statamic-rad-pack/meilisearch

Add the following variables to your env file:

MEILISEARCH_HOST=http://127.0.0.1:7700
MEILISEARCH_KEY=

The master key is like a password, if you auto-deploy a Meilisearch server they will most likely provide you with keys. On localhost you can make up your own master key then use that to generate your private and public keys. You will need these keys for front-end clients:

# Export the key
$ export MEILISEARCH_KEY=AWESOMESAUCE

# Start the meilisearch server again
$ meilisearch

# Generate the keys
curl \
  -H 'Authorization: Bearer AWESOMESAUCE' \
  -X GET 'http://localhost:7700/keys'

Add the new driver to the statamic/search.php config file:

'drivers' => [

    // other drivers

    'meilisearch' => [
        'credentials' => [
            'url' => env('MEILISEARCH_HOST', 'http://localhost:7700'),
            'secret' => env('MEILISEARCH_KEY', ''),
            // 'search_api_key' => env('MEILISEARCH_SEARCH_KEY')
        ],
    ],
],

You can optionally add search_api_key which makes it easier to call the key on the frontend javascript code:

<script type="text/javascript">
window.meilisearch = new meilisearch({
    host: '{{ config:statamic:search:drivers:meilisearch:credentials:url }}',
    apiKey: '{{ config:statamic:search:drivers:meilisearch:credentials:search_api_key }}',
});
</script>

You can optionally publish the config file for this package using:

php artisan vendor:publish --tag=statamic-meilisearch-config

Few words about Document IDs in meilisearch

When you index your Statamic Entries, the driver will always transform the ID. This is required because meilisearch only allows id to be a string containing alphanumeric characters (a-Z, 0-9), hyphens (-) and underscores (_). You can read more about this in the meilisearch documentation

As an Entry, Asset, User or Taxonomy reference is a combination of the type, handle/container and ID separated with a :: (e.g. assets::heros/human01.jpg, categories::cats) this could not be indexed by meilisearch.

As a Workaround, we take care add reference while indexing your entries automatically ๐ŸŽ‰.

Internally Statamic will use \Statamic\Facades\Data::find($reference) to resolve the corresponding Statamic Entry, Asset, User or Taxonomy.

Search Settings

Any additional settings you want to define per index can be included in the statamic/search.php config file. The settings will be updated when the index is created.

// articles
'articles' => [
    'driver' => 'meilisearch',
    'searchables' => ['collection:articles'],
    'fields' => ['id', 'title', 'url', 'type', 'content', 'locale'],
    'settings' => [
      'filterableAttributes' => ['type', 'locale'],
    ],
],

You may include different types of settings in each index:

'articles' => [
    'driver' => 'meilisearch',
    'searchables' => ['collection:articles'],
    'settings' => [
        'filterableAttributes' => ['type', 'country', 'locale'],
        'distinctAttribute' => 'thread',
        'stopWords' => ['the', 'of', 'to'],
        'sortableAttributes' => ['timestamp'],
        'rankingRules' => [
          'sort',
          'words',
          'typo',
          'proximity',
          'attribute',
          'exactness',
        ],
    ],
 ],

Search Pagination

By default we limit the maxTotalHits to 1000000, if you want to modify this or any other pagination settings on the index, specify a pagination key:

// articles
'articles' => [
    'driver' => 'meilisearch',
    'searchables' => ['collection:articles'],
    'fields' => ['id', 'title', 'url', 'type', 'content', 'locale'],
    'pagination' => [
      'maxTotalHits' => 100,
    ],
],

Extending

You can extend the drivers functionality (e.g. in order to customize calls to meilisearch) by creating a class that extends StatamicRadPack\meilisearch\meilisearch\Index and instructing Laravel's service container to use it:

class MyIndex extends Index {
    // Your custom logic here
}
// app/Providers/AppServiceProvider.php

$this->app->bind(\StatamicRadPack\meilisearch\meilisearch\Index::class, MyIndex::class);

Common Errors

413 Request Entity Too Large

You may encounter this bug on Laravel Forge for example, when you try sync the search documents for the first time. To overcome this you need to update the upload size limit in nginx.

Add client_max_body_size to the http section on /etc/nginx/nginx.conf:

http {
  client_max_body_size 100M;
  // other settings
}

Then restart the server, or run sudo service nginx restart.

meilisearch's People

Contributors

duncanmcclean avatar godismyjudge95 avatar j6s avatar lakkes-ra avatar naabster avatar okaufmann avatar ryanmitchell avatar tao avatar z3d0x avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

meilisearch's Issues

Support for Statamic 5 and Laravel 11

Description

I think the title is self-explanatory. In any case, it would be great if the addon is updated soon.

Steps to reproduce

See above

Environment

See above

Updating the Meilisearch indexes via Control Panel in a multisite setup doesn't work anymore

Description

Since version 3.1.0 of the add-on updating the indexes via Control Panel produces 500 errors. We assume it's related to the dynamic change of the index names, which now includes the sites handle.

For example:

In v 3.0.3 the shown index name in the control panel looks like this and updating it works just fine:
image

After updating to v3.1.0 it looks like this and produces an error (see the debug output here: https://flareapp.io/share/4m4KoV45#context):
image

Steps to reproduce

  1. Setup a multisite website
  2. Add Meilisearch add-on in version 3.1.0
  3. Setup search indexes with the Meilisearch drivers
  4. Try to update the indexes in the Control Panel

Environment

Environment
Application Name: xxx
Laravel Version: 9.52.16
PHP Version: 8.2.11
Composer Version: 2.5.5
Environment: local
Debug Mode: ENABLED
URL: xxx
Maintenance Mode: OFF

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: log
Cache: statamic
Database: mysql
Logs: stack / daily
Mail: smtp
Queue: redis
Session: file

Statamic
Addons: 2
Antlers: runtime
Stache Watcher: Enabled
Static Caching: half
Version: 4.39.0 PRO

Statamic Addons
statamic-rad-pack/meilisearch: 3.1.0
statamic/eloquent-driver: 2.10.0

Statamic Eloquent Driver
Asset Containers: file
Assets: eloquent
Blueprints: file
Collection Trees: file
Collections: eloquent
Entries: eloquent
Forms: eloquent
Global Sets: eloquent
Global Variables: file
Navigation Trees: file
Navigations: eloquent
Revisions: eloquent
Taxonomies: eloquent
Terms: eloquent

meilisearch Version

3.1.0

Limit doesn't seem to be applied

Describe the bug
When using the {{ search:results }} tag, using {{ search:results limit=10 }} doesn't seem to properly limit the results.

Versions

  • elvenstar/statamic-meilisearch package version: 1.1.0

Expected behavior
When using the limit modifier, the returned results should be limited to the specified number.

Additional context
This seems like a bug, but it's complicated by how Meilisearch handles limits, I think.

For the limit to actually apply to what Meilisearch returns, it needs to be passed in via the searchUsingApi method.
public function searchUsingApi($query, $filters = [], $options = [])

Specifically, as a filter according to this: https://docs.meilisearch.com/reference/api/search.html#limit.

By changing the Query's call from this:
return $this->index->searchUsingApi($query);
to this:
return $this->index->searchUsingApi($query, ['limit' => 100]);

I can see search results up to 100 being reported. By default, Meilisearch will only return the first 20 results, unless its limit value is overridden.

This is actually kind of a big deal, as it's locking all search:results requests to 20 results, no matter what's actually being requested.

Looking at the Search Settings page, Meilisearch also seems to support Offset values in much the same way, so it might be worth supporting that as well?

As it stands right now, I'm not sure how to work around this, short of editing the actual Query.php file (which I'd like to avoid). Is it possible to grab the data from the search tag and use it in the Index?

Thanks for your work on the addon so far, it's super handy (aside from this locked search result issue)!

Question regarding filters

Hello and sorry for the disturbance ! Feel free to close the issue without explanation if this is not the place to ask questions.

I have trouble making a filter work using the plugin, here is what I'm doing :
Statamic\Facades\Search::index('my_index')->searchUsingApi(Request::input('q'), ['filter' => ['category = a-taxonomy-term']])

Here is what a record in meilisearch looks like :

{
  "id":"be604825-d780-481d-b623-e13b30c82c2d",
  "title":"test title",
  "reponse":"<p>test<br></p>",
  "tags":["test1","test2","test3"],
  "category":["a-taxonomy-term","another-one"],
  "collection":"my-collection"
}

I can't seem to make my filter work, I'm trying to get only results that are linked to a specify taxonomy term. Am I doing something wrong ?

Thanks a lot for the great plugin !

Pagination not working when searching collection entries from collection page in Statamic CP

Describe the bug
When providing a search_index for any collection, and making a search from the route
cp/collections/{collection}
(so not from the main search field) the parameters passed in the query string (page, perPage) are not passed to the search, so the pagination is not working in this case.
The number of returned items is the default value for Meilisearch "hitsPerPage" parameter.

Versions

  • elvenstar/statamic-meilisearch package version:
    2.0.0

PHP version: 8.2.7
Laravel version: v9.52.10
MeiliSearch version: 1.2.0
Statamic version: v4.9.2

To Reproduce
Steps to reproduce the behavior:

  1. Go to the list of entries on any collection which has a search_index set.
  2. Enter any search string in the search field
  3. See error

Expected behavior
The pagination should work the same way, as if no search_index is set for the collection.

Sites option is ignored

Description

On a multi site when setting the sites config option it still indexes all entries from all sites not those specified

'products' => [
            'driver'       => 'meilisearch',
            'searchables'  => 'collection:products',
            'locale'        => ['default'],
            'settings' => [
                'filterableAttributes' => ['
                    //...
                ],
                'sortableAttributes' => [
                    //...
                ],
            ],
            'fields'       => [
                //...
            ],
        ],

Steps to reproduce

  1. Create a multi site install
  2. Create a collection with multiple sites
  3. Index collection

Environment

Statamic
Addons: 15
Antlers: runtime
Stache Watcher: Disabled
Static Caching: Disabled
Version: 4.30.0 PRO

Statamic Addons
appswithlove/statamic-one-click-content-translation: 4.18.2
aryehraber/statamic-logbook: 3.1.0
doublethreedigital/runway: 5.4.0
doublethreedigital/simple-commerce: 5.6.0
jacksleight/statamic-bard-texstyle: 3.1.3
jonassiewertsen/statamic-livewire: 2.12.0
nineteensquared/instagram: 1.2.0
rootstudio/cache-primer: 0.9
rootstudio/monitor: 0.2
rootstudio/mux-video: 0.5
rootstudio/turnstile: 0.2
statamic-rad-pack/meilisearch: 3.0.3
statamic/collaboration: 0.7.3
statamic/eloquent-driver: 2.8.0
stillat/relationships: 2.1.1

Statamic Eloquent Driver
Asset Containers: file
Assets: file
Blueprints: file
Collection Trees: file
Collections: file
Entries: eloquent
Forms: eloquent
Global Sets: eloquent
Global Variables: file
Navigation Trees: file
Navigations: eloquent
Revisions: eloquent
Taxonomies: file
Terms: eloquent

meilisearch Version

3.0.3

Error on Publishing (single) Entry

Description

If you publish a single entry after saving and changing some content, an error is returned:

Statamic\Search\Searchables::fields(): Argument #1 ($searchable) must be of type Statamic\Contracts\Search\Searchable, null given, called in XXX/vendor/statamic-rad-pack/meilisearch/src/Meilisearch/Index.php on line 45

Steps to reproduce

use meilisearch for a collection and save some entry

Environment

Environment
Application Name: XXX
Laravel Version: 10.37.3
PHP Version: 8.2.12
Composer Version: 2.6.5
Environment: local
Debug Mode: OFF
URL: XXX
Maintenance Mode: OFF

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: log
Cache: redis
Database: mysql
Logs: stack / single
Mail: smtp
Queue: redis
Session: redis

Statamic
Addons: 11
Antlers: runtime
Stache Watcher: Enabled
Static Caching: Disabled
Version: 4.41.0 PRO

Statamic Addons
...
statamic-rad-pack/meilisearch: 3.2.0
statamic/seo-pro: 5.3.0
steadfastcollective/statamic-csv-exporter: 2.2.1
stillat/relationships: 2.1.3
thoughtco/statamic-restrict-fields: 1.1.0
webographen/statamic-widget-continue-editing: 1.1.0

meilisearch Version

1.5.0

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.