繁体   English   中英

轮胎指数弹性搜索

[英]Tire index elasticsearch

通过轮胎进行Elasticsearch对我来说很好。 我想对我的代码中遇到的问题提出建议。

  mapping do
    indexes :id, :index => :not_analyzed
    indexes :locality
  end

这是我映射的一部分。局部性对应于模型中定义为的方法

def locality
  self.locality.name
end

该模型也

belongs_to :locality

因此,您可以观察到被调用的方法将陷入无限循环。 我有一个局限性,因为我们要避免前端中的相应更改,因此无法更改映射中的名称局部性。 一种选择是在“位置”模型中定义一种方法

def locality_name
  self.name
end

我试图在to_indexed_json中包含locality_name方法,并尝试通过这种方式进行映射,但失败了。

mapping do
  indexes :id, :index => :not_analyzed
  indexes :locality do
    indexes :locality_name
  end

end

我希望在不更改模型Locality的情况下将本地名称在结果中索引为“ locality”。

您将需要在模型中提供to_indexed_json的实现, to_indexed_json索引中的locality字段提供不同的值。 这个问题中还有关于to_indexed_json更多详细信息,但是类似的东西应该可以工作:

self.include_root_in_json = false
def to_indexed_json
  # Get the default json for the instance
  hash = as_json(:root => false)
  # Add a locality field mapped to the name of the locality
  hash.update(:locality => self.locality.name).to_json
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM