Elasticsearch JSON查询的轮胎等效项

时间:2012-08-20 19:42:16

标签: ruby elasticsearch tire

我有一个标准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}}]}}

但我无法为等效轮胎查询找到合适的syntex。我试过了:

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块中放入多个查询时出现问题。请帮忙。

先谢谢

-Azitabh

1 个答案:

答案 0 :(得分: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