ajidnair Posted August 30, 2006 Share Posted August 30, 2006 i have a searching script that is integrated with lucene but i am not able to sort by relevance.following is the code <html><head><head><body><?php#java_set_library_path("/usr/lib/jre/lib/ext/lucene-1.4.3.jar:/usr/lib/jre/lib/ext/my.jar");function microtime_float(){ list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec);} $location="/var/www/html/test/index";//$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(); ?><form><table><tr><td> <a href="fieldssearch.php?startat=<?=$start+$max?>&maxresults=<?=$max?>">More</a></td></tr></table></form></body></html> Quote Link to comment https://forums.phpfreaks.com/topic/19113-lucene-relevance-sorting/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.