代码之家  ›  专栏  ›  技术社区  ›  Rubytastic

弹性搜索轨道上的轮胎,返回所有年龄段的男性,但也返回女性

  •  2
  • Rubytastic  · 技术社区  · 13 年前

    使用elasticsearch和Tire im尝试搜索一些个人资料记录。

    试着得到性别=男性,年龄=18,年龄=30的结果。目前,通过这种设置,它可以返回这个年龄范围内的所有人,甚至是女性。我怎样才能让它表现得好 只归还这个年龄段的雄性?

    if params['people']
          tire.search(load: true, page: params[:page], per_page: 50, :default_operator => 'AND', :use_dis_max => true) do
            query do
              boolean do
                should { range :age, {gte: params['people'][:age_from], lte: params['people'][:age_to]} }
    
                should { string 'gender:male' } if params['people'][:gender] == "male"
                should { string 'gender:female' } if params['people'][:gender] == "female"
    
              end
            end
            to_curl
          end
       end
    
    2 回复  |  直到 12 年前
        1
  •  2
  •   dadoonet    13 年前

    使用“必须”而不是“应该”。 它应该起作用。

        2
  •  0
  •   Louis Ricci    13 年前

    我不编程是ruby,但是。。。

    :default_operator => 'OR'
    

    尝试将“OR”更改为“AND”

    编辑 好的,第二轮,这很有趣。。。

    在我看来这是错误的

    should { string 'gender:male' } if params['people'][:gender] == "male"
    should { string 'gender:female' } if params['people'][:gender] == "female"
    

    只要看看:年龄在一岁以上,这不是更好吗。。。

    should { string :gender, {eq: params['people'][:gender]} }
    

    但再说一遍,我不是用ruby编程的。。。

    推荐文章