3
我想將構面添加到has_many關聯。如何在輪胎(elasticsearch)中添加構面到has_many關聯
我正在從PostgreSQL全文搜索遷移到elasticsearch。
目前,我有以下SQL查詢做搜索(PostgreSQL的方式):
rt = "#{Rule.table_name}"
Sentence.
joins(:rules).joins(:regulations).joins(:speakers).
where(:authority_name => params[:authority_name], :authority_detail_1 => params[:authority_detail_1]).
where(:regulations => {:regulation_name => params[:regulation_name]}).
where("#{rt}.description @@ #{Sentence.sanitize(words)} OR #{rt}.headline @@ #{Sentence.sanitize(words)}")
我看着SO回答類似的問題(HABTM協會)
Facet Troubles with Elasticsearch on Query
,並實現代碼類似的方法
- 句子AR類
class Sentence < ActiveRecord::Base
# other staff
include Tire::Model::Search
include Tire::Model::Callbacks
mapping do
indexes :id
indexes :authority_name, :type => :string, :index => "not_analyzed"
indexes :authority_detail_1, :type => :string, :index => "not_analyzed"
indexes :authority_detail_2, :type => :string, :index => "not_analyzed"
indexes :regulations, :type => :object, :properties => {
:regulation_name => { :type => :string, :index => "not_analyzed" }
}
end
def to_indexed_json
to_json(:include => [:rules, :regulations, :speaker])
end
def self.search(params)
tire.search(page: params[:page], per_page: 5, load: true) do
query do
boolean do
must { string params[:query], default_operator: "AND" } if params[:query].present?
must { term :authority_name, params[:authority_name] } if params[:authority_name].present?
must { term :authority_detail_1, params[:authority_detail_1] } if params[:authority_detail_1].present?
must { term :authority_detail_2, params[:authority_detail_2] } if params[:authority_detail_2].present?
must { terms 'regulations.regulation_name', params[:regulation_name] } if params[:regulation_name].present?
end
end
facet 'authorities' do
terms :authority_name
terms :authority_detail_1
terms :authority_detail_2
end
facet 'regulations' do
terms 'regulations.regulation_name'
end
# raise to_curl
end
end
# other staff
end # end of Class
但是這段代碼不起作用。
調用API後,我得到了一個錯誤:
Tire::Search::SearchRequestFailed in SentencesController#search
{
"error": "SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[-ocpucv-TwGCmLfPd3U9hQ][sentences][4]: SearchParseException[[sentences][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"bool\":{\"must\":[{\"query_string\":{\"query\":\"appalto\",\"default_operator\":\"AND\"}},{\"terms\":{\"regulations.regulation_name\":\"Cod. civ.\"}}]}},\"facets\":{\"authorities\":{\"terms\":{\"field\":\"authority_detail_2\",\"size\":10,\"all_terms\":false}},\"regulations\":{\"terms\":{\"field\":\"regulations.regulation_name\",\"size\":10,\"all_terms\":false}}},\"size\":5,\"from\":0}]]]; nested: }{[-ocpucv-TwGCmLfPd3U9hQ][sentences][0]: SearchParseException[[sentences][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"bool\":{\"must\":[{\"query_string\":{\"query\":\"appalto\",\"default_operator\":\"AND\"}},{\"terms\":{\"regulations.regulation_name\":\"Cod. civ.\"}}]}},\"facets\":{\"authorities\":{\"terms\":{\"field\":\"authority_detail_2\",\"size\":10,\"all_terms\":false}},\"regulations\":{\"terms\":{\"field\":\"regulations.regulation_name\",\"size\":10,\"all_terms\":false}}},\"size\":5,\"from\":0}]]]; nested: }{[-ocpucv-TwGCmLfPd3U9hQ][sentences][1]: SearchParseException[[sentences][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"bool\":{\"must\":[{\"query_string\":{\"query\":\"appalto\",\"default_operator\":\"AND\"}},{\"terms\":{\"regulations.regulation_name\":\"Cod. civ.\"}}]}},\"facets\":{\"authorities\":{\"terms\":{\"field\":\"authority_detail_2\",\"size\":10,\"all_terms\":false}},\"regulations\":{\"terms\":{\"field\":\"regulations.regulation_name\",\"size\":10,\"all_terms\":false}}},\"size\":5,\"from\":0}]]]; nested: }]",
"status": 500
}
這裏JSON錯誤解析到更多的 '胡馬爾可讀的形式':
SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[-ocpucv-TwGCmLfPd3U9hQ][sentences][4]: SearchParseException[[sentences][4]: from[-1],size[-1]: Parse Failure [Failed to parse source
[{"query":
{"bool":
{"must": [
{"query_string":{"query":"appalto","default_operator":"AND"}},
{"terms":{"regulations.regulation_name":"Cod. civ."}}
]}
},
"facets":
{"authorities":
{"terms":
{"field":"authority_detail_2","size":10,"all_terms":false}
},
"regulations":
{"terms":
{"field":"regulations.regulation_name","size":10,"all_terms":false}
}
},
"size":5,"from":0}]
]];
nested: }{[-ocpucv-TwGCmLfPd3U9hQ][sentences][0]: SearchParseException[[sentences][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"bool":{"must":[{"query_string":{"query":"appalto","default_operator":"AND"}},{"terms":{"regulations.regulation_name":"Cod. civ."}}]}},"facets":{"authorities":{"terms":{"field":"authority_detail_2","size":10,"all_terms":false}},"regulations":{"terms":{"field":"regulations.regulation_name","size":10,"all_terms":false}}},"size":5,"from":0}]]];
nested: }{[-ocpucv-TwGCmLfPd3U9hQ][sentences][1]: SearchParseException[[sentences][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"bool":{"must":[{"query_string":{"query":"appalto","default_operator":"AND"}},{"terms":{"regulations.regulation_name":"Cod. civ."}}]}},"facets":{"authorities":{"terms":{"field":"authority_detail_2","size":10,"all_terms":false}},"regulations":{"terms":{"field":"regulations.regulation_name","size":10,"all_terms":false}}},"size":5,"from":0}]]];
nested: }]
任何想法如何實施在輪胎中正確的has_many關聯?
PS是的我更改了輪胎映射後運行了rake任務。
編輯:
確定。我通過更改代碼中的一行(術語=>術語)修復了500
錯誤。
must { term 'regulations.regulation_name', params[:regulation_name] } if params[:regulation_name].present?
但現在我的搜索結果是空的?有人知道怎麼修這個東西嗎? 也許我應該使用其他類型的比賽(我目前使用boolean
+ must
組合)。