简体   繁体   English

mysql全文搜索,部分匹配

[英]mysql fulltext search with partial matches

I have a table containing all US cities. 我有一张包含美国所有城市的表格。 The structure and sample row looks like: 结构和示例行如下所示:

| id    | zip   | state | city   | lat       |  lng        |
------------------------------------------------------------
| 29431 | 82732 | WY    | Wright | 43.829349 | -105.532327 |

The goal is to build an AJAX autocomplete search that will check input against zip , state or city . 目标是建立一个AJAX autocomplete搜索,该搜索将根据zipstatecity检查输入。 These are some searches I've done and the corresponding results: 这些是我已经完成的一些搜索以及相应的结果:

- wright
// returns all records containing wright as city

- 82732
// returns a single matching record

- wrig
// returns all records that partially match

- wright wy
// returns nothing

So the last query returns nothing, yet I need it to return the only matching record. 所以最后一个查询什么都不返回,但是我需要它返回唯一匹配的记录。

The search query in use where the terms with + and * are generated by PHP: 使用的搜索查询,其中带有+*的术语由PHP生成:

MATCH (zip, state, city) AGAINST ('+wright* +wy*' IN BOOLEAN MODE)

So how can I modify this search so that searches such as wright wy will only return the exact matching record, NOT including other locations in the results? 那么,如何修改此搜索,以使诸如wright wy搜索仅返回完全匹配的记录, NOT在结果中包括其他位置?

Also it would be nice but not at all necessary if the order of the terms didn't matter. 同样,如果条款的顺序无关紧要,那将是不错的选择,但完全没有必要。 ie wy wright still returns valid results. wy wright仍返回有效结果。

How about a two-step search. 两步搜索怎么样。 First do a split of RIGHT(entry,2) which you first search for against only the State column for a match. 首先对RIGHT(entry,2)进行拆分,然后首先仅针对State列搜索匹配项。 If it matches, then you remove 2 characters and do a search for city on the remaining. 如果匹配,则删除2个字符并在其余字符上搜索城市。 After the strip, you continue with your existing code. 在剥离之后,继续使用现有代码。

If no state found, then proceed to your existing code directly. 如果未找到状态,则直接进入现有代码。

In practice, I would also put a step if the state is found to strip both right ',' and ending spaces. 在实践中,如果发现状态会同时剥夺正确的','和结尾空格,我也会采取措施。

Arno 亚诺

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

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