PHP code example of shabushabu / laravel-paradedb-search

1. Go to this page and download the library: Download shabushabu/laravel-paradedb-search 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/ */

    

shabushabu / laravel-paradedb-search example snippets


return [
    'index_suffix' => env('PG_SEARCH_INDEX_SUFFIX', '_idx'),
    'highlighting_tag' => env('PG_SEARCH_HIGHLIGHTING_TAG', '<b></b>'),
];

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('products', static function (Blueprint $table) {
            // all your product fields
            
           $table->index(['id', 'name', 'description', 'currency', 'amount', 'is_available', 'options', 'size', 'created_at', 'deleted_at'], 'products_bm25_idx')
                ->algorithm('bm25')
                ->with([
                    'key_field' => 'id'
                ]);
        });
    }
};

use ShabuShabu\ParadeDB\TantivyQL\Query;

Query::string()->where('description', 'keyboard')->get();

// results in: description:keyboard

Query::string()
    ->where('description', ['keyboard', 'toy'])
    ->get();

// results in: description:IN [keyboard, toy]

(string) Query::string()
    ->where('category', 'electronics')
    ->whereNot('description', 'keyboard');

// results in: category:electronics AND NOT description:keyboard

Query::string()
    ->where('description', 'keyboard', boost: 1)
    ->get();

// results in: description:keyboard^1

Query::string()
    ->where('description', 'ergonomic keyboard', slop: 1)
    ->get();

// results in: description:"ergonomic keyboard"~1

Query::string()
    ->where('description', ['keyboard', 'toy'])
    ->where(
        fn (Builder $builder) => $builder
            ->where('category', 'electronics')
            ->orWhere('tag', 'office')
    )
    ->get();

// results in: description:IN [keyboard, toy] AND (category:electronics OR tag:office)

use ShabuShabu\ParadeDB\TantivyQL\Operators\Filter;

Query::string()
    ->whereFilter('rating', Filter::equals, 4)
    ->get();

// results in: rating:4

use ShabuShabu\ParadeDB\TantivyQL\Operators\Filter;

Query::string()
    ->whereFilter('is_available', '=', false)
    ->get();

// results in: is_available:false

use ShabuShabu\ParadeDB\TantivyQL\Operators\Filter;

Query::string()
    ->whereFilter('rating', '>', 4)
    ->get();

// results in: rating:>4

use ShabuShabu\ParadeDB\TantivyQL\Operators\Range;

Query::string()
    ->whereFilter('rating', Range::

use ShabuShabu\ParadeDB\TantivyQL\Operators\Range;

Query::string()
    ->whereFilter('rating', Range::excludeAll, [2, 5])
    ->get();

// results in: rating:{2 TO 5}

Product::query()
    ->where('description', '@@@', 'shoes')
    ->get();

use ShabuShabu\ParadeDB\Expressions\JsonB;

Product::query()
    ->where('id', '@@@', new JsonB([
        'fuzzy_term' => [
            'field' => 'description',
            'value' => 'shoez'
        ]       
    ]))
    ->get();

use ShabuShabu\ParadeDB\Expressions\All;
use ShabuShabu\ParadeDB\Expressions\Term;
use ShabuShabu\ParadeDB\Expressions\Boolean;

Product::query()
    ->where('id', '@@@', new Boolean(
        should: new All(),
        mustNot: new Term('description', 'shoes')
    ))
    ->get();

use ShabuShabu\ParadeDB\Expressions\Term;
use ShabuShabu\ParadeDB\Expressions\Exists;
use ShabuShabu\ParadeDB\Expressions\Boolean;

Product::query()
    ->where('id', '@@@', new Boolean(
        must: [
            new Term('description', 'shoes'),
            new Exists('rating')
        ],
    ))
    ->limit(5)
    ->get();

use ShabuShabu\ParadeDB\Expressions\Blank;

Product::query()
    ->where('id', '@@@', new Blank())
    ->get();

use ShabuShabu\ParadeDB\Expressions\Term;
use ShabuShabu\ParadeDB\Expressions\Boost;
use ShabuShabu\ParadeDB\Expressions\Boolean;

Product::query()
    ->where('id', '@@@', new Boolean(
        should: [
            new Term('description', 'shoes'),
            new Boost(new Term('description', 'running'), 2.0)
        ]
    ))
    ->get();

use ShabuShabu\ParadeDB\Expressions\All;
use ShabuShabu\ParadeDB\Expressions\Score;
use ShabuShabu\ParadeDB\Expressions\Boolean;
use ShabuShabu\ParadeDB\Expressions\ConstScore;

Product::query()
    ->selectWithScore()
    ->where('id', '@@@', new Boolean(
        should: [
            new ConstScore(new Term('description', 'shoes'), 1.0),
            new Term('description', 'running'),
        ]   
    ))
    ->get();

use ShabuShabu\ParadeDB\Expressions\Term;
use ShabuShabu\ParadeDB\Expressions\Score;
use ShabuShabu\ParadeDB\Expressions\DisjunctionMax;

Product::query()
    ->select(['*', new Score()])
    ->where('id', '@@@', new DisjunctionMax([
        new Term('description', 'shoes'),
        new Term('description', 'running'),
    ]))
    ->get();

use ShabuShabu\ParadeDB\TantivyQL\Query;
use ShabuShabu\ParadeDB\Expressions\Term;
use ShabuShabu\ParadeDB\Expressions\Score;
use ShabuShabu\ParadeDB\Expressions\DisjunctionMax;
    
Product::query()
    ->select(['*', new Score()])
    ->where('id', '@@@', DisjunctionMax::query()
        ->add(Query::string()->where('description', 'shoes'))
        ->add('description:running')
        ->tieBreaker(1.2)
    )
    ->get();

use ShabuShabu\ParadeDB\TantivyQL\Query;
use ShabuShabu\ParadeDB\Expressions\Term;
use ShabuShabu\ParadeDB\Expressions\Score;
use ShabuShabu\ParadeDB\Expressions\DisjunctionMax;

Product::query()
    ->select(['*', new Score()])
    ->where('id', '@@@', DisjunctionMax::query()
        ->add(Query::string()->where('description', 'shoes'))
        ->add('description:running', when: false)
    )
    ->get();

use ShabuShabu\ParadeDB\Expressions\FuzzyTerm;

Product::query()
    ->where('id', '@@@', new FuzzyTerm('description', 'shoez'))
    ->get();

use ShabuShabu\ParadeDB\Expressions\FuzzyPhrase;

Product::query()
    ->where('id', '@@@', new FuzzyPhrase('description', 'ruining shoez'))
    ->get();

use ShabuShabu\ParadeDB\Expressions\Parse;

Product::query()
    ->where('id', '@@@', new Parse('description:"running shoes" OR category:footwear'))
    ->get();

use ShabuShabu\ParadeDB\TantivyQL\Query;
use ShabuShabu\ParadeDB\Expressions\Parse;

Product::query()
    ->where('id', '@@@', new Parse(
        Query::string()
            ->where('description', 'running shoes')
            ->orWhere('category', 'footwear')
    ))
    ->get();

use ShabuShabu\ParadeDB\Expressions\ParseWithField;

Product::query()
    ->where('id', '@@@', new ParseWithField(
        field: 'description', 
        query: 'speaker bluetooth', 
        conjunctionMode: true,
    ))
    ->get();

use ShabuShabu\ParadeDB\Expressions\Snippet;

Product::query()
    ->select(['id', new Snippet('description')])
    ->where('description', '@@@', 'shoes')
    ->limit(5)
    ->get();

use ShabuShabu\ParadeDB\Expressions\Phrase;

Product::query()
    ->where('id', '@@@', new Phrase(
        field: 'description',
        phrases: ['sleek', 'shoes'],
        slop: 1,
    ))
    ->get();

use ShabuShabu\ParadeDB\Expressions\PhrasePrefix;

Product::query()
    ->where('id', '@@@', new PhrasePrefix('description', ['running', 'sh']))
    ->get();

use ShabuShabu\ParadeDB\Expressions\Range;
use ShabuShabu\ParadeDB\Expressions\Ranges\Int4;
use ShabuShabu\ParadeDB\Expressions\Ranges\Bounds;

Product::query()
    ->where('id', '@@@', new Range('rating', new Int4(1, 3, Bounds::

use ShabuShabu\ParadeDB\Expressions\Term;
use ShabuShabu\ParadeDB\Expressions\Boolean;
use ShabuShabu\ParadeDB\Expressions\RangeTerm;

Product::query()
    ->where('id', '@@@', new Boolean(
        must: [
            new RangeTerm('weight_range', 1),
            new Term('category', 'footwear')
        ]
    ))
    ->get();

use ShabuShabu\ParadeDB\Expressions\RangeTerm;
use ShabuShabu\ParadeDB\Expressions\Ranges\Int4;
use ShabuShabu\ParadeDB\Expressions\Ranges\Relation;

Product::query()
    ->where('id', '@@@', new RangeTerm(
        field: 'weight_range',
        term: new Int4(10, 12),
        relation: Relation::intersects,
    ))
    ->get();

use ShabuShabu\ParadeDB\Expressions\Regex;

Product::query()
    ->where('id', '@@@', new Regex('description', '(plush|leather)'))
    ->get();

use ShabuShabu\ParadeDB\Expressions\Term;

Product::query()
    ->where('id', '@@@', new Term('rating', 4))
    ->get();

use ShabuShabu\ParadeDB\Expressions\Term;
use ShabuShabu\ParadeDB\Expressions\TermSet;

Product::query()
    ->where('id', '@@@', new TermSet([
        new Term('description', 'shoes'),
        new Term('description', 'running'),
    ]))
    ->get();

Product::query()
    ->where('id', '@@@', TermSet::query()
        ->add(new Term('description', 'shoes'))
        ->add(new Term('description', 'running'))
    )
    ->get();

$when = false;

Product::query()
    ->where('id', '@@@', TermSet::query()
        ->add(new Term('description', 'shoes'), $when)
    )
    ->get();

use ShabuShabu\ParadeDB\Expressions\Term;
use ShabuShabu\ParadeDB\Expressions\Boolean;
use ShabuShabu\ParadeDB\Expressions\FuzzyTerm;
use ShabuShabu\ParadeDB\Expressions\Ranges\Int4;
use ShabuShabu\ParadeDB\Expressions\Ranges\Bounds;

Product::query()
    ->where('id', '@@@', new Boolean(
        should: new Term('description', 'headphones'),
        must: [
            new Term('category', 'electronics'),
            new FuzzyTerm('description', 'bluetooht'),
        ],
        mustNot: new Range('rating', new Int4(null, 2, Bounds::excludeAll)),
    ))
    ->get();

use ShabuShabu\ParadeDB\Expressions\Term;
use ShabuShabu\ParadeDB\Operators\FullText;
use ShabuShabu\ParadeDB\Expressions\Boolean;
use ShabuShabu\ParadeDB\Expressions\FuzzyTerm;
use ShabuShabu\ParadeDB\Expressions\Ranges\Int4;
use ShabuShabu\ParadeDB\Expressions\Ranges\Bounds;

Product::query()
    ->where('id', FullText::search->value, Boolean::query()
        ->should(new Term('description', 'headphones'))
        ->must(new Term('category', 'electronics'))
        ->must(new FuzzyTerm('description', 'bluetooht'))
        ->mustNot(new Range('rating', new Int4(null, 2, Bounds::excludeAll)))
    )
    ->get();

use ShabuShabu\ParadeDB\Expressions\Term;
use ShabuShabu\ParadeDB\Expressions\Boolean;
use ShabuShabu\ParadeDB\Expressions\FuzzyTerm;
use ShabuShabu\ParadeDB\Expressions\Ranges\Int4;
use ShabuShabu\ParadeDB\Expressions\Ranges\Bounds;

$when = false;

Product::query()
    ->where('id', '@@@', Boolean::query()
        ->should(new Term('description', 'headphones'))
        ->must(new Term('category', 'electronics'))
        ->must(new FuzzyTerm('description', 'bluetooht'), $when)
        ->mustNot(new Range('rating', new Int4(null, 2, Bounds::excludeAll)))
    )
    ->get();

use ShabuShabu\ParadeDB\Expressions\Score;

Product::query()
    ->addSelect(new Score())
    ->where('description', '@@@', 'shoes')
    ->orderBy(new Score())
    ->limit(5)
    ->get();

use ShabuShabu\ParadeDB\Expressions\MoreLikeThis;

Product::query()
    ->where('id', '@@@', new MoreLikeThis(
        idOrFields: 3,
        minTermFrequency: 1,
    ))
    ->get();

use ShabuShabu\ParadeDB\Expressions\MoreLikeThis;

Product::query()
    ->where('id', '@@@', new MoreLikeThis(
        idOrFields: ['description' => 'shoes'], 
        minDocFrequency: 0, 
        maxDocFrequency: 100, 
        minTermFrequency: 1,
    ))
    ->get();

use ShabuShabu\ParadeDB\Expressions\MoreLikeThis;

Product::query()
    ->whereSearch(new MoreLikeThis(idOrFields: 3, minTermFrequency: 1))
    ->get();

use Tpetry\QueryExpressions\Value\Value;
use ShabuShabu\ParadeDB\Expressions\Rank;
use ShabuShabu\ParadeDB\Expressions\Score;
use Tpetry\QueryExpressions\Language\Alias;
use ShabuShabu\ParadeDB\Operators\Distance;
use ShabuShabu\ParadeDB\Expressions\Similarity;
use Tpetry\QueryExpressions\Operator\Arithmetic\Add;
use Tpetry\QueryExpressions\Operator\Arithmetic\Divide;
use Tpetry\QueryExpressions\Function\Conditional\Coalesce;

Product::query()
    ->withExpression('semantic_search', Product::query()
        ->select([
            'id',
            new Alias(new Rank([
                [new Similarity('embedding', Distance::cosine, [1, 2, 3]), 'asc']
            ]), 'rank'),
        ])
        ->orderBy(new Similarity('embedding', Distance::cosine, [1, 2, 3]))
        ->limit(20)
    )
    ->withExpression('bm25_search', Product::query()
        ->select([
            'id', 
            new Alias(new Rank([new Score(), 'asc']), 'rank'),
        ])
        ->where('description', '@@@', 'keyboard')
        ->limit(20)
    )
    ->select([
        new Alias(new Coalesce(['semantic_search.id', 'bm25_search.id']), 'id'),
        new Alias(new Add(
            new Coalesce([
                new Divide(new Value(1.0), new Add(new Value(60), 'semantic_search.rank')),
                new Value(0.0),
            ]),
            new Coalesce([
                new Divide(new Value(1.0), new Add(new Value(60), 'bm25_search.rank')),
                new Value(0.0),
            ]),
        ), 'score'),
        'products.description',
        'products.embedding'
    ])
    ->from('semantic_search')
    ->join('bm25_search', 'semantic_search.id', '=', 'bm25_search.id', 'full outer')
    ->join('products', 'products.id', '=', new Coalesce(['semantic_search.id', 'bm25_search.id']))
    ->orderByDesc('score')
    ->orderBy('description')
    ->limit(5);
bash
php artisan paradedb:help
bash
composer analyse
bash
php artisan paradedb:test-table create