I am using sphinx search in my rails project. I have sphinx.yml file, i-e.
defaults: &defaults
enable_wildcard: 1
min_prefix_len: 2
enable_star: 1
max_matches: 25000
development:
<<: *defaults
test:
<<: *defaults
production:
<<: *defaults
pid_file: "RAILS_ROOT/log/searchd.pid"
searchd_file_path: "RAILS_ROOT/config/db/sphinx"
indexer_binary_name: "/usr/local/bin/indexer"
searchd_binary_name: "/usr/local/bin/searchd"
port: 9314
In my controller's action, i have follwing code.
Application.search(query,:with => options,:order => "updated_at DESC",:max_matches=> 25_000,
:match_mode => :extended).page(params[:page]).per(11)
i am getting error like this.
searchd error (status: 1): per-query max_matches=25000 out of bounds (per-server max_matches=1000)
Any good suggestion?? please.
You need to increase max_matches in sphinx.conf and restart searchd process. max_matches is a searching setting , has nothing to do with indexing process.
Related
I am relatively new to Ruby on Rails and I am trying to build a robust system with plenty of wonderful tests. I am currently trying to run a test on my database queries but I am having an error when I run the query.
Disclaimer -> I know that this query is a bit messy, but I am more familiar with SQL queries and this was the best method for me.
def setup
#parts = Part.find_by_sql("SELECT DISTINCT num AS Part_Number,description AS Description, QTYONHAND as On_Hand, QTYONORDER as On_Order,
QTYALLOCATED as Allocated, QTYNOTAVAILABLETOPICK as Not_Avail_Pick FROM (part JOIN qtyinventorytotals ON part.id = qtyinventorytotals.PARTID) WHERE part.id in
(SELECT PARTID FROM qtyinventorytotals);").to_a
end
This is just the setup function for the test case and the error is:
Run options: --seed 34672
# Running:
E
Error:
PartTest#test_parts_pull_from_db:
ActiveRecord::NotNullViolation: Mysql2::Error: Field 'activeFlag' doesn't have a default value: INSERT INTO `part` (`id`) VALUES (980190962)
bin/rails test test/models/part_test.rb:11
Finished in 7.505619s, 0.1332 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
Here is my database.yml file with the database. I am running a local MySql and querying from the localhost.
# MySQL. Versions 5.1.10 and up are supported.
#
# Install the MySQL driver
# gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
# gem 'mysql2'
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.7/en/old-client.html
#
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: root
host: localhost
development:
<<: *default
database: xxxxxxxx
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: xxxxxxxx
username: root
password: root
# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
# DATABASE_URL="mysql2://myuser:mypass#localhost/somedatabase"
#
# You can use this database configuration with:
#
# production:
# url: <%= ENV['DATABASE_URL'] %>
#
production:
<<: *default
database: xxxxxxxx
username: root
password: root
Again, I am new to Ruby on Rails and all of the wonderful Ruby Magic that happens. I have a feeling that the error is in the database.yml file but I haven't found any resources to steer my in the right direction. Any assistance would be greatly appreciated so that I can really get testing.
Also, I am only reading from this database and there will be no writing, updating or deleting.
EDIT:
Here is the full test class:
require 'test_helper'
class PartTest < ActiveSupport::TestCase
def setup
#parts = Part.find_by_sql("SELECT DISTINCT num AS Part_Number,description AS Description, QTYONHAND as On_Hand, QTYONORDER as On_Order,
QTYALLOCATED as Allocated, QTYNOTAVAILABLETOPICK as Not_Avail_Pick FROM (part JOIN qtyinventorytotals ON part.id = qtyinventorytotals.PARTID) WHERE part.id in
(SELECT PARTID FROM qtyinventorytotals);").to_a
end
# ActiveRecord will return a nil object if there is an error with the DB pull.
test "parts_pull_from_db" do
assert_not #parts.nil?
end
end
New
When I run this query in the console, the result is
Part Load (21549.0ms) SELECT DISTINCT num AS Part_Number,description AS Description, QTYONHAND as On_Hand, QTYONORDER as On_Order,
QTYALLOCATED as Allocated, QTYNOTAVAILABLETOPICK as Not_Avail_Pick FROM (part JOIN qtyinventorytotals ON part.id = qtyinventorytotals.PARTID) WHERE part.id in
(SELECT PARTID FROM qtyinventorytotals);
=> [#<Part id: nil>, #<Part id: nil>, #<Part id: nil>, #<Part id: nil>, #<Part id: nil>, #<Part id: nil>, #<Part id: nil>, #<Part id: nil>, #<Part id: nil>, #<Part id: nil>, #<Part id: nil
>, #<Part id: nil>, #<Part id: nil>, ...]
When I run the rails server, the application successfully pulls the data from the database and renders the data nicely. Why am I getting these results when running from the rails console or the testing environment and not from running the application?
I am creating the rails app with 'whenever' gem.
And I defined the function in the User model and I want to execute the function every 7am.
But my function is not executing correctly and it shows the error.
schedule.rb
set :output, 'log/crontab.log'
set :environment, :production
every 1.day, at: '7:00 am' do
runner 'User.create_group'
end
crontab.log
/Users/michel/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/mysql2-0.4.8/lib/mysql2/client.rb:89:in `connect': Access denied for user ''#'localhost' to database 'appnorth_production' (Mysql2::Error)
here is my database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password:
socket: /tmp/mysql.sock
development:
<<: *default
database: appnorth_development
test:
<<: *default
database: appnorth_test
production:
<<: *default
database: appnorth_production
username: appnorth
password: <%= ENV['APPNORTH_DATABASE_PASSWORD'] %>
Does anyone help me?
Please insert the following line to schedule.rb.
And, Could you try?
job_type :runner, "export :environment_variable=:environment && cd :path && bin/rails runner ':task' :output"
I have a rails application (ruby 2.0.0, Rails 4.0.1) that connects to several MySQL database.
The connection to the local database always work properly, this is my configuration:
production:
adapter: mysql
encoding: utf8
database: xxx
username: yyy
password: zzz
host: x.x.x.x
port: 3306
reconnect: true
But connections to remote databases often return an error, such as connecting to my radius external database returns the following error:
Mysql :: Error:: SELECT 1 AS one FROM WHERE `` radacct` radacct`.`username` =? LIMIT 1
Updating the page several times, the connection is restored and I can see the data properly. This is my configuration:
radius:
port: 3306
adapter: mysql
encoding: utf8
database: xxx
username: yyy
password: zzz
host: x.x.x.x
reconnect: true
connect_timeout: 60
read_timeout: 90
write_timeout: 90
checkout_timeout: 10
reaping_frequency: 30
dead_connection_timeout: 30
I tried to change the configuration of the timers in different ways, but the problem persists.
to connect to the radius server I created the following model common to all:
class RadActiveRecord <ActiveRecord :: Base
self.abstract_class = true
establish_connection "radius"
end
for example for the table radacct use the following model:
class RadAcctDetail <RadActiveRecord
self.table_name = "radacct"
end
the error in question is with any queries, such as:
def index
#rad_acct_details = RadAcctDetail.all.paginate = (
: order => "radacctid DESC"
: page => params [: page] || 1
: per_page => params [: per_page] || 25)
end
Does anyone have any suggestions?
I'm trying to run an initialize script to help me with retrieving data from my config.yml file.
config.yml currently looks like this:
production: &default
log_server: log_server
deploy_to: /path/to/deploy
unicorn:
port: 8080
uid: user
gid: group
development: &dev
<<: *default
deploy_to:path/to/deploy
unicorn:
port: 80
uid: unicorn
gid: unicorngroup
Here's the config.rb file that I have so far:
require 'yaml'
require 'erb'
config = YAML.load(ERB.new(File.read("/Users/NguyenC/gomoto_server/config/api_settings.yml")).result)
If I 'puts config', the output looks like this:
{"production"=>{"log_server"=>"log_server", "deploy_to"=>"path/to/deploy", "unicorn"=>{"port"=>8080, "uid"=>"user", "gid"=>"group"}}, "development"=>{"log_server"=>"log_server", "deploy_to"=>"path/to/deploy", "unicorn"=>{"port"=>80, "uid"=>"unicorn", "gid"=>"unicorn group"}}
How then would I call the variables if I wanted to only get certain ones from either prod or development? My goal is to be able to call any variable depending on the Env that I use.
I have model to store database connection parameters (host, database name, username, password) and filling it by form. Before create or update I need to check if connection be good with entered parameters. I create validate :check_connection validator:
# don`t change primary connection
class Remote < ActiveRecord::Base; end
def check_connection
return if errors.any? || (!new_record? && password.empty?)
begin
Remote.establish_connection(
adapter: 'mysql2',
host: host,
username: username,
password: password,
database: database,
connect_timeout: 5,
reconnect: false
)
# maybe need to make some sql request? did try it
rescue Exception => e
errors.add :connection, 'Could not connect to database'
end
end
When I try enter accessible host (localhost), code like above works good. But if host like 192.168.1.1 (unaccessible) page freezing after submit form. I see attempts to connect every second and it did not stop after ESC (stop loading page) at browser (I see attempts at tcpdump on network interface). And attempts did not stop..
So, how can I validate connection to database? And if connection can not be established page must will not be load long time.
I did use gem 'mysql2' and bundle install 0.3.11 version. This version ignore connect_timeout and bug fixed at newer version. After I try 0.3.12b4 (gem 'mysql2', '~> 0.3.12b4') all works fine.
Variable connect_timeout is a global variable. Therefore, mysql2 maybe ignore it.
on mysql5.6:
mysql[(none)]> set connect_timeout = 123;
ERROR 1229 (HY000): Variable 'connect_timeout' is a GLOBAL variable and should be set with SET GLOBAL
I set timeout variables when initializing mysql2 but it's not reflected. A README of mysql2 says that you can set the *timeout options, but I think the README is outdated or broken.
on mysql2 0.3.14(gem):
client = Mysql2::Client.new(
host: 'localhost',
database: 'test',
username: 'root',
password: '',
connect_timeout: 3,
read_timeout: 3,
write_timeout: 3,
wait_timeout: 3);
client.query('show variables like "%timeout%"').map{|r| [r["Variable_name"], r["Value"]] }
=> [["connect_timeout", "10"],
["delayed_insert_timeout", "300"],
["innodb_lock_wait_timeout", "50"],
["innodb_rollback_on_timeout", "OFF"],
["interactive_timeout", "28800"],
["lock_wait_timeout", "31536000"],
["net_read_timeout", "30"], # Maybe older mysql has read_timeout?
["net_write_timeout", "60"], # Maybe older mysql has write_timeout?
["slave_net_timeout", "3600"],
["wait_timeout", "28800"]]
If you use ActiveRecord, you can set only wait_timeout variable by database.yml.
in database.yml:
development:
adapter: mysql2
encoding: utf8
charset: utf8
database: test
pool: 5
username: root
password:
host: localhost
connect_timeout: 3
read_timeout: 3
write_timeout: 3
wait_timeout: 3
A result of ActiveRecord 4.0.1:
> ActiveRecord::Base.connection.execute('show variables like "%timeout%"').to_a
=> [["connect_timeout", "10"],
["delayed_insert_timeout", "300"],
["innodb_flush_log_at_timeout", "1"],
["innodb_lock_wait_timeout", "50"],
["innodb_rollback_on_timeout", "OFF"],
["interactive_timeout", "28800"],
["lock_wait_timeout", "31536000"],
["net_read_timeout", "30"],
["net_write_timeout", "60"],
["rpl_stop_slave_timeout", "31536000"],
["slave_net_timeout", "3600"],
["wait_timeout", "3"]]
ActiveRecord set wait_timeout variable in abstract_mysql_adapter.rb.
see:
abstract_mysql_adapter.rb
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
mysql2_adapter.rb
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb