1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! Full-text search over models, with the index kept in step with model writes.
//!
//! Declare a model with `#[model(searchable)]`, mark the fields a search reads with `#[search]`,
//! and implement [`Searchable`]. `Post::search(terms)` then returns a [`Query`], which takes the
//! same `filter`, `order_by`, `latest`, and `oldest` as a model query and returns models and
//! paginators:
//!
//! ```no_run
//! use laravel::model::model;
//! use laravel::search::Searchable;
//! use laravel::time::Timestamp;
//!
//! #[model(searchable)]
//! pub struct Post {
//! pub id: i64,
//! #[search(weight = 3)]
//! pub title: String,
//! #[search(full_text)]
//! pub body: String,
//! #[search(filterable)]
//! pub published: bool,
//! #[search(filterable, sortable)]
//! pub created_at: Timestamp,
//! pub updated_at: Timestamp,
//! }
//!
//! impl Searchable for Post {}
//!
//! # async fn example() -> laravel::Result<()> {
//! let posts = Post::search("rust").filter(Post::published.eq(true)).paginate(15).await?;
//! # Ok(()) }
//! ```
//!
//! | Mark | The field is |
//! | --- | --- |
//! | `#[search]` | Text that contains the terms |
//! | `#[search(prefix)]` | Text that starts with the terms, such as an email |
//! | `#[search(full_text)]` | Long text with a full-text index, ranked by relevance on PostgreSQL, MySQL, and MariaDB |
//! | `#[search(weight = 3)]` | Text whose matches rank higher than other text's |
//! | `#[search(filterable)]` | A value that a search may filter on |
//! | `#[search(sortable)]` | A value that a search may order by |
//!
//! `SEARCH_ENGINE` chooses the engine, and `database`, the default, searches the model's table.
//! Laravel syncs a searchable model's index after each `create!`, `update!`, `delete`, and
//! `destroy` commits, inline or from a queued job with `SEARCH_QUEUE=true`. [`sync`] syncs the rows
//! of writes through a query, and [`without_syncing`] skips syncing. The `search:import` and
//! `search:flush` commands fill and empty a model's index.
//!
//! The [search guide](https://github.com/laravel/rust-framework/blob/0.x/docs/search.md) covers
//! each part.
//!
//! A bare `#[search]` matches text, so a field of another type is a compile error that suggests
//! `filter` or `sort`:
//!
//! ```compile_fail,E0277
//! # use laravel::model::model;
//! # use laravel::search::Searchable;
//! #[model(searchable)]
//! pub struct Post {
//! pub id: i64,
//! #[search]
//! pub published: bool,
//! }
//! # impl Searchable for Post {}
//! ```
//!
//! A search doesn't match the model's key, so a text mark on the key is a compile error too:
//!
//! ```compile_fail
//! # use laravel::model::model;
//! # use laravel::search::Searchable;
//! #[model(searchable)]
//! pub struct Post {
//! #[search]
//! pub id: i64,
//! #[search]
//! pub title: String,
//! }
//! # impl Searchable for Post {}
//! ```
//!
//! So is a text mark on a search document's field named after the key:
//!
//! ```compile_fail,E0080
//! # use laravel::model::model;
//! # use laravel::search::{self, Searchable};
//! #[model(searchable(document = PostDocument))]
//! pub struct Post {
//! pub id: i64,
//! pub title: String,
//! }
//!
//! #[derive(search::Document)]
//! pub struct PostDocument {
//! #[search]
//! id: String,
//! #[search]
//! title: String,
//! }
//!
//! impl From<&Post> for PostDocument {
//! fn from(post: &Post) -> Self {
//! Self {
//! id: post.id.to_string(),
//! title: post.title.clone(),
//! }
//! }
//! }
//! # impl Searchable for Post {}
//! ```
//!
//! So is a mark on a model that isn't `searchable`, or a mark the macro doesn't know:
//!
//! ```compile_fail
//! # use laravel::model::model;
//! #[model]
//! pub struct Post {
//! pub id: i64,
//! #[search(filterable)]
//! pub published: bool,
//! }
//! ```
//!
//! ```compile_fail
//! # use laravel::model::model;
//! # use laravel::search::Searchable;
//! #[model(searchable)]
//! pub struct Post {
//! pub id: i64,
//! #[search(rank)]
//! pub title: String,
//! }
//! # impl Searchable for Post {}
//! ```
//!
//! A model declares its embeddings in `#[model(..)]` and keeps them in the table's `embedding`
//! column, which it doesn't map, so an `embedding` mark is a compile error, and so is
//! `embedding` without its dimensions:
//!
//! ```compile_fail
//! # use laravel::model::model;
//! # use laravel::search::Searchable;
//! #[model(searchable)]
//! pub struct Post {
//! pub id: i64,
//! #[search]
//! pub title: String,
//! #[search(embedding, dimensions = 3)]
//! pub embedding: Option<laravel::db::Vector>,
//! }
//! # impl Searchable for Post {}
//! ```
//!
//! ```compile_fail
//! # use laravel::model::model;
//! # use laravel::search::Searchable;
//! #[model(searchable(embedding))]
//! pub struct Post {
//! pub id: i64,
//! #[search]
//! pub title: String,
//! }
//! # impl Searchable for Post {}
//! ```
use crate;
use crateServiceProvider;
/// Derive a search document's fields from its `#[search(..)]` marks, for
/// `#[model(searchable(document = PostDocument))]`.
pub use Document;
pub use ;
pub use ;
pub ;