Skip to content

Commit 4807e81

Browse files
committed
Update readme.
1 parent 9264601 commit 4807e81

File tree

1 file changed

+118
-1
lines changed

1 file changed

+118
-1
lines changed

README.md

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,124 @@ Specify the database connection that should be used to access indexed documents
5454

5555
## Configuration
5656

57-
TBD
57+
### Configuring PostgreSQL
58+
59+
Make sure that an appropriate [default text search configuration](https://www.postgresql.org/docs/9.5/static/runtime-config-client.html#GUC-DEFAULT-TEXT-SEARCH-CONFIG) is set globbaly (in `postgresql.conf`), for a particular database (`ALTER DATABASE ... SET ...`) or alternatively set `default_text_search_config` in each session.
60+
61+
To check the current value
62+
63+
```sql
64+
SHOW default_text_search_config;
65+
66+
```
67+
68+
69+
70+
71+
### Prepare the schema
72+
73+
By default the engine expects that parsed documents (model data) are stored in the same table as the Model in a column `searchable` of type `tsvector`. You'd need to create this column and an index in your schema. You can choose between `GIN` and `GiST` indexes in PostgreSQL.
74+
75+
```php
76+
class CreatePostsTable extends Migration
77+
{
78+
public function up()
79+
{
80+
Schema::create('posts', function (Blueprint $table) {
81+
$table->increments('id');
82+
$table->text('title');
83+
$table->text('content')->nullable();
84+
$table->integer('user_id');
85+
$table->timestamps();
86+
});
87+
88+
DB::statement('ALTER TABLE posts ADD searchable tsvector NULL');
89+
DB::statement('CREATE INDEX posts_searchable_index ON posts USING GIN (searchable)');
90+
// Or alternatively
91+
// DB::statement('CREATE INDEX posts_searchable_index ON posts USING GIST (searchable)');
92+
}
93+
94+
public function down()
95+
{
96+
Schema::drop('posts');
97+
}
98+
}
99+
```
100+
101+
### Configuring Searchable Data
102+
103+
In addition to Model's attributes you can bring other any other data to the index document. I.e. a list of Tags for a Post.
104+
105+
```php
106+
public function toSearchableArray()
107+
{
108+
return [
109+
'title' => $this->title,
110+
'content' => $this->content,
111+
'author' => $this->user->name,
112+
'tags' => $this->tags->pluck('tag')->implode(' '),
113+
];
114+
}
115+
116+
```
117+
118+
### Configuring the Model
119+
120+
You may fine tune the engine behavior for a particular Model by implemeting `searchableOptions()` in your Model.
121+
122+
```php
123+
class Post extends Model
124+
{
125+
use Searchable;
126+
127+
...
128+
public function searchableOptions()
129+
{
130+
return [
131+
// You may wish to change the default name of the column
132+
// that holds parsed documents
133+
'column' => 'indexable',
134+
// You may want to store the index outside of the Model table
135+
// In that case let the engine know by setting this parameter to true.
136+
'external' => true,
137+
// If you don't want scout to maintain the index for you
138+
// You can turn it off either for a Model or globally
139+
'maintain_index' => true,
140+
// Ranking groups that will be assigned to fields
141+
// when document is being parsed.
142+
// Available groups: A, B, C and D.
143+
'rank' => [
144+
'fields' => [
145+
'title' => 'A',
146+
'content' => 'B',
147+
'author' => 'D',
148+
'tags' => 'C',
149+
],
150+
// Ranking weights for searches.
151+
// [D-weight, C-weight, B-weight, A-weight].
152+
// Default [0.1, 0.2, 0.4, 1.0].
153+
'weights' => [0.1, 0.2, 0.4, 1.0],
154+
// Ranking function [ts_rank | ts_rank_cd]. Default ts_rank.
155+
'function' => 'ts_rank_cd',
156+
// Normalization index. Default 0.
157+
'normalization' => 32,
158+
],
159+
];
160+
}
161+
}
162+
...
163+
```
164+
165+
If you decide to keep your Model's index outside of the Model's table you can let engine know that you want to push additional fields in the index table that you can than use to filter the result set by using `where()` with the Scout `Builder`. In this case you'd need to implement `searchableAdditionalArray()` on your Model. Of course the schema for the external table should include these additional columns.
166+
167+
```php
168+
public function searchableAdditionalArray()
169+
{
170+
return [
171+
'user_id' => $this->user_id,
172+
];
173+
}
174+
```
58175

59176
## Usage
60177

0 commit comments

Comments
 (0)