0

我有一个标准 JSON 格式的查询,当直接从命令行查询时效果很好。json是:

{"fields" : ["id"],
 "query":{
    "bool":{
      "must":[
        {
          "custom_score" : {
              "query" : {
                "bool" : {
                  "should" : [
                    {
                      "term" : {"a.in.group" : "abcd"}
                    },
                    {
                      "term" : {"a.in.group": "ALL"}
                    }
                   ]
                 }
              },
            "boost" : 1.0,
            "script" : "_score"
            }
        },

        {
          "custom_score" : {
              "query" : {
                "bool" : {
                  "should" : [
                    {
                      "term" : {"b.in.prefix" : pre}
                    },
                    {
                      "term" : {"b.in.group" : "abc"}
                    }
                   ]
                 }
              },
            "boost" : 1.0,
            "script" : "_score"
            }
        }
       ]
      }
    }
}

这给了我想要的结果:

curl -X GET "localhost:9200/test/rule/_search" -d @query.json
{"took":6,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":2.3251607,"hits":[{"_index":"memphis","_type":"rule","_id":"1","_score":2.3251607,"fields":{"id":1}}]}}

但是我无法为等效的轮胎查询找到正确的语法。我试过了:

s=Tire.search('test/rule') do
    query do
        boolean do
            must [
                query do
                    boolean do
                        should { string "a.in.group:abcd" }
                        should { string "a.in.group:ALL" }
                    end
                end
                query do
                    boolean do
                        should { string "b.in.prefix:pre" }
                        should { string "b.in.group:abc" }
                    end
                end
            ]
        end
    end

    fields :id
end

但这给了我语法错误:

ruby -c query.rb 
query.rb:7: syntax error, unexpected keyword_do_block, expecting ']'
query.rb:19: syntax error, unexpected ']', expecting keyword_end
query.rb:46: syntax error, unexpected $end, expecting keyword_end

我在任何其他论坛上都找不到任何帮助。当我尝试在 must 块中放置多个查询时会出现问题。请帮忙。

提前致谢

-阿兹塔布

4

1 回答 1

2

看看这是否可以解决问题:

s = Tire.search 'test/rule' do
    query do
      boolean do
          must do
              custom_score :boost => 1.0, :script => "_score" do
                boolean do
                    should { term 'a.in.group', "abcd" }
                    should { term 'a.in.group', "ALL" }
                end
              end
          end
          must do
              custom_score :boost => 1.0, :script => "_score" do
                boolean do
                    should { term 'b.in.prefix', pre }
                    should { term 'b.in.group', "abc" }
                end
              end
          end
      end
    end
    fields :id
end
于 2012-08-21T21:12:07.580 回答