【问题标题】:ElasticSearch - Pass Array of Terms to FilterElasticSearch - 将术语数组传递给过滤器
【发布时间】:2018-02-17 21:23:27
【问题描述】:

我有一个查询,我想将动态数量的术语传递给过滤器。反正有没有把它作为一个数组传递?我尝试了几种不同的方法,但总是以来自 elasticsearch 的格式错误的请求错误告终。我将 Ruby on Rails 与 elasticsearch-rails gem 一起使用。这是我的查询:

@products = Product.search(
     query:{
       function_score:{
         query:{
           bool:{
             must:{
               multi_match:{
                 fields: ['brand^5', '_all'],
                 query: "#{query}",
                 fuzziness: "AUTO"
               }
             },

               filter:{
                 bool:{
                   must:[
                     {term: {"brand":"NordicTrack"}},
                     {term: {"product_type":"Treadmill"}}
                   ]
                 }
               }

           }
         },
         field_value_factor:{
            field: "popularity",
            modifier: "log1p"
         }
       }
     }).page(page).per(25)

我想传递一个这样的数组:

array = ['{term: {"brand":"NordicTrack"}}', {term: {"product_type":"Treadmill"}}]

进入“必须”这样的声明:

 @products = Product.search(
     query:{
       function_score:{
         query:{
           bool:{
             must:{
               multi_match:{
                 fields: ['brand^5', '_all'],
                 query: "#{query}",
                 fuzziness: "AUTO"
               }
             },

               filter:{
                 bool:{
                   must: array
                 }
               }

           }
         },
         field_value_factor:{
            field: "popularity",
            modifier: "log1p"
         }
       }
     }).page(page).per(25)

但是,这会导致 ES 出现格式错误的请求错误。

【问题讨论】:

    标签: ruby-on-rails ruby elasticsearch ruby-on-rails-5 elasticsearch-rails


    【解决方案1】:

    确保您的数组元素不是字符串。您的示例显示:

    array = ['{term: {"brand":"NordicTrack"}}', {term: {"product_type":"Treadmill"}}]
    

    应该是:

    array = [{term: {"brand":"NordicTrack"}}, {term: {"product_type":"Treadmill"}}]
    

    如果你在 ruby​​ 中构建数组,它应该看起来像这样:

    array = []
    array.push term: {"brand":"NordicTrack"}
    array.push term: {"product_type":"Treadmill"}
    

    【讨论】:

      猜你喜欢
      • 2013-11-13
      • 1970-01-01
      • 2014-07-31
      • 2015-08-02
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      • 2016-04-17
      • 2021-12-02
      相关资源
      最近更新 更多