简体   繁体   中英

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 . 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:

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?

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.

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. If it matches, then you remove 2 characters and do a search for city on the remaining. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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