简体   繁体   English

使用PHP MySql进行关键字搜索?

[英]Keyword search using PHP MySql?

I have title (varchar), description (text), keywords (varchar) fields in my mysql table. 我在mysql表中有标题(varchar),描述(文本),关键字(varchar)字段。

I kept keywords field as I thought I would be searching in this field only. 我保留了关键字字段,因为我以为我只会在此字段中搜索。 But I now require to search among all three fields. 但是我现在需要在所有三个字段中进行搜索。 so for keywords "word1 word2 word3", my query becomes 因此,对于关键字“ word1 word2 word3”,我的查询变为

SELECT * FROM myTable
WHERE (
name LIKE '%word1%' OR description LIKE '%word1%' OR keywords LIKE '%word1%' 
OR name LIKE '%word2%' OR description LIKE '%word2%' OR keywords LIKE '%word2%' 
OR name LIKE '%word3%' OR description LIKE '%word3%' OR keywords LIKE '%word3%') 
AND status = 'live'

Looks a bit messy but this works. 看起来有点混乱,但这可行。 But now I need to implement synonym search. 但是现在我需要实现同义词搜索。 so for a given word assuming there are a few synonyms available this query becomes more messy as I loop through all of the words. 因此,对于给定的单词,假设有几个同义词可用,那么当我遍历所有单词时,该查询会变得更加混乱。 As the requirements are getting clearer, I will need to join this myTable to some other tables as well. 随着需求变得越来越清晰,我还将需要将该myTable加入其他一些表中。

So 所以

  • Do you think the above way is messy and will cause problems as the data grow? 您是否认为上述方法比较混乱,会随着数据的增长而引起问题?

  • How can I avoid above mess? 如何避免上述混乱? Is there any cleaner solution I can go by? 我可以通过任何更清洁的解决方案吗? Any example will help me. 任何例子都会对我有帮助。

  • Is there any other method/technique you can recommend to me? 您还有其他可以推荐给我的方法/技术吗?

With thanks 谢谢你

EDIT 编辑

@Peter Stuifzand suggested me that I could create one search_index table and store all 3 fields (title,keyword,desc) info on that and do full text search. @Peter Stuifzand建议我可以创建一个search_index表并存储所有3个字段(标题,关键字,desc)的信息,并进行全文搜索。 I understand that additionally this table will include reference to myTable primary key as well. 我知道该表还将包括对myTable主键的引用。

But my advanced search may include joining mytable with Category table, geographic_location table (for searching within 10, 20 miles etc), filtering by someother criteria and of course, sorting of search results. 但是,我的高级搜索可能包括将mytable与Category表,geoge_location表(用于在10英里,20英里范围内进行搜索)结合在一起,通过其他条件过滤,当然还包括对搜索结果的排序。 Do you think using mysql fulltext will not slow it down? 您认为使用mysql fulltext不会减慢速度吗?

When your queries are getting out of hand, it's sometimes better to write parts of it in SQL and other parts in your programming language of choice. 当查询失控时,有时最好用SQL编写查询的一部分,并用您选择的编程语言编写其他部分。

And you could also use fulltext search for searching. 您还可以使用全文搜索进行搜索。 You can create separate table with all fields that you want to search and add the FULLTEXT modifier. 您可以使用要搜索的所有字段创建单独的表,然后添加FULLTEXT修饰符。

CREATE TABLE `search_index` (
    `id` INT NOT NULL,
    `data` TEXT FULLTEXT,
);

SELECT `id` FROM `search_index` WHERE MATCH(`data`) AGAINST('word1 word2 word3');

One more way (sometimes it's better but it depends...) 另一种方法(有时会更好,但要视情况而定...)

SELECT 
    id, name, description, keywords 
FROM 
    myTable
WHERE
    name REGEXP '.*(word1|word2|word3).*' OR
    description REGEXP '.*(word1|word2|word3).*' OR
    keywords REGEXP '.*(word1|word2|word3).*'
;

PS: But MATCH(cols) AGAINST('expr') possibly is better for your case. PS:但是MATCH(cols) AGAINST('expr')可能更适合您的情况。

如果可能的话,您应该研究全文搜索

Given the expanded requirements, you might want consider using apache solr (see http://lucene.apache.org/solr/ ) it is a faceted search engine, designed for full text searching. 考虑到扩展的需求,您可能需要考虑使用apache solr(请参阅http://lucene.apache.org/solr/ ),这是一个多面搜索引擎,旨在进行全文搜索。 It has a RESTful interface that can return XML or JSON. 它具有RESTful接口,可以返回XML或JSON。 I am using it with a few projects - works well. 我在一些项目中使用它-效果很好。

The only area I see you hitting some problems is potentially with the proximity search, but with some additional logic for building the query it should work. 我看到您遇到问题的唯一区域可能是邻近搜索,但是具有一些用于构建查询的其他逻辑,它应该可以工作。

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

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