searching for empty fields

456 views
Skip to first unread message

Simon

unread,
Jan 10, 2011, 9:10:02 PM1/10/11
to Thinking Sphinx
Hi there,

I am trying to search for empty strings, and am getting back no
results. I am indexing two values, title and body, in my define_index
block. I am then trying to something along the lines of
Article.search :conditions => {:body => ""} but I get back no
results. I know there are indexed entries with blank bodies, so I
feel I must be missing something here.

If this is possible and I get it going, I would also like to be able
to search for Articles with both blank bodies and titles. Can I just
add both fields into the conditions hash? Will this get treated as an
AND condition?

Thanks,

Simon

Pat Allan

unread,
Jan 13, 2011, 2:33:26 AM1/13/11
to thinkin...@googlegroups.com
Hi Simon

Sphinx has no concept of searching for empty fields. I think a better option would be to add an attribute indicating whether the specific column is blank:

has "LENGTH(body) < 0", :as => :empty_body, :type => :boolean

That should work for MySQL - for PostgreSQL, I think the function is char_length.

And then in searches, you can do the following:

Article.search :with => {:empty_body => true}

Add another attribute for the title as well, and then you can use that filter.

Hope this helps.

--
Pat

> --
> You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group.
> To post to this group, send email to thinkin...@googlegroups.com.
> To unsubscribe from this group, send email to thinking-sphi...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/thinking-sphinx?hl=en.
>

Simon

unread,
Jan 13, 2011, 3:28:12 PM1/13/11
to Thinking Sphinx
Clever. Thanks for the help!

Simon

yuri.r...@vidiemme.it

unread,
Jan 27, 2016, 4:57:58 AM1/27/16
to Thinking Sphinx
Hi,

I'm trying to do the same thing but still have an empty result

In my case:

has "LENGTH(has_countries) < 0", :as => :empty_countries, :type => :boolean

result = Digitalasset.search match_mode: :extended, :with => {:empty_countries => true}, page: page, per_page: per_page, :order => :updated_at, :sort_mode => :desc

Thanks in advance and sorry for my english.

Pat Allan

unread,
Jan 27, 2016, 6:42:24 AM1/27/16
to thinkin...@googlegroups.com
Hi Yuri

What you’re doing looks correct - which version of Thinking Sphinx are you using?

— 
Pat

To unsubscribe from this group and stop receiving emails from it, send an email to thinking-sphi...@googlegroups.com.

To post to this group, send email to thinkin...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages