0

私はRING Appを持っており、Thinking Sphinxを検索に使いたいと思っています。私は次のモデルの間に多くの関係がありますが、ProductにはTypeからProductTypeまでの多くがあります。私ProductsController indexアクションでRails 5、思考のスフィンクス、索引付けと検索には人間関係があります。

# Product.rb 
has_many :product_types 
has_many :types, through: :product_types 

# Type.rb 
has_many :product_types 
has_many :products, through: :product_types 

# ProductType.rb 
belongs_to :product 
belongs_to :type 

私は与えられたVariant IDに基づいて、図に示されている製品をフィルタリングすることができるようにしたいです。

私の関連するインデックスは現在、この(私は長い時間でThinkingSphinxを使用していないが、注意してください)のようになります。

# product_index.rb 
ThinkingSphinx::Index.define :product, :with => :active_record do 
    indexes name, :sortable => true 
    indexes description 
    indexes brand.name, as: :brand, sortable: true 

    indexes product_types.type.id, as: :product_types 

    has created_at, updated_at 
end 

# type_index.rb 
ThinkingSphinx::Index.define :type, :with => :active_record do 
    indexes name, :sortable => true 
end 

# product_type_index.rb 
ThinkingSphinx::Index.define :product_type, :with => :active_record do 
    has product_id, type: :integer 
    has type_id, type: :integer 
end 

私は現在、(私を聞かせてこのように、link_to:product_types IDの配列を渡します

:私の ProductsController

= link_to "Web shop", products_path(product_types: Type.all.map(&:id), brand: Brand.all.map(&:id)), class: "nav-link" 

私はこのような特定のType IDに基づいて結果をフィルタリングしてみてください:より良いそれを行う方法)があるかどうかを知ります

product_types = params[:product_types] 
@products = Product.search with_all: { product_types: product_types.collect(&:to_i) } 

私はrake ts:rebuildを実行すると、私は次のエラーを取得する:

indexing index 'product_type_core'... 
ERROR: index 'product_type_core': No fields in schema - will not index 

そして私は、私は次のエラーを取得するブラウザでビューを表示しようとします:

index product_core: no such filter attribute 'product_types' 
- SELECT * FROM `product_core` WHERE `sphinx_deleted` = 0 AND 
`product_types` = 1 AND `product_types` = 2 AND `product_types` = 3 
LIMIT 0, 20; SHOW META 

任意のアイデアをどのようにこのケースのインデックス(およびクエリ)を正しく設定するには? - テキストデータ、あなたのための無indexes通話

まず、あなたがrake ts:rebuild中に見ているエラーは、あなたでProductTypeスフィンクスインデックス内の任意のフィールドを設定していませんでしたことを指摘されています

答えて

1

ここで注意することはいくつかの問題があります検索したい実際にProductTypeを検索していますか?もしそうなら、どんなテキストで人々が一致すると期待していますか?

もしあなたがそのモデルで検索していないなら、そのモデルのためのスフィンクスインデックスを持つ必要はありません。

第2に、検索の問題 - product_typesでフィルタリングしていますが、意味があります。ただし、インデックスでは、hasを使用して属性(indexesを使用)ではなく、product_typesをフィールドとして定義しています。それが整数値であるため、誰かがIDを検索入力に入力することを期待していない可能性が高いので、これを代わりに属性にすることをお勧めします。indexesをProductインデックス定義内のその行のhasに変更してください、ts:rebuildを実行します。

+0

ありがとう@pat!私はインデックスを次のように更新しました: 'has product_types.type.id as::type_ids'。 [Github](https://github.com/pat/thinking-sphinx-examples)で高度な検索例を見つけましたが、同様のことをやろうとしました。現在、検索の関連部分は次のようになります。 '@products = Product.search params [:query]、with_all:options [:with_all]'、 'options [:with_all]'は次のようになります: '{:type_ids => [2,3]} '。しかし、私は常に0の結果を得ます: 'SELECT * FROM 'product_core' WHERE 'sphinx_deleted' = 0 AND 'type_ids' = 2 AND 'type_ids' = 3 LIMIT 0、20'。私は何が欠けているすべてのアイデア? – Anders

+0

ちょうど確認する:あなたは*タイプid2とタイプid3の両方に接続されているすべての製品を得ることを期待していますか?(どちらか一方に接続されている製品ではなく、必ずしも両方に接続されている製品ではありません)。また、共有したコードには表示されませんが、 'params [:product_types]'をある時点でTypeインスタンスに変換していますか? – pat

+0

いいえ、タイプ1またはタイプ3のすべての製品が必要です。オリジナルの質問から、そのビットを 'params [:product_types]'で変更しました。私はそれを 'options = {:with_all => {}}'、 'options [:with_all] [:type_ids] = params [:filter] [:type] .keys.map(&:to_i)'に変更しました。結果は '{:type_ids => [2、3]}'と '@products = Product.search params [:query]、with_all:options [:with_all]'という結果になります。あなたの助けのために@pat! – Anders

関連する問題