具有多重匹配的弹性搜索字段值因子?

时间:2017-09-25 23:55:29

标签: ruby-on-rails elasticsearch search filter

我用它来搜索我的服装:

def self.search(query, purchasedlow, purchasedhigh)
__elasticsearch__.search(
{
  query: {
    function_score: {
      query: {
        bool: {
          filter: [
            {
              multi_match: {
                query: query,
                fields: ['name','description','material']
              }
            },
            {
              range: {
                purchased: { lte: purchasedhigh.to_i, gte: purchasedlow.to_i},
              },          
            }
          ]
        }
      }
    }
  }
}
)
end

但我不知道如何添加此代码:

    field_value_factor: {
      field: "likes",
      factor: "100"
    }

我知道我应该把它放在功能评分之后,这样计算得分就会乘以喜欢的数量来得到最终得分,但是当我把代码放在function_score之后,我得到以下错误:

[400] {"error":{"root_cause":[{"type":"parsing_exception","reason":"[function_score] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":232}],"type":"parsing_exception","reason":"[function_score] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":232},"status":400}

我需要在哪里放置字段值因子才能正常工作?

1 个答案:

答案 0 :(得分:0)

我在我的一个查询中使用了field_value_factor:

@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"
     }
   }
 })