pat / thinking-sphinx

Sphinx/Manticore plugin for ActiveRecord/Rails
http://freelancing-gods.com/thinking-sphinx
MIT License
1.63k stars 470 forks source link

Unable to filter using date range #1142

Closed siva-wal closed 5 years ago

siva-wal commented 5 years ago
ThinkingSphinx::Index.define :article_page_detail, with: :active_record do
  indexes :title
  indexes :plain_text
  indexes :html_text
  indexes article("name"),        as: :article_name
  indexes :created_at
end

On execution of

ArticlePageDetail.search('book', with: {created_at: 1.week.ago..Time.now}).count

giving following error:

Caused by ThinkingSphinx::QueryExecutionError: index article_page_detail_core: no such filter attribute 'created_at' - SELECT * FROM `article_page_detail_core` WHERE MATCH('book') AND `created_at` BETWEEN 1567068772 AND 1567673572 AND `sphinx_deleted` = 0 LIMIT 0, 20; SHOW META
from /Users/sivagollapalli/.rvm/gems/ruby-2.6.3/gems/thinking-sphinx-4.4.1/lib/thinking_sphinx/connection/client.rb:65:in `rescue in perform'
Caused by Mysql2::Error: index article_page_detail_core: no such filter attribute 'created_at'
from /Users/sivagollapalli/.rvm/gems/ruby-2.6.3/gems/mysql2-0.5.2/lib/mysql2/client.rb:131:in `_query'
pat commented 5 years ago

Hi @siva-wal,

The indexes method is for fields, which are text data you expect search keywords to match on. For ordering and filters, you need to use attributes, which are defined using the has method instead:

ThinkingSphinx::Index.define :article_page_detail, with: :active_record do
  indexes title, plain_text, html_text
  index article.name, as: :article_name

  has created_at
end

Also, I've updated the index syntax in the above example - the approach you were using above with symbols is quite old (perhaps from an old blog post somewhere?).

The difference between fields and attributes is discussed here in the documentation - https://freelancing-gods.com/thinking-sphinx/v4/sphinx_basics.html - though I know it's not an obvious distinction for anyone not familiar with Sphinx!