There are two ways to search the index. The first method uses Query Parser to construct query from a string. The second provides the ability to create your own queries through the ZSearch API.
Before choosing to use the provided Query Parser, please consider the following:
Both ways use the same API method to search through the index:
require_once('ZSearch/ZSearch.php'); $index = new ZSearch('/data/my_index'); $index->find($query);
The ZSearch::find()
method determines input type automatically and
uses query parser to construct appropriate ZSearchQuery object
from a string.
It is important to note that find()
IS case sensitive. By default,
LuceneIndexCreation.jar normalizes all documents to lowercase. This can be turned
off with a command line switch (type LuceneIndexCreation.jar with no arguments
for help). The case of the text supplied to find()
must match that
of the index. If the index is normalized to lowercase, then all text supplied
to find()
must pass through strtolower()
, or else it
may not match.
The search result is an array of ZSearchQueryHit objects. Each of these has
two properties: $hit->document
is a document number within
the index and $hit->score
is a score of the hit in
a search result. Result is ordered by score (top scores come first).
The ZSearchQueryHit object also exposes each field of the ZSearchDocument found by the hit as a property of the hit. In this example, a hit is returned and the corresponding document has two fields: title and author.
require_once('ZSearch/ZSearch.php'); $index = new ZSearch('/data/my_index'); $hits = $index->find($query); foreach ($hits as $hit) { echo $hit->id; echo $hit->score; echo $hit->title; echo $hit->author; }
Optionally, the original ZSearchDocument object can be returned from the
ZSearchQueryHit.
You can retrieve indexed parts of the document by using the getDocument()
method of the index object and then get them by
getFieldValue()
method:
require_once('ZSearch/ZSearch.php'); $index = new ZSearch('/data/my_index'); $hits = $index->find($query); foreach ($hits as $hit) { // return ZSearchDocument object for this hit echo $document = $hit->getDocument(); // return a ZSearchField object from the ZSearchDocument echo $document->getField('title'); // return the string value of the ZSearchField object echo $document->getFieldValue('title'); // same as getFieldValue() echo $document->title; }
The fields available from the ZSearchDocument 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).
Pay attention, that document identity ('path' in our example) is also stored in the index and must be retrieved from them.