Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: simple search engine

  1. #1
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default simple search engine

    I am developing a simple search engine using php.
    what is the logic behind searching with "AND" and "parenthesis" advance search

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,413
    Thanks
    101
    Thanked 115 Times in 113 Posts

    Default

    Considering you are posting in this forum I will assume that you are also using MySQL as well. The fundamental formula to begin a search engine for your website looks like this:

    Code:
    SELECT column 
    FROM table 
    WHERE column LIKE '%term%'
    The % acts as a wild card. You can elaborate on this indefinitely

    Code:
    SELECT column 
    FROM table 
    WHERE column LIKE '%term%' 
    AND column LIKE '%word%' 
    AND (column NOT LIKE '%bad word%' OR column NOT LIKE '%bad term%')
    The above is an example that uses both parentheses and AND. I separated the code to different lines for readability. The MySQL words were capitalized merely to set them apart. In this second example we are gathering all of the rows where "column" contains the terms "term" as well as "word" and only one of the following two terms: "bad word" or "bad term", but never all 4 terms together in one field. The only result displayed from each row retrieved will be from the field "column".

    I hope this makes sense.
    Last edited by james438; 06-26-2011 at 01:01 AM. Reason: Added some clarification.
    To choose the lesser of two evils is still to choose evil. My personal site

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    In my opinion there is no such thing as a "simple" search engine. They're always at least a little complicated. If you allow for exactly one term then using LIKE "%term%" is a fairly easy way. But beyond that then it gets complicated.

    Is the answer above what you were looking for or were you wondering about user input involving AND and ()? User input is complicated. You will need patience and lots of string functions or regex to parse the input. Then you'll need to generate something like what Traq has above.

    A while ago I wrote my own user search to query conversion functions. It was complicated, but they work pretty well. I'm surprised by how fast MySQL is. Depending on what you need, I could share those with you. I think I posted them here while I was working on them and possibly the final versions. Let me know (reply) if that would help.
    Last edited by djr33; 06-26-2011 at 02:58 AM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. The Following User Says Thank You to djr33 For This Useful Post:

    dipika (06-26-2011)

  5. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,413
    Thanks
    101
    Thanked 115 Times in 113 Posts

    Default

    While I agree with your statement djr33, I want to start simple so as not to discourage him if he wants to start down this road. This was how I started; with something simple, which became much more complicated as I came to a better understanding of how I wanted it to work.

    The thread you are referring to is here.

    There are other search scripts out there. When I started working on a search script for my site I wanted to have a search script that would pull results in real time, one that would search for precisely what I searched for as opposed to an approximation, one where I could search for phrases or negate phrases or individual terms. I wanted the results to be manageable visibly yet complete. I wanted the user to have some control over which tables were searched, such as anime, news, or articles. I also wanted to have a simple to use admin control panel to make updating how the control operates simple to operate. For these reasons I created my own.

    There are other plugins out there, such as a google plugin to search your site if your requirements are not as stringent or another, which I have not tried called sphider. I believe I skipped sphider, because I wanted to be more intimately involved with the security of the script.

    I wish I could share my script with you, but I'd rather not until I get a minor, but quite complicated, security upgrade in place, which should take a few more days yet. Basically, when the user is not logged in the results returned from a search will come back and the hidden results will not be retrieved, which is as it should, but instead of saying that there are 4 results and display 4 results it will say that 5 results were retrieved when only 4 are displayed. The reason the number is 5 is that it is counting the hidden results that should only be visible to the admin. This came about after normalizing a few of my database tables.

    I'm talkative tonight.
    Last edited by james438; 06-26-2011 at 05:22 AM. Reason: added 3 more paragraphs.
    To choose the lesser of two evils is still to choose evil. My personal site

  6. #5
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default And operator

    Thanks for the refered thread. but i am still confused in "AND" searching.
    suppose user input is "(events And news) or local".
    i wrote a sql like this:
    SELECT * FROM `page` AS a1 LEFT JOIN `page_tag` AS a2 ON a2.page_id=a1.id WHERE ((Tag_id= 3 and Tag_id= 1) or Tag_id= 5)

    i have generated the above where statement using str_replace function and
    tag_id are the id of the above keyword.

    sice both events and local are in the same table and same field. tag_id can't be two different value at a same time.

    How can i get the result of AND operation.

  7. #6
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,413
    Thanks
    101
    Thanked 115 Times in 113 Posts

    Default

    I'm not sure if you can pull results from multiple categories using MySQL. I wouldn't be surprised if I am wrong, though, either.

    I would perform two queries. The first would pull page_id where Tag_id= 3 or Tag_id= 1 then put these results into an array and retrieve only the duplicate results.

    The second query would pull page_id where Tag_id=5. Combine the results from array1 and array2. This should give you an array of the page_id that you want.

    AND just means that both events are true.

    ((a='b' AND a='c') or a='d')

    In the above the condition is that col a must = both 'a' and 'c' at the same time. The OR located outside of the parentheses means that if the first condition is not met then see if the second condition is true where a='d'. If neither is valid then do not pull the result.
    To choose the lesser of two evils is still to choose evil. My personal site

  8. The Following User Says Thank You to james438 For This Useful Post:

    dipika (06-26-2011)

  9. #7
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33 View Post
    In my opinion there is no such thing as a "simple" search engine. They're always at least a little complicated. If you allow for exactly one term then using LIKE "%term%" is a fairly easy way. But beyond that then it gets complicated.

    Is the answer above what you were looking for or were you wondering about user input involving AND and ()? User input is complicated. You will need patience and lots of string functions or regex to parse the input. Then you'll need to generate something like what Traq has above.

    A while ago I wrote my own user search to query conversion functions. It was complicated, but they work pretty well. I'm surprised by how fast MySQL is. Depending on what you need, I could share those with you. I think I posted them here while I was working on them and possibly the final versions. Let me know (reply) if that would help.
    yes,it would be really helpful. Actually i want to use user input and i tried to parse the input and finally created a query from that but its not working.
    Thanks a lot

  10. #8
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by james438 View Post
    I'm not sure if you can pull results from multiple categories using MySQL. I wouldn't be surprised if I am wrong, though, either.

    I would perform two queries. The first would pull page_id where Tag_id= 3 or Tag_id= 1 then put these results into an array and retrieve only the duplicate results.

    The second query would pull page_id where Tag_id=5. Combine the results from array1 and array2. This should give you an array of the page_id that you want.

    AND just means that both events are true.

    ((a='b' AND a='c') or a='d')

    In the above the condition is that col a must = both 'a' and 'c' at the same time. The OR located outside of the parentheses means that if the first condition is not met then see if the second condition is true where a='d'. If neither is valid then do not pull the result.
    Actually ,user inputs the search statement. then how do I parse input string .

  11. #9
    Join Date
    Jun 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    $len=strlen($word);
    for($i=0;$i<=$len;$i++)
    {
    if($word[$i]=='(' )
    {
    $bracket[]=$word[$i];
    }
    if($word[$i]=="+" or $word[$i]=="|" or $word[$i]==$word[$len])
    {
    $wordes[]=$part;
    $part="";

    }
    if (ctype_alpha($word[$i]))
    {
    $part.= $word[$i];
    }

    }
    $count=count($wordes);
    $j=0;
    for($j=0;$j<=$count;$j++)
    {
    $page=select * from tag where('name=%s ',$wordes[$j])->getXml();

    foreach($page->Tag as $tagid)
    {
    $id[]=$tagid['id'];
    }

    }
    $query=str_replace('+'," and ",$word);
    $query1=str_replace('|'," or ",$query);
    for($i=0;$i<$count;$i++)
    {
    $query1=str_replace($wordes[$i], "Tag_id= ".$id[$i], $query1);
    }
    //echo $query1;
    echo $query1;
    $page=select *from page LEFT JOIN page_tag where( $query1 );


    This is the query i used to parse my input string

  12. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    See the link to the thread above. If you want to do a full scale search engine, there is a lot of information there and some working code toward the end. I'm not sure that's my newest version but it is working. If you need the finalized version I could send that to you. At the moment I'm a bit busy.
    If you want to try something easier, look into using a single term and using LIKE "%TERM%" as explained above. There is no easy way to make a comprehensive accurate search engine. It's just difficult. I don't mean at all to say you shouldn't try, but also don't think you'll find a shortcut. It's a lot of work, but very useful when completed.

    Take the time to read and understand (most of) the other thread and if that helps, great. If not, try something simpler at least for now. I've been using PHP for years and that was difficult for me. I'm just being honest-- again, I'm not suggesting you should give up or anything.


    It is possible to compare things across tables in MySQL using temporary tables and so forth, but it's complicated and not particularly efficient. Think about ways you might make it more efficient, and if you can't find anything then look into advanced MySQL queries. There's a lot you can do in MySQL beyond the basics, but it's also not needed that often-- and I don't understand most of it. A lot of the time you can get away with using PHP to do the more complicated things, although you lose some efficiency that way as well. The best strategy is a well organized database, and that just takes experience with various data structures to figure out what will best fit your project.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •