ranguba / rroonga

The Ruby bindings of Groonga.
http://ranguba.org/#about-rroonga
66 stars 26 forks source link

Query syntax for searching (multiple) nil values #197

Closed tomas closed 3 years ago

tomas commented 4 years ago

Hi, me again. :)

I'm able to perform using two or more filters where one of them is blank/nil, for example:

table.select("+name:foo +description:\\").first # matches name: foo', description: nil

This works as long as the nil column is at the end of the list, because if I do:

table.select("+description:\\ +name:foo").first

I get a nasty column lookup failed error.

So the question is: how can I make the second example work?

This is important because I want to be able to search for two nil columns at the same time, and this problem is preventing me from doing so.

Hope you can help me out. Thanks!

kou commented 4 years ago

How about the following?


table.select do |record|
  conditions = []
  conditions << (record.name =~ name) if name
  conditions << (record.description =~ description) if description
  conditions
end
tomas commented 4 years ago

Thanks, but I need to make it work using the query syntax (not the block syntax). This is because I want to combine different criteria dynamically.

If not possible, can this be fixed in a future version of rroonga?

kou commented 4 years ago

You can use the query syntax with record.name.match(name).

tomas commented 4 years ago

Sorry, I meant the table.select("+column:value") syntax, as shown in the first message. Not sure how it's called.

kou commented 4 years ago

How do you create the query syntax? End users specify it? Or your program builds it?

tomas commented 4 years ago

I'm building an ORM around Groonga (rroonga) so that different apps can declare their models in an ActiveRecord-like fashion (using relationships, named scopes, etc).

I'm currently using this ORM in a specific app where I need to run a query to find records with nil values for two columns.

kou commented 4 years ago

Ah, sorry. I misunderstood.

The following will work:

table.select do |record|
  conditions = []
  conditions << (record.name == name.to_s)
  conditions << (record.description == description.to_s)
  conditions
end

I think that you can use the above API in your ORM. (I'm not sure why you need the query syntax with ORM.)

tomas commented 4 years ago

Using the block syntax would make implementation much more complex, so I'd rather keep on using the query/string syntax (I've managed to do everything else so far).

Where would I modify this in the rroonga source code? Either to submit a PR or to start my own fork of this project.

Thanks again

kou commented 4 years ago

I don't know why the block syntax causes more complex implementation but does table.select("+description:\"\" +name:foo") work for you?

tomas commented 3 years ago

Forgot to say that the \"\" thing worked. :)

Thanks!