Git Product home page Git Product logo

searchable's Introduction

Build Status

Laravel MySQL fulltext search

This package creates a MySQL fulltext index for models and enables you to search through those.

Requirements

  • Laravel >= 5.7
  • MySQL >= 5.6 / MariaDB >= 10.0.15

Important!

In config/database.php set

'mysql' => [
...
    'strict' => false,
...
]

Install

  1. Install with composer composer require provision/searchable.
  2. Publish migrations and config php artisan vendor:publish --tag=searchable
  3. Migrate the database php artisan migrate

Usage

The package uses a model observer to update the index when models change. If you want to run a full index you can use the console commands.

Models

Add the SearchableTrait trait to the model you want to have indexed and define the columns you'd like to index as title and content.

Example

class Clients extends Model
{

    use \ProVision\Searchable\Traits\SearchableTrait;

    /**
     * @inheritDoc
     */
    protected function getSearchableTitleColumns(): array
    {
        return [
            'name'
        ];
    }

    /**
     * @inheritDoc
     */
    protected function getSearchableContentColumns(): array
    {
        return [
            'description',
            'address',
            'vat_number',
            'contacts.value',
            'contactPersons.first_name',
            'contactPersons.last_name',
            'contactPersons.contacts.value',
        ];
    }

}

You can use a dot notation to query relationships for the model, like contacts.value.

Relation model indexing

On related model for indexing use SearchableRelationTrait and method getSearchableRelationName to return relation name.

Listen for changes on relation and update parent model

Example

class Contact extends Model
{
    use \ProVision\Searchable\Traits\SearchableRelationTrait;

     /**
     * @return MorphTo
     */
    public function contactable()
    {
        return $this->morphTo();
    }

    /**
     * @inheritDoc
     */
    static function getSearchableRelationName(): string
    {
        return 'contactable';
    }
}

Searching

You can search using the search method.

$clientsCollection = Clients::search('John Doe')->paginate();

Search with specific fulltext search mode

use ProVision\Searchable\SearchableModes;
---
$clientsCollection = Clients::search('John Doe', SearchableModes::Boolean)->paginate();

Available modes

  • NaturalLanguage - IN NATURAL LANGUAGE MODE
  • NaturalLanguageWithQueryExpression - IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION
  • Boolean - IN BOOLEAN MODE
  • QueryExpression - WITH QUERY EXPANSION

MySQL fulltext search documentation: https://dev.mysql.com/doc/refman/8.0/en/fulltext-search.html

Search with relations & additional wheres

$clientsCollection = Clients::search('John Doe')->where('active', 1)->with(['contacts'])->paginate();

Order searchable score

$clientsCollection = Clients::search('John Doe')->searchableOrder('asc')->paginate();

Available options:

  • ASC
  • DESC

Commands

searchable:index

Index all models for a certain class

 php artisan  searchable:index
 
Usage:
  searchable:index <model_class> {id?}

Arguments:
  model_class           Classname of the model to index
  id                    Model id to index (optional)

Example
  • Indexing all clients

php artisan searchable:index "\App\Models\Client"

  • Indexing specific client by id

php artisan searchable:index "\App\Models\Client" 1

searchable:unindex

UnIndex all models for a certain class

 php artisan  searchable:unindex
 
Usage:
  searchable:unindex <model_class> {id?}

Arguments:
  model_class           Classname of the model to index
  id                    Model id to unindex (optional)

Example
  • UnIndexing all clients

php artisan searchable:unindex "\App\Models\Client"

  • UnIndexing specific client by id

php artisan searchable:unindex "\App\Models\Client" 1

Config options

db_connection

Choose the database connection to use, defaults to the default database connection. When you are NOT using the default database connection, this MUST be set before running the migration to work correctly.

table_name

Table name of index

command_prefix

Prefix of commands

weight.title, weight.content

Results on title or content are weighted in the results. Search result score is multiplied by the weight in this config

cleaners

Clean searching keywords for prevent breaking the MySQL query.

Testing

$ composer test

searchable's People

Contributors

andreiio avatar veneliniliev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

searchable's Issues

Not returning results as should have

When I type "cultivator" return 0 result. Type "cultivators", returns 23

If I type "tractor", nothing returns. Type tractors, it returns a result.
So, part of the word is not recognized, therefore kind of useless.

image

image

Select list is not in GROUP BY

I checked your closed ticket and changed the database config file set to strict=false and mode as suggested, yet, still getting the same error.

Mysql : mysql Ver 8.0.22 for osx10.16 on x86_64 (Homebrew)

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #120 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'agri.searchable_index.title' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select count(*) as aggregate from (select ads.*, (
                1.5 * (MATCH (searchable_index.title) AGAINST (some deere )) +
                1 * (MATCH (searchable_index.title, searchable_index.content) AGAINST (some deere ))
                ) as searchableScore from `ads` left join `searchable_index` on `searchable_index`.`searchable_id` = `ads`.`id` and `searchable_index`.`searchable_type` = App\Ad where MATCH(searchable_index.title, searchable_index.content) AGAINST(some deere ) group by `ads`.`id`) as `aggregate_table`)
                ```

Is there a way to partly search words?

Assume I have a search index containing 15 Leafbrook Crescent. By default I have to search for Leafbrook before the result shows up. Is there a way to return the result when I search for Leaf only?

Undefined object: 7 ERROR: type "fulltext_title" does not exist

Hello,

Is anyone has a problem like me, while i run command php artisan migrate, this error will appear.

SQLSTATE[42704]: Undefined object: 7 ERROR: type "fulltext_title" does not exist at character 43 (SQL: ALTER TABLE searchable_index ADD FULLTEXT fulltext_title(title))

Please help,
Thankyou

Can't return response as JSON after saving new model

Hello,

I'm using your package and it works fine, however, I've faced an issue after saving a new Model and returning it as JSON.

When your trait update the indexes of the model being saved, something happens that you can't return your model directly into your response as json.

For example:

In my controller on the Store method:

$modelTest = new ModelTest(); // it uses your trait.

$modelTest->title = 'test';
$modelTest->description = 'apsdoj';

$modelTest->save();

return response()->json('modelTest', $modelTest, 201); // here it throws an error saying "Recursion detected".

That's it, to bypass that I've made another select to the model's id created and then I returned it.

Do you have an idea of what can it be?

Thanks.

Expression #14 of SELECT list is not in GROUP BY clause and contains nonaggregated column

I've installed the package and generated the indexes as per the instructions. The indices are visible in searchable_index but whenever I try to search I'm getting a 500 error of 1055 Expression #14 of SELECT list is not in GROUP BY clause and contains nonaggregated column.

The full error is "message": "SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #14 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'app.searchable_index.title' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select models.*, (\n 1.5 * (MATCH (searchable_index.title) AGAINST (BTT )) +\n 1 * (MATCH (searchable_index.title, searchable_index.content) AGAINST (BTT ))\n ) as searchableScore frommodelsleft joinsearchable_indexonsearchable_index.searchable_id=models.idandsearchable_index.searchable_type= App\\Model where MATCH(searchable_index.title, searchable_index.content) AGAINST(BTT ) group bymodels.idorder bysearchableScoredesc)",

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.