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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
|
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.search.lucene.searching">
<title>Searching an Index</title>
<sect2 id="zend.search.lucene.searching.query_building">
<title>Building Queries</title>
<para>
There are two ways to search the index. The first method uses
query parser to construct a query from a string. The second is
to programmatically create your own queries through the
<classname>Zend_Search_Lucene</classname> <acronym>API</acronym>.
</para>
<para>
Before choosing to use the provided query parser, please consider
the following:
<orderedlist>
<listitem>
<para>
If you are programmatically creating a query string and then parsing
it with the query parser then you should consider building your queries
directly with the query <acronym>API</acronym>. Generally speaking, the
query parser is designed for human-entered text, not for program-generated
text.
</para>
</listitem>
<listitem>
<para>
Untokenized fields are best added directly to queries and not through
the query parser. If a field's values are generated programmatically
by the application, then the query clauses for this field should also
be constructed programmatically.
An analyzer, which the query parser uses, is designed to convert
human-entered text to terms. Program-generated values, like dates,
keywords, etc., should be added with the query <acronym>API</acronym>.
</para>
</listitem>
<listitem>
<para>
In a query form, fields that are general text should use the query parser.
All others, such as date ranges, keywords, etc., are better added directly
through the query <acronym>API</acronym>. A field with a limited set of
values that can be specified with a pull-down menu should not be added to a
query string that is subsequently parsed but instead should be added as a
TermQuery clause.
</para>
</listitem>
<listitem>
<para>
Boolean queries allow the programmer to logically combine two or more
queries into new one. Thus it's the best way to add additional criteria to a
search defined by a query string.
</para>
</listitem>
</orderedlist>
</para>
<para>
Both ways use the same <acronym>API</acronym> method to search through the index:
</para>
<programlisting language="php"><![CDATA[
$index = Zend_Search_Lucene::open('/data/my_index');
$index->find($query);
]]></programlisting>
<para>
You can also search multiple indexes simultaneously using MultiSearcher, which operates
using the same <acronym>API</acronym> as searching on a single index:
</para>
<programlisting language="php"><![CDATA[
$multi = new Zend_Search_Lucene_MultiSearcher();
$multi->addIndex(Zend_Search_Lucene::open('/data/my_index_one');
$multi->addIndex(Zend_Search_Lucene::open('/data/my_index_two');
$multi->find($query);
]]></programlisting>
<para>
The <methodname>Zend_Search_Lucene::find()</methodname> method determines the input type
automatically and uses the query parser to construct an appropriate
<classname>Zend_Search_Lucene_Search_Query</classname> object from an input of type
string.
</para>
<para>
It is important to note that the query parser uses the standard analyzer to tokenize
separate parts of query string. Thus all transformations which are applied to indexed
text are also applied to query strings.
</para>
<para>
The standard analyzer may transform the query string to lower case for
case-insensitivity, remove stop-words, and stem among other transformations.
</para>
<para>
The <acronym>API</acronym> method doesn't transform or filter input terms in any way.
It's therefore more suitable for computer generated or untokenized fields.
</para>
<sect3 id="zend.search.lucene.searching.query_building.parsing">
<title>Query Parsing</title>
<para>
<methodname>Zend_Search_Lucene_Search_QueryParser::parse()</methodname> method may
be used to parse query strings into query objects.
</para>
<para>
This query object may be used in query construction <acronym>API</acronym> methods
to combine user entered queries with programmatically generated queries.
</para>
<para>
Actually, in some cases it's the only way to search for values within untokenized
fields:
</para>
<programlisting language="php"><![CDATA[
$userQuery = Zend_Search_Lucene_Search_QueryParser::parse($queryStr);
$pathTerm = new Zend_Search_Lucene_Index_Term(
'/data/doc_dir/' . $filename, 'path'
);
$pathQuery = new Zend_Search_Lucene_Search_Query_Term($pathTerm);
$query = new Zend_Search_Lucene_Search_Query_Boolean();
$query->addSubquery($userQuery, true /* required */);
$query->addSubquery($pathQuery, true /* required */);
$hits = $index->find($query);
]]></programlisting>
<para>
<methodname>Zend_Search_Lucene_Search_QueryParser::parse()</methodname> method also
takes an optional encoding parameter, which can specify query string encoding:
</para>
<programlisting language="php"><![CDATA[
$userQuery = Zend_Search_Lucene_Search_QueryParser::parse($queryStr,
'iso-8859-5');
]]></programlisting>
<para>
If the encoding parameter is omitted, then current locale is used.
</para>
<para>
It's also possible to specify the default query string encoding with
<methodname>Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding()</methodname>
method:
</para>
<programlisting language="php"><![CDATA[
Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('iso-8859-5');
...
$userQuery = Zend_Search_Lucene_Search_QueryParser::parse($queryStr);
]]></programlisting>
<para>
<methodname>Zend_Search_Lucene_Search_QueryParser::getDefaultEncoding()</methodname>
returns the current default query string encoding (the empty string means "current
locale").
</para>
</sect3>
</sect2>
<sect2 id="zend.search.lucene.searching.results">
<title>Search Results</title>
<para>
The search result is an array of
<classname>Zend_Search_Lucene_Search_QueryHit</classname> objects. Each of these has two
properties: <code>$hit->id</code> is a document number within the index and
<code>$hit->score</code> is a score of the hit in a search result. The results are
ordered by score (descending from highest score).
</para>
<para>
The <classname>Zend_Search_Lucene_Search_QueryHit</classname> object also exposes each
field of the <classname>Zend_Search_Lucene_Document</classname> found in the search as a
property of the hit. In the following example, a hit is returned with two fields from
the corresponding document: title and author.
</para>
<programlisting language="php"><![CDATA[
$index = Zend_Search_Lucene::open('/data/my_index');
$hits = $index->find($query);
foreach ($hits as $hit) {
echo $hit->score;
echo $hit->title;
echo $hit->author;
}
]]></programlisting>
<para>
Stored fields are always returned in UTF-8 encoding.
</para>
<para>
Optionally, the original <classname>Zend_Search_Lucene_Document</classname> object can
be returned from the <classname>Zend_Search_Lucene_Search_QueryHit</classname>.
You can retrieve stored parts of the document by using the
<methodname>getDocument()</methodname> method of the index object and then get them by
<methodname>getFieldValue()</methodname> method:
</para>
<programlisting language="php"><![CDATA[
$index = Zend_Search_Lucene::open('/data/my_index');
$hits = $index->find($query);
foreach ($hits as $hit) {
// return Zend_Search_Lucene_Document object for this hit
echo $document = $hit->getDocument();
// return a Zend_Search_Lucene_Field object
// from the Zend_Search_Lucene_Document
echo $document->getField('title');
// return the string value of the Zend_Search_Lucene_Field object
echo $document->getFieldValue('title');
// same as getFieldValue()
echo $document->title;
}
]]></programlisting>
<para>
The fields available from the <classname>Zend_Search_Lucene_Document</classname> object
are determined at the time of indexing. The document fields are either indexed, or
index and stored, in the document by the indexing application
(e.g. LuceneIndexCreation.jar).
</para>
<para>
Note that the document identity ('path' in our example) is also stored
in the index and must be retrieved from it.
</para>
</sect2>
<sect2 id="zend.search.lucene.searching.results-limiting">
<title>Limiting the Result Set</title>
<para>
The most computationally expensive part of searching is score calculation. It may take
several seconds for large result sets (tens of thousands of hits).
</para>
<para>
<classname>Zend_Search_Lucene</classname> gives the possibility to limit result set size
with <methodname>getResultSetLimit()</methodname> and
<methodname>setResultSetLimit()</methodname> methods:
</para>
<programlisting language="php"><![CDATA[
$currentResultSetLimit = Zend_Search_Lucene::getResultSetLimit();
Zend_Search_Lucene::setResultSetLimit($newLimit);
]]></programlisting>
<para>
The default value of 0 means 'no limit'.
</para>
<para>
It doesn't give the 'best N' results, but only the 'first N'
<footnote>
<para>
Returned hits are still ordered by score or by the specified order, if given.
</para>
</footnote>.
</para>
</sect2>
<sect2 id="zend.search.lucene.searching.results-scoring">
<title>Results Scoring</title>
<para>
<classname>Zend_Search_Lucene</classname> uses the same scoring algorithms as Java
Lucene. All hits in the search result are ordered by score by default. Hits with greater
score come first, and documents having higher scores should match the query more
precisely than documents having lower scores.
</para>
<para>
Roughly speaking, search hits that contain the searched term or phrase more frequently
will have a higher score.
</para>
<para>
A hit's score can be retrieved by accessing the <code>score</code> property of the hit:
</para>
<programlisting language="php"><![CDATA[
$hits = $index->find($query);
foreach ($hits as $hit) {
echo $hit->id;
echo $hit->score;
}
]]></programlisting>
<para>
The <classname>Zend_Search_Lucene_Search_Similarity</classname> class is used to
calculate the score for each hit. See <link
linkend="zend.search.lucene.extending.scoring">Extensibility. Scoring
Algorithms</link> section for details.
</para>
</sect2>
<sect2 id="zend.search.lucene.searching.sorting">
<title>Search Result Sorting</title>
<para>
By default, the search results are ordered by score. The programmer can change this
behavior by setting a sort field (or a list of fields), sort type and sort order
parameters.
</para>
<para>
<code>$index->find()</code> call may take several optional parameters:
</para>
<programlisting language="php"><![CDATA[
$index->find($query [, $sortField [, $sortType [, $sortOrder]]]
[, $sortField2 [, $sortType [, $sortOrder]]]
...);
]]></programlisting>
<para>
A name of stored field by which to sort result should be passed as the
<varname>$sortField</varname> parameter.
</para>
<para>
<varname>$sortType</varname> may be omitted or take the following enumerated values:
<constant>SORT_REGULAR</constant> (compare items normally- default value),
<constant>SORT_NUMERIC</constant> (compare items numerically),
<constant>SORT_STRING</constant> (compare items as strings).
</para>
<para>
<varname>$sortOrder</varname> may be omitted or take the following enumerated values:
<constant>SORT_ASC</constant> (sort in ascending order- default value),
<constant>SORT_DESC</constant> (sort in descending order).
</para>
<para>
Examples:
</para>
<programlisting language="php"><![CDATA[
$index->find($query, 'quantity', SORT_NUMERIC, SORT_DESC);
]]></programlisting>
<programlisting language="php"><![CDATA[
$index->find($query, 'fname', SORT_STRING, 'lname', SORT_STRING);
]]></programlisting>
<programlisting language="php"><![CDATA[
$index->find($query, 'name', SORT_STRING, 'quantity', SORT_NUMERIC, SORT_DESC);
]]></programlisting>
<para>
Please use caution when using a non-default search order; the query needs to retrieve
documents completely from an index, which may dramatically reduce search performance.
</para>
</sect2>
<sect2 id="zend.search.lucene.searching.highlighting">
<title>Search Results Highlighting</title>
<para>
<classname>Zend_Search_Lucene</classname> provides two options for search results
highlighting.
</para>
<para>
The first one is utilizing <classname>Zend_Search_Lucene_Document_Html</classname> class
(see <link linkend="zend.search.lucene.index-creation.html-documents">HTML documents
section</link> for details) using the following methods:
</para>
<programlisting language="php"><![CDATA[
/**
* Highlight text with specified color
*
* @param string|array $words
* @param string $colour
* @return string
*/
public function highlight($words, $colour = '#66ffff');
]]></programlisting>
<programlisting language="php"><![CDATA[
/**
* Highlight text using specified View helper or callback function.
*
* @param string|array $words Words to highlight. Words could be organized
using the array or string.
* @param callback $callback Callback method, used to transform
(highlighting) text.
* @param array $params Array of additionall callback parameters passed
through into it (first non-optional parameter
is an HTML fragment for highlighting)
* @return string
* @throws Zend_Search_Lucene_Exception
*/
public function highlightExtended($words, $callback, $params = array())
]]></programlisting>
<para>
To customize highlighting behavior use <methodname>highlightExtended()</methodname>
method with specified callback, which takes one or more parameters
<footnote>
<para>
The first is an <acronym>HTML</acronym> fragment for highlighting and others are
callback behavior dependent. Returned value is a highlighted
<acronym>HTML</acronym> fragment.
</para>
</footnote>
, or extend <classname>Zend_Search_Lucene_Document_Html</classname> class and redefine
<methodname>applyColour($stringToHighlight, $colour)</methodname> method used as a
default highlighting callback.
<footnote>
<para>
In both cases returned <acronym>HTML</acronym> is automatically transformed into
valid <acronym>XHTML</acronym>.
</para>
</footnote>
</para>
<para>
<link linkend="zend.view.helpers">View helpers</link> also can be used as callbacks in
context of view script:
</para>
<programlisting language="php"><![CDATA[
$doc->highlightExtended('word1 word2 word3...', array($this, 'myViewHelper'));
]]></programlisting>
<para>
The result of highlighting operation is retrieved by
<code>Zend_Search_Lucene_Document_Html->getHTML()</code> method.
</para>
<note>
<para>
Highlighting is performed in terms of current analyzer. So all forms of the word(s)
recognized by analyzer are highlighted.
</para>
<para>
E.g. if current analyzer is case insensitive and we request to highlight 'text'
word, then 'text', 'Text', 'TEXT' and other case combinations will be highlighted.
</para>
<para>
In the same way, if current analyzer supports stemming and we request to highlight
'indexed', then 'index', 'indexing', 'indices' and other word forms will be
highlighted.
</para>
<para>
On the other hand, if word is skipped by current analyzer (e.g. if short words
filter is applied to the analyzer), then nothing will be highlighted.
</para>
</note>
<para>
The second option is to use
<code>Zend_Search_Lucene_Search_Query->highlightMatches(string $inputHTML[,
$defaultEncoding = 'UTF-8'[,
Zend_Search_Lucene_Search_Highlighter_Interface $highlighter]])</code> method:
</para>
<programlisting language="php"><![CDATA[
$query = Zend_Search_Lucene_Search_QueryParser::parse($queryStr);
$highlightedHTML = $query->highlightMatches($sourceHTML);
]]></programlisting>
<para>
Optional second parameter is a default <acronym>HTML</acronym> document encoding. It's
used if encoding is not specified using Content-type HTTP-EQUIV meta tag.
</para>
<para>
Optional third parameter is a highlighter object which has to implement
<classname>Zend_Search_Lucene_Search_Highlighter_Interface</classname> interface:
</para>
<programlisting language="php"><![CDATA[
interface Zend_Search_Lucene_Search_Highlighter_Interface
{
/**
* Set document for highlighting.
*
* @param Zend_Search_Lucene_Document_Html $document
*/
public function setDocument(Zend_Search_Lucene_Document_Html $document);
/**
* Get document for highlighting.
*
* @return Zend_Search_Lucene_Document_Html $document
*/
public function getDocument();
/**
* Highlight specified words (method is invoked once per subquery)
*
* @param string|array $words Words to highlight. They could be
organized using the array or string.
*/
public function highlight($words);
}
]]></programlisting>
<para>
Where <classname>Zend_Search_Lucene_Document_Html</classname> object is an object
constructed from the source <acronym>HTML</acronym> provided to the
<classname>Zend_Search_Lucene_Search_Query->highlightMatches()</classname> method.
</para>
<para>
If <varname>$highlighter</varname> parameter is omitted, then
<classname>Zend_Search_Lucene_Search_Highlighter_Default</classname> object is
instantiated and used.
</para>
<para>
Highlighter <methodname>highlight()</methodname> method is invoked once per subquery, so
it has an ability to differentiate highlighting for them.
</para>
<para>
Actually, default highlighter does this walking through predefined color table. So you
can implement your own highlighter or just extend the default and redefine color table.
</para>
<para>
<code>Zend_Search_Lucene_Search_Query->htmlFragmentHighlightMatches()</code> has similar
behavior. The only difference is that it takes as an input and returns
<acronym>HTML</acronym> fragment without <>HTML>, <HEAD>, <BODY> tags.
Nevertheless, fragment is automatically transformed to valid <acronym>XHTML</acronym>.
</para>
</sect2>
</sect1>
|