如何在elasticsearch索引中基于created_date过滤文档?

时间:2018-02-06 07:18:38

标签: ruby ruby-on-rails-4 elasticsearch

我需要获取今天创建的文档

说A是我的模型,以下是映射和索引列

def as_indexed_json(options={})
    self.as_json({only: [:id,:user_id,:user_agent, :browser,:created_at]})
end

  settings :index => { :number_of_shards => 1  } do
    mapping :dynamic => 'false' do
      indexes :id, :type => 'integer'
      indexes :user_agent, :type => 'string'
      indexes :created_at, :type => 'date',:format => "dd/MM/yyyy"
    end
  end

我可以使用范围过滤器和elasticrange-model gem获取日期范围内的文档。

query = "{\"query\": {\"range\" : {\"created_at\" : {\"gte\": \"05/01/2018\", \"lte\": \"05/02/2018\", \"format\": \"dd/MM/yyyy||dd/MM/yyyy\"} } },\"aggs\": {\"group_by_browser\": {\"terms\": {\"field\": \"browser\"} } } }"

response = A.__elasticsearch__.search query
response.response["aggregations"]["group_by_browser"]["buckets"].map{|k| [k["key"],k["doc_count"]]}

但是,无法找到与今日数据文档匹配的过滤器

0 个答案:

没有答案