Today, I ran to Joyo to check if the books I needed arrived. By the way, I searched for other books. I found that there was automatic filling, but I didn't find it. Shame.
Case:
If I enter de, there will be "German", "dechuan jiakang" and other entries, along with multiple search results.
Problem:
How is Joyo implemented?
Analysis:
Of course, I cannot see its source code and database schema. Follow my own ideas to solve this problem.
First, let's take a look at the effect of Joyo:
- When you enter"De", The following results are displayed:
We can see that"De"Chinese Characters in pronunciation, including English.
- When"Deyu"What will it do? Figure:
This is a whole input"Deyu"Pinyin, so the following areGermanOf course, if there is"Moral Education.
- Look at the input"De yu"What will it do? I guess there is no result. In fact:
This indicates that there cannot be spaces in the middle. Unless "German" is "German", the following results will be returned.
What conclusions can you draw from above?
My conclusion is:
- Pinyin is in the Chinese order. If there is space, there is space in the corresponding position of the pinyin. For example, "German" corresponds to "deyu", and "German" is "de yu ".
- Uppercase/lowercase letters.
Since we have reached the conclusion above, how can we proceed? For programs that are not large, it is very easy to set directly in the database.
Suppose there is a table named item_t in the database. The reason why item is selected is that Joyo buys books, sells CD and other products, so it cannot be replaced by book_t.
Item_t should have the following fields:
- Id of each book
- Price the price of each book
- Name: the name of each book.
- Name_alias is used to store the pinyin above.
Here we will introduce name_alias, which is the key field. The corresponding pinyin should be stored during input. According to the general habit, the English title should be stored in English. Chinese characters are stored in pinyin.
Implementation
With the above analysis, what else can't be solved. It is obvious that ajax technology is used. To make the search accurate, you should query the name and name_alias fields. Write an SQL statement:
SELECT id, name FROM item_t WHERE name LIKE "% keyword_you_search % OR name_alias LIKE '% keyword_you_search %'
You may ask, here the output is the entire title, which is different from the output. Indeed.
So how does Joyo do it? I guess sheStore keywords entered by each user. This makes query easier. But in the end, the first step will not be required.
Extended
Some may ask, what are you talking about too simple? What Will Google China do? I don't know.
From the Google homepage alone, it is actually similar to joyo. However, the problem is that Google should not use SQL Server or Oracle databases for storage, because it is not conducive to query and other operations.
Therefore, the above name_alias may not work. Otherwise, manual input is required every time, which is a lot of effort. The only possibility is automatic processing.
What should we do? The worst one is to extract the Pinyin of every Chinese character. I believe this is not difficult, or how Google's input method is implemented.
I think everyone knows how to do the following.
More
I will not describe how to use Ajax here. Some people may mention that Chinese language cannot be output. These are basic knowledge. I will not describe them here.
Finally, you are welcome to make a brick.
Feedback
According to comments, many people are very concerned about performance issues, which is different from the original intention I wrote this article at the time. Since performance is mentioned, I think I will discuss it in future articles.