order results by relevancy

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deiv82
    New Member
    • Jul 2010
    • 10

    order results by relevancy

    Hello everybody, I'm trying to find a way to order by relevancy the result of my simple search engine...
    I've found on the web different solutions, but I'm not able to adapt them to my case.

    Code:
    $querystr = "SELECT img,link,numclick,description,order FROM table1 WHERE 1=1 ";
    
    			for ($x = 0; $x < count($keys); $x++) {
    			   $querystr .= "and concat( description, order ) like \"%$keys[$x]%\"";
    			}
    
    			$querystr .= " limit 20";
    Some advices?
    Many thanks
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    What do you see as relevancy?

    Comment

    • deiv82
      New Member
      • Jul 2010
      • 10

      #3
      At the moment I get the results in the order by how they are in the database and not by the most similarity of the word searched...
      I've tried using MATCH and AGAINST but I don't know how to use it in my code and also no idea if it's useful for my case.

      If not I found also this solution it seemed to be good but I can't make it work ...

      Code:
      $querystr = "SELECT 
      ( if ( instr( title , $keys[0] ) > 0 , 3 , 0 )
      
      + if ( instr( title , $keys[1] ) > 0 , 3 , 0 )
      
      + if ( instr( title , $keys[2] ) > 0 , 3 , 0 )
      
      + if ( instr( description, $keys[0]) ‘ ) > 0 , 1 , 0 )
      
      + if ( instr( description, $keys[1]) ‘ ) > 0 , 1 , 0 )
      
      + if ( instr( description, $keys[2]) ‘ ) > 0 , 1 , 0 )
      
      ) as relevance ,
      
      img,
      link,
      numclick,
      description,
      title 
      
      FROM MyTable 
      
      WHERE 
      
      concat_ws(‘ ‘ , title , description) like ‘%$keys[0]%’
      
      and concat_ws(‘ ‘ , title , description) like ‘%$keys[1]%’
      
      and concat_ws(‘ ‘ , title , description) like ‘%$keys[2]%’";
      
      			for ($x = 0; $x < count($keys); $x++) {
      			   $querystr .= "and concat( description, title ) like \"%$keys[$x]%\"";
      			}
      			$querystr .= " ORDER BY relevance DESC LIMIT 20";

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        This thread seems suspiciously similar to this one

        Is it assignment time again

        Comment

        • Oralloy
          Recognized Expert Contributor
          • Jun 2010
          • 988

          #5
          @code_green,

          Your link doesn't work.

          What I see is
          Code:
          http://how%20to%20return%20the%20record%20above%20and%20the%20record%20below/
          Will you please fix it?
          Last edited by Oralloy; Aug 23 '10, 07:36 PM. Reason: Changed HTML pre tags to [code] tags

          Comment

          • deiv82
            New Member
            • Jul 2010
            • 10

            #6
            I have not understood what is the problem...?

            Comment

            Working...