RubyOnRails 응용 프로그램에서 작업하고 있습니다. 나는 Tire
gem을 통해 Elasticsearch를 사용하고 있습니다. Elasticsearch는 3 가지 모델을 색인화했습니다. 이 모델은 News, NewsTag 및 Categorization입니다. 뉴스는 NewsTag와 다 대다 관계를 가지고 있으며 NewsTag는 분류와 다 대다 관계를 맺습니다. 이 모델들은 모두 몽고입니다.*** NoMethodError 예외 : 정의되지 않은 메서드 '_id'
매핑 인덱스 : -
mapping do
indexes :cover, analyzer: 'snowball'
indexes :title_en
indexes :title_ar
indexes :content_brief_ar
indexes :content_brief_en
indexes :body_ar
indexes :body_en
indexes :likes, type: 'nested' do
end
indexes :comments, type: 'nested' do
end
indexes :news_tags, type: 'nested' do
indexes :name_en, analyzer: 'snowball'
indexes :name_ar, analyzer: 'snowball'
indexes :categorizations, type: 'nested' do
indexes :ar_name, analyzer: 'snowball'
indexes :en_name, analyzer: 'snowball'
end
end
end
def to_indexed_json
{
cover: self.cover,
title_ar: self.title_ar,
title_en: self.title_en,
content_brief_ar: self.content_brief_ar,
content_brief_en: self.content_brief_en,
body_ar: self.body_ar,
body_en: self.body_en,
news_tags: self.news_tags,
likes: self.likes,
comments: self.comments
}.to_json
end
내가 검색하는 방법은
@news = News.tire.search do
query do
nested path: 'news_tags' do
query do
boolean do
should {terms 'news_tags.categorization_ids', categorization_ids }
end
end
end
end
page = page_param.to_i
search_size = per_page_param.to_i
from (page - 1) * search_size
size search_size
end
나는 검색 결과의 유형이 뉴스 아니라는 것을 깨달았까지 잘 일하고 있었다 즉,이 항목이며, 모델 (News)에서 자주 정의되는 몇 가지 메소드가 정의되어있다. 그래서 Item을 뉴스로 바꾸기 위해 검색했고 흥미로운 것을 발견했습니다.
이 줄을 추가로 설정/intializers/tire.rbTire.configure do
wrapper ProxyObject
end
과이 솔루션은 Elasticsearch에 대한 자습서 프로젝트를 매우 성공적으로
class ProxyObject < SimpleDelegator
delegate :class, :is_a?, :to => :_proxied_object
def initialize(attrs={})
klass = attrs['_type'].camelize.classify.constantize
@_proxied_object = klass.new
_assign_attrs(attrs)
super(_proxied_object)
end
private
def _proxied_object
@_proxied_object
end
def _assign_attrs(attrs={})
attrs.each_pair do |key, value|
unless _proxied_object.respond_to?("#{key}=".to_sym)
_proxied_object.class.send(:attr_accessor, key.to_sym)
end
_proxied_object.send("#{key}=".to_sym, value)
end
end
end
이 파일 응용 프로그램/모델/proxy_object.rb . 이 솔루션을 통해 모델 메서드를 호출 할 수있었습니다. 그러나, 그것은 내 프로젝트에 대해 작동하지 않습니다. 나는 중요 할 수있는 것을 알아 차렸다. 뉴스 색인.
GET 로컬 호스트 : 9200/뉴스/뉴스가/당신이 발견으로 58889bbb6f6d613fff050000
{
"_index": "news",
"_type": "news",
"_id": "58889bbb6f6d613fff050000",
"_version": 1,
"found": true,
"_source": {
"cover": "new2",
"title_ar": "new2",
"title_en": "new2",
"content_brief_ar": "new2",
"content_brief_en": "new2",
"body_ar": "<p>new2</p>\r\n",
"body_en": "<p>new2</p>\r\n",
"news_tags": [
{
"_id": "56dec06769702d1578000000",
"categorization_ids": [
"5888990f6f6d613fff000000"
],
"created_at": "2016-03-08T14:07:03+02:00",
"name_ar": "صحة الطفل",
"name_en": "Baby Health",
"news_ids": [
"56dec03569702d156a010000",
"5704f92769702d4c2b010000",
"574efc7969702d370a130000",
"578515d369702d4f11000000",
"58889bbb6f6d613fff050000",
"58889c3f6f6d613fff080000"
],
"updated_at": "2016-03-08T14:07:03+02:00"
}
],
"likes": [],
"comments": []
}
}
, news_ids
이 news_tags
에 포함되며, news_tags
이 news
에 포함된다. 그래서, 나는 일종의 재귀가 있다고 생각합니다.
내가 검색에서 검색도 count
@news
객체에 어떤 방법을 적용 할 때 오류 메시지를 받고 있습니다 내 질문
(byebug) @news.count
MOPED: 127.0.0.1:27017 COMMAND database=admin command={:ismaster=>1} runtime: 1.9747ms
MOPED: 127.0.0.1:27017 UPDATE database=nabda-net_staging collection=news_tags selector={"$and"=>[{"_id"=>{"$in"=>[]}}]} update={"$pull"=>{"news_ids"=>BSON::ObjectId('5888aaba6f6d6154d0000000')}} flags=[:multi]
COMMAND database=nabda-net_staging command={:getlasterror=>1, :w=>1} runtime: 0.6387ms
*** NoMethodError Exception: undefined method `_id' for #<Hash:0x00000004b78190>
이유는이 오류 메시지가 무엇입니까? 설명이 쉽지 않습니다. 나는 문제가 어디 있는지 모른다. 그래서, 어느 누구도 나를 도울 수 있습니다.