PHPackages                             foryoufeng/elasticsearch - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. foryoufeng/elasticsearch

ActivePackage[Search &amp; Filtering](/categories/search)

foryoufeng/elasticsearch
========================

Laravel, Lumen and Native php elasticseach query builder to build complex queries using an elegant syntax

1.4.0(7y ago)0242MITPHPPHP &gt;=5.6.6

Since May 22Pushed 7y ago1 watchersCompare

[ Source](https://github.com/foryoufeng/Elasticsearch)[ Packagist](https://packagist.org/packages/foryoufeng/elasticsearch)[ Docs](https://github.com/foryoufeng)[ RSS](/packages/foryoufeng-elasticsearch/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (6)Versions (9)Used By (0)

Laravel, Lumen and Native php elasticseach query builder to build complex queries/laravel使用elasticseach进行全文检索
-------------------------------------------------------------------------------------------------------------

[](#laravel-lumen-and-native-php-elasticseach-query-builder-to-build-complex-querieslaravel使用elasticseach进行全文检索)

[中文文档](readme_zh_CN.md)
-----------------------

[](#中文文档)

- Keeps you away from wasting your time by replacing array queries with a simple and elegant syntax you will love.
- Elasticsearch data model for types and indices inspired from laravel eloquent.
- Feeling free to create, drop, mapping and reindexing through easy artisan console commands.
- Lumen framework support.
- Native php and composer based applications support.
- Can be used as a [laravel scout](https://laravel.com/docs/5.4/scout) driver.
- Dealing with multiple elasticsearch connections at the same time.
- Awesome pagination based on [LengthAwarePagination](https://github.com/illuminate/pagination).
- Caching queries using a caching layer over query builder built on [laravel cache](https://laravel.com/docs/5.4/cache).
- you also can see [basemkhirat/elasticsearch](https://github.com/basemkhirat/elasticsearch) &amp;&amp; [elastic/elasticsearch-php](https://github.com/elastic/elasticsearch-php)

Requirements
------------

[](#requirements)

- `php` &gt;= 5.6.6
- `laravel/laravel` &gt;= 5.\* or `laravel/lumen` &gt;= 5.\* or `composer application`

Documentation
-------------

[](#documentation)

Installation
------------

[](#installation)

### Laravel Installation

[](#laravel-installation)

##### 1) Install package using composer.

[](#1-install-package-using-composer)

```
$ composer require foryoufeng/elasticsearch
```

##### 2) Add package service provider (&lt; laravel 5.5).

[](#2-add-package-service-provider--laravel-55)

```
Foryoufeng\Elasticsearch\ElasticsearchServiceProvider::class
```

##### 3) Add package alias (&lt; laravel 5.5).

[](#3-add-package-alias--laravel-55)

```
'ES' => Foryoufeng\Elasticsearch\Facades\ES::class
```

##### 4) Publishing.

[](#4-publishing)

```
$ php artisan vendor:publish --provider="Foryoufeng\Elasticsearch\ElasticsearchServiceProvider"
```

Configuration (Laravel &amp; Lumen)
-----------------------------------

[](#configuration-laravel--lumen)

After publishing, two configuration files will be created.

- `config/es.php` where you can add more than one elasticsearch server.

```
# Here you can define the default connection name.

'default' => env('ELASTIC_CONNECTION', 'default'),

# Here you can define your connections.

'connections' => [
	'default' => [
	    'servers' => [
	        [
	            "host" => env("ELASTIC_HOST", "127.0.0.1"),
	            "port" => env("ELASTIC_PORT", 9200),
	            'user' => env('ELASTIC_USER', ''),
	            'pass' => env('ELASTIC_PASS', ''),
	            'scheme' => env('ELASTIC_SCHEME', 'http'),
	        ]
	    ],

		// Custom handlers
		// 'handler' => new MyCustomHandler(),

		'index' => env('ELASTIC_INDEX', 'my_index')
	]
],

# Here you can define your indices.

'indices' => [
	'my_index_1' => [
	    "aliases" => [
	        "my_index"
	    ],
	    'settings' => [
	        "number_of_shards" => 1,
	        "number_of_replicas" => 0,
	    ],
	    'mappings' => [
	        'posts' => [
                'properties' => [
                    'title' => [
                        'type' => 'string'
                    ]
                ]
	        ]
	    ]
	]
]
```

Usage as a Laravel Scout driver
-------------------------------

[](#usage-as-a-laravel-scout-driver)

First, follow [Laravel Scout installation](https://laravel.com/docs/5.4/scout#installation).

All you have to do is updating these lines in `config/scout.php` configuration file.

```
# change the default driver to 'es'

'driver' => env('SCOUT_DRIVER', 'es'),

# link `es` driver with default elasticsearch connection in config/es.php

'es' => [
    'connection' => env('ELASTIC_CONNECTION', 'default'),
],
```

Have a look at [laravel Scout documentation](https://laravel.com/docs/5.4/scout#configuration).

Scout search
------------

[](#scout-search)

now you can use between or in and so on method such as

```

$res=Goods::search('test')
            ->where('shop_price', ['between'=>[650,5560]])
            ->paginate(10);
        $res->each(function ($item) {
            print_r($item->goods_name.'===>'.$item->shop_price."\n");
        });

```

// more infomation you can see the test code

[ScoutSearchTest.php](./tests/ScoutSearchTest.php).

---

Elasticsearch data model
------------------------

[](#elasticsearch-data-model)

Each index type has a corresponding "Model" which is used to interact with that type. Models allow you to query for data in your types or indices, as well as insert new documents into the type.

##### Basic usage

[](#basic-usage)

```
