简体   繁体   English

ferret / act_as_ferret:指定字段为空

[英]ferret / acts_as_ferret : Specify that a field should be blank

This relates to the lucene-based search engine, Ferret. 这与基于Lucene的搜索引擎Ferret有关。 https://github.com/dbalmain/ferret https://github.com/dbalmain/ferret

Let's say i have a model with two fields, myfield1 and myfield2 . 假设我有一个模型,其中包含两个字段myfield1myfield2 I want to get records that have myfield equal to "foo", or that have null (or an empty string) for myfield but have myfield2 set to "foo". 我想获取myfield等于“ foo”或myfield为null (或空字符串)但myfield2设置为“ foo”的记录。

I DON'T want to get records that have, for example, myfield = "bar" and myfield2 = "foo". 我不想获取具有例如myfield =“ bar”和myfield2 =“ foo”的记录。 So, it's not as simple as just saying "myfield:foo || myfield2: foo" - i only want to look at myfield2 if myfield is empty. 因此,这不只是说“ myfield:foo || myfield2:foo”那么简单-如果myfield为空,我只想查看myfield2。

The sql equivalent would be where (myfield = 'foo') or ((myfield is null or myfield = '') and myfield2 = 'foo') . 等效的sql将是where (myfield = 'foo') or ((myfield is null or myfield = '') and myfield2 = 'foo') What would the ferret search string equivalent of this be? 雪貂搜索字符串等效于什么?

The following doesn't work, but it's an example of the sort of thing I'm after: 以下内容不起作用 ,但这只是我所追求的事情的一个示例:

"myfield:foo || (myfield:<blank> && myfield2:foo)"

thanks, Max 谢谢,马克斯

BTW in case it's relevant i'm using acts_as_ferret in ruby on rails, but i think my question really just relates to a ferret search string. 顺便说一句,如果相关的话,我在rails上的ruby中使用acts_as_ferret,但是我认为我的问题确实只与雪貂搜索字符串有关。 I'm using the ferret gem, v=0.11.6 我正在使用雪貂宝石,v = 0.11.6

EDIT: Slightly dirty-feeling solution below, would still like to know if it's possible just with the query string like above. 编辑:下面的感觉有点脏的解决方案,仍然想知道是否有可能仅使用上面的查询字符串。

OK, i got around this by adding a new method, "myfield_blank": 好的,我通过添加一个新方法“ myfield_blank”来解决此问题:

def myfield_blank
  myfield_blank?.to_s
end

then adding myfield_blank => {}, to my acts_as_ferret index definition. 然后将myfield_blank => {},添加到我的acts_as_ferret索引定义中。 So now i can say 所以现在我可以说

"myfield:foo || (myfield_blank:true && myfield2:foo)" 

This works but like I say i'd still like to know if I can just do it in the query, without making new fields: this approach would be unacceptably hacky if i wanted to do it for lots of different fields. 这行得通,但就像我说的那样,我仍然想知道是否可以在查询中执行此操作而不创建新字段:如果我想对许多不同的字段执行此操作,那么这种方法将难以接受。 thanks 谢谢

According to the source ?* should match any nonempty string, so you can try to do it this way: 根据源代码, ?*应当匹配任何非空字符串,因此您可以尝试通过以下方式进行操作:

'myfield:"foo" || (-myfield:"?*" && myfield2:"foo")'

And also I don't really see why 'myfield:"foo" || (myfield:"" && myfield2:"foo")' 而且我也不太明白为什么'myfield:"foo" || (myfield:"" && myfield2:"foo")' 'myfield:"foo" || (myfield:"" && myfield2:"foo")' shouldn't work, but you probably already tried it... 'myfield:"foo" || (myfield:"" && myfield2:"foo")'不起作用,但您可能已经尝试过...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM