简体   繁体   中英

Best way to search data for text auto-complete

I'm facing a problem where I need to show suggested / auto-complete results below a text field based on the names of people. Should I load everyone into a list and sort as a collection on startup or can I index by name in the database and only SELECT rows where the person's name starts with a given character sequence?

Of course I'd rather not have to keep a potentially huge list in memory, so if someone could suggest how I could go about using SQL here that would be awesome, thanks.

I think putting everything in collection will be an overhead for the entire names

Approach

  1. once user types in 1st character, you can hit the DB. put the data into a collection ( tree-set would be preferred) for sorted unique values.
  2. there after filter the data in the collection using some java logic
  3. once user deletes the 1st character, reload the collection with new set after DB hit

Querying your table in DB : This will be relatively slow if you making db call every time.

Storing in Middle Layer : This will be faster than the db call. But here again you can use multiple approaches.

1) Fetch data from DB and store
2) Pre-populate cache and then query your cache.

Storing on Client Side : Store data/names in js file and use js library to perform searching in efficient manner. I have tried this js library and found it pretty good WICK: Web Input Completion Kit .
Also now html 5 supports local storage, so you can evaluate this as well.

So you need to evaluate all options and then find the ONE which you need.

provide a AJAX function which is fecth list of users name like 'input%' to DB .

call this function onkeyPress() event in Your input field

auto-complete is meant to search the results based on the characters inputted by the users, i would just retrieve the rows which match the pattern using this query.

   SELECT uname FROM table WHERE uname like 'cha%';

here cha is the input from the user.

autocomplete would retrieve

  chaten
  chaitanya
  charles

I will suggest you to use jQuery , that will help to provides suggestions while you type into the field. Here is demo

The datasource will be server-side script which returns JSON data, specified via a simple URL for the source-option. In addition, the minLength-option is set to 2 to avoid queries that would return too many results and the select-event is used to display some feedback.

您可以使用SELECT * FROM TABLE_NAME WHERE NAME LIKE %manu%并在表上显示(如果要使用类似搜索的选项)

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