#####1) Install package via composer:
composer require basemkhirat/elasticsearch
#####2) Add package service provider:
Basemkhirat\Elasticsearch\ElasticsearchServiceProvider::class
#####3) Add package alias:
'ES' => Basemkhirat\Elasticsearch\Facades\ES::class
#####4) Publishing:
php artisan vendor:publish --provider="Basemkhirat\Elasticsearch\ElasticsearchServiceProvider"
After publishing, the config file is placed here config/es.php
where you can add more than one elasticsearch node.
ES::index("my_index")->create();
# or
ES::create("my_index");
# [optional] you can create index with custom options
ES::index("my_index")->create(function($index){
$index->shards(5)->replicas(1)->mappping([
'my_type' => [
'properties' => [
'first_name' => [
'type' => 'string',
],
'age' => [
'type' => 'integer'
]
]
]
])
});
# or
ES::create("my_index", function($index){
$index->shards(5)->replicas(1)->mappping([
'my_type' => [
'properties' => [
'first_name' => [
'type' => 'string',
],
'age' => [
'type' => 'integer'
]
]
]
])
});
ES::index("my_index")->drop();
# or
ES::drop("my_index");
$documents = ES::connection("default")
->index("my_index")
->type("my_type")
->get(); // return collection of results
you can rewite the above query to
$documents = ES::get(); // return collection of results
the query builder will use the default connection, index, and type names setted in configuration file es.php
.
Index and type names setted in query will override values the configuration file
$documents = ES::_id(3)->get();
$documents = ES::orderBy("created_at", "desc")->get();
$documents = ES::orderBy("_score")->get();
$documents = ES::take(10)->skip(5)->get();
$documents = ES::select("title", "content")->take(10)->skip(5)->get();
ES::where("views", 150)->get(); or ES::where("views", "=", 150)->get();
ES::where("views", ">", 150)->get();
ES::where("views", ">=", 150)->get();
ES::where("views", "<", 150)->get();
ES::where("views", "<=", 150)->get();
ES::where("title", "like", "foo")->get();
ES::where("hobbies", "exists", true)->get(); or ES::whereExists("hobbies", true)->get();
ES::whereIn("id", [100, 150])->get();
ES::whereBetween("id", 100, 150)->get();
ES::whereNot("views", 150)->get(); or ES::where("views", "=", 150)->get();
ES::whereNot("views", ">", 150)->get();
ES::whereNot("views", ">=", 150)->get();
ES::whereNot("views", "<", 150)->get();
ES::whereNot("views", "<=", 150)->get();
ES::whereNot("title", "like", "foo")->get();
ES::whereNot("hobbies", "exists", true)->get(); or ES::whereExists("hobbies", true)->get();
ES::whereNotIn("id", [100, 150])->get();
ES::whereNotBetween("id", 100, 150)->get();
ES::search("bar")->get();
# search with Boost = 2
ES::search("bar", 2)->get();
ES::search("bar")->first();
ES::search("bar")->count();
$documents = ES::search("bar")->paginate(5);
# getting pagination links
$documents->links();
ES::raw()->search([
"index" => "my_index",
"type" => "my_type",
"body" => [
"query": [
"bool": [
"must": [
[ "match": [ "address": "mill" ] ],
[ "match": [ "address": "lane" ] ]
]
]
]
]
]);
ES::_id(3)->insert([
"title" => "Test document",
"content" => "Sample content"
]);
A new document will be inserted with _id = 3.
[id is optional] if not specified, a unique hash key will be generated
ES::bulk(
10 => [
"title" => "Test document 1",
"content" => "Sample content 1"
],
11 => [
"title" => "Test document 2",
"content" => "Sample content 2"
],
);
The two given documents will be inserted with its associated ids
ES::_id(3)->update([
"title" => "Test document",
"content" => "sample content"
]);
Document has _id = 3 will be updated.
[id is required]
ES::_id(3)->increment("views");
Document has _id = 3 will be incremented by 1.
ES::_id(3)->increment("views", 3);
Document has _id = 3 will be incremented by 3.
[id is required]
ES::_id(3)->decrement("views");
Document has _id = 3 will be decremented by 1.
ES::_id(3)->decrement("views", 3);
Document has _id = 3 will be decremented by 3.
[id is required]
# icrement field by script
ES::_id(3)->script(
"ctx._source.$field -= params.count",
["count" => 1]
);
# add php tag to tags array list
ES::_id(3)->script(
"ctx._source.tags.add(params.tag)",
["tag" => "php"]
);
# delete the doc if the tags field contain mongodb, otherwise it does nothing (noop)
ES::_id(3)->script(
"if (ctx._source.tags.contains(params.tag)) { ctx.op = \"delete\" } else { ctx.op = \"none\" }",
["tag" => "mongodb"]
);
ES::_id(3)->delete();
Document has _id = 3 will be deleted.
[id is required]
Good luck
Dont forget to send a feedback..