Skip to content

Commit c7557e8

Browse files
committed
IP field type for migrations
1 parent d8ea338 commit c7557e8

File tree

2 files changed

+38
-40
lines changed

2 files changed

+38
-40
lines changed

README.md

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Elasticsearch in laravel as if it were native to Laravel, meaning:
1212
- Data returned as Collections
1313
- [Soft Deletes](#soft-deletes)
1414
- [Aggregations](#aggregation)
15-
- [Migrations](#schema/index)
15+
- [Migrations](#migrations)
1616
- ES features like [Geo Filtering](#geo) & [Regular Expressions](#regex-in-where)
1717

1818
- No need to write your own DSL queries ([unless you want to](#raw-dsl)!)
@@ -35,6 +35,7 @@ Installation
3535
===============
3636

3737
[Known] Elasticsearch compatible versions:
38+
3839
- 7.16
3940
- 8.0
4041

@@ -80,9 +81,6 @@ ES_API_KEY=
8081
ES_SSL_CERT=
8182
```
8283

83-
84-
85-
8684
<details>
8785
<summary>Example cloud config .env: (Click to expand)</summary>
8886

@@ -336,7 +334,7 @@ $stats = Product::whereNotIn('color', ['red', 'green'])->matrix(['price', 'order
336334

337335
<details>
338336
<summary>Matrix results return as: (Click to expand)</summary>
339-
337+
340338
```json
341339
{
342340
"matrix": {
@@ -381,7 +379,6 @@ $stats = Product::whereNotIn('color', ['red', 'green'])->matrix(['price', 'order
381379

382380
</details>
383381

384-
385382
### Ordering
386383

387384
When searching text fields Elasticsearch uses an internal scoring to rank and sort by the most relevant results as a
@@ -420,8 +417,6 @@ Pagination links (Blade)
420417
{{ $products->appends(request()->query())->links() }}
421418
```
422419

423-
424-
425420
Elasticsearch specific queries
426421
-----------------------------
427422

@@ -443,22 +438,22 @@ UserLog::where('status',7)->filterGeoBox('agent.geo',[-10,10],[10,-10])->get();
443438
Filters results that fall within a radius distance from a `point[lat,lon]`
444439

445440
- **Method**: `filterGeoPoint($field,$distance,$point)`
446-
- `$distance` is a string value of distance and distance-unit, see [https://www.elastic.co/guide/en/elasticsearch/reference/current/api-conventions.html#distance-units](distance units)
441+
- `$distance` is a string value of distance and distance-unit,
442+
see [https://www.elastic.co/guide/en/elasticsearch/reference/current/api-conventions.html#distance-units](distance units)
447443

448444
```php
449445
UserLog::where('status',7)->filterGeoPoint('agent.geo','20km',[0,0])->get();
450446
```
451447

452-
**Note:** the field **must be of type geo otherwise your [shards will fail](#error-all-shards-failed) **, make sure to set the geo field in your [migration](#migrations), ex:
448+
**Note:** the field **must be of type geo otherwise your [shards will fail](#error-all-shards-failed) **, make sure to
449+
set the geo field in your [migration](#migrations), ex:
453450

454451
```php
455452
Schema::create('user_logs',function (IndexBlueprint $index){
456453
$index->geo('agent.geo');
457454
});
458455
```
459456

460-
461-
462457
#### Regex (in where)
463458

464459
[Syntax](https://www.elastic.co/guide/en/elasticsearch/reference/current/regexp-syntax.html)
@@ -468,8 +463,6 @@ Product::whereRegex('color','bl(ue)?(ack)?')->get(); //Returns blue or black
468463
Product::whereRegex('color','bl...*')->get(); //Returns blue or black or blond or blush etc..
469464
```
470465

471-
472-
473466
Saving Models
474467
-------------
475468

@@ -609,15 +602,15 @@ $product->forceDelete();
609602

610603
```
611604

612-
613-
614605
Elasticsearching
615606
===============
616607

617608
The Search Query
618609
----------------
619610

620-
The search query is different from the `where()->get()` methods as search is performed over all (or selected) fields in the index. Building a search query is easy and intuitive to seasoned Eloquenters with a slight twist; simply static call off your model with `term()`, chain your ORM clauses, then end your chain with `search()` to perform your search, ie:
611+
The search query is different from the `where()->get()` methods as search is performed over all (or selected) fields in
612+
the index. Building a search query is easy and intuitive to seasoned Eloquenters with a slight twist; simply static call
613+
off your model with `term()`, chain your ORM clauses, then end your chain with `search()` to perform your search, ie:
621614

622615
```php
623616
MyModel::term('XYZ')->.........->search()
@@ -627,8 +620,9 @@ MyModel::term('XYZ')->.........->search()
627620

628621
**1.1 Simple example**
629622

630-
- To search across all the fields in the **books** index for '**eric**' (case-insensitive if the default analyser is set),
631-
- Results ordered by most relevant first (score in desc order)
623+
- To search across all the fields in the **books** index for '**eric**' (case-insensitive if the default analyser is
624+
set),
625+
- Results ordered by most relevant first (score in desc order)
632626

633627
```php
634628
Book::term('Eric')->search();
@@ -637,7 +631,8 @@ Book::term('Eric')->search();
637631
**1.2 Multiple terms**
638632

639633
- To search across all the fields in the **books** index for: **eric OR (lean AND startup)**
640-
- ***Note**: You can't start a search query chain with and/or and you can't have subsequent chained terms without and/or - **ordering matters***
634+
- ***Note**: You can't start a search query chain with and/or and you can't have subsequent chained terms without and/or
635+
- **ordering matters***
641636

642637
```php
643638
Book::term('Eric')->orTerm('Lean')->andTerm('Startup')->search();
@@ -668,7 +663,7 @@ Book::term('Eric')->fields(['title','author','description'])->search();
668663
- **title** is boosted by a factor of 3, search hits here will be the most relevant
669664
- **author** is boosted by a factor of 2, search hits here will be the second most relevant
670665
- **description** has no boost, search hits here will be the least relevant
671-
- *The results, as per the default, are ordered by most relevant first (score in desc order)*
666+
- *The results, as per the default, are ordered by most relevant first (score in desc order)*
672667

673668
```php
674669
Book::term('Eric')->field('title',3)->field('author',2)->field('description')->search();
@@ -678,9 +673,9 @@ Book::term('Eric')->field('title',3)->field('author',2)->field('description')->s
678673

679674
- Controls how many 'should' clauses the query should match
680675
- Caveats:
681-
- Fields must be specified in your query
682-
- You can have no standard clauses in your query (ex `where()`)
683-
- Won't work on SoftDelete enabled models
676+
- Fields must be specified in your query
677+
- You can have no standard clauses in your query (ex `where()`)
678+
- Won't work on SoftDelete enabled models
684679
- https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html
685680

686681
- Match at least 2 of the 3 terms:
@@ -692,28 +687,27 @@ Book::term('Eric')->orTerm('Lean')->orTerm('Startup')->field('title')->field('au
692687
**1.7 Min Score**
693688

694689
- Sets a min_score filter for the search
695-
- (Optional, float) Minimum 'relevance score' for matching documents. Documents with a lower 'score' are not included in the search results.
690+
- (Optional, float) Minimum 'relevance score' for matching documents. Documents with a lower 'score' are not included in
691+
the search results.
696692

697693
```php
698694
Book::term('Eric')->field('title',3)->field('author',2)->field('description')->minScore(2.1)->search();
699695
```
700696

701-
**1.8 Blend Search with [most] standard eloquent queries**
697+
**1.8 Blend Search with [most] standard eloquent queries**
702698

703699
- Search for 'david' where field `is_active` is `true`:
704700

705701
```php
706702
Book::term('David')->field('title',3)->field('author',2)->field('description')->minScore(2.1)->where('is_active',true)->search();
707703
```
708704

709-
710-
711705
### 2. FuzzyTerm:
712706

713-
- Same usage as `term()` `andTerm()` `orTerm()` but as
714-
- `fuzzyTerm()`
715-
- `orFuzzyTerm()`
716-
- `andFuzzyTerm()`
707+
- Same usage as `term()` `andTerm()` `orTerm()` but as
708+
- `fuzzyTerm()`
709+
- `orFuzzyTerm()`
710+
- `andFuzzyTerm()`
717711

718712
```php
719713
Book::fuzzyTerm('quikc')->orFuzzyTerm('brwn')->andFuzzyTerm('foks')->search();
@@ -723,17 +717,15 @@ Book::fuzzyTerm('quikc')->orFuzzyTerm('brwn')->andFuzzyTerm('foks')->search();
723717

724718
https://www.elastic.co/guide/en/elasticsearch/reference/current/regexp-syntax.html
725719

726-
- Same usage as `term()` `andTerm()` `orTerm()` but as
727-
- `regEx()`
728-
- `orRegEx()`
729-
- `andRegEx()`
720+
- Same usage as `term()` `andTerm()` `orTerm()` but as
721+
- `regEx()`
722+
- `orRegEx()`
723+
- `andRegEx()`
730724

731725
```php
732726
Book::regEx('joh?n(ath[oa]n)')->andRegEx('doey*')->search();
733727
```
734728

735-
736-
737729
Mutators & Casting
738730
-------------
739731

@@ -804,6 +796,7 @@ class Company extends Model
804796
}
805797

806798
```
799+
807800
</details>
808801

809802

@@ -1107,7 +1100,6 @@ class UserProfile extends Model
11071100

11081101
</details>
11091102

1110-
11111103
- Company (as example before) where user has the field `company_id` as $company->_id
11121104
- Avatar: (as before) having `imageable_id` as $user->id and `imageable_type` as 'App\Models\User'
11131105
- Photo: (as before) having `photoable_id` as $user->id and `photoable_type` as 'App\Models\User'
@@ -1343,7 +1335,8 @@ values you have two options:
13431335

13441336
Elasticsearch can not order by text fields due to how the values are indexed and tokenized. If you do not define a
13451337
string value upfront in your [Schema](#Schema/index) then Elasticsearch will default to saving the field as a `text`
1346-
field. If you try to sort by that field the database engine will fail with the error: [all shards failed](#a-error-all-shards-failed). Options:
1338+
field. If you try to sort by that field the database engine will fail with the
1339+
error: [all shards failed](#a-error-all-shards-failed). Options:
13471340

13481341
1. If you do not need to search the text within the field and ordering is important, then use a `keyword` field type: To
13491342
do so define your index upfront in the [Schema](#Schema/index) and set `$index->keyword('email')`

src/Schema/IndexBlueprint.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ public function alias($field, $path): Definitions\FieldDefinition
9898
return $this->addField('alias', $field, ['path' => $path]);
9999
}
100100

101+
public function ip($field, $path): Definitions\FieldDefinition
102+
{
103+
return $this->addField('ip', $field, ['path' => $path]);
104+
}
105+
101106
public function mapProperty($field, $type): Definitions\FieldDefinition
102107
{
103108
return $this->addField($type, $field);

0 commit comments

Comments
 (0)