-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathREADME
More file actions
370 lines (287 loc) · 12.1 KB
/
README
File metadata and controls
370 lines (287 loc) · 12.1 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
This is the documentation of the Doctrine Lucene Behaviour. This will provide you with full text search in the admin generator filters, and a convenient way to roll out your own powerful search engine.
Knowledge of lucene is not nescesairy, but it is recommended. The jobeet tutorial on searching is an excellent place to start.
Simple Tutorial: Easy admin generator integration
===================
This tutorial will show you how to get fulltext doctrine query with just a few lines.
**1. Install the plugin**
symfony plugin:install ajDoctrineLucenablePlugin --stability=beta
**2. Configure the plugin**
By default, no configuration is required, but it's good to look into the lucene.yml file.
To configure the plugin: copy the lucene.yml from the ajDoctrineLucenablePlugin/config/lucene.yml to your SF_ROOT/config.
See the yml file for details on the configuration
**2. Enable the lucene behaviour for your model**
[yaml]
Project:
columns:
id:
type: integer(4)
primary: true
autoincrement: true
location:
type: string(255)
notblank: true
body:
type: clob
name:
type: string(255)
notblank: true
actAs:
Luceneable:
fields:
- name: unstored
- location: unstored
- body: unstored
This is a key/value pair, in which the name corresponds with the field (actually the model method for the field, so it can be your custom getFullName() method, then you use the name 'full_name'), and the value corresponds with the Zend Lucene field type (for reference http://framework.zend.com/manual/en/zend.search.lucene.overview.html). This can either be keyword, unindexed, text or unstored. If you're unsure, you will probably want to use unindexed.
Since we 99% of the time only use the lucene index to retrieve document id's, the default value is UnStored. This saves the metadata, but not the data.
**3. (re)build the model**
symfony doctrine:build --model
**4. Add the filter to the model**
To create a searchable filter for a model, add this to the ProjectFormFilter.class.php:
[php]
class ProjectFormFilter extends BaseProjectFormFilter
{
public function configure()
{
$this->widgetSchema['search'] = new sfWidgetFormFilterInput(array('with_empty' => false));
$this->validatorSchema['search'] = new sfValidatorPass(array('required' => false));
$this->useFields(array('search'));
}
public function addSearchColumnQuery($query,$field,$value)
{
$ids = LuceneSearch::find($value['text'])
->fuzzy()
// search fuzzy?
->in($this->getModelName())
->setFilterQuery($query);
return $query;
}
}
**5. You're done.**
Example 2: Custom model (recommended)
=========================================
[yaml]
Project:
columns:
id:
type: integer(4)
primary: true
autoincrement: true
location:
type: string(255)
notblank: true
body:
type: clob
name:
type: string(255)
notblank: true
actAs: [Luceneable]
**In lib/models/doctrine/Project.class.php**:
Here we add to code that is being called when the model is inserted or updated. This example shows how to load and parse a HTML document to the lucene index.
The updateLucene method should always return a Zend_Search_Lucene_Document.
[php]
public function updateLucene()
{
$doc = Zend_Search_Lucene_Document_Html::loadHTML($this->getBody());
$doc->addField(Zend_Search_Lucene_Field::Unstored('name', $this->getLocation(), 'utf-8'));
$doc->addField(Zend_Search_Lucene_Field::Unstored('location', $this->getLocation(), 'utf-8'));
return $doc;
}
You can override the parameters from schema.yml from the model by defining a public attribute $luceneSearchFields.
[php]
public $luceneSearchFields = array(
'name' => 'unstored',
'location' => 'text'
);
Filters for the Admin Generator
-------
To create a searchable filter for a model (and the admin generator), add this to the **lib/filters/doctrine/FormFilter.class.php**:
[php]
class OrganisationFormFilter extends BaseOrganisationFormFilter
{
public function configure()
{
$this->widgetSchema['search'] = new sfWidgetFormFilterInput(array('with_empty' => false));
$this->validatorSchema['search'] = new sfValidatorPass(array('required' => false));
$this->useFields(array('search'));
}
public function addSearchColumnQuery($query,$field,$value)
{
LuceneSearch::find($value['text'])
->fuzzy() // do we want to search fuzzy?
->in($this->getModelName())
->setFilterQuery($query);
return $query;
}
}
Adding the ->fuzzy() true will mean fuzzy searching (or in fact adding a ~ behind every keyword in the search string.)
This means fuzzy searching. This is great for newbees and noobs, but not so great if you wish to use the cool Zend Lucene Query Language.
Example 3. Creating your own search engine
======================
The trick in creating a simple search engine is that for each model you have to render a custom partial for that model.
**in search/actions/actions.class.php:**
[php]
public function executeSearch(sfWebRequest $request)
{
$this->getResponse()->setTitle('Search results for: ' . $request->getParameter('query'));
$this->searchquery = $request->getParameter('query');
$query = LuceneSearch::find($query)->fuzzy()->in(array('News','Member','Project','Event','Content','Organisation'));
$this->results = $query->getRecords();
$this->hits = $query->getHits();
}
**in search/templates/searchSuccess.php:**
[php]
<?php $count = count($hits);
if ($count == 0) {
echo "No results found for '<strong>" . $searchquery . "</strong>':";
} elseif ($count == 1)
{
echo "1 result found for '<strong>" . $searchquery . "</strong>':";
} else {
echo sprintf("%d results found",$count) . " for '<strong>" . $searchquery . "</strong>':";
}
?>
<?php
foreach ($hits as $hit)
{
echo get_partial($hit->model, array(
'obj' => $results[$hit->model][$hit->pk],
'pk' => $hit)
);
}
?>
in the get_partial() line lies the magic, here you can load for each model a seperate template. My template for news looks like this:
**in templates/_News.php**
[php]
<?php use_helper('Date'); ?>
<?php $data = $obj;
if ($data->is_published):
?>
<div class="contentItem clickableItem">
<div class="itemDescription">
<p>
<h1 class="searchCatagory">News</h1><br />
<?php echo format_date($data['created_at'], 'EEE dd MMMM yyyy') ?>:
<h1><?php echo $data['title'] ?></h1>
<?php
$abstract = substr($data['abstract'], 0, 180);
echo nl2br($abstract) . '... <br />' . link_to('> READ MORE','news/show?id='. $data['id'],array('class'=>'follow'));
?>
</p>
</div>
<div class="itemImage"><img src="/uploads/news/s/<?php echo $data['image']; ?>"></div>
<div class="borderDiv"> </div>
</div>
<?php endif; ?>
Likewise, you can create such a template for each model that you wish to include in your search engine. (in this case for: 'News','Member','Project','Event','Content','Organisation')
Searching
=====
There is a convenient class to search models:
[php]
$query = LuceneSearch::find($string)
->in('model1','model2','model3');
Results
-------
This method will return the Pk's (primary keys, per model)
[php]
$hits = $query->getHits();
// get an array of lucene hits
$ids = $query->getPks();
// get an array of lucene hits in the form:
array (
'Model' => array(1,2,3,45,5,6,7),
'Model2' => array(5,2,1,4)
)
Connection with the model:
------
[php]
$ids = LuceneSearch::find($value['text'])
//->fuzzy()
->in($this->getModelName())
->getPksForModel();
$query = Doctrine_Query::create()
->select('m.*')
->from('myModel')
->andWhereIn('id',$ids);
This will return you the data from the database.
Tasks
=====
This plugin provides a task to (re)index your models, when data has been modified in the database by other means that doctrine.
lucene:reindex
--------------
**usage**
<pre>
symfony lucene:reindex
</pre>
This will use the models defined in lucene.yml (see example in the ajDoctrineLucenablePlugin/config) folder for documentation
<pre>
symfony lucene:reindex Model1 Model2
This will update and optimize Model1 and Model2.
</pre>
lucene:optimize
--------------
**usage**
<pre>
symfony lucene:optimize
</pre>
This will optimize the index file.
Using your own zend framework
===============
By default, the plugin will use the packaged Zend Framework. To disable this behaviour:
**in: lucene.yml**
<pre>
use_packaged_lucene: false
</pre>
Then add in: ProjectConfiguration.class.php:
[php]
public function setup()
{
// ... load plugins here ... //
// Here add the listener for the event lucenable.autoload, and connect it to your own
// ZF autloading method
$this->dispatcher->connect('luceneable.autoload','ProjectConfiguration::registerZend');
}
static protected $zendLoaded = false;
static public function registerZend()
{
if (self::$zendLoaded)
{
return;
}
set_include_path(sfConfig::get('sf_lib_dir').'/vendor'.PATH_SEPARATOR.get_include_path());
require_once sfConfig::get('sf_lib_dir').'/vendor/Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
self::$zendLoaded = true;
}
**Download Zend library** from: http://framework.zend.com/download/current/
To use a minimal zend library, download the entire zip, and from the ZendFramwork-1.x.x/library/Zend copy only these to your **/lib/vendor/Zend** folder:
<pre>
Zend/Loader/*
Zend/Search/*
Zend/Exception.php
Zend/Loader.php
</pre>
Extra Documentation
==============
* [Zend Documentation on the Lucene Query language](http://framework.zend.com/manual/en/zend.search.lucene.query-language.html)
* [Apache Lucene](http://lucene.apache.org/java/2_3_2/queryparsersyntax.html)
Things you should know
======================
* lucenable requires you to have a primary key defined, and to call it 'id' (The admin generator will also become funky if you don't do so.)
* The index folder should be writable
* It's useful to optimize the index every now and then. (symfony lucene:optimize)
* This has been tested on small records. For datasets of >100000 perhaps a more industrial solution as java based lucene or solr is recommended.
Classes
=======
The plugin consists of 5 classes
* **LuceneHandler** (Global class, to handle the autoloading and singleton class for lucene)
* **LuceneRecord** (Callback methods, for the lucenable listener)
* **LuceneSearch** (to ease the searching of indexes)
* **Luceneable** (doctrine Behvaiour)
* **LuceneableListener** (Doctrine_Record_Listener)
TODO
=====
* make it compatible with i18n (can be accomplished by a method that adds a new record in each language, and by adding a mandatory language field to each record).
* automagickly correct the encoding type
* create a better task to update it
* write tests (performance tests, etc.)
* Evaluate the performance and document the ways to optimize the search results (hand picking a lot of results is not always cool.)