Skip to content

Commit 922ec54

Browse files
committed
first commit
0 parents  commit 922ec54

File tree

8 files changed

+1231
-0
lines changed

8 files changed

+1231
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.idea/
2+
/vendor
3+
composer.lock
4+
.gitattributes
5+
.DS_Store

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "basemkhirat/elasticsearch",
3+
"description": "Elasticsearch query builder",
4+
"keywords": ["elasticsearch", "full text", "builder", "laravel"],
5+
"license": "MIT",
6+
"type": "package",
7+
"authors": [
8+
{
9+
"name": "basemkhirat",
10+
"email": "basemkhirat@gmail.com"
11+
}
12+
],
13+
14+
"autoload": {
15+
"psr-4" : {
16+
"Basemkhirat\\Elasticsearch\\": "src/"
17+
}
18+
},
19+
20+
"require": {
21+
"elasticsearch/elasticsearch": "5.0.0",
22+
"illuminate/pagination": "*",
23+
"illuminate/support": "*"
24+
}
25+
}

readme.md

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
### Flexible elasticseach builder to run complex queries with an easier way.
2+
3+
#####1) Install package via composer:
4+
5+
composer require basemkhirat/elasticsearch
6+
7+
#####2) Add package service provider:
8+
9+
Basemkhirat\Elasticsearch\ElasticsearchServiceProvider::class
10+
11+
#####3) Add package alias:
12+
13+
'ES' => Basemkhirat\API\Facades\ES::class
14+
15+
#####4) Publishing:
16+
17+
php artisan vendor:publish --provider="Basemkhirat\Elasticsearch\ElasticsearchServiceProvider"
18+
19+
### Usage:
20+
21+
#### Setting your connections
22+
23+
24+
After publishing, the config file is placed here `config/es.php`
25+
where you can add more than one elasticsearch server.
26+
27+
28+
#### Running queries:
29+
30+
$documents = ES::connection("default")
31+
->index("my_index")
32+
->type("my_type")
33+
->get(); // return collection of results
34+
35+
you can rewite the above query to
36+
37+
$documents = ES::get(); // return collection of results
38+
39+
the query builder will use the default connection, index, and type names setted in configuration file `es.php`.
40+
41+
Index and type names setted in query will override values the configuration file
42+
43+
#### Available methods:
44+
45+
46+
// sorting
47+
48+
$documents = ES::orderBy("created_at", "desc")->get();
49+
$documents = ES::orderBy("_score")->get();
50+
51+
// limit and offset
52+
53+
$documents = ES::take(10)->skip(5)->get();
54+
55+
// select only specific fields
56+
57+
$documents = ES::select("title", "content")->take(10)->skip(5)->get();
58+
59+
// where clause
60+
61+
ES::where("id", 150)->get(); or ES::where("id", "=", 150)->get();
62+
ES::where("id", ">", 150)->get();
63+
ES::where("id", ">=", 150)->get();
64+
ES::where("id", "<", 150)->get();
65+
ES::where("id", "<=", 150)->get();
66+
ES::where("title", "like", "foo")->get();
67+
ES::where("hobbies", "exists", true)->get(); or ES::whereExists("hobbies", true)->get();
68+
69+
// where in clause
70+
71+
ES::whereIn("id", [100, 150])->get();
72+
73+
// where between clause
74+
75+
ES::whereBetween("id", 100, 150)->get();
76+
77+
-
78+
79+
// where not clause
80+
81+
ES::whereNot("id", 150)->get(); or ES::where("id", "=", 150)->get();
82+
ES::whereNot("id", ">", 150)->get();
83+
ES::whereNot("id", ">=", 150)->get();
84+
ES::whereNot("id", "<", 150)->get();
85+
ES::whereNot("id", "<=", 150)->get();
86+
ES::whereNot("title", "like", "foo")->get();
87+
ES::whereNot("hobbies", "exists", true)->get(); or ES::whereExists("hobbies", true)->get();
88+
89+
// where not in clause
90+
91+
ES::whereNotIn("id", [100, 150])->get();
92+
93+
// where not between clause
94+
95+
ES::whereNotBetween("id", 100, 150)->get();
96+
97+
98+
-
99+
100+
// Search the entire document
101+
102+
ES::search("bar")->get();
103+
104+
105+
-
106+
107+
// Return only first record
108+
109+
ES::search("bar")->first();
110+
111+
-
112+
113+
// Return only count
114+
115+
ES::search("bar")->count();
116+
117+
-
118+
119+
// paginate results with per_page = 5
120+
121+
$documents = ES::search("bar")->paginate(5);
122+
123+
// getting pagination links
124+
125+
$documents->links();
126+
127+
128+
-
129+
130+
// Executing elasticsearch raw queries
131+
132+
ES::raw()->search([
133+
"index" => "my_index",
134+
"type" => "my_type",
135+
"body" => [
136+
"query": [
137+
"bool": [
138+
"must": [
139+
[ "match": [ "address": "mill" ] ],
140+
[ "match": [ "address": "lane" ] ]
141+
]
142+
]
143+
]
144+
]
145+
]);
146+
147+
148+
-
149+
150+
// insert a new document
151+
152+
ES::insert([
153+
"title" => "Test document",
154+
"content" => "sample content"
155+
], 3);
156+
157+
158+
A new document will be inserted with _id = 3.
159+
160+
[id is optional] if not specified, a unique hash key will be generated
161+
162+
163+
-
164+
165+
// Update an existing document
166+
167+
ES::update([
168+
"title" => "Test document",
169+
"content" => "sample content"
170+
], 3);
171+
172+
173+
Document has _id = 3 will be updated.
174+
175+
[id is required]
176+
177+
-
178+
179+
// delete a document
180+
181+
ES::delete(3);
182+
183+
Document has _id = 3 will be deleted.
184+
185+
[id is required]
186+
187+
188+
`Good luck`
189+
190+
`Dont forget to send a feedback..`

