나는 검색을 위해 생각 스핑크스를 사용하고자하는 레일 애플 리케이션이 있습니다. 나는 다음과 같은 모델 사이에 많은 관계를 가지고 있지만, Product
은 Type
부터 ProductType
까지 많습니다. 내 ProductsController
index 액션에서레일 5, 생각 스핑크스, 색인 생성 및 검색 관계를 통해 meny있다.
# 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를 기반으로 결과를 필터링하려고 : 더 나은 그것을 할 방법)이 있는지 알고 내가 rake ts:rebuild
을 실행하면
product_types = params[:product_types]
@products = Product.search with_all: { product_types: product_types.collect(&:to_i) }
나는 다음과 같은 오류 얻을 : 방법에
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
어떤 아이디어 :
indexing index 'product_type_core'...
ERROR: index 'product_type_core': No fields in schema - will not index
을 그리고 나는 다음과 같은 오류를 얻을 브라우저에서보기를 볼하려고 할 때 이 경우에 대한 내 색인 (및 검색어)을 올바르게 설정하려면 어떻게해야합니까? - 텍스트 데이터 당신을 위해 더 indexes
전화
첫째, 당신이 rake ts:rebuild
동안보고있는 오류가 당신이 당신의 ProductType 스핑크스 지수의 모든 필드를 설정할 수없는 한 것을 지적한다 :
감사합니다. @pat! 내 색인을 다음과 같이 업데이트했습니다 :'has product_types.type.id as 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'어디에서 'sphinx_deleted'= 0 AND 'type_ids'= 2 AND 'type_ids'= 3 LIMIT 0, 20'. 내가 누락 된 아이디어가 있습니까? – Anders
그냥 확인하십시오 : * 유형 ID 2와 유형 ID 3 모두에 연결된 모든 제품을 가져 오기를 기대합니까?(둘 중 하나에 연결되어 있지만 반드시 둘 다 아닌 제품). 또한, 공유 한 코드에는 표시되지 않지만,'params [: product_types]'를 어떤 시점에서 Type 인스턴스로 변환하고 있습니까? – pat
아니요, 유형 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