项目作者: kind-work

项目描述 :
Use MeiliSearch to provide instant search in Statamic V3
高级语言: PHP
项目地址: git://github.com/kind-work/meili-search.git
创建时间: 2020-09-20T12:02:38Z
项目社区:https://github.com/kind-work/meili-search

开源协议:

下载


This addon is no longer supported. For a supported addon please see: https://statamic.com/addons/elvenstar/statamic-meilisearch

Use MeiliSearch to provide instant search

Statamic 3.0+
Commercial License

This Statamic v3 addon provides an easy way to integrate with MeiliSearch, a powerful, fast, open-source, easy to use and deploy search engine.

Requirements

  • MeiliSearch 0.14+
  • PHP 7.2+
  • Statamic v3+
  • Laravel 7+

Installation

You can install this addon via composer with the following command or from the Statamic control panel.

  1. composer require kind-work/meili-search

For instructions on how to install MeiliSearch please see their documentation.

Configuration

.env

Configure the addon by setting your MeiliSearch URL and API Keys in your .env file.

  1. MEILI_URL=http://localhost:7700
  2. MEILI_MASTER_KEY=your-master-key-here
  3. MEILI_PRIVATE_KEY=your-private-key-here
  4. MEILI_PUBLIC_KEY=your-public-key-here

After you add your master key you can use the following command to get your private and public keys.

  1. php please meili-search:keys

Settings

To configure what collections you would like to index, publish the config file to config/meili-search.php by running the following command. Then customize the indexes section of the file.

  1. php artisan vendor:publish --tag="meili-search-config"

Indexing

When a collection entry is created, published, unpublished, saved or deleted via the Statamic control panel it will automatically be added, updated in or removed from the indexes configured for the appropriate collection.

The following connivence commands are available to help indexing, especially when updating content files manually.

  1. php please meili-search:keys
  2. php please meili-search:index help
  3. php please meili-search:index create [Your MeiliSearch uid]
  4. php please meili-search:index list
  5. php please meili-search:index clear [Your MeiliSearch uid]
  6. php please meili-search:index delete [Your MeiliSearch uid]
  7. php please meili-search:documents help
  8. php please meili-search:documents update

Searching

Searching is best done with JavaScript talking to MeiliSearch directly. This will give you the most performant real time searches. Here is a simple example of how you could do this with AlpineJS and TailwindCCS.

*Note: These steps assume you already have AlpineJS and Tailwind CCS already set up and working in your project.*

Install the MeiliSearch NPM module

  1. npm install meilisearch

or

  1. yarn add meilisearch

Import and set up MeiliSearch

  1. import { MeiliSearch } from 'meilisearch'
  2. window.client = new MeiliSearch({
  3. host: 'http(s)://Your MeiliSearch address & port',
  4. apiKey: 'Your MeiliSearch PUBLIC Key',
  5. });

Customize your Search Component

Here is a basic autocomplete using AlpineJS and Tailwind CCS, feel free copy it, customize it, or just use it as inspiration to do something completely different.

  1. <div
  2. x-data="{
  3. searchString: '',
  4. results: {
  5. hits: [],
  6. },
  7. index: window.client.index('Your Index UID Goes Here'),
  8. async search() {
  9. this.state = 'searching';
  10. this.results = await this.index.search(this.searchString);
  11. }
  12. }"
  13. class="relative"
  14. >
  15. <label
  16. class="sr-only"
  17. >
  18. Search
  19. </label>
  20. <input
  21. x-model="searchString"
  22. x-on:input="search()"
  23. type="search"
  24. class="
  25. px-2 py-1
  26. border-2 border-solid border-gray-300 rounded
  27. focus:outline-none focus:shadow-outline
  28. "
  29. />
  30. <div
  31. x-cloak
  32. x-show="searchString.length > 0"
  33. class="
  34. absolute left-0
  35. w-full
  36. mt-1 py-1
  37. bg-white
  38. border-2 border-solid border-grey-200 rounded
  39. "
  40. >
  41. <p
  42. x-show='results.hits.length < 1'
  43. >
  44. No results
  45. </p>
  46. <ul
  47. x-show='results.hits.length > 0'
  48. >
  49. <template
  50. x-for='result in results.hits'
  51. :key="result.id"
  52. >
  53. <li>
  54. <a
  55. :href="result.uri"
  56. class="
  57. px-2 py-1
  58. hover:bg-gray-300 focus:bg-gray-300
  59. transition-colors duration-100 ease-in-out delay-75
  60. focus:outline-none focus:shadow-outline
  61. "
  62. >
  63. <h2
  64. x-text="result.title"
  65. ></h2>
  66. </a>
  67. </li>
  68. </template>
  69. </ul>
  70. </div>
  71. </div>

Changelog

Please see the Release Notes for more information what has changed recently.

Security

If you discover any security-related issues, please email security@kind.work instead of using the issue tracker.

License

This is commercial software. You may use the package for your sites. Each site requires its own license. You can purchase a licence from The Statamic Marketplace.