src/Connection.php

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
3+
namespace Basemkhirat\Elasticsearch;
4+
5+
use Elasticsearch\ClientBuilder;
6+
7+
/**
8+
* Class Builder
9+
* @package Basemkhirat\Elasticsearch\Builder
10+
*/
11+
class Connection
12+
{
13+
14+
/**
15+
* Laravel app instance
16+
* @var \Illuminate\Foundation\Application
17+
*/
18+
protected $app;
19+
20+
/**
21+
* Elastic config content
22+
* @var
23+
*/
24+
protected $config;
25+
26+
/**
27+
* The current connection
28+
* @var
29+
*/
30+
protected $connection;
31+
32+
/**
33+
* all available connections
34+
* @var array
35+
*/
36+
protected $connections = [];
37+
38+
39+
/**
40+
* Connection constructor.
41+
*/
42+
function __construct()
43+
{
44+
$this->app = app();
45+
46+
$this->config = $this->app['config']['es'];
47+
}
48+
49+
50+
/**
51+
* Create a connection
52+
* @param $name
53+
* @return Query
54+
*/
55+
function connection($name)
56+
{
57+
58+
// Check if connection is already loaded.
59+
60+
if ($this->isLoaded($name)) {
61+
62+
$this->connection = $this->connections[$name];
63+
64+
return $this->query($name);
65+
66+
}
67+
68+
// Create a new connection.
69+
70+
if (array_key_exists($name, $this->config["connections"])) {
71+
72+
$config = $this->config["connections"][$name];
73+
74+
// Instantiate a new ClientBuilder
75+
$clientBuilder = ClientBuilder::create();
76+
77+
$clientBuilder->setHosts($this->config["connections"][$name]["servers"]);
78+
79+
// Build the client object
80+
$connection = $clientBuilder->build();
81+
82+
$this->connection = $connection;
83+
84+
$this->connections[$name] = $connection;
85+
86+
return $this->query($name);
87+
}
88+
89+
$this->app->abort(500, "Invalid elasticsearch connection driver `" . $name . "`");
90+
91+
}
92+
93+
94+
/**
95+
* route the request to query class
96+
* @param $connection
97+
* @return Query
98+
*/
99+
function query($connection)
100+
{
101+
102+
$config = $this->config["connections"][$connection];
103+
104+
$query = new Query($this->connections[$connection]);
105+
106+
if (array_key_exists("index", $config) and $config["index"] != "") {
107+
$query->index($config["index"]);
108+
}
109+
110+
if (array_key_exists("type", $config) and $config["type"] != "") {
111+
$query->type($config["type"]);
112+
}
113+
114+
return $query;
115+
116+
}
117+
118+
/**
119+
* Check if the connection is already loaded
120+
* @param $name
121+
* @return bool
122+
*/
123+
function isLoaded($name)
124+
{
125+
126+
if (array_key_exists($name, $this->connections)) {
127+
return true;
128+
}
129+
130+
return false;
131+
132+
}
133+
134+
135+
/**
136+
* Set the default connection
137+
* @param $name
138+
* @param $arguments
139+
* @return mixed
140+
*/
141+
function __call($name, $arguments)
142+
{
143+
if (method_exists($this, $name)) {
144+
145+
return call_user_func_array([$this, $name], $arguments);
146+
147+
} else {
148+
149+
// if no connection, use default.
150+
151+
$query = $this->connection($this->config["default"]);
152+
153+
return call_user_func_array([$query, $name], $arguments);
154+
155+
}
156+
}
157+
158+
}

0 commit comments

Comments
 (0)