Elasticsearch

Elasticsearch is an open source search engine based on Apache Lucene(TM). Lucene is considered to be the most advanced, high-performance, and full-featured search engine library to date, both open source and proprietary.

However, Lucene is just a library. To take full advantage of this, you need to use Java and integrate it into your application. Lucene is very complex, and you need to dig deep into retrieval knowledge to understand how it works.

Elasticsearch is also written in Java and uses Lucene to index and search, but it is intended to make full-text search easy and hide the complexity of Lucene with a simple and coherent RESTful API.

Elasticsearch is more than just Lucene and a full text search engine, however, it also provides:

  • Distributed real-time file storage where each field is indexed and searchable
  • Distributed search engine for real-time analysis
  • Scalable to hundreds of servers, processing petabytes of structured or unstructured data

Moreover, all of these functions are integrated into a single server that your application can interact with through simple RESTful apis, clients in various languages, and even the command line. Getting started with Elasticsearch is very simple, providing many reasonable defaults and hiding complex search engine theory from beginners. It is available out of the box and requires little learning to be used in a production environment.

Elasticsearch is licensed under the Apache 2 license and is free to download, use, and modify.

ElasticSearch installation

ElasticSearch is already integrated with Laradock. We can just use:

docker-compose up -d elasticsearch
Copy the code

To install plug-ins, run the following command:

docker-compose execElasticsearch/usr/share/elasticsearch/bin/elasticsearch - plugin install {plugin - name} / / restart container docker - compose restart elasticsearchCopy the code

Note:

  • The vm.max_map_count kernel setting must be set to at least 262144 for production use.

Sysctl -w vm. Max_map_count =262144

  • The default user name and password are elastic and Changeme, and port number is 9200

ElasticHQ

ElasticHQ is an open source application that offers a simplified interface for managing and monitoring Elasticsearch clusters.

Management and Monitoring for Elasticsearch.

www.elastichq.org/

  • Real-Time Monitoring
  • Full Cluster Management
  • Full Cluster Monitoring
  • Elasticsearch Version Agnostic
  • Easy Install – Always On
  • Works with X-Pack

Enter our Elasticsearch Host to go into the background.

The default is created:

One cluster cluster: laradock-cluster One node Node: laradock-node one index index:.elastichq

IK word divider installed

ElasticSearch is a plugin for ElasticSearch (7.5.1), which can be used to search your own blog or official accounts.

Github.com/medcl/elast…

// Install docker-composeexec elasticsearch /usr/share/elasticsearch/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.5.1/elasticsearch-analysis-ik-7.5.1.zip
Copy the code

Note: you can download the ZIP file first, and then install it, it will be faster.

Test the segmentation effect

Elasticsearch API test results:

 ~ curl -X POST "http://your_host/_analyze? pretty" -H 'Content-Type: application/json' -d'{" analyzer ":" ik_max_word ", "text" : "I am a Chinese"}'

{
  "tokens": [{"token" : "我"."start_offset": 0."end_offset" : 1,
      "type" : "CN_CHAR"."position": 0}, {"token" : "Yes"."start_offset" : 1,
      "end_offset": 2."type" : "CN_CHAR"."position": 1}, {"token" : "Chinaman"."start_offset": 2."end_offset" : 5,
      "type" : "CN_WORD"."position": 2}, {"token" : "China"."start_offset": 2."end_offset": 4."type" : "CN_WORD"."position": 3}, {"token" : "Chinese people"."start_offset": 3."end_offset" : 5,
      "type" : "CN_WORD"."position": 4}]}Copy the code

In combination with Laravel

The Elasticsearch plugin is available for PHP, but we wanted to be more closely integrated with Laravel, so we chose To use Scout with Tamayo/Laravel-Scouter-Elastic.

composer require tamayo/laravel-scout-elastic
 
composer require laravel/scout
 
php artisan vendor:publish
Copy the code

Options: Laravel \ Scout \ ScoutServiceProvider

Change driver to elasticSearch:

'driver' => env('SCOUT_DRIVER'.'elasticsearch'),
Copy the code

Create indexes

There are several ways to create an index, one of which can be created directly using the Ela visualization tool ElasticHQ.

Next we need to update the index to add the Mappings section, which can be Postman.

Another approach is to use Laravel’s built-in Artisan command-line functionality.

Here we recommend using the Artisan command line.

php artisan make:command ESOpenCommand
Copy the code

Elasticsearch: select * from ESOpenCommand to send a PUT request to the Elasticsearch server. Elasticsearch PHP is already installed when we use tamayo/ Laravel-Scouter-Elastic:

