Http://tech.meituan.com/pinyin-suggest.html
---------------------------------------------------------------------
Snapshot: Issue background
Search Keyword Smart tips is a standard search application, the main role is to prevent users from entering the wrong search terms, and to guide users to the appropriate keywords, to enhance the user search experience.
There are millions of merchants in our CRM system, in order to let users quickly find the target business, we implement the Merchant search module based on Solrcloud. In order to improve the user's search experience and input efficiency, the user mainly enters the merchant name and the merchant address to search for the merchant, this paper realizes a keyword intelligent hint (suggestion) based on SOLR prefix matching query.
Demand analysis
Support for prefix matching principle
Enter "Sea Bottom" in the search box, under the search box will be prefixed with seabed, display "seabed fishing", "seabed fishing hot pot", "Underwater World" and so on search words, enter "Wanda", will prompt "Wanda Imax", "Wanda Plaza", "Wanda department store" and other search terms.
Supports both Chinese and pinyin input
Because of the characteristics of Chinese, if the search automatically prompts can support pinyin will bring greater convenience to users, so as not to switch input method. For example, input "Haidi" prompt keyword and input "seabed" hint, the input "Wanda" and enter "Wanda" prompt keyword.
Support Polyphone input Prompt
For example, input "Chongqing" or "zhongqing" can prompt "Chongqing hotpot", "Chongqing Grilled Fish", "Chongqing Little Swan".
Support Pinyin abbreviation input
For longer keywords, it is necessary to provide pinyin abbreviation input in order to improve the input efficiency. For example, the input "HD" should be able to prompt "Haidi" Similar keywords, input "WD" also can be prompted "Wanda" keyword.
Sort by keyword heat based on the user's historical search behavior
In order to provide the accuracy of the Suggest keyword, the final query results, according to the frequency of user query keywords to sort, such as input [Chongqing,chongqing,cq,zhongqing,zq]-> ["Chongqing hotpot" (F1), "Chongqing Grilled Fish" (F2), "Chongqing Little Swan "(F3), ...], query frequency f1 > F2 > F3.
Solution Solutions
Keyword Collection
When the user enters a prefix, there are a lot of candidates for the prompt, how to choose, which shows in front, which shows in the back? This is a question of search heat. Users in the use of search engines to find a business, will enter a large number of keywords, each input is the keyword of a vote, then the number of keywords are entered more, it corresponds to the query is more popular, so need to query the keywords recorded down, and statistics of the frequency of each keyword, convenient to prompt the results by frequency sorting. The search engine logs all the retrieved strings used by the user each time they are retrieved through a log file, with a length of 1-255 bytes for each query string.
Kanji to Pinyin
User input keywords may be Chinese characters, numbers, English, pinyin, special characters and so on, due to the need to implement phonetic cues, we need to convert Chinese characters into Pinyin, Java, consider using the PINYIN4J component to achieve the conversion.
Pinyin abbreviation Extraction
Taking into account the need to support pinyin abbreviations, kanji conversion Pinyin process, by the way to remove pinyin abbreviations, such as "Chongqing", "zhongqing"---> "CQ", "ZQ".
-
polyphone full array
to support the Polyphone hint, after the query string is converted to pinyin, you need to implement a full-permutation combination, the string polyphone the full permutation algorithm as follows:
public static list Getpermutationsentence (list<list> termarrays,int start) {
if (Collectionutils.isempty ( termarrays)) return collections.emptylist (); int size = Termarrays.size (); if (Start < 0 | | start >= size) {return collections.emptylist (); } if (start = = size-1) {return termarrays.get (start); } list<string> strings = Termarrays.get (start); list<string> permutationsentences = getpermutationsentence (termarrays, start + 1); if (Collectionutils.isempty (strings)) {return permutationsentences; } if (Collectionutils.isempty (permutationsentences)) {return strings; } list<string> result = new arraylist<string> (); for (string pre:strings) {for (string suffix:permutationsentences) {Result.add (pre+suffix); }} return result;
}
Index and prefix query
Scheme one trie tree + TOPK algorithm
trie Tree is a dictionary tree, also known as the word search tree or key tree, is a tree-shaped structure, is a hash tree variant. Typical applications are used to count and sort large numbers of strings (but not limited to strings), so they are often used by search engine systems for text frequency statistics. It has the advantage of minimizing unnecessary string comparisons and querying efficiencies over hash tables. Trie is a tree that stores multiple strings. The edges between adjacent nodes represent one character, so that each branch of the tree represents a substring, and the leaf node of the tree represents the complete string. Unlike normal trees, the same string prefix shares the same branch. For example, given a set of Word Inn, int, at, age, ADV, ant, we can get the following trie:
From the point of view, when the user input prefix I, the search box may show the I prefix "in", "Inn", "int" and other keywords, and then when the user input prefix A, the search box may be prompted with a prefix "ate" and other keywords. Thus, the first step to implement the search engine intelligence hint suggestion is clear, that is, using trie tree to store a large number of strings, the current prefix fixed, storage relatively hot suffix.
The TOPK algorithm is used to solve the problem of statistical hot words. There are two main strategies for solving TOPK problems: hashmap statistics + sorting, heap ordering
HashMap Statistics: First preprocessing This batch of massive data. The method is to maintain a key is the query string, Value is the number of occurrences of the query Hashtable, that is, Hash_map (query,value), each read a query, if the string is not in the table, then add the string, And the value is set to 1, if the string in the table, then the count of the string is added to a, and finally in the time complexity of O (N) with the hash table to complete the statistics.
Heap sequencing: With the help of heap, the data structure is used to find the top K and the time complexity is n ' logk. With the help of the heap structure, we can find and adjust/move within the time of the log magnitude. Therefore, maintain a K (10) Size of the small Gan, and then traverse 3 million of the query, respectively, and the root element to compare. So, our final time complexity is: O (n) + N ' * O (LOGK), (n is 10 million, N ' is 3 million).
The problem with this scenario is:
- When you build indexes and queries, you convert Chinese characters to pinyin, and after the query is finished, you have to convert the pinyin into Chinese characters, and you need to consider numbers and special characters.
- Need to maintain pinyin, abbreviated two trie trees.
Scenario Two SOLR comes with suggest smart tips
SOLR, as a widely used search engine system, has built-in smart hints, called suggest modules. The module can choose to do smart hints based on the text of the cue word, and also supports smart hints by establishing an indexed thesaurus for a field in the index. (See the SOLR wiki page http://wiki.apache.org/solr/Suggester)
The problem with this scenario is:
- The returned results are sorted based on the word frequency of the fields in the index, not how often the user searches for keywords, so you can't put some hot keywords in front of them.
- Phonetic hints, polyphone, abbreviations or additional indexed fields.
Scenario three Solrcloud establish a separate collection, using SOLR prefix query implementation
As mentioned above, there are some problems in the implementation of the above two programs, Trie tree +TOPK algorithm, in the processing of Chinese characters suggest is not very elegant, and the need to maintain two trie trees, the implementation of a more complex The problem with the Suggest smart hint component in SOLR is that using the freq sorting algorithm, the returned results are based entirely on the number of occurrences of the characters in the index and do not take into account the frequency with which the user is searching for words, so it is not possible to rank some of the hot words in a more forward position. As a result, we continue to look for a more elegant solution to this problem.
At this point, we consider establishing an index collection specifically for the keyword, using the SOLR prefix query implementation. The Copyfield in SOLR solves the need to index multiple fields at the same time (kanji, pinyin, abbre), and the multivalued property of field is set to True to resolve the polyphone composition of the same keyword. The configuration is as follows:
Schema.xml:<field name= "kw" Type= "string" indexed= "true" stored= "true"/> <field name= "Pinyin" type= "string" Indexed= "true" stored= "false" multivalued= "true"/><field name= "Abbre" type= "string" indexed= "true" stored= " False "multivalued=" true "/><field name=" kwfreq "type=" int "indexed=" true "stored=" true "/><field name=" _ Version_ "type=" Long "indexed=" true "stored=" true "/><field name=" suggest "type=" Suggest_text "indexed=" true " Stored= "false" multivalued= "true"/>------------------ Multivalued indicates that the field is multivalued-------------------------------------<uniqueKey>kw</uniqueKey>< Defaultsearchfield>suggest</defaultsearchfield> Description: KW is the multivalued=true of the original keyword pinyin and abbre, when using SOLRJ to build this index , defined as a collection type: the Pinyin field for the keyword "Chongqing" is {chongqing,zhongqing}, the Abbre field is {CQ, Zq}kwfreq is the key frequency for the user to search, Sort-------------------------------------------------------<copyfield source= "kw" dest= "suggest"/> for queries <copyfield source= "Pinyin" dest= "suggest"/><copyfield source= "Abbre" dest= "suggest"/>------------------suggest_text----------------------------------<fieldtype name= "Suggest_ Text "class=" SOLR. TextField "positionincrementgap=" autogeneratephrasequeries= "true" > <analyzer type= "index" > < ; Tokenizer class= "SOLR. Keywordtokenizerfactory "/> <filter class=" SOLR. Synonymfilterfactory "synonyms=" Synonyms.txt "ignorecase=" true " Expand= "true"/> <filter class= "SOLR. Stopfilterfactory "ignorecase=" true "words=" Stopwords.txt "ena Blepositionincrements= "true"/> <filter class= "SOLR. Lowercasefilterfactory "/> <filter class=" SOLR. Keywordmarkerfilterfactory "protected=" Protwords.txt "/> </analyzer> <analyzer type=" Query "> <tokenizer class= "SOLR. Keywordtokenizerfactory "/> <filter class=" SOLR. StopfiLterfactory "ignorecase=" true "words=" Stopwords.txt "Enablepos Itionincrements= "true"/> <filter class= "SOLR. Lowercasefilterfactory "/> <filter class=" SOLR. Keywordmarkerfilterfactory "protected=" Protwords.txt "/> </analyzer></fieldType>
Keywordtokenizerfactory: This word breaker does not make any participle! The entire character stream becomes a single word element. The string field type also has a similar effect, but it cannot configure other processing components of text parsing, such as case conversions. Any index field used for sorting and most faceting functions, this index field can only have one word element in the original domain value.
Prefix query constructs:
private SolrQuery getSuggestQuery(String prefix, Integer limit) { SolrQuery solrQuery = new SolrQuery(); StringBuilder sb = new StringBuilder(); sb.append(“suggest:").append(prefix).append("*"); solrQuery.setQuery(sb.toString()); solrQuery.addField("kw"); solrQuery.addField("kwfreq"); solrQuery.addSort("kwfreq", SolrQuery.ORDER.desc); solrQuery.setStart(0); solrQuery.setRows(limit); return solrQuery;}
The effect is as follows:
Reference
- From the trie tree spoke the suffix tree http://blog.csdn.net/v_july_v/article/details/6897097
- Search Smart tips suggestion, nearby locations search http://blog.csdn.net/v_july_v/article/details/11288807
- SOLR Suggester Http://wiki.apache.org/solr/Suggester
US search-an implementation of the intelligent hints for search engine keywords [go]