PHP code example of tamayo / stretchy

1. Go to this page and download the library: Download tamayo/stretchy library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tamayo / stretchy example snippets


        'Tamayo\Stretchy\StretchyServiceProvider'

		'Index'    => 'Tamayo\Stretchy\Facades\Index',
		'Document' => 'Tamayo\Stretchy\Facades\Document',
		'Stretchy' => 'Tamayo\Stretchy\Facades\Stretchy'

Index::create('foo');

Index::create('foo', function($index)
	{
		$index->shards(5);
		$index->replicas(1);
	});

Index::delete('foo');

Document::index('foo')
    ->type('tweet')
    ->id(13) // Optional (if not specified elastic will generate an unique id)
    ->insert([
        'username' => '@ericktamayo',
        'tweet'    => 'Hello world!'
    ]);

Document::index('foo')
    ->type('tweet')
    ->id(13)
    ->update(['tweet' => 'Hello world!!!']);

Document::index('foo')->type('tweet')->Id(13)->get();

Document::index('foo')->type('tweet')->Id(13)->delete();

Stretchy::search('foo')->match('bar', 'Stretchy')->get();

Stretchy::search('foo')
	->match('bar', 'baz', ['operator' => 'and', 'zero_terms_query' => 'all'])
	->get();

Stretchy::search('foo')
	->match('bar', 'Stretchy', function($match)
	{
		$match->operator('and');
		$match->zeroTermsQuery('all');
		$match->cutoffFrequency(0.001);
	})
	->get();

Stretchy::search('foo')->term('bar', 'baz')->get();

Stretchy::search('foo')->term('bar', 'baz', ['boost' => 2])->get();

Stretchy::search('foo')
	->term('bar', 'baz', function($term)
	{
		$term->boost(2);
	})
	->get();

Stretchy::search('foo')
	->bool(function($query)
	{
		$query->must(function($must)
		{
			$must->match('bar', 'baz');
		});

		$query->mustNot(function($mustNot)
		{
			$mustNot->match('bar', 'qux');
		});

		$query->should(function($should)
		{
			$should->match('bar', 'bah');
		});

		$query->minimumShouldMatch(1);
	})
	->get();
sh
php artisan config:publish tamayo/stretchy