Autocomplete feature in Redis 3.0 - redis

I need to build an auto complete feature with the help of redis.I went through Antirez's article regarding this.However i dont need to give all the possible combinations of the given word to the user.For ex.
I need to provide,
1.The new york times
2.The bombay times
only
to the user when he type "t","th","The" and so on.
How do i acheive this using redis commands.

I found my answer with below link
https://www.cucumbertown.com/craft/autocomplete-using-redis-nginx-lua/
However how to deal with members having same score , i need those sorted with below priorities:
1.score
2.lexographically
example :
127.0.0.1:6379> zrange auto 0 -1 withscores
1) "the british times"
2) "0"
3) "the new york times"
4) "0"
5) "the sydney times"
6) "0"
7) "the bombay times"
8) "1"
127.0.0.1:6379> zrangebylex auto (the "(the\xff"
1) "the british times"
2) "the new york times"
3) "the sydney times"
4) "the bombay times"
I want bombay times to be at 2 nd place and not fourth.How to do this ?

Related

how to enter into an infinite loop of expect operations with Tcl?

How can I intentionally enter an infinite loop with the telnet server? (Not literally infinite, but to "cycle" through a sequence with the server.)
whenever presented with a menu of "WEATHER UNDERGROUND MAIN MENU" or "CITY FORECAST MENU" I'd to enter selection "1" at least for a few iterations.
However, expect is somewhat as it stands.
Can I create a sort of "list" of "triggers" so that whenever a line is read it iterates through a list and selects the first match?
Currently the dict of cities only has the single entry, but the notion would be to iterate through that list of, say, five cities.
automated result (I simply select the connect method of 1):
thufir#dur:~/NetBeansProjects/spawnTelnet/telnet$
thufir#dur:~/NetBeansProjects/spawnTelnet/telnet$ tclsh main.tcl
locations
---------
1 nyc
connect to wunderground with:
-----------------------------
1) noControlFlow
2) connect
connection method: 1
connecting with 1
spawn telnet rainmaker.wunderground.com
getting weather for nyc
Trying 35.160.169.47...
Connected to rainmaker.wunderground.com.
Escape character is '^]'.
------------------------------------------------------------------------------
* Welcome to THE WEATHER UNDERGROUND telnet service! *
------------------------------------------------------------------------------
* *
* National Weather Service information provided by Alden Electronics, Inc. *
* and updated each minute as reports come in over our data feed. *
* *
* **Note: If you cannot get past this opening screen, you must use a *
* different version of the "telnet" program--some of the ones for IBM *
* compatible PC's have a bug that prevents proper connection. *
* *
* comments: jmasters#wunderground.com *
------------------------------------------------------------------------------
Press Return to continue:
Press Return for menu
or enter 3 letter forecast city code--
nyc
WEATHER UNDERGROUND MAIN MENU
******************************
1) U.S. forecasts and climate data
2) Canadian forecasts
3) Current weather observations
4) Ski conditions
5) Long-range forecasts
6) Latest earthquake reports
7) Severe weather
8) Hurricane advisories
9) Weather summary for the past month
10) International data
11) Marine forecasts and observations
12) Ultraviolet light forecast
X) Exit program
C) Change scrolling to screen
H) Help and information for new users
?) Answers to all your questions
Selection:1
Not a valid option. Type a number 1 to 12.
WEATHER UNDERGROUND MAIN MENU
******************************
1) U.S. forecasts and climate data
2) Canadian forecasts
3) Current weather observations
4) Ski conditions
5) Long-range forecasts
6) Latest earthquake reports
7) Severe weather
8) Hurricane advisories
9) Weather summary for the past month
10) International data
11) Marine forecasts and observations
12) Ultraviolet light forecast
X) Exit program
C) Change scrolling to screen
H) Help and information for new users
?) Answers to all your questions
Selection:
CITY FORECAST MENU
---------------------------------------------------
1) Print forecast for selected city
2) Print climatic data for selected city
3) Display 3-letter city codes for a selected state
4) Display all 2-letter state codes
M) Return to main menu
X) Exit program
?) Help
Selection:1
Enter 3-letter city code: nyc
Weather Conditions at 06:51 AM EDT on 11 May 2020 for New York JFK, NY.
Temp(F) Humidity(%) Wind(mph) Pressure(in) Weather
========================================================================
49 93% SSE at 6 29.88 Mostly Cloudy
Forecast for New York, NY
527 am EDT Mon may 11 2020
.Today...Mostly cloudy. A slight chance of showers early, then a
chance of showers. Isolated thunderstorms this afternoon. Highs
in the lower 60s. Southwest winds 5 to 10 mph with gusts up to
20 mph, increasing to west 15 to 20 mph with gusts up to 30 mph
this afternoon. Chance of rain 50 percent.
.Tonight...Partly cloudy with a slight chance of showers with
isolated thunderstorms in the evening, then mostly clear after
midnight. Lows in the lower 40s. Northwest winds 15 to 20 mph
with gusts up to 30 mph. Chance of rain 20 percent.
.Tuesday...Sunny. Highs in the upper 50s. Northwest winds 15 to
20 mph with gusts up to 30 mph.
.Tuesday night...Partly cloudy in the evening, then clearing.
Lows in the lower 40s. Northwest winds 10 to 15 mph. Gusts up to
25 mph in the evening.
.Wednesday...Sunny. Highs in the lower 60s. West winds 5 to
Press Return to continue, M to return to menu, X to exit: thufir#dur:~/NetBeansProjects/spawnTelnet/telnet$
code:
package provide weather 1.0
package require Expect
namespace eval ::wunderground {
}
#works
proc ::wunderground::noControlFlow {city} {
variable telnet [spawn telnet rainmaker.wunderground.com]
puts "getting weather for $city"
expect "Press Return to continue:"
send "\r"
expect "Press Return for menu:"
send "\r"
#assuming actually a dictionary of cities
expect "or enter 3 letter forecast city code--"
send "$city\r"
expect "WEATHER UNDERGROUND MAIN MENU"
send "1\r"
expect "CITY FORECAST MENU"
send "1\r"
expect "Enter 3-letter city code:"
send "$city\r"
expect "Press Return to continue, M to return to menu, X to exit:"
send "M\r"
}
main:
lappend auto_path /home/thufir/NetBeansProjects/spawnTelnet/telnet/weather
package require weather 1.0
package require locations 1.0
set cities [cities::dictionary]
puts "locations\n---------"
dict for {k v} $cities {puts $k\t$v}
#################
puts "\n\n\nconnect to wunderground with:"
puts "-----------------------------"
puts "1)\tnoControlFlow"
puts "2)\tconnect\n\n"
puts -nonewline "connection method: "
flush stdout
gets stdin prompt
puts "connecting with $prompt"
if {$prompt == 1 } {
wunderground::noControlFlow "nyc"
} else {
wunderground::connect "nyc"
}

How to decode values in Redis database?

I stored the geoset in redis database with GEOADD command as -
GEOADD city 13.361389 38.115556 'paris'
and able to get the stored values with following command -
127.0.0.1:6379> ZRANGE city 0 -1
but I am getting response as -
1) "1"
2) "3484047800163752"
3) "10"
4) "3484047800163752"
5) "2"
6) "3484047800163752"
Can anybody please help me to find out decoded values like 13.361389 38.115556
Thanks
Read the documentation, specifically that of the GEOPOS command:
redis> GEOPOS city 1 2 3

redis zrank: get "last 7 days" rank

I want to show three rankings: overall, last 30 days and last 7 days.
I decided to use redis although I've never worked with it before.
The overall rankings seem pretty easy:
Every time a user registers I create a new member to my collection and increase the counter every time he gets a new point.
Then I can get the rank with ZRANK
But how can I get the rank with the points the user got in the last 7 days?
I think I've got it (please provide criticism and ideas for improvement):
As Pieter Noordhuis wrote in this answer: https://groups.google.com/forum/#!topic/redis-db/0xh7tVQsi9Q
all I have to do is:
ZINCRBY day:2014-03-03 1 userID2
ZINCRBY day:2014-03-03 1 userID
ZINCRBY day:2014-03-04 1 userID
ZUNIONSTORE out 2 day:2014-03-03 posts:day:2014-03-04
ZRANGE out 0 -1 WITHSCORES
Result is:
1) "userID"
2) "2"
3) "userID2"
4) "1"

Redis zrange and zrangebyscore

Are these 2 Redis commands different, except for the second having optional LIMIT argument?
http://redis.io/commands/zrange
http://redis.io/commands/zrangebyscore
They are different:
ZRANGE key start stop ...: start and stop are zero-based indexes (i.e they correspond to a position of an element within the sorted set),
ZRANGEBYSCORE key min max ...: min and max refer to scores (i.e they are used to specify a score range).
So, the first one operates by indexes while the second one (as its name implies) operates by scores. Thus they are used for different purposes.
say, the sorted set is:
value score
tom 0
bob 1
alice 100
lucy 102
when you use zrangebyscores, and the max score is 2, the min score is 0, then, you will get tom and bob;
when you use zrange, and the start is 0, the stop is 2, then you will get tom, bob and alice.
127.0.0.1:6379> zadd example 0 tom 1 bob 100 alice 102 lucy
127.0.0.1:6379> zrange example 0 2 WITHSCORES
1) "tom"
2) "0"
3) "bob"
4) "1"
5) "alice"
6) "100"
127.0.0.1:6379> zrangebyscore example 0 2 WITHSCORES
1) "tom"
2) "0"
3) "bob"
4) "1"
this is the key difference.

What are the top movies in this Redis sorted set?

I've downloaded the movies from IMDB (movies-list) from here: http://www.imdb.com/interfaces
I want to count how often a given movie appears in the list with help of Redis sorted sets, but I am bit confused of the outcome:
redis 127.0.0.1:6379> zrangebyscore 'movies:title' 5000 +inf WITHSCORES
1) "Countdown"
2) "5254"
3) "The Bold and the Beautiful"
4) "5322"
5) "Days of Our Lives"
6) "5451"
7) "Neighbours"
8) "6442"
9) "The New Price Is Right"
10) "7633"
11) "Coronation Street"
12) "8097"
I would like to have the movie that appears most often at the top. Also, I am bit confused by the score. What does this 5k, 6k, 7k mean?
The script I use for my experiment is a Rake task like this:
task :import do
file = File.new(ENV['file'])
redis = Redis.new
file.each_line do |l|
if l =~ /^"(.*)"/
puts $1
redis.zincrby 'movies:title', 1, $1
end
end
You might want to try using zrevrangebyscore instead of zrangebyscore.
Syntax:
ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
For example, in your case:
zrangebyscore 'movies:title' +inf 5000 WITHSCORES
Reference