Hmset Example Screenshot
This is my demo hmset example, I try to get all hmset list by Redis::hgetall("hmset_demo:user_id:*"), but it return a empty array [].
How can I get this return:
[{
"id": "1",
"name": "name edited 1",
"description": "desc edited 1",
"avatar": "xxx 1"
},
{
"id": "1",
"name": "name edited 1",
"description": "desc edited 1",
"avatar": "xxx 1"
},
...
]
PS: I use the following code to insert:
Redis::hmset("hmset_demo:user_id:".$id, [
'id' => $id,
'name' => 'name edited '.$id,
'description' => 'desc edited'.$id,
'avatar' => 'xxx'.$id,
]);
I can get specific hmset data by this code:
$id = 1;
Redis::hgetall("hmset_demo:user_id:".$id);
You cannot use wildcards in key names.
Let's see an example. We store two hash type keys
127.0.0.1:6379> HSET hmset_demo:user_id:0 NAME PETER
127.0.0.1:6379> HSET hmset_demo:user_id:1 NAME GWEN
We can retrieve their value by its key
127.0.0.1:6379> HGETALL hmset_demo:user_id:0
1) "NAME"
2) "PETER"
127.0.0.1:6379> HGETALL hmset_demo:user_id:1
1) "NAME"
2) "GWEN"
But we cannot use wildcards with HGETALL command
127.0.0.1:6379> HGETALL hmset_demo:user_id:*
(empty array)
If you need to use wildcards to retrieve a lot of keys, you can use SCAN COMMAND
127.0.0.1:6379> SCAN 0 MATCH hmset_demo:user_id:* COUNT 10
1) "0"
2) 1) "hmset_demo:user_id:1"
2) "hmset_demo:user_id:0"
Iterate each element from the resulset and retrieve its values using HGETALL
There is no way to retrieve everything using just one Redis command
Also, HMSET command is deprecated. You should use HSET instead.
Related
There is a column of an index in Kibana, which has an array of data
E.g. Below is a sample column = blocked_by
"blocked_by": [
{
"error_category_name": "Record is not a new one",
"error_category_owner": "AB",
"created_when": "2022-05-18T09:52:44.000Z",
"name": "ERROR IN RCS: Delete Subscriber",
"resolved_when": "2022-05-18T10:52:55.963+01:00",
"id": "8163578639440138764"
},
{
"error_category_name": "NM-1009 Phone Number is not in appropriate state",
"error_category_owner": "AB",
"created_when": "2022-05-18T09:52:45.000Z",
"name": "ERROR IN NC NM: Change MSISDN status",
"resolved_when": "2022-05-18T10:53:16.230+01:00",
"id": "8163578637640138764"
},
I want to extract only the latest record out of this column in my timelion expression
Can someone help me out, if this is possible to do so in timelion
My expression:
.es(index=sales_order,timefield=created_when,q='blocked_by.error_category_owner.keyword:(AB OR Undefined OR null OR "") AND _exists_:blocked_by').divide(.es(index=sales_order,timefield=created_when)).yaxis(2,position=right,units=percent).label(Fallout)
Here's an official example of a RediSearch query:
127.0.0.1:6379> FT.SEARCH myIdx "hello world" LIMIT 0 10
1) (integer) 1
2) "doc1"
3) 1) "title"
2) "hello world"
3) "body"
4) "lorem ipsum"
5) "url"
6) "http://redis.io"
My question is, how could I request just one or two fields, e.g. just to get back to the "title" value ("hello world") or the "ID" and "title" fields ([1, "hello world"]). Mainly for performance reasons.
Yes, it supports it with the RETURN option.
127.0.0.1:6379> FT.SEARCH myIdx "hello world" LIMIT 0 10 RETURN 2 title url
See: https://oss.redislabs.com/redisearch/Commands/#ftsearch
Currently I am caching data from active record to redis by doing following:
redis.rb
$redis = Redis::Namespace.new("bookstore", :redis => Redis.new)
authors_helper.rb
def fetch_authors
authors = $redis.get('authors')
if authors.nil?
authors = Author.all.to_json
$redis.set("authors", authors).to_json
$redis.expire("authors", 5.hour.to_i)
end
JSON.load authors
end
So currently I am using basic set and get to cache and read data from redis.
I want to use hmset instead of just set. The redis way to to this job is as follows:
(Just an example)
HMSET user:1001 name "Mary Jones" password "hidden" email "mjones#example.com"
The authors table in my app consists of following field: id ,name, created_at, updated_at
What is the ruby way to use hmset so that I can cache authors data in a redis hash?
I don't think you can save all authors this way. This is because a hash can store only one value for each key. So name and created_at cannot be keys, because all authors need to store their own values for these keys, but you can use each key only once.
If you're using Ruby on Rails, using Rails.cache is preferred - this way you don't have to worry about the way Rails stores the object in Redis.
However, if you want to use hmset for some reason, I believe the best you can do is something like this:
authors = Author.all.flat_map { |author| [author.id.to_s, author.attributes.to_json] }
$redis.hmset("authors", *authors_data)
The first line will return something like this:
['1', '{"name": "Mary Jones", "email": "m#example.com"}', '2', '{"name": "Another name", "email": "e#example.com"']
hmset command does not accept array, but a flat list of attributes, that's why in the 2nd line you need to pass *authors_data to the function.
Then, internally it will look like this:
{
'1' => '{"name": "Mary Jones", "email": "m#example.com"}',
'2' => '{"name": "Another name", "email": "e#example.com"'
}
Later you can do $redis.hmget("authors", '1') which will return a String '{"name": "Mary Jones", "email": "m#example.com"}'
I have documents that one of their field looks like the following -
"ingredients": [{
"unit": "MG",
"value": 123,
"key": "abc"
}]
And I would like to sort the different records according to the ascending value of specific ingredient. That is if I have 2 records which have use ingredient with key "abc", one with value 1 and one with value 2. The one with ingredient value 1 should appear first.
Each of those records may have more than on ingredient.
Thank you in advance!
The search query to sort will be:
{
"sort":{
"ingredients.value":{
"order":"asc"}
}}
I have two documents "Users" and "Roles" and i am merging these two tables records with following query
r.db('myDB').table('users').merge({
roles: r.db('myDB').table('Roles').getAll(r.args(r.row('roles')))('Name').coerceTo('ARRAY')
})
User Document:
{"id": "1" ,"password": "123" ,"roles": "[1]" ,"userName": "user1"}
{"id": "2" ,"password": "123" ,"roles": ["1","2"] ,"userName": "user2"}
its working fine when there is more than 1 Role against a user. But it return error if a user has only 1 Role the error is
"RqlRuntimeError: Expected type ARRAY but found STRING in:"
It seems like your roles maybe a string instead of an array. If that's the case, try this:
r.db('myDB').table('users').merge({
roles: r.db('myDB').table('roles').getAll(r.args(
r.branch(r.row('roles').typeOf().eq('ARRAY'), r.row('roles'),[r.row('roles')]))
)('Name').coerceTo('ARRAY')
});