Redis Tips-Auto-complete feature implementation

Source: Internet
Author: User

Auto-completion is usually accompanied by a search box, which is what the user helps to complete automatically when they enter.

For example, to complement the idiom, the existing following idiom: wholeheartedly, the two use, smooth sailing.

Two ways to achieve this:

Implementation method One:

Each prefix of each idiom uses a collection type key to store the idiom name corresponding to the prefix, and in order to implement the ordering, we use an ordered set, and the score are all 0, which is sorted by the dictionary order of the element values. If you want to achieve the sort of word heat, you need to create an ordered set, store words and score, and finally intersect the query result with this set, which avoids the need to update the heat of a word to update multiple collections, because a word appears in multiple collections.

Practice:

For the idiom above, it creates an ordered set of the following form (Key:value,... ), the score is 0:

One: Single-minded, two-tasking, smooth sailing

Heart: Wholeheartedly, two-use

One heart: single-minded

2:1 Heart for Two

Sail: Smooth Sailing

Sail the Wind: smooth sailing

The below implementation generates code for the ordered collection of these prefixes

$VALUEARR= [' wholeheartedly ', ' two-purpose ', ' plain sailing '];//Chinese characters note the specified encoding and cannot be obtained in $str[$i] Wayforeach($VALUEARR  as $item) {     for($i= 1,$len= Mb_strlen ($item, "Utf-8");$i<$len;$i++) {        $k=$prefix. MB_SUBSTR ($item, 0,$i, "Utf-8"); $redis->zadd ($k, 0,$item); }}

The corresponding ordered set can be obtained by the corresponding keyword.

$search 1 = "one"; $search 2 = "one Sail"; $ret 1 $redis->zrange ($$prefix.  $search 1, 0,-1); $ret 2 $redis->zrange ($$prefix.  $search 2, 0,-1);

If you need to achieve the heat sorting, only need to take the keyword set and all idioms of the heat set intersection can be specified by the parameters of two sets of fractional combination mode

$redis->zinter ($retZset, [$prefix.  $search$hotZset// fetch intersection $ret$redis->zrevrange ($retZset  // Reverse

Implementation method Two:

Implemented by an ordered set, this method is introduced by the Redis author. Because the ordered set when the score phase of the element, according to the dictionary order of the elements, using this feature can only use an ordered set to achieve the label completion.

Practice:

Put all the idiom prefixes into an orderly set, and then add the whole idiom behind the * number and then deposit the ordered set, the score is 0.

Gets the rank of the keyword in zset, then gets the rank n elements, and finally iterates through the results, finding the end of the * and the element that starts with the corresponding keyword is the result.

Implement the deposit Code first

//iterate through all prefixes and idioms into an ordered setforeach($VALUEARR  as $item) {     for($i= 1,$len= Mb_strlen ($item, "Utf-8");$i<=$len;$i++) {        $member= Mb_substr ($item, 0,$i, "Utf-8"); if($i==$len) {            $member.= "*";//non-prefixes need to be mended in the back *        }        $redis->zadd ("Auto:zset", 0,$member); }}

Re-implement Get code

//Enquiry$search= "One Sail";$rank=$redis->zrank ("Auto:zset",$search);$ret=$redis->zrange ("Auto:zset",$rank+ 1,$rank+ 10);//Get 10 +$ret=Array_filter($ret,function($d){    return("*" = = = Mb_substr ($d,-1));//This does not remove the *, no matching prefix, more than the number of mismatched bars can be recommended});Echo"<pre>";Print_r($ret);Exit;

Summarize:

The way one needs N collection storage, but the amount of data for each collection is small, the way two only with a set implementation, but the data volume is large, while the implementation of heat sequencing is more difficult, this look at the specific use of scenarios and data volume selection bar.

Redis Tips-Auto-complete feature implementation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.