Now we can use the plugin to create our Index and see the code directly:

    public function handle(a)
    {
    $host = config('scout.elasticsearch.hosts');
    $index = config('scout.elasticsearch.index');
    $client = ClientBuilder::create()->setHosts($host)->build();

    if ($client->indices()->exists(['index' => $index])) {
        $this->warn("Index {$index} exists, deleting...");
        $client->indices()->delete(['index' => $index]);
    }

    $this->info("Creating index: {$index}");

    return $client->indices()->create([
        'index' => $index,
        'body'= > ['settings'= > ['number_of_shards'= >1.'number_of_replicas'= >0].'mappings'= > ['_source'= > ['enabled'= >true].'properties'= > ['id'= > ['type'= >'long'].'title'= > ['type'= >'text'.'analyzer'= >'ik_max_word'.'search_analyzer'= >'ik_smart'].'subtitle'= > ['type'= >'text'.'analyzer'= >'ik_max_word'.'search_analyzer'= >'ik_smart'].'content'= > ['type'= >'text'.'analyzer'= >'ik_max_word'.'search_analyzer'= >'ik_smart'[], []]); }Copy the code

Ok, we execute Kibana and see we have created Index:

Note Kibana native Docker installation:

How to use Kibana will be highlighted later

docker run -d --name kibana -e ELASTICSEARCH_HOSTS=http://elasticsearch_host -p 5601:5601 -eThe kibana SERVER_NAME = ki: 7.5.2Copy the code

To verify that Index is available, insert a single piece of data:

curl -XPOST your_host/coding01_open/_create/1 -H 'Content-Type:application/json' -d'{" Content ":" Investigation on Sino-SOUTH Korean Fishing Police Conflict: South Korean police Detain 1 Chinese fishing boat per day on average "}Copy the code

You can view the corresponding data through the browser:

With Index, the next step is to import, update, query, and so on in combination with Laravel.

Laravel Model using

The Laravel framework has recommended the use of Scout full text search, we just need to add the official content to the Article Model, it is very simple, we recommend you to see the Scout use documentation: learnku.com/docs/larave… , let’s go directly to the code:


      

namespace App;

use App\Tools\Markdowner;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Scout\Searchable;

class Article extends Model
{
    use Searchable;

    protected $connection = 'blog';
    protected $table = 'articles';
    use SoftDeletes;

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['published_at'.'created_at'.'deleted_at'];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id'.'last_user_id'.'category_id'.'title'.'subtitle'.'slug'.'page_image'.'content'.'meta_description'.'is_draft'.'is_original'.'published_at'.'wechat_url',];protected $casts = [
        'content'= >'array'
    ];

    /**
     * Set the content attribute.
     *
     * @param $value
     */
    public function setContentAttribute($value)
    {
        $data = [
            'raw'  => $value,
            'html'= > (new Markdowner)->convertMarkdownToHtml($value)
        ];

        $this->attributes['content'] = json_encode($data);
    }

    /** * Get searchable data for the model **@return array
     */
    public function toSearchableArray(a)
    {
        $data = [
            'id'= >$this->id,
            'title'= >$this->title,
            'subtitle'= >$this->subtitle,
            'content'= >$this->content['html']].return $data;
    }

    public function searchableAs(a)
    {
        return '_doc'; }}Copy the code

Scout provides the Artisan command import to import all existing records into the search index.

php artisan scout:import "App\Article"
Copy the code

Looking at Kibana, 12 data pieces have been saved, which matches the number in the database.

With the data, we can test to see if we can query the data.

Again, create a command:

class ElasearchCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:search {query}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(a)
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle(a)
    {
        $article = Article::search($this->argument('query'))->first();
        $this->info($article->title); }}Copy the code

Here’s my titles, which I type in with a random keyword: “Listings” to see if it hits.

conclusion

Complete as a whole:

  1. Elasticsearch installation;
  2. Elasticsearch IK splitter plugin installed
  3. Elasticsearch visualization tools for ElasticHQ and Kibana
  4. The use of Scout;
  5. Elasticsearch and Scout are used together.

The next step is to store more content in Elasticsearch to provide full text search for your blog, public account, automated search, etc.

reference

  • Recommend a command line application development tools – Laravel Zero mp.weixin.qq.com/s/RKEuz5gd8…

  • Artisan command line learnku.com/docs/larave…

  • Scout Full Text Search learnku.com/docs/larave…

  • How to Integrate Elasticsearch in Your Laravel App — 2019 Edition MadeWithLove. Be/How-to-Inte…

  • Kibana Guide www.elastic.co/guide/en/ki…

  • Elasticsearch PHP – API [www.elastic.co/guide/en/el]… (www.elastic.co/guide/en/el)…