2013-01-17 2 views
1

나는 Rails에서 Sunspot/Solr을 사용하는 법을 배우기 시작했다. Sunspot, Solr이 모든 레코드를 반환하는 것을 막는 방법 params [: search] = ""

나는 내가 검색어가 없습니다 및 검색 필드가 SOLR는 기본적으로 제 모델의 모든 결과를 반환, 빈 경우이 http://railscasts.com/episodes/278-search-with-sunspot

을 RailsCast 따랐다.

Started GET "/articles?utf8=%E2%9C%93&search=" for 127.0.0.1 at 2013-01-17 12:28:09 -0800 
    Processing by ArticlesController#index as HTML 
    Parameters: {"utf8"=>"✓", "search"=>""} 
    SOLR Request (17.0ms) [ path=#<RSolr::Client:0x007f9b5a6643e0> parameters={data: fq=type%3AArticle&start=0&rows=30&q=%2A%3A%2A, method: post, params: {:wt=>:ruby}, query: wt=ruby, headers: {"Content-Type"=>"application/x-www-form-urlencoded; charset=UTF-8"}, path: select, uri: http://localhost:8982/solr/select?wt=ruby, open_timeout: , read_timeout: } ] 
    Article Load (0.4ms) SELECT "articles".* FROM "articles" 
    Article Load (0.4ms) SELECT "articles".* FROM "articles" WHERE "articles"."id" IN (1, 2, 3, 4, 5, 6, 7, 8) 
Rendered articles/index.html.erb within layouts/application (7.6ms) 
Completed 200 OK in 74ms (Views: 10.8ms | ActiveRecord: 0.8ms | Solr: 17.0ms) 
[2013-01-17 12:28:09] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true 

내가 원하는 동작은 다음과 같습니다

여기

def index 
    @search = Article.search do 
     fulltext params[:search] 
    end 
    @articles = Article.all 
    @search_results = @search.results 
end 
검색 창에 "더 PARAMS 단지"를 입력하지 내 ArticlesController 번호 지수의 콘솔은 다음과 같은 출력 검색 용어가 주어지지 않으면 반환 할 것이 없다. 아무것도 행동을 요구한다 SOLR에서 반환되지 않습니다 param[:search] = "" 그래서 내가 검색 필드에 아무것도 입력하지 않을 때,이 상태로

def index 
    if params[:search] = "" 
     @search_results = [] 

    else 
     @search = Article.search do 
     fulltext params[:search] 
     end 
     @search_results = @search.results 

    end 
    @articles = Article.all 
    end 

: 나는 다음과 같이 ArticlesController 번호 인덱스를 수정했습니다. 내가 검색어 "루비"를 입력하면 param[:search] = "ruby" 있도록

Started GET "/articles?utf8=%E2%9C%93&search=" for 127.0.0.1 at 2013-01-18 12:02:52 -0800 
    Processing by ArticlesController#index as HTML 
    Parameters: {"utf8"=>"✓", "search"=>""} 
    Article Load (0.5ms) SELECT "articles".* FROM "articles" 
Rendered articles/index.html.erb within layouts/application (11.1ms) 
Completed 200 OK in 23ms (Views: 14.1ms | ActiveRecord: 0.5ms | Solr: 0.0ms) 
[2013-01-18 12:02:52] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true 

그러나, 아무 것도 반환되지 않으며 SOLR는 전혀 호출되지 않습니다.

Started GET "/articles?utf8=%E2%9C%93&search=ruby" for 127.0.0.1 at 2013-01-17 12:37:58 -0800 
    Processing by ArticlesController#index as HTML 
    Parameters: {"utf8"=>"✓", "search"=>"ruby"} 
    Article Load (0.5ms) SELECT "articles".* FROM "articles" 
Rendered articles/index.html.erb within layouts/application (11.5ms) 
Completed 200 OK in 23ms (Views: 14.5ms | ActiveRecord: 0.5ms | Solr: 0.0ms) 
[2013-01-17 12:37:58] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true 

내 조건부 논리에 문제가 있습니까? 아니면 솔라가 어떻게 작동하는지에 대해 뭔가 빠졌나요? 감사!

+0

왜이 호출하고 있습니다 :'@articles = Article.all'를? – jdl

+0

Article # index에서 사용할 수있는 두 개의 인스턴스 변수가 필요합니다 ...사용자가 검색어를 입력 할 때 모든 기사를 표시하는'@ articles '및 검색 결과에 대한'@ search_results'. 검색 필드가 공백이고'params [: search] = ""' –

답변

1

q 매개 변수가 제공되지 않거나 값이 빈 문자열 인 경우 Solr 스키마가 실행되도록 defaultQuery을 정의합니다.

Sunspot의 defaultQuery*:* (match-all-docs 쿼리)입니다. 더구나, Sunspot 자체는 fulltext이 공백 문자열과 함께 제공 될 때보다 명시 적으로 *:*을 보냅니다.

이 경우 match-all-documents 쿼리를 원하지 않으면 컨트롤러에서 사용자가 제공 한 쿼리를 확인하고 Solr에 대한 호출을 완전히 건너 뛰는 것이 가장 좋습니다.

실제로이 질문은 귀하의 예에서 수행 한 것과 정확히 동일하므로 실제 질문에 대해서는 분명치 않습니다. 당신은 "내가 원하는 행동은 아무것도 돌려 보내지 않는 것입니다."라고 말하면서 조건문을 기술합니다. "이 상태에서는 아무 것도 돌아 오지 않습니다." 당신이 이미 당신 자신의 문제를 해결 한 것처럼 보입니까? 설명에


편집 :이하지 않는 한

은 아마 if params[:search] == ""if params[:search] = ""을 변경하려면, 오타입니다. 동등 대 대입 연산자를 주목하십시오.

(또는 더 나은 아직 : params[:search].blank?)

+0

일 때조차도 @ search_results가 채워지는 것을 제외하고는 정상적으로 작동합니다. Solr 기본 스키마를 설명해 주셔서 감사합니다. 내 질문을 분명히해야했습니다. 문제는 컨트롤러에서 사용자가 제공 한 쿼리를 확인한 후에 원하는대로 매개 변수를 제공하지 않으면 Solr을 완전히 건너 뛴다는 것입니다. 그러나 결과를 리턴해야하는 param을 시도했을 때 Solr에 대한 호출은 여전히 ​​건너 뜁니다. 조건부에 문제가 있습니까? 아니면 내가 솔르에 대해 뭔가를 오해하고있는 것입니까? 도움을 주셔서 감사합니다 –

+0

'params [: search] = ""'typo입니까? 내 편집을 참조하십시오. –

+0

감사합니다 닉, 그것은 ==가 빠진 것처럼 간단했습니다. –