2014-11-21 2 views
1
내가 SOLR

어떻게 SOLR 레일에서 선택된 열을 가져올 수

내가 한 사용자가 검색 할

내 사용자 테이블과 레일을 사용하고

내 모드는 사용자에게

이름, 도시, 주, 이미지를 12 필드가 내가 사용하여 검색 할 때, 내 사용자 모델

searchable do 
    text :name, :city 
end 

에 ... 등주의,

을 상태를 언급

@search = User.search do 
    fulltext params[:search] 
end 
@users = @search.results 

이것은 데이터베이스에서 모든 필드를 반환하지만 3 필드 이름, 도시, 상태는 어떻게됩니까? solr 검색을 사용하여 필드를 3 개만 얻을 수 있습니까? 어떻게 이것을 얻을 수 있습니까?

답변

0

왜 그렇게 할지는 모르지만 어쨌든 @search.results 대신 @search.hits을 사용하고 ID를 사용하여 원하는 것을로드하십시오.

0

@search.results에 전화하면 sunspot은 SQL 데이터베이스에서 일치하는 모든 레코드를로드합니다.

SQL 데이터베이스를 치지 않으려면 Solr에 필요한 필드를 저장할 수 있습니다. 당신은 당신의 검색 정의를

searchable do 
    text :name, stored: true 
    text :city, stored: true 
end 

을 업데이트 한 다음 모델 User.reindex 인덱스를 다시 할 필요가있다. 그럼 당신은 당신이 검색 할에서

<field name="name" type="string" indexed="false" stored="true"/> 
<field name="city" type="string" indexed="false" stored="true"/> 
<field name="state" type="string" indexed="false" stored="true"/> 

<field name="copiedfield" type="string" indexed="true" stored="false" multiValued="true"/> 

지금, 사용 copyField 태그의 설정 소스 값에 소스 필드에서 모든 필드 복사 스키마에 그것을 할 수

@search = User.search do 
    fulltext params[:search] 
end 
@users = @search.hits.each do |hit| 
    puts "#{hit.stored(:name)} #{hit.stored(:city)}" 
end 
0

같은 데이터를 조회 할 수 있습니다 이명 령 예 스키마 :

지금
<copyField source="name" dest="copiedfield"/> 
<copyField source="city" dest="copiedfield"/> 
<copyField source="state" dest="copiedfield"/> 

만 copiedfield로 검색 할 수 있습니다

당신이 다른 방법을 할 수

EDIT 혼자 모든 열에서 검색 할 수 있지만,이 방법은

copyField보다 느리다