Elasticsearch gem - API调用格式

时间:2014-01-31 07:13:51

标签: ruby-on-rails ruby elasticsearch

我将此gem用于elasticsearch API 我试图将以下curl语句转换为等效的API调用

curl -X GET 'localhost:9200/_search?pretty=true' -d '{
  "size": 100,
  "fields": [
    "@message",
    "@timestamp"
  ],
  "query": {
    "term": {
      "@message": "drop"
    }
  }
}'

我尝试了这些但没有得到预期的结果

Elasticsearch API

@esearch = Elasticsearch::Client.new log: true
@data2 = @esearch.search q: {
                              term:{
                                "@message" => "drop"
                              }
                            },
                            size:'100',
                            fields:'["@message", "@timestamp"]'

传输API

client = Elasticsearch::Client.new
@data = client.perform_request 'GET', '_search', {
                                                    :size=> 100,
                                                    :query=> {
                                                      :term=> {
                                                        "message"=> "drop"
                                                      }
                                                    },
                                                    {
                                                      :fields=> [
                                                        '@message',
                                                        '@timestamp'
                                                      ]
                                                    }
                                                  }

请帮忙

1 个答案:

答案 0 :(得分:1)

您需要将所有这些参数包装在body元素中:

@data2 = @esearch.search 
    body: {
        query: {term:{"@message" => "drop"}}, 
        size:'100', 
        fields:'["@message", "@timestamp"]
   }