Skip to content

Commit 6992d13

Browse files
committed
Initial commit.
0 parents  commit 6992d13

File tree

8 files changed

+833
-0
lines changed

8 files changed

+833
-0
lines changed

.gitignore

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

composer.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "adamfairholm/elasticquent",
3+
"type": "library",
4+
"description": "Map Larvel Eloquent models to ElasticSearch types.",
5+
"keywords": [
6+
"elasticsearch",
7+
"eloquent",
8+
"laravel"
9+
],
10+
"homepage": "https://github.com/adamfairholm/Elasticquent",
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Adam Fairholm",
15+
"email": "[email protected]",
16+
"homepage": "http://adamfairholm.com"
17+
}
18+
],
19+
"require": {
20+
"php": ">=5.4.0"
21+
},
22+
"require-dev": {
23+
"phpunit/phpunit": "~4.0",
24+
"illuminate/support": "~4.2",
25+
"illuminate/database": "~4.2",
26+
"nesbot/carbon": "~1.0"
27+
},
28+
"autoload": {
29+
"psr-0": {
30+
"Adamfairholm\\Elasticquent\\": "src/"
31+
}
32+
}
33+
}

readme.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Elastiquent: Elasticsearch for Eloquent Laravel Models
2+
3+
Elastiquent makes working with Elasticsearch and Eloquent models easier by mapping them to Elasticsearch types. You can use the default settings or define how Elasticsearch should index and search your Eloquent models right in the model.
4+
5+
Elastiquent uses the [official Elasticsearch PHP API](https://github.com/elasticsearch/elasticsearch-php). To get started, you should have a basic knowledge of how ElasticSearch works (indexes, types, mappings, etc). This is meant for use with Elasticsearch 1.x.
6+
7+
## Setup
8+
9+
To get started, add Elastiquent to you composer.json file:
10+
11+
"adamfairholm/elastiquent" => "master-dev"
12+
13+
Once you've run a `composer update`, add the Elastiquent trait to any Eloquent model that you want to be able to index in Elasticsearch:
14+
15+
```php
16+
use Adamfairholm\Elastiquent\ElastiquentTrait;
17+
18+
class Book extends Eloquent {
19+
20+
use ElastiquentTrait;
21+
22+
}
23+
```
24+
25+
Now your Eloquent model has some extra methods that make it easier to index your model's data using Elasticsearch.
26+
27+
## Basic Usage
28+
29+
To index all the entries in an Eloquent model, use `addAllToIndex`:
30+
31+
Book::addAllToIndex();
32+
33+
You can also index a collection of models:
34+
35+
$books = Book::where('id', '<', 200)->get();
36+
$books->addToIndex();
37+
38+
You can index individual entries as well:
39+
40+
$book = Book::find($id);
41+
$book->addToIndex();
42+
43+
### Setting the Type Name
44+
45+
By default, Elastiquent will use the table name for your model as the type name for indexing. If you'd like to override it, you can with the `getTypeName` function.
46+
47+
48+
```php
49+
function getTypeName()
50+
{
51+
return 'custom\_type\_name';
52+
}
53+
```
54+
55+
### Setting Up / Tearing Down
56+
57+
If you don't want to manually set up your Elasticsearch types, you can do some basic set up and tear down commands via your Elastiquent models.
58+
59+
For instance, if you'd like to setup a model's type mapping, you can do:
60+
61+
Book::putMapping($ignoreConflicts = true);
62+
63+
To delete a mapping
64+
65+
Book::deleteMapping();
66+
67+
To rebuild (delete and re-add, useful when you make important changes to your mapping) the mapping:
68+
69+
Book::rebuildMapping();
70+
71+
### Document IDs
72+
73+
Elastiquent will use whatever is set as the `primaryKey` for your Eloquent models as the id for your Elasticsearch documents.
74+
75+
### Document Data
76+
77+
By default, Elastiquent will use the entire attribute array for your Elasticsearch documents. However, if you want to customize how your search documents are structured, you can set a `getIndexDocumentData` function that returns you own custom document array.
78+
79+
```php
80+
function getIndexDocumentData()
81+
{
82+
return array(
83+
'id' => $this->id,
84+
'title' => $this->title,
85+
'custom' => 'variable'
86+
);
87+
}
88+
```
89+
90+
### Setting Mapping Properties
91+
92+
You can set mapping properties in a
93+
94+
### More Options
95+
96+
By default, document sources are enabled. To turn document sources off, set a property in your Eloquent model:
97+
98+
protected $enableDocumentSource = false;
99+
100+
_Note that you must rebuild your mapping and re-index for this to take effect._
101+
102+
103+
104+
To check if the type for the Elastiquent model exists yet, use `typeExists`:
105+
106+
$typeExists = Book::typeExists();
107+
108+
### Using Elastiquent With Custom Collections
109+
110+
If you are using a custom collection with your Eloquent models, you just need to add the `ElastiquentCollectionTrait` to your collection so you can use `addToIndex`.
111+
112+
```php
113+
class MyCollection extends \Illuminate\Database\Eloquent\Collection {
114+
115+
use ElastiquentCollectionTrait;
116+
}
117+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php namespace Adamfairholm\Elasticquent;
2+
3+
class ElasticqueryCollection extends \Illuminate\Database\Eloquent\Collection {
4+
5+
use ElasticquentCollectionTrait;
6+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php namespace Adamfairholm\Elasticquent;
2+
3+
4+
/**
5+
* Elasticquent Collection Trait
6+
*
7+
* Elasticsearch functions that you
8+
* can run on collections of documents.
9+
*/
10+
trait ElasticquentCollectionTrait {
11+
12+
/**
13+
* Add To Index
14+
*
15+
* Add all documents in this collection to
16+
* to the Elasticsearch document index.
17+
*
18+
* @return mixed
19+
*/
20+
public function addToIndex()
21+
{
22+
if ($this->isEmpty()) {
23+
return null;
24+
}
25+
26+
$params = array();
27+
28+
foreach ($this->all() as $item) {
29+
30+
$params['body'][] = array(
31+
'index' => array(
32+
'_id' => $item->getKey()
33+
)
34+
);
35+
36+
$params['body'][] = array(
37+
'doc' => $item->getIndexDocumentData()
38+
);
39+
}
40+
41+
return $this->getElasticSearchClient()->bulk($params);
42+
}
43+
44+
/**
45+
* Get ElasticSearch Client
46+
*
47+
* @return Elasticsearch\Client
48+
*/
49+
public function getElasticSearchClient()
50+
{
51+
return new Elasticsearch\Client();
52+
}
53+
54+
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php namespace Adamfairholm\Elasticquent;
2+
3+
interface ElasticquentInterface {
4+
5+
/**
6+
* Get ElasticSearch Client
7+
*
8+
* @return Elasticsearch\Client
9+
*/
10+
public function getElasticSearchClient();
11+
12+
/**
13+
* New Collection
14+
*
15+
* @return
16+
*/
17+
public function newCollection(array $models = array());
18+
19+
/**
20+
* Get Index Name
21+
*
22+
* @return string
23+
*/
24+
public function getIndexName();
25+
26+
/**
27+
* Get Type Name
28+
*
29+
* @return string
30+
*/
31+
public function getTypeName();
32+
33+
/**
34+
* Uses Timestamps In Index
35+
*
36+
* @return void
37+
*/
38+
public function usesTimestampsInIndex();
39+
40+
/**
41+
* Get Mapping Properties
42+
*
43+
* @return array
44+
*/
45+
public function getMappingProperties();
46+
47+
/**
48+
* Set Mapping Properties
49+
*
50+
* @param array $mappingProperties
51+
* @return void
52+
*/
53+
public function setMappingProperties($mapping);
54+
55+
/**
56+
* Get Index Document Data
57+
*
58+
* Get the data that ElasticSearch will
59+
* index for this particular document.
60+
*
61+
* @return array
62+
*/
63+
public function getIndexDocumentData();
64+
65+
/**
66+
* Get Index Document Routing
67+
*
68+
* Get the routing string for this document.
69+
*
70+
* @return void
71+
*/
72+
public function getIndexDocumentRouting();
73+
74+
/**
75+
* Index Documents
76+
*
77+
* Index all documents in an Eloquent model.
78+
*
79+
* @param array $columns
80+
* @return void
81+
*/
82+
public static function addAllToIndex($columns = array('*'));
83+
84+
/**
85+
* Search a Type
86+
*
87+
*
88+
* @return void
89+
*/
90+
public static function search($query = array());
91+
92+
/**
93+
* Add to Search Index
94+
*
95+
* @return
96+
*/
97+
public function addToIndex();
98+
99+
/**
100+
* Remove From Search Index
101+
*
102+
* @return
103+
*/
104+
public function removeFromIndex();
105+
106+
/**
107+
* Get Search Document
108+
*
109+
* Retrieve an ElasticSearch document
110+
* for this enty.
111+
*
112+
* @return
113+
*/
114+
public function getIndexedDocument();
115+
116+
/**
117+
* Get Basic Elasticsearch Params
118+
*
119+
* Most Elasticsearch API calls need the index and
120+
* type passed in a parameter array.
121+
*
122+
* @param bool $getIdIfPossible
123+
* @return array
124+
*/
125+
function getBasicEsParams($getIdIfPossible = true);
126+
127+
/**
128+
* Put Mapping
129+
*
130+
* @param bool $ignoreConflicts
131+
* @return
132+
*/
133+
public static function putMapping($ignoreConflicts = false);
134+
135+
/**
136+
* Delete Mapping
137+
*
138+
* @return
139+
*/
140+
public static function deleteMapping();
141+
142+
/**
143+
* Rebuild Mapping
144+
*
145+
* This will delete and then re-add
146+
* the mapping for this model.
147+
*
148+
* @return
149+
*/
150+
public function rebuildMapping();
151+
152+
/**
153+
* Get Mapping
154+
*
155+
* Get our existing Elasticsearch mapping
156+
* for this model.
157+
*
158+
* @return
159+
*/
160+
public static function getMapping();
161+
162+
/**
163+
* Type Exists
164+
*
165+
* Does this type exist?
166+
*
167+
* @return bool
168+
*/
169+
public static function typeExists();
170+
171+
}

0 commit comments

Comments
 (0)