英文:
Ruby method that creates a large hash
问题
I am using Ruby on Rails.
我正在使用Ruby on Rails。
I have a controller which makes a subsequent request to an API that requires a large array parameter with some values inserted.
我有一个控制器,它会向一个需要大型数组参数的API发出后续请求,并插入一些值。
I'm generating this parameter with a method that looks like this:
我是用类似这样的方法生成这个参数的:
def generate_params(search)
[
{
script_score: {
query: {
neural: {
passage_embedding: {
query_text: search,
k: 100
}
}
},
script: {
source: '_score * 1.5'
}
}
},
{
script_score: {
query: {
multi_match: {
query: search,
fields: %w[field_a field_b field_c field_d]
}
},
script: {
source: '_score * 1.7'
}
}
}
]
end
The search
parameter gets inserted into the array that is returned from the method.
search
参数被插入到从该方法返回的数组中。
My question is: What's the best practice for defining templates for large objects like this in Ruby / Ruby on Rails?
我的问题是:在Ruby / Ruby on Rails中定义类似这样的大型对象的模板的最佳实践是什么?
As you can see from the example, 95% of it is static template syntax, and only 2 values are populated by variables.
正如您从示例中所看到的,其中95%都是静态模板语法,只有2个值是由变量填充的。
I want to reduce the method size to as small as possible, and was wondering if there is some way of defining this array as a template somewhere outside the method? Specifically, what is the best practice approach in Ruby?
我想将该方法的大小减小到尽可能小,并想知道是否有一种方式可以在方法之外的某个地方将这个数组定义为模板?具体来说,在Ruby中最佳的实践方法是什么?
英文:
I am using Ruby on Rails.
I have a controller which make makes a subsequent request to an API that requires large array parameter with some values inserted.
I'm generating this parameter with a method that looks like this:
def generate_params(search)
[
{
script_score: {
query: {
neural: {
passage_embedding: {
query_text: search,
k: 100
}
}
},
script: {
source: '_score * 1.5'
}
}
},
{
script_score: {
query: {
multi_match: {
query: search,
fields: %w[field_a field_b field_c field_d]
}
},
script: {
source: '_score * 1.7'
}
}
}
]
end
The search
parameter gets inserted into the array that is returned from the method.
My question is: What's the best practice for defining templates for large objects like this in Ruby / Ruby on Rails?
As you can see from the example, 95% of it is static template syntax, and only 2 values are populated by variables.
I want to reduce the method size to as small as possible, and was wondering if there is some way of defining this array as a template somewhere outside the method? Specifically what is the best practice approach in Ruby?
答案1
得分: 3
不确定是否有一种公认的最佳做法,但我经常看到这种类型的内容被存储在YAML文件中。
因此,如果您创建一个带有您的配置的YAML文件,您可以执行类似以下的操作:
require 'yaml'
my_configuration = YAML.load_file('path_to_yaml_file.yml')
这将允许您像处理哈希表一样处理YAML文件中的值:
my_configuration[:script_score][:query][:multi_match][:query] = 'some_value_or_method_call'
您可以将其放入数组或任何其他需要的地方 - 关键是您可以通过YAML文件加载大部分属性,并根据需要修改对象/哈希表。
或者,您还可以将相同的属性存储在模块内的常量中,加载该模块,并以这种方式分配搜索属性。
就像我说的,不确定是否有关于这种情况的最佳做法,但我已经在许多情况下看到了这两种模式。
英文:
Not sure if there's an agreed upon best practice, but I frequently see this kind of thing being stored in YAML files.
So if you create a YAML file with your configuration you can do something similar to:
require 'yaml'
my_configuration = YAML.load_file('path_to_yaml_file.yml')
This would allow you to treat the values of your YAML file like a hash:
my_configuration[:script_score][:query][:multi_match][:query] = 'some_value_or_method_call'
You can dump that in an array or whatever else you need to do with it - the point is that you can load most of your attributes via a YAML file and amend the object/hash as needed.
Alternatively, you could store the same attributes in a constant inside a module, load that module, and assign the search attribute that way.
Like I said, not sure if there's a best practice around this kind of thing, but I've seen both of those patterns numerous times in the wild.
答案2
得分: 0
以下是您要翻译的内容:
您当然可以构建一个唯一位的数组(数组中的每个元素都是一个哈希),然后将这些元素映射到所需的数组。
def generate_params(search)
distinct_elements = [
{
query: {
neural: {
passage_embedding: {
query_text: search,
k: 100
}
}
},
source_score: '1.5'
},
{
query: {
multi_match: {
query: search,
fields: %w[field_a field_b field_c field_d]
}
},
source_score: '1.7'
}
]
distinct_elements.map do |h|
{
script_score: { query: h[:query] },
script: { source: "_score * #{ h[:source_score] }" }
}
end
end
生成参数('everywhere')
#=>
[
{
:script_score=>{
:query=>{
:neural=>{
:passage_embedding=>{
:query_text=>"everywhere",
:k=>100
}
}
}
},
:script=>{
:source=>"_score * 1.5"
}
},
{
:script_score=>{
:query=>{
:multi_match=>{
:query=>"everywhere",
:fields=>["field_a", "field_b", "field_c", "field_d"]
}
}
},
:script=>{
:source=>"_score * 1.7"
}
}
]
请注意,我已经将HTML注释部分从翻译中省略掉了。如果您需要进一步的翻译,请告诉我。
<details>
<summary>英文:</summary>
You could of course construct an array of the unique bits (each element of the array a hash) and then map those elements to the desired array.
def generate_params(search)
distinct_elements = [
{
query: {
neural: {
passage_embedding: {
query_text: search,
k: 100
}
}
},
source_score: '1.5'
},
{
query: {
multi_match: {
query: search,
fields: %w[field_a field_b field_c field_d]
}
},
source_score: '1.7'
}
]
distinct_elements.map do |h|
{
script_score: { query: h[:query] },
script: { source: "_score * #{ h[:source_score] }" }
}
end
end
<!-->
generate_params('everywhere')
#=> [
# {
# :script_score=>{
# :query=>{
# :neural=>{
# :passage_embedding=>{
# :query_text=>"everywhere",
# :k=>100
# }
# }
# }
# },
# :script=>{
# :source=>"_score * 1.5"
# }
# },
# {
# :script_score=>{
# :query=>{
# :multi_match=>{
# :query=>"everywhere",
# :fields=>["field_a", "field_b", "field_c", "field_d"]
# }
# }
# },
# :script=>{:
# source=>"_score * 1.7"
# }
# }
# ]
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论