创建一个大型哈希的Ruby方法

huangapple go评论89阅读模式
英文:

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:

我是用类似这样的方法生成这个参数的:

  1. def generate_params(search)
  2. [
  3. {
  4. script_score: {
  5. query: {
  6. neural: {
  7. passage_embedding: {
  8. query_text: search,
  9. k: 100
  10. }
  11. }
  12. },
  13. script: {
  14. source: '_score * 1.5'
  15. }
  16. }
  17. },
  18. {
  19. script_score: {
  20. query: {
  21. multi_match: {
  22. query: search,
  23. fields: %w[field_a field_b field_c field_d]
  24. }
  25. },
  26. script: {
  27. source: '_score * 1.7'
  28. }
  29. }
  30. }
  31. ]
  32. 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:

  1. def generate_params(search)
  2. [
  3. {
  4. script_score: {
  5. query: {
  6. neural: {
  7. passage_embedding: {
  8. query_text: search,
  9. k: 100
  10. }
  11. }
  12. },
  13. script: {
  14. source: '_score * 1.5'
  15. }
  16. }
  17. },
  18. {
  19. script_score: {
  20. query: {
  21. multi_match: {
  22. query: search,
  23. fields: %w[field_a field_b field_c field_d]
  24. }
  25. },
  26. script: {
  27. source: '_score * 1.7'
  28. }
  29. }
  30. }
  31. ]
  32. 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文件,您可以执行类似以下的操作:

  1. require 'yaml'
  2. my_configuration = YAML.load_file('path_to_yaml_file.yml')

这将允许您像处理哈希表一样处理YAML文件中的值:

  1. 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:

  1. require 'yaml'
  2. 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:

  1. 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

以下是您要翻译的内容:

您当然可以构建一个唯一位的数组(数组中的每个元素都是一个哈希),然后将这些元素映射到所需的数组。

  1. def generate_params(search)
  2. distinct_elements = [
  3. {
  4. query: {
  5. neural: {
  6. passage_embedding: {
  7. query_text: search,
  8. k: 100
  9. }
  10. }
  11. },
  12. source_score: '1.5'
  13. },
  14. {
  15. query: {
  16. multi_match: {
  17. query: search,
  18. fields: %w[field_a field_b field_c field_d]
  19. }
  20. },
  21. source_score: '1.7'
  22. }
  23. ]
  24. distinct_elements.map do |h|
  25. {
  26. script_score: { query: h[:query] },
  27. script: { source: "_score * #{ h[:source_score] }" }
  28. }
  29. end
  30. 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"

}

}

]

  1. 请注意,我已经将HTML注释部分从翻译中省略掉了。如果您需要进一步的翻译,请告诉我。
  2. <details>
  3. <summary>英文:</summary>
  4. 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.
  5. def generate_params(search)
  6. distinct_elements = [
  7. {
  8. query: {
  9. neural: {
  10. passage_embedding: {
  11. query_text: search,
  12. k: 100
  13. }
  14. }
  15. },
  16. source_score: &#39;1.5&#39;
  17. },
  18. {
  19. query: {
  20. multi_match: {
  21. query: search,
  22. fields: %w[field_a field_b field_c field_d]
  23. }
  24. },
  25. source_score: &#39;1.7&#39;
  26. }
  27. ]
  28. distinct_elements.map do |h|
  29. {
  30. script_score: { query: h[:query] },
  31. script: { source: &quot;_score * #{ h[:source_score] }&quot; }
  32. }
  33. end
  34. end
  35. &lt;!--&gt;
  36. generate_params(&#39;everywhere&#39;)
  37. #=&gt; [
  38. # {
  39. # :script_score=&gt;{
  40. # :query=&gt;{
  41. # :neural=&gt;{
  42. # :passage_embedding=&gt;{
  43. # :query_text=&gt;&quot;everywhere&quot;,
  44. # :k=&gt;100
  45. # }
  46. # }
  47. # }
  48. # },
  49. # :script=&gt;{
  50. # :source=&gt;&quot;_score * 1.5&quot;
  51. # }
  52. # },
  53. # {
  54. # :script_score=&gt;{
  55. # :query=&gt;{
  56. # :multi_match=&gt;{
  57. # :query=&gt;&quot;everywhere&quot;,
  58. # :fields=&gt;[&quot;field_a&quot;, &quot;field_b&quot;, &quot;field_c&quot;, &quot;field_d&quot;]
  59. # }
  60. # }
  61. # },
  62. # :script=&gt;{:
  63. # source=&gt;&quot;_score * 1.7&quot;
  64. # }
  65. # }
  66. # ]
  67. </details>

huangapple
  • 本文由 发表于 2023年5月25日 19:28:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定