0

私は製品のデータベースを持っており、ThinkingSphinxを使用してそれらを検索しています。正しいカテゴリと詳細で適切な製品を見つけるための条件はかなり複雑です。ただし、商品モデルのフロート属性である商品の価格と数量を制限できるように、検索を絞り込みたいと思います。

例:

1.9.2p290 :014 > Product.search('eggs').map { |p| "#{p.name}, $#{p.price}" }
  Sphinx Query (4.6ms)  eggs
  Sphinx  Found 984 results
  Product Load (0.9ms)  SELECT "products".* FROM "products" WHERE "products"."id" IN (7635, 7636, 7637, 7638, 7639, 7640, 7641, 7642, 7643, 7644, 7645, 7646, 7647, 7648, 7650, 7651, 7652, 7653, 7654, 7655)
[
    [ 0] "Egg Beaters Pour Spout, $2.89",
    [ 1] "Fleischman Regular Egg Beaters Egg Substitute, $3.09",
    [ 2] "Fleischman Southwest Egg Beaters Egg Substitute, $2.89",
    [ 3] "Lucerne Best Of The Egg, $2.79",
    [ 4] "Lucerne Best Of The Egg, $2.69",
    [ 5] "Lucerne Best Of The Egg, $5.29",
    [ 6] "Lucerne Best Of The Egg Whites, $2.79",
    [ 7] "Lucerne Best Of The Egg Whites, $5.29",
    [ 8] "Papetti Foods All Whites Liquid Egg Whites, $5.89",
    [ 9] "Papetti Foods Healthier Real Egg Product Better N Eggs, $5.89",
    [10] "Papettis 100% All Egg Whites, $2.5",
    [11] "Eating Right Eggs With Omega A, $3.99",
    [12] "Egglands Best Cage Free Large Eggs, $3.29",
    [13] "Egglands Best Cage Free Large Grade AA Brown Eggs, $4.39",
    [14] "Egglands Best Classic Large Eggs, $5.49",
    [15] "Egglands Best Grade AA Large Eggs, $4.09",
    [16] "Hard Boiled Eggs Peeled And Ready To Eat, $1.49",
    [17] "Land O Lakes Cage Free All Natural Large Grade A Brown Eggs, $4.39",
    [18] "Land O Lakes Farm Fresh Extra Large Brown Eggs, $2.49",
    [19] "Land O Lakes Organic All Natural Large Grade A Brown Eggs, $5.49"
]



1.9.2p290 :015 > Product.search('eggs').class
ThinkingSphinx::Search < Array



1.9.2p290 :016 > Product.search('eggs').where("price < ?", 3)
NoMethodError:   Sphinx Query (5.5ms)  eggs
  Sphinx  Found 984 results
  Product Load (0.8ms)  SELECT "products".* FROM "products" WHERE "products"."id" IN (7635, 7636, 7637, 7638, 7639, 7640, 7641, 7642, 7643, 7644, 7645, 7646, 7647, 7648, 7650, 7651, 7652, 7653, 7654, 7655)
undefined method `where' for #<ThinkingSphinx::Search:0x007feaf30e1880>
  from /Users/neil/.rvm/gems/ruby-1.9.2-p290/gems/thinking-sphinx-2.0.10/lib/thinking_sphinx/search.rb:174:in `method_missing'
  from (irb):16
  from /Users/neil/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.5/lib/rails/commands/console.rb:47:in `start'
  from /Users/neil/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.5/lib/rails/commands/console.rb:8:in `start'
  from /Users/neil/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.5/lib/rails/commands.rb:41:in `<top (required)>'
  from script/rails:6:in `require'
  from script/rails:6:in `<main>'

TSが一致するテキストを見つけた後、ActiveRelationを使用して検索を絞り込むにはどうすればよいですか?

4

1 に答える 1

0

これは機能するはずです:

Product.search 'eggs', :with => {:price => price, :quantity => quantity}

検索文字列('eggs')、the、priceおよびを定義するための3つの異なる入力ボックスがあるとすると、quantity完全一致を検索するときに次のようなものを使用します。

scope = params[:query].present? Product.search params[:query] : scope = Product.search nil

scope = scope.search with: { :price => params[:price] } if params[:price].present?
scope = scope.search with: { :price => params[:quantity] } if params[:quantity].present?

@products = scope

しかし、それは「xより低い価格」または「xとyの間の数量」をフィルタリングしません。そのためには、思考から範囲を使用する必要があります-スフィンクス。たとえば、特定の値よりも低い価格の商品を検索する場合は、次のようになります。

Product.search 'eggs', :with => {:price => 0..value}

'eggs'これにより、価格が0から指定された値までのクエリに一致する商品が返されます。

指定された値よりも高い価格の商品を検索する場合:

Product.search 'eggs', :with => {:price => value..Float::MAX}

'eggs'これにより、指定された値と表現可能な最大の有限浮動小数点数の間の価格でクエリに一致する製品が返されます。

于 2012-08-29T09:06:16.213 に答える