<?php

/**
 * clucene.php
 *
 * A simple script to query a lucene index.
 *
 * usage: php clucene.php [query]
 */

/**
 * Retrieves the path of the index directory
 */
function get_index_path() {
	$tests_dir = dirname(__FILE__);
	$len = strlen($tests_dir) - strlen("examples");
	return substr($tests_dir, 0, $len) . "data/index";
}

/* $path is a string, e.g. "/some/path/to/index" */
$path  = get_index_path();
/* $field is the default field to be searched. */
$field = "contents";

/* Creation of an IndexSearcher instance */
try {
	$searcher = new IndexSearcher($path, $field);
} catch (Exception $e) {
	echo 'Message: ' . $e->getMessage() . ', ';
	echo 'File: '    . $e->getFile()    . ', ';
	echo 'Line: '    . $e->getLine();
	exit();
}

$query = '';
if (isset($_SERVER['argv'][1])) {
	$query = $_SERVER['argv'][1];
} else {
	$query = "time"; /* Should return 3 results */
}

echo "Search results for: '$query'\n";
$hits = $searcher->search($query);

$length = $hits->length();
echo "Number of results: $length\n";

for ($i = 0; $i < $length; $i++) {
	echo 'path: '  . $hits->get($i, 'path')     . ", ";
	echo 'id: '    . $hits->id($i)              . ", ";
	echo 'score: ' . round($hits->score($i), 3) . "\n";
}

/* Optional */
$searcher->close();

?>
