简体   繁体   English

Php / MySql'高级搜索'页面

[英]Php/ MySql 'Advanced Search' Page

I'm working on an 'advanced search' page on a site where you would enter a keyword such as 'I like apples' and it can search the database using the following options: 我正在网站上的“高级搜索”页面上输入关键字,例如“我喜欢苹果”,它可以使用以下选项搜索数据库:

Find : With all the words, With the exact phrase , With at least one of the words, Without the words 查找:使用所有单词,使用确切的短语,至少有一个单词,没有单词

I can take care of the 'Exact phrase' by: 我可以通过以下方式处理'确切短语':

SELECT * FROM myTable WHERE field='$keyword';

'At least one of the words' by: '至少有一个'by:

SELECT * FROM myTable WHERE field LIKE '%$keyword%';//Let me know if this is the wrong approach

But its the 'With at least one of the words' and 'Without the words' that I'm stuck on. 但它的'至少有一个词'和'没有词',我坚持。

Any suggestions on how to implement these two? 关于如何实现这两个的任何建议?

Edit: Regarding 'At least one word' it wouldn't be a good approach to use explode() to break the keywords into words, and run a loop to add 编辑:关于'至少一个单词',使用explode()将关键字分解为单词并运行循环来添加不是一个好方法

(field='$keywords') OR ($field='$keywords) (OR)....

Because there are some other AND/OR clauses in the query also and I'm not aware of the maximum number of clauses there can be. 因为查询中还有一些其他的AND / OR子句,我不知道可以有的最大子句数。

I would suggest the use of MySQL FullText Search using this with the Boolean Full-Text Searches functionality you should be able to get your desired result. 我建议使用MySQL FullText Search布尔全文搜索功能,你应该能够得到你想要的结果。

Edit: 编辑:

Requested example based on your requested conditions ("Its just one field and they can pick either of the 4 options (ie 1 word, exact words, at least 1 word, without the term).") 请求的示例基于您请求的条件(“它只是一个字段,他们可以选择4个选项中的任何一个(即1个单词,确切的单词,至少1个单词,没有该术语)。”)

I am assuming you are using php based on your initial post 我假设您正在使用基于您的初始帖子的PHP

<?php
$choice = $_POST['choice'];
$query = $_POST['query'];

if ($choice == "oneWord") {
    //Not 100% sure what you mean by one word but this is the simplest form
    //This assumes $query = a single word
    $result = mysql_query("SELECT * FROM table WHERE MATCH (field) AGAINST ('{$query}' IN BOOLEAN MODE)");
} elseif ($choice == "exactWords") {
    $result = mysql_query("SELECT * FROM table WHERE MATCH (field) AGAINST ('\"{$query}\"' IN BOOLEAN MODE)");
} elseif ($choice == "atLeastOneWord") {
    //The default with no operators if given multiple words will return rows that contains at least one of the words
    $result = mysql_query("SELECT * FROM table WHERE MATCH (field) AGAINST ('{$query}' IN BOOLEAN MODE)");
} elseif ($choice == "withoutTheTerm") {
    $result = mysql_query("SELECT * FROM table WHERE MATCH (field) AGAINST ('-{$query}' IN BOOLEAN MODE)");
}
?>

hope this helps for full use of the operators in boolean matches see Boolean Full-Text Searches 希望这有助于在布尔匹配中充分使用运算符,请参阅布尔全文搜索

You could use 你可以用

With at least one of the words 至少有一个词

SELECT * FROM myTable WHERE field LIKE '%$keyword%' 
or field LIKE '%$keyword2%' 
or field LIKE '%$keyword3%';

Without the word 没有这个词

SELECT * FROM myTable WHERE field NOT LIKE '%$keyword%';

I'm not sure you could easily do those search options in a naive manner as the other two. 我不确定你能否像其他两个那样以天真的方式轻松地做出那些搜索选项。

It would be worth your while implementing a better search engine if you need to support those scenarios. 如果您需要支持这些方案,那么在实施更好的搜索引擎时是值得的。 A simple one that could probably get you by is something along these lines: 一个可能让你得到的简单的东西是这样的:

When an item is added to the database, it is split up into the individual words. 将项目添加到数据库时,会将其拆分为单个单词。 At this point "common" words (the, a, etc...) are removed (probably based on a common_words table). 此时,“常用”字(a,a等)被删除(可能基于common_words表)。 The remaining words are added to a words table if they are not already present. 如果剩余的单词尚未存在,则将其添加到单词表中。 There is then a link made between the word entry and the item entry. 然后在单词条目和项目条目之间建立链接。

When searching, it is then a case of getting the word ids from the word table and the appropriate lookup of item ids in the joining table. 搜索时,就是从单词表中获取单词ID以及在连接表中对项目ID进行适当查找的情况。

Search is notoriously difficult to do well. 众所周知,搜索很难做得很好。

You should Consider using a third party search engine using something like Lucene or Sphider . 您应该考虑使用像LuceneSphider这样的第三方搜索引擎。

Giraffe and Re0sless pooseted 2 good answers. 长颈鹿和Re0sless提出了两个很好的答案。

notes: "SELECT * " sucks... only select the columns that you need. 注意:“SELECT *”很糟糕......只选择你需要的列。 Re0sless puts a "OR" between keywords. Re0sless在关键字之间放置“OR”。 - you should eliminate common words (" ","i","am","and"..etc) - mysql has a 8kb i belive limit on the size of the query, so for really long SELECTS you should slipt it into separate queries. - 你应该删除常用词(“”,“我”,“上午”,“和”等等) - mysql对查询的大小有8kb i belive限制,所以对于很长的SELECTS你应该把它放到单独的查询。 - try to eliminate duplicate keywords (if i search for "you know you like it" the SELECT should basically only search for "you" once and elimnate common words as "it") - 尝试消除重复的关键字(如果我搜索“你知道你喜欢它”,SELECT应该基本上只搜索“你”一次并将常用词理解为“它”)

Also try to use "LIKE" and "MATCH LIKE" (see mysql man page) it could do wonders for "fuzzy" searches 还尝试使用“LIKE”和“MATCH LIKE”(参见mysql手册页)它可以为“模糊”搜索创造奇迹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM