RethinkDB – 使用Match()按照同一数据集和表中的值进行筛选

huangapple go评论93阅读模式
英文:

RethinkDB - Filter using Match() by value in same dataset and table

问题

所以,由于显然我太笨了,无法自己解决这个问题,我会在这里向大家请教。

基本上,我有一个数据结构,看起来像这样:

....,
{
"id": 12345

....

"policy_subjects": [
{
"compiled": "^(user|max|anonymous)$",
"template": "<user|max|anonymous>"
},
{
"compiled": "^max$",
"template": "max"
}
]
....
}

compiled 是一个“编译过的”正则表达式

template 是没有正则表达式修饰符的相同正则表达式


我想要做的是在 RethinkDB 中使用“compiled”值进行简单的查询,并将其与字符串(比如“max”)进行匹配。

基本上,我想要做的是:

r.table("regex_policies").filter(function(policy_row) {
return "max".match("(?i)"+policy_row("policy_subjects")("compiled"))
}

这是我想要做的(不区分大小写的搜索)。

数据库中当然有很多 policy_subjects,所以在这个例子中,结果应该是与“max”匹配的整个数据集(1个结果)。因为在这种情况下,“max”出现了两次,并且它同时匹配了两个正则表达式(一次匹配就足够了)。

在这个例子中,“foobar”将不会产生任何结果,因为任何一个编译过的正则表达式都不匹配“foobar”。

有人知道如何执行这个相对简单的查询吗?

英文:

So, since I'm too dumb obviously to figure this out myself, I'll ask you better folks here on SO instead.

Basically i have a datastructure that looks like the following:

....,
{
 &quot;id&quot;: 12345

 ....

 &quot;policy_subjects&quot;: [
   {
     &quot;compiled&quot;:  &quot;^(user|max|anonymous)$&quot;,
     &quot;template&quot;:  &quot;&lt;user|max|anonymous&gt;&quot;
   },
   {
     &quot;compiled&quot;:  &quot;^max$&quot;,
     &quot;template&quot;:  &quot;max&quot;
   }
 ]
....
}

compiled is a "compiled" regex

template is the same regex without regex-modifiers


What I want is to do a simple query in RethinkDB using the "compiled" value and matching that against a string, say "max".

Basically 

r.table(&quot;regex_policies&quot;).filter(function(policy_row) {
  return &quot;max&quot;.match(&quot;(?i)&quot;+policy_row(&quot;policy_subjects&quot;)(&quot;compiled&quot;))
}

Is what i want to do (+case-insensitive search)

There are of course lots of policy_subjects in the database so in this example the result should be the whole dataset (1 result) that matches "max". Since "max" exists twice in this case and it matches both regexes (once would have been enough).

"foobar" would likewise in this example yield 0 results, since any of the compiled regexes does not match "foobar".

Does anyone know how to do this relatively simple query?

答案1

得分: 1

你肯定想在这里使用r.expr,我成功运行了这个例子:

r.expr([{
    "id": 12345,
    "policy_subjects": [
        {
            "compiled": "^(user|max|anonymous)$",
            "template": "<user|max|anonymous>"
        },
        {
            "compiled": "^max$",
            "template": "max"
        }
    ]
}]).merge(function(policy_row) {  
    return {
        "policy_subjects": policy_row("policy_subjects").filter(function(item){
            return r.expr("max").match(r.expr("(?i)").add(item("compiled"))).ne(null);
        })
    }
})

max更改为不匹配的其他内容,将返回没有policy_subjects元素的文档。

例如,将max更改为wat(我有史以来最喜欢的测试字符串),如下所示:

.merge(function(policy_row) {  
    return {
        "policy_subjects": policy_row("policy_subjects").filter(function(item){
            return r.expr("wat").match(r.expr("(?i)").add(item("compiled"))).ne(null);
        })
    }
})

结果如下:

[
    {
        "id": 12345,
        "policy_subjects": []
    }
]

我认为你想要缩减到你想要的一个policy_subject文档的逻辑可能有点主观,所以我不确定正确答案是什么,但你可以使用.reduce(...)来返回最右边的值。

英文:

You definitely want to use r.expr here and I got this example to work:

r.expr([{
 &quot;id&quot;: 12345,
 &quot;policy_subjects&quot;: [
   {
     compiled:  &quot;^(user|max|anonymous)$&quot;,
     template:  &quot;&lt;user|max|anonymous&gt;&quot;
   },
   {
     compiled:  &quot;^max$&quot;,
     template:  &quot;max&quot;
   }
 ]
}]).merge(function(policy_row) {  
  return {
    &quot;policy_subjects&quot;: policy_row(&quot;policy_subjects&quot;).filter(function(item){
			return r.expr(&quot;max&quot;).match(r.expr(&quot;(?i)&quot;).add(item(&quot;compiled&quot;))).ne(null);
		})
  }
})

Changing max to something else that does not match, returns the document with no elements inside policy_subjects.

For example, changing max => to wat (my favorite test string of all time) looks like this:

.merge(function(policy_row) {  
  return {
    &quot;policy_subjects&quot;: policy_row(&quot;policy_subjects&quot;).filter(function(item){
			return r.expr(&quot;wat&quot;).match(r.expr(&quot;(?i)&quot;).add(item(&quot;compiled&quot;))).ne(null);
		})
  }
})

And results in this:

[
  {
    &quot;id&quot;: 12345 ,
    &quot;policy_subjects&quot;: [ ]
  }
]

I think your logic for reducing to the one policy_subject document you want might be a little subjective to your use case so I'm not sure what the right answer is but you can use .reduce(...) to just return the right-most value.

huangapple
  • 本文由 发表于 2016年2月9日 17:00:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/35287733.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定