~ubuntu-branches/ubuntu/jaunty/moodle/jaunty

« back to all changes in this revision

Viewing changes to search/Zend/Search/Lucene/Search/Query/Boolean.php

  • Committer: Bazaar Package Importer
  • Author(s): Jordan Mantha, Matt Oquist
  • Date: 2009-02-25 15:16:22 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090225151622-0ekt1liwhv2obfza
Tags: 1.9.4.dfsg-0ubuntu1
* Merge with Debian git (Closes LP: #322961, #239481, #334611):
  - use Ubuntu's smarty lib directory for linking
  - use internal yui library 
  - add update-notifier support back in

[Matt Oquist]
  * renamed prerm script
  * significantly rewrote postinst and other maintainer scripts to improve
    user experience and package maintainability
    (Closes LP: #225662, #325450, #327843, #303078, #234609)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Zend Framework
 
4
 *
 
5
 * LICENSE
 
6
 *
 
7
 * This source file is subject to the new BSD license that is bundled
 
8
 * with this package in the file LICENSE.txt.
 
9
 * It is also available through the world-wide-web at this URL:
 
10
 * http://framework.zend.com/license/new-bsd
 
11
 * If you did not receive a copy of the license and are unable to
 
12
 * obtain it through the world-wide-web, please send an email
 
13
 * to license@zend.com so we can send you a copy immediately.
 
14
 *
 
15
 * @category   Zend
 
16
 * @package    Zend_Search_Lucene
 
17
 * @subpackage Search
 
18
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
19
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
20
 */
 
21
 
 
22
 
 
23
/** Zend_Search_Lucene_Search_Query */
 
24
require_once 'Zend/Search/Lucene/Search/Query.php';
 
25
 
 
26
/** Zend_Search_Lucene_Search_Weight_Boolean */
 
27
require_once 'Zend/Search/Lucene/Search/Weight/Boolean.php';
 
28
 
 
29
 
 
30
/**
 
31
 * @category   Zend
 
32
 * @package    Zend_Search_Lucene
 
33
 * @subpackage Search
 
34
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
35
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
36
 */
 
37
class Zend_Search_Lucene_Search_Query_Boolean extends Zend_Search_Lucene_Search_Query
 
38
{
 
39
 
 
40
    /**
 
41
     * Subqueries
 
42
     * Array of Zend_Search_Lucene_Search_Query
 
43
     *
 
44
     * @var array
 
45
     */
 
46
    private $_subqueries = array();
 
47
 
 
48
    /**
 
49
     * Subqueries signs.
 
50
     * If true then subquery is required.
 
51
     * If false then subquery is prohibited.
 
52
     * If null then subquery is neither prohibited, nor required
 
53
     *
 
54
     * If array is null then all subqueries are required
 
55
     *
 
56
     * @var array
 
57
     */
 
58
    private $_signs = array();
 
59
 
 
60
    /**
 
61
     * Result vector.
 
62
     *
 
63
     * @var array
 
64
     */
 
65
    private $_resVector = null;
 
66
 
 
67
    /**
 
68
     * A score factor based on the fraction of all query subqueries
 
69
     * that a document contains.
 
70
     * float for conjunction queries
 
71
     * array of float for non conjunction queries
 
72
     *
 
73
     * @var mixed
 
74
     */
 
75
    private $_coord = null;
 
76
 
 
77
 
 
78
    /**
 
79
     * Class constructor.  Create a new Boolean query object.
 
80
     *
 
81
     * if $signs array is omitted then all subqueries are required
 
82
     * it differs from addSubquery() behavior, but should never be used
 
83
     *
 
84
     * @param array $subqueries    Array of Zend_Search_Search_Query objects
 
85
     * @param array $signs    Array of signs.  Sign is boolean|null.
 
86
     * @return void
 
87
     */
 
88
    public function __construct($subqueries = null, $signs = null)
 
89
    {
 
90
        if (is_array($subqueries)) {
 
91
            $this->_subqueries = $subqueries;
 
92
 
 
93
            $this->_signs = null;
 
94
            // Check if all subqueries are required
 
95
            if (is_array($signs)) {
 
96
                foreach ($signs as $sign ) {
 
97
                    if ($sign !== true) {
 
98
                        $this->_signs = $signs;
 
99
                        break;
 
100
                    }
 
101
                }
 
102
            }
 
103
        }
 
104
    }
 
105
 
 
106
 
 
107
    /**
 
108
     * Add a $subquery (Zend_Search_Lucene_Search_Query) to this query.
 
109
     *
 
110
     * The sign is specified as:
 
111
     *     TRUE  - subquery is required
 
112
     *     FALSE - subquery is prohibited
 
113
     *     NULL  - subquery is neither prohibited, nor required
 
114
     *
 
115
     * @param  Zend_Search_Lucene_Search_Query $subquery
 
116
     * @param  boolean|null $sign
 
117
     * @return void
 
118
     */
 
119
    public function addSubquery(Zend_Search_Lucene_Search_Query $subquery, $sign=null) {
 
120
        if ($sign !== true || $this->_signs !== null) {       // Skip, if all subqueries are required
 
121
            if ($this->_signs === null) {                     // Check, If all previous subqueries are required
 
122
                $this->_signs = array();
 
123
                foreach ($this->_subqueries as $prevSubquery) {
 
124
                    $this->_signs[] = true;
 
125
                }
 
126
            }
 
127
            $this->_signs[] = $sign;
 
128
        }
 
129
 
 
130
        $this->_subqueries[] = $subquery;
 
131
    }
 
132
 
 
133
    /**
 
134
     * Re-write queries into primitive queries
 
135
     *
 
136
     * @param Zend_Search_Lucene_Interface $index
 
137
     * @return Zend_Search_Lucene_Search_Query
 
138
     */
 
139
    public function rewrite(Zend_Search_Lucene_Interface $index)
 
140
    {
 
141
        $query = new Zend_Search_Lucene_Search_Query_Boolean();
 
142
        $query->setBoost($this->getBoost());
 
143
 
 
144
        foreach ($this->_subqueries as $subqueryId => $subquery) {
 
145
            $query->addSubquery($subquery->rewrite($index),
 
146
                                ($this->_signs === null)?  true : $this->_signs[$subqueryId]);
 
147
        }
 
148
 
 
149
        return $query;
 
150
    }
 
151
 
 
152
    /**
 
153
     * Optimize query in the context of specified index
 
154
     *
 
155
     * @param Zend_Search_Lucene_Interface $index
 
156
     * @return Zend_Search_Lucene_Search_Query
 
157
     */
 
158
    public function optimize(Zend_Search_Lucene_Interface $index)
 
159
    {
 
160
        $subqueries = array();
 
161
        $signs      = array();
 
162
 
 
163
        // Optimize all subqueries
 
164
        foreach ($this->_subqueries as $id => $subquery) {
 
165
            $subqueries[] = $subquery->optimize($index);
 
166
            $signs[]      = ($this->_signs === null)? true : $this->_signs[$id];
 
167
        }
 
168
 
 
169
        // Remove insignificant subqueries
 
170
        foreach ($subqueries as $id => $subquery) {
 
171
            if ($subquery instanceof Zend_Search_Lucene_Search_Query_Insignificant) {
 
172
                // Insignificant subquery has to be removed anyway
 
173
                unset($subqueries[$id]);
 
174
                unset($signs[$id]);
 
175
            }
 
176
        }
 
177
        if (count($subqueries) == 0) {
 
178
            // Boolean query doesn't has non-insignificant subqueries
 
179
            return new Zend_Search_Lucene_Search_Query_Insignificant();
 
180
        }
 
181
        // Check if all non-insignificant subqueries are prohibited
 
182
        $allProhibited = true;
 
183
        foreach ($signs as $sign) {
 
184
            if ($sign !== false) {
 
185
                $allProhibited = false;
 
186
                break;
 
187
            }
 
188
        }
 
189
        if ($allProhibited) {
 
190
            return new Zend_Search_Lucene_Search_Query_Insignificant();
 
191
        }
 
192
 
 
193
 
 
194
        // Check for empty subqueries
 
195
        foreach ($subqueries as $id => $subquery) {
 
196
            if ($subquery instanceof Zend_Search_Lucene_Search_Query_Empty) {
 
197
                if ($signs[$id] === true) {
 
198
                    // Matching is required, but is actually empty
 
199
                    return new Zend_Search_Lucene_Search_Query_Empty();
 
200
                } else {
 
201
                    // Matching is optional or prohibited, but is empty
 
202
                    // Remove it from subqueries and signs list
 
203
                    unset($subqueries[$id]);
 
204
                    unset($signs[$id]);
 
205
                }
 
206
            }
 
207
        }
 
208
 
 
209
        // Check, if reduced subqueries list is empty
 
210
        if (count($subqueries) == 0) {
 
211
            return new Zend_Search_Lucene_Search_Query_Empty();
 
212
        }
 
213
 
 
214
        // Check if all non-empty subqueries are prohibited
 
215
        $allProhibited = true;
 
216
        foreach ($signs as $sign) {
 
217
            if ($sign !== false) {
 
218
                $allProhibited = false;
 
219
                break;
 
220
            }
 
221
        }
 
222
        if ($allProhibited) {
 
223
            return new Zend_Search_Lucene_Search_Query_Empty();
 
224
        }
 
225
 
 
226
 
 
227
        // Check, if reduced subqueries list has only one entry
 
228
        if (count($subqueries) == 1) {
 
229
            // It's a query with only one required or optional clause
 
230
            // (it's already checked, that it's not a prohibited clause)
 
231
 
 
232
            if ($this->getBoost() == 1) {
 
233
                return reset($subqueries);
 
234
            }
 
235
 
 
236
            $optimizedQuery = clone reset($subqueries);
 
237
            $optimizedQuery->setBoost($optimizedQuery->getBoost()*$this->getBoost());
 
238
 
 
239
            return $optimizedQuery;
 
240
        }
 
241
 
 
242
 
 
243
        // Prepare first candidate for optimized query
 
244
        $optimizedQuery = new Zend_Search_Lucene_Search_Query_Boolean($subqueries, $signs);
 
245
        $optimizedQuery->setBoost($this->getBoost());
 
246
 
 
247
 
 
248
        $terms        = array();
 
249
        $tsigns       = array();
 
250
        $boostFactors = array();
 
251
 
 
252
        // Try to decompose term and multi-term subqueries
 
253
        foreach ($subqueries as $id => $subquery) {
 
254
            if ($subquery instanceof Zend_Search_Lucene_Search_Query_Term) {
 
255
                $terms[]        = $subquery->getTerm();
 
256
                $tsigns[]       = $signs[$id];
 
257
                $boostFactors[] = $subquery->getBoost();
 
258
 
 
259
                // remove subquery from a subqueries list
 
260
                unset($subqueries[$id]);
 
261
                unset($signs[$id]);
 
262
           } else if ($subquery instanceof Zend_Search_Lucene_Search_Query_MultiTerm) {
 
263
                $subTerms = $subquery->getTerms();
 
264
                $subSigns = $subquery->getSigns();
 
265
 
 
266
                if ($signs[$id] === true) {
 
267
                    // It's a required multi-term subquery.
 
268
                    // Something like '... +(+term1 -term2 term3 ...) ...'
 
269
 
 
270
                    // Multi-term required subquery can be decomposed only if it contains
 
271
                    // required terms and doesn't contain prohibited terms:
 
272
                    // ... +(+term1 term2 ...) ... => ... +term1 term2 ...
 
273
                    //
 
274
                    // Check this
 
275
                    $hasRequired   = false;
 
276
                    $hasProhibited = false;
 
277
                    if ($subSigns === null) {
 
278
                        // All subterms are required
 
279
                        $hasRequired = true;
 
280
                    } else {
 
281
                        foreach ($subSigns as $sign) {
 
282
                            if ($sign === true) {
 
283
                                $hasRequired   = true;
 
284
                            } else if ($sign === false) {
 
285
                                $hasProhibited = true;
 
286
                                break;
 
287
                            }
 
288
                        }
 
289
                    }
 
290
                    // Continue if subquery has prohibited terms or doesn't have required terms
 
291
                    if ($hasProhibited  ||  !$hasRequired) {
 
292
                        continue;
 
293
                    }
 
294
 
 
295
                    foreach ($subTerms as $termId => $term) {
 
296
                        $terms[]        = $term;
 
297
                        $tsigns[]       = ($subSigns === null)? true : $subSigns[$termId];
 
298
                        $boostFactors[] = $subquery->getBoost();
 
299
                    }
 
300
 
 
301
                    // remove subquery from a subqueries list
 
302
                    unset($subqueries[$id]);
 
303
                    unset($signs[$id]);
 
304
 
 
305
                } else { // $signs[$id] === null  ||  $signs[$id] === false
 
306
                    // It's an optional or prohibited multi-term subquery.
 
307
                    // Something like '... (+term1 -term2 term3 ...) ...'
 
308
                    // or
 
309
                    // something like '... -(+term1 -term2 term3 ...) ...'
 
310
 
 
311
                    // Multi-term optional and required subqueries can be decomposed
 
312
                    // only if all terms are optional.
 
313
                    //
 
314
                    // Check if all terms are optional.
 
315
                    $onlyOptional = true;
 
316
                    if ($subSigns === null) {
 
317
                        // All subterms are required
 
318
                        $onlyOptional = false;
 
319
                    } else {
 
320
                        foreach ($subSigns as $sign) {
 
321
                            if ($sign !== null) {
 
322
                                $onlyOptional = false;
 
323
                                break;
 
324
                            }
 
325
                        }
 
326
                    }
 
327
 
 
328
                    // Continue if non-optional terms are presented in this multi-term subquery
 
329
                    if (!$onlyOptional) {
 
330
                        continue;
 
331
                    }
 
332
 
 
333
                    foreach ($subTerms as $termId => $term) {
 
334
                        $terms[]  = $term;
 
335
                        $tsigns[] = ($signs[$id] === null)? null  /* optional */ :
 
336
                                                            false /* prohibited */;
 
337
                        $boostFactors[] = $subquery->getBoost();
 
338
                    }
 
339
 
 
340
                    // remove subquery from a subqueries list
 
341
                    unset($subqueries[$id]);
 
342
                    unset($signs[$id]);
 
343
                }
 
344
            }
 
345
        }
 
346
 
 
347
 
 
348
        // Check, if there are no decomposed subqueries
 
349
        if (count($terms) == 0 ) {
 
350
            // return prepared candidate
 
351
            return $optimizedQuery;
 
352
        }
 
353
 
 
354
 
 
355
        // Check, if all subqueries have been decomposed and all terms has the same boost factor
 
356
        if (count($subqueries) == 0  &&  count(array_unique($boostFactors)) == 1) {
 
357
            $optimizedQuery = new Zend_Search_Lucene_Search_Query_MultiTerm($terms, $tsigns);
 
358
            $optimizedQuery->setBoost(reset($boostFactors)*$this->getBoost());
 
359
 
 
360
            return $optimizedQuery;
 
361
        }
 
362
 
 
363
 
 
364
        // This boolean query can't be transformed to Term/MultiTerm query and still contains
 
365
        // several subqueries
 
366
 
 
367
        // Separate prohibited terms
 
368
        $prohibitedTerms        = array();
 
369
        foreach ($terms as $id => $term) {
 
370
            if ($tsigns[$id] === false) {
 
371
                $prohibitedTerms[]        = $term;
 
372
 
 
373
                unset($terms[$id]);
 
374
                unset($tsigns[$id]);
 
375
                unset($boostFactors[$id]);
 
376
            }
 
377
        }
 
378
 
 
379
        if (count($terms) == 1) {
 
380
            $clause = new Zend_Search_Lucene_Search_Query_Term(reset($terms));
 
381
            $clause->setBoost(reset($boostFactors));
 
382
 
 
383
            $subqueries[] = $clause;
 
384
            $signs[]      = reset($tsigns);
 
385
 
 
386
            // Clear terms list
 
387
            $terms = array();
 
388
        } else if (count($terms) > 1  &&  count(array_unique($boostFactors)) == 1) {
 
389
            $clause = new Zend_Search_Lucene_Search_Query_MultiTerm($terms, $tsigns);
 
390
            $clause->setBoost(reset($boostFactors));
 
391
 
 
392
            $subqueries[] = $clause;
 
393
            // Clause sign is 'required' if clause contains required terms. 'Optional' otherwise.
 
394
            $signs[]      = (in_array(true, $tsigns))? true : null;
 
395
 
 
396
            // Clear terms list
 
397
            $terms = array();
 
398
        }
 
399
 
 
400
        if (count($prohibitedTerms) == 1) {
 
401
            // (boost factors are not significant for prohibited clauses)
 
402
            $subqueries[] = new Zend_Search_Lucene_Search_Query_Term(reset($prohibitedTerms));
 
403
            $signs[]      = false;
 
404
 
 
405
            // Clear prohibited terms list
 
406
            $prohibitedTerms = array();
 
407
        } else if (count($prohibitedTerms) > 1) {
 
408
            // prepare signs array
 
409
            $prohibitedSigns = array();
 
410
            foreach ($prohibitedTerms as $id => $term) {
 
411
                // all prohibited term are grouped as optional into multi-term query
 
412
                $prohibitedSigns[$id] = null;
 
413
            }
 
414
 
 
415
            // (boost factors are not significant for prohibited clauses)
 
416
            $subqueries[] = new Zend_Search_Lucene_Search_Query_MultiTerm($prohibitedTerms, $prohibitedSigns);
 
417
            // Clause sign is 'prohibited'
 
418
            $signs[]      = false;
 
419
 
 
420
            // Clear terms list
 
421
            $prohibitedTerms = array();
 
422
        }
 
423
 
 
424
        /** @todo Group terms with the same boost factors together */
 
425
 
 
426
        // Check, that all terms are processed
 
427
        // Replace candidate for optimized query
 
428
        if (count($terms) == 0  &&  count($prohibitedTerms) == 0) {
 
429
            $optimizedQuery = new Zend_Search_Lucene_Search_Query_Boolean($subqueries, $signs);
 
430
            $optimizedQuery->setBoost($this->getBoost());
 
431
        }
 
432
 
 
433
        return $optimizedQuery;
 
434
    }
 
435
 
 
436
    /**
 
437
     * Returns subqueries
 
438
     *
 
439
     * @return array
 
440
     */
 
441
    public function getSubqueries()
 
442
    {
 
443
        return $this->_subqueries;
 
444
    }
 
445
 
 
446
 
 
447
    /**
 
448
     * Return subqueries signs
 
449
     *
 
450
     * @return array
 
451
     */
 
452
    public function getSigns()
 
453
    {
 
454
        return $this->_signs;
 
455
    }
 
456
 
 
457
 
 
458
    /**
 
459
     * Constructs an appropriate Weight implementation for this query.
 
460
     *
 
461
     * @param Zend_Search_Lucene_Interface $reader
 
462
     * @return Zend_Search_Lucene_Search_Weight
 
463
     */
 
464
    public function createWeight(Zend_Search_Lucene_Interface $reader)
 
465
    {
 
466
        $this->_weight = new Zend_Search_Lucene_Search_Weight_Boolean($this, $reader);
 
467
        return $this->_weight;
 
468
    }
 
469
 
 
470
 
 
471
    /**
 
472
     * Calculate result vector for Conjunction query
 
473
     * (like '<subquery1> AND <subquery2> AND <subquery3>')
 
474
     */
 
475
    private function _calculateConjunctionResult()
 
476
    {
 
477
        $this->_resVector = null;
 
478
 
 
479
        if (count($this->_subqueries) == 0) {
 
480
            $this->_resVector = array();
 
481
        }
 
482
 
 
483
        $resVectors      = array();
 
484
        $resVectorsSizes = array();
 
485
        $resVectorsIds   = array(); // is used to prevent arrays comparison
 
486
        foreach ($this->_subqueries as $subqueryId => $subquery) {
 
487
            $resVectors[]      = $subquery->matchedDocs();
 
488
            $resVectorsSizes[] = count(end($resVectors));
 
489
            $resVectorsIds[]   = $subqueryId;
 
490
        }
 
491
        // sort resvectors in order of subquery cardinality increasing
 
492
        array_multisort($resVectorsSizes, SORT_ASC, SORT_NUMERIC,
 
493
                        $resVectorsIds,   SORT_ASC, SORT_NUMERIC,
 
494
                        $resVectors);
 
495
        
 
496
        foreach ($resVectors as $nextResVector) {
 
497
            if($this->_resVector === null) {
 
498
                $this->_resVector = $nextResVector;
 
499
            } else {
 
500
                //$this->_resVector = array_intersect_key($this->_resVector, $nextResVector);
 
501
                
 
502
                /**
 
503
                 * This code is used as workaround for array_intersect_key() slowness problem.
 
504
                 */
 
505
                $updatedVector = array();
 
506
                foreach ($this->_resVector as $id => $value) {
 
507
                    if (isset($nextResVector[$id])) {
 
508
                        $updatedVector[$id] = $value;
 
509
                    }
 
510
                }
 
511
                $this->_resVector = $updatedVector;
 
512
            }
 
513
 
 
514
            if (count($this->_resVector) == 0) {
 
515
                // Empty result set, we don't need to check other terms
 
516
                break;
 
517
            }
 
518
        }
 
519
 
 
520
        // ksort($this->_resVector, SORT_NUMERIC);
 
521
        // Used algorithm doesn't change elements order
 
522
    }
 
523
 
 
524
 
 
525
    /**
 
526
     * Calculate result vector for non Conjunction query
 
527
     * (like '<subquery1> AND <subquery2> AND NOT <subquery3> OR <subquery4>')
 
528
     */
 
529
    private function _calculateNonConjunctionResult()
 
530
    {
 
531
        $requiredVectors      = array();
 
532
        $requiredVectorsSizes = array();
 
533
        $requiredVectorsIds   = array(); // is used to prevent arrays comparison
 
534
 
 
535
        $optional = array();
 
536
 
 
537
        foreach ($this->_subqueries as $subqueryId => $subquery) {
 
538
            if ($this->_signs[$subqueryId] === true) {
 
539
                // required
 
540
                $requiredVectors[]      = $subquery->matchedDocs();
 
541
                $requiredVectorsSizes[] = count(end($requiredVectors));
 
542
                $requiredVectorsIds[]   = $subqueryId;
 
543
            } elseif ($this->_signs[$subqueryId] === false) {
 
544
                // prohibited
 
545
                // Do nothing. matchedDocs() may include non-matching id's
 
546
                // Calculating prohibited vector may take significant time, but do not affect the result
 
547
                // Skipped.  
 
548
            } else {
 
549
                // neither required, nor prohibited
 
550
                // array union
 
551
                $optional += $subquery->matchedDocs();
 
552
            }
 
553
        }
 
554
 
 
555
        // sort resvectors in order of subquery cardinality increasing
 
556
        array_multisort($requiredVectorsSizes, SORT_ASC, SORT_NUMERIC,
 
557
                        $requiredVectorsIds,   SORT_ASC, SORT_NUMERIC,
 
558
                        $requiredVectors);
 
559
        
 
560
        $required = null;
 
561
        foreach ($requiredVectors as $nextResVector) {
 
562
            if($required === null) {
 
563
                $required = $nextResVector;
 
564
            } else {
 
565
                //$required = array_intersect_key($required, $nextResVector);
 
566
                
 
567
                /**
 
568
                 * This code is used as workaround for array_intersect_key() slowness problem.
 
569
                 */
 
570
                $updatedVector = array();
 
571
                foreach ($required as $id => $value) {
 
572
                    if (isset($nextResVector[$id])) {
 
573
                        $updatedVector[$id] = $value;
 
574
                    }
 
575
                }
 
576
                $required = $updatedVector;
 
577
            }
 
578
 
 
579
            if (count($required) == 0) {
 
580
                // Empty result set, we don't need to check other terms
 
581
                break;
 
582
            }
 
583
        }
 
584
                
 
585
        
 
586
        if ($required !== null) {
 
587
            $this->_resVector = &$required;
 
588
        } else {
 
589
            $this->_resVector = &$optional;
 
590
        }
 
591
 
 
592
        ksort($this->_resVector, SORT_NUMERIC);
 
593
    }
 
594
 
 
595
 
 
596
    /**
 
597
     * Score calculator for conjunction queries (all subqueries are required)
 
598
     *
 
599
     * @param integer $docId
 
600
     * @param Zend_Search_Lucene_Interface $reader
 
601
     * @return float
 
602
     */
 
603
    public function _conjunctionScore($docId, Zend_Search_Lucene_Interface $reader)
 
604
    {
 
605
        if ($this->_coord === null) {
 
606
            $this->_coord = $reader->getSimilarity()->coord(count($this->_subqueries),
 
607
                                                            count($this->_subqueries) );
 
608
        }
 
609
 
 
610
        $score = 0;
 
611
 
 
612
        foreach ($this->_subqueries as $subquery) {
 
613
            $subscore = $subquery->score($docId, $reader);
 
614
 
 
615
            if ($subscore == 0) {
 
616
                return 0;
 
617
            }
 
618
 
 
619
            $score += $subquery->score($docId, $reader) * $this->_coord;
 
620
        }
 
621
 
 
622
        return $score * $this->_coord * $this->getBoost();
 
623
    }
 
624
 
 
625
 
 
626
    /**
 
627
     * Score calculator for non conjunction queries (not all subqueries are required)
 
628
     *
 
629
     * @param integer $docId
 
630
     * @param Zend_Search_Lucene_Interface $reader
 
631
     * @return float
 
632
     */
 
633
    public function _nonConjunctionScore($docId, Zend_Search_Lucene_Interface $reader)
 
634
    {
 
635
        if ($this->_coord === null) {
 
636
            $this->_coord = array();
 
637
 
 
638
            $maxCoord = 0;
 
639
            foreach ($this->_signs as $sign) {
 
640
                if ($sign !== false /* not prohibited */) {
 
641
                    $maxCoord++;
 
642
                }
 
643
            }
 
644
 
 
645
            for ($count = 0; $count <= $maxCoord; $count++) {
 
646
                $this->_coord[$count] = $reader->getSimilarity()->coord($count, $maxCoord);
 
647
            }
 
648
        }
 
649
 
 
650
        $score = 0;
 
651
        $matchedSubqueries = 0;
 
652
        foreach ($this->_subqueries as $subqueryId => $subquery) {
 
653
            $subscore = $subquery->score($docId, $reader);
 
654
 
 
655
            // Prohibited
 
656
            if ($this->_signs[$subqueryId] === false && $subscore != 0) {
 
657
                return 0;
 
658
            }
 
659
 
 
660
            // is required, but doen't match
 
661
            if ($this->_signs[$subqueryId] === true &&  $subscore == 0) {
 
662
                return 0;
 
663
            }
 
664
 
 
665
            if ($subscore != 0) {
 
666
                $matchedSubqueries++;
 
667
                $score += $subscore;
 
668
            }
 
669
        }
 
670
 
 
671
        return $score * $this->_coord[$matchedSubqueries] * $this->getBoost();
 
672
    }
 
673
 
 
674
    /**
 
675
     * Execute query in context of index reader
 
676
     * It also initializes necessary internal structures
 
677
     *
 
678
     * @param Zend_Search_Lucene_Interface $reader
 
679
     */
 
680
    public function execute(Zend_Search_Lucene_Interface $reader)
 
681
    {
 
682
        // Initialize weight if it's not done yet
 
683
        $this->_initWeight($reader);
 
684
 
 
685
        foreach ($this->_subqueries as $subquery) {
 
686
            $subquery->execute($reader);
 
687
        }
 
688
 
 
689
        if ($this->_signs === null) {
 
690
            $this->_calculateConjunctionResult();
 
691
        } else {
 
692
            $this->_calculateNonConjunctionResult();
 
693
        }
 
694
    }
 
695
 
 
696
 
 
697
 
 
698
    /**
 
699
     * Get document ids likely matching the query
 
700
     *
 
701
     * It's an array with document ids as keys (performance considerations)
 
702
     *
 
703
     * @return array
 
704
     */
 
705
    public function matchedDocs()
 
706
    {
 
707
        return $this->_resVector;
 
708
    }
 
709
 
 
710
    /**
 
711
     * Score specified document
 
712
     *
 
713
     * @param integer $docId
 
714
     * @param Zend_Search_Lucene_Interface $reader
 
715
     * @return float
 
716
     */
 
717
    public function score($docId, Zend_Search_Lucene_Interface $reader)
 
718
    {
 
719
        if (isset($this->_resVector[$docId])) {
 
720
            if ($this->_signs === null) {
 
721
                return $this->_conjunctionScore($docId, $reader);
 
722
            } else {
 
723
                return $this->_nonConjunctionScore($docId, $reader);
 
724
            }
 
725
        } else {
 
726
            return 0;
 
727
        }
 
728
    }
 
729
 
 
730
    /**
 
731
     * Return query terms
 
732
     *
 
733
     * @return array
 
734
     */
 
735
    public function getQueryTerms()
 
736
    {
 
737
        $terms = array();
 
738
 
 
739
        foreach ($this->_subqueries as $id => $subquery) {
 
740
            if ($this->_signs === null  ||  $this->_signs[$id] !== false) {
 
741
                $terms = array_merge($terms, $subquery->getQueryTerms());
 
742
            }
 
743
        }
 
744
 
 
745
        return $terms;
 
746
    }
 
747
 
 
748
    /**
 
749
     * Highlight query terms
 
750
     *
 
751
     * @param integer &$colorIndex
 
752
     * @param Zend_Search_Lucene_Document_Html $doc
 
753
     */
 
754
    public function highlightMatchesDOM(Zend_Search_Lucene_Document_Html $doc, &$colorIndex)
 
755
    {
 
756
        foreach ($this->_subqueries as $id => $subquery) {
 
757
            if ($this->_signs === null  ||  $this->_signs[$id] !== false) {
 
758
                $subquery->highlightMatchesDOM($doc, $colorIndex);
 
759
            }
 
760
        }
 
761
    }
 
762
 
 
763
    /**
 
764
     * Print a query
 
765
     *
 
766
     * @return string
 
767
     */
 
768
    public function __toString()
 
769
    {
 
770
        // It's used only for query visualisation, so we don't care about characters escaping
 
771
 
 
772
        $query = '';
 
773
 
 
774
        foreach ($this->_subqueries as $id => $subquery) {
 
775
            if ($id != 0) {
 
776
                $query .= ' ';
 
777
            }
 
778
 
 
779
            if ($this->_signs === null || $this->_signs[$id] === true) {
 
780
                $query .= '+';
 
781
            } else if ($this->_signs[$id] === false) {
 
782
                $query .= '-';
 
783
            }
 
784
 
 
785
            $query .= '(' . $subquery->__toString() . ')';
 
786
 
 
787
            if ($subquery->getBoost() != 1) {
 
788
                $query .= '^' . round($subquery->getBoost(), 4);
 
789
            }
 
790
        }
 
791
 
 
792
        return $query;
 
793
    }
 
794
}
 
795