Jump to content

lucene-relevance sorting


Recommended Posts

i have a searching script that is integrated with lucene but i am not able to sort by relevance.following is the code

function microtime_float()
  list($usec, $sec) = explode(" ", microtime());
  return ((float)$usec + (float)$sec);

//$location = "/var/www/index/emp";
//$location = "/var/www/html/fieldsindex";
//$location = "/var/www/html/js/indexer/index";
$time_start = microtime();
$hits = new java("org.apache.lucene.search.Hits");
$indexreader = new java("org.apache.lucene.index.IndexReader");
//echo $indexreader;
$reader_ref = $indexreader->open($location);

$searcher = new java("org.apache.lucene.search.IndexSearcher",$reader_ref);
$queryparser = new java("org.apache.lucene.queryParser.QueryParser");
$analyzer = new java("org.apache.lucene.custom.MyAnalyzer");
//$query = $queryparser->parse( 'gender:male','content',$analyzer);
$query = $queryparser->parse('sequence:(java^4)','name',$analyzer);
//$query = $queryparser->parse('salary:"0000" AND keywords:"php"','content',$analyzer);

$sortfield = new java("java.lang.String","DocRef");
//$sortfield = new java("org.apache.lucene.search.SortField","sequence");
$sort = new java("org.apache.lucene.search.Sort",$sortfield);
$system = new Java('java.lang.System');
$startTime = $system->currentTimeMillis();
$hits = $searcher->search($query,$sort);
$endTime = $system->currentTimeMillis();

//$time_end = microtime(true);
$start = 0;
$max = 50;
echo "hits".$hits->length();
echo "<br>";
if ($hits->length()!=0 ) {
if (@$_GET['startat'] && @$_GET['maxresults']) {
$start = $_GET['startat'];
$max = $_GET['maxresults'];
$thispage = $max;
if ($start+$max > $hits->length()) {
        $thispage = $hits->length()-$start;
$document = new java('org.apache.lucene.document.Document');

for ($i=$start;$i<$thispage+$start;$i++) {
$document = $hits->doc($i);
$docref = $document->get("DocRef");
//$docid = $document->get("docref");
echo 'DocRef:'.$docref.'<br>';
$search_time = ($endTime - $startTime)/1000.0;
$search_time = round($search_time,3);
echo 'search response:'.$search_time.'secs<br>';
echo 'number of hits:'.$hits->length();
<a href="fieldssearch.php?startat=<?=$start+$max?>&maxresults=<?=$max?>">More</a>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.