Basically I would like to store the top 5 from a sorted set in different set.
In redis 6.2 I can do it with ZRANGESTORE with REV option.
What is the alternative in lower versions?
Looks like this does what I need:
eval "
redis.call('DEL','final')
local set=redis.call('ZREVRANGE','out',0,4,'withscores')
for i = 1, #set/2 do
set[2*i-1], set[2*i] = set[2*i], set[2*i-1]
end
return redis.call('ZADD','final',unpack(set))
" 0
Related
I am writing a Lua script to execute in redis. I am working with Sorted Sets that look like this:
Member: 96954_1_1557705600
Score: 1557705600
The score is a unix epoch time stamp.
I would like to first get the results that are between two time stamps, then filter those based of a glob pattern in the member. Something like MATCH *_1_*.
My script looks like this, but it's failing when I try and pass the Lua table to zscan:
local start_date = KEYS[1]
local end_date = KEYS[2]
local limited_by_date = redis.call('zrangebyscore','rooms', start_date, end_date)
return redis.call('zscan', unpack(limited_by_date), 'match *_1_*')
limited_by_date correct contains the values I expect, but how can I search through them with zscan now?
When you get limited_by_date, you can iterate the array yourself, and output items that match the pattern.
local result = {}
for i, mem in ipairs(limited_by_date) do
if string.match(mem, ".+_1_.+") then result[#result + 1] = mem end
end
return result
There's no need to use the ZSCAN command. In fact, if you use ZSCAN, you have to intersect ZSCAN result and ZRANGEBYSCORE result to get the final answer.
is there a way to do multiple updates based on other field value
WHERE, not CASE
idea is below
thanks
#standardSQL
UPDATE dataset.people
SET CBSA_CODE = '54620' where substr(zip,1,5) = '99047',
SET CBSA_CODE = '31793' where substr(zip,1,5) = '45700'
A CASE expression is in fact the typical way you would handle this logic:
UPDATE dataset.people
SET CBSA_CODE = CASE SUBSTR(zip, 1, 5)
WHEN '99047' THEN '54620'
WHEN '45700' THEN '31793' END
WHERE
SUBSTR(zip, 1, 5) IN ('99047', '45700');
The only alternative to this which I can see would be to run mutliple update statements, one for each ZIP code value. But that seems unwieldy and undesirable as compared to using a CASE expression.
Use SQL CASE, which is part of Standard SQL (see official BQ docs):
#standardSQL
UPDATE dataset.people
SET CBSA_CODE = CASE
WHEN substr(zip,1,5) = '99047' THEN '54620'
WHEN substr(zip,1,5) = '45700' THEN '31793'
END
WHERE substr(zip,1,5) IN('99047', '45700')
Spell check is not working in AEM 6.1 even after creating the suggested indexes as specified in --
https://docs.adobe.com/docs/en/aem/6-1/deploy/platform/queries-and-indexing.html and
https://jackrabbit.apache.org/oak/docs/query/lucene.html
As per the docs spellcheck has been implemented in OAK since versions 1.1.17 and 1.0.13. My OAK version is 1.22 so it should work.The AEM version is 6.1.0.20150507
Created an index like -
/oak:index/lucene-spellcheck
- jcr:primaryType = "oak:QueryIndexDefinition"
- compatVersion = 2
- type = "lucene"
- async = "async"
+ indexRules
- jcr:primaryType = "nt:unstructured"
+ nt:base
+ properties
- jcr:primaryType = "nt:unstructured"
+ jcr:title
- propertyIndex = true
- analyzed = true
- useInSpellcheck = true
I get zero results when I run this query using CRX-DE's query tool as well as using query manager from a jsp.
SELECT [rep:spellcheck()] FROM nt:base WHERE [jcr:path] = '/content/abc' AND SPELLCHECK('tetspage')
('testpage' is a page; misspelled it as 'tetspage')
Running this query as mentioned in the docs
SELECT [rep:spellcheck()] FROM nt:base WHERE [jcr:path] = '/' AND SPELLCHECK('jackrabit')
returns a single node.
Am I doing anything wrong; is this index working in anyone's AEM 6.1 ?
This query works for me
SELECT [rep:spellcheck()] FROM [nt:base] WHERE SPELLCHECK('tetspage') AND ISDESCENDANTNODE('/content/abc')
I had a similar problem with a "rep:suggest()" query, instead of a "rep:spellcheck()", but maybe the solution works for you.
When I tried the query in the CRX DE directly, the result was a single node, and I couldn't see anything on that node.
However, executing the query from my code I found out that the results of the query were stored as "rows". To process that "rows" I used this code, hope it helps you:
String sql = "SELECT [rep:suggest()] FROM cq:PageContent WHERE ISDESCENDANTNODE('" + path + "/') AND SUGGEST('" + text + "')";
Query q = qm.createQuery(sql, Query.SQL);
List<String> results = Lists.newArrayList(); // <--- Actual query results
RowIterator it = q.execute().getRows();
while (it.hasNext()) {
Row row = it.nextRow();
results.add(row.getValue("rep:suggest()").getString());
}
Following code segment is used to add a label to a node using ModeShape. What query string I could use to query this node with the QueryManager? It seems that the nodes from versionHistory side cannot be queried with the QueryManager.
VersionHistory history = session.getWorkspace().getVersionManager()
.getVersionHistory(node.getPath());
history.addVersionLabel(version.getName(), "label", true);
I cannot find any nodes with the following query relating to version history. Is there anything missing from this query?
Query query = queryManager
.createQuery("select * from [nt:base]", Query.JCR_SQL2);
Thanks!
All of the version information is stored on the nt:versionHistory nodes under the /jcr:system/jcr:versionStorage area of the workspace (shared amongst all workspaces in a repository). The structure of this area looks something like this:
+ jcr:system
+ jcr:versionStorage {jcr:primaryType = mode:versionStorage}
+ b4 {jcr:primaryType = mode:versionHistoryFolder}
+ 6d {jcr:primaryType = mode:versionHistoryFolder}
+ de {jcr:primaryType = mode:versionHistoryFolder}
+ 298905f76361779339fa3ccacc4f47664255 {jcr:primaryType = nt:versionHistory}
+ jcr:versionLabels {jcr:primaryType = nt:versionLabels}
+ jcr:rootVersion {jcr:primaryType = nt:version}
- jcr:uuid = ...
- jcr:created = ...
+ jcr:frozenNode {jcr:primaryType = nt:frozenNode}
- jcr:frozenUuid
- jcr:frozenPrimaryType
- jcr:frozenMixinTypes
The jcr:versionLabels node contains a property for each label, where the name of the property is the label string and the value of the property is a REFERENCE to the version to which that label applies.
In order to find the labeled version of a versionable node, you'll actually have to query the contents of this "/jcr:system/jcr:versionStorage" area, using the appropriate types and join constraints.
For example, to find the version with a particular label "foo", you'd do something like this:
SELECT versioned.* FROM [nt:frozenNode] AS versioned JOIN [nt:version] AS version ON ISCHILDNODE(versioned,version) JOIN [nt:versionLabels] AS label ON label.myLabel = version.[jcr:uuid]
This search all of the nt:versionLabels nodes looking for a property named 'myLabel' (which is the name of your label), and then finds the nt:version node to which that points, and the nt:frozenNode that is the copy of the versionable node for that version.
As you can see, the version history representations as prescribed by the JCR specification are quite difficult to query.
Alternatively, you could do this in two steps:
use a query to find the jcr:uuid of the version(s) that has a particular label
for each of those identifiers, find the UUID of the versionable node and use the VersionManager to look up the version history for that node and find the labeled version.
I have a table called " Stat " in my MongoDB database in Rails 3 .
In that table, there is an array field called "services" .
I want to find all Stats that have a services array that contains the value "lights" .
I want to do something like this :
#stats = Stat.all
#stats1 = #stats.where("services contains lights")
Rails.logger.info "result: #{#stats1.count} "
I've tried various things and Googled it extensively, found some leads but nothing that seems to work. I have four records that should match this query but the above returns a zero set.
Is what I want to do possible in rails 3 / mongo ?
Try this,
#stats = Stat.all
#stats1 = #stats.where("'lights' = ANY (services)")
Ok I found the answer to this question:
#stats = #stats.where(:services.in =>['lights'] )
and I also found by poking around that the inverse is:
#stats = #stats.where(:services.nin =>['lights'] )
nin instead of in