NEST elasticsearch DateRange查询

时间:2017-04-21 15:08:18

标签: elasticsearch nest

我正在尝试使用NEST api在预定义的日期范围内获得elasticsearch的结果,但我没有管理。这个示例代码我试图检索一周的数据,但它忽略了我的日期过滤器并返回旧数据而不是我的初始日期。

提前感谢您的帮助!

var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node);
var client = new ElasticClient(settings);

settings.DefaultIndex("wholelog-2017.04*");

client.CreateIndex("wholelog-2017.04*",
create => create.Mappings(
    mappings => mappings.Map<LogLine>(type => 
        type.AutoMap()
        )
        ));

DateTime InitDate = DateTime.Now.AddDays(-7);

var filterClauses = new List<QueryContainer>();

filterClauses.Add(new DateRangeQuery
{
     Field = new Field("logTime"),
     LessThanOrEqualTo = DateTime.Now,
     GreaterThanOrEqualTo = InitDate
});

var searchRequest = new SearchRequest<LogLine>()
{
     Size = 10000,
     From = 0,
     Scroll = "1m",
     Query = new BoolQuery
     {
           Filter = filterClauses
     }
};

var searchResult = client.Search<LogLine>(searchRequest);


[ElasticsearchType(Name="logLine")]
public class LogLine
{
    public string Message {get;set;}
    public DateTime logTime {get;set;}
    public string type {get;set;}
    public string logServer {get;set;}
}

logstash配置文件

output {
    elasticsearch{
       hosts => "localhost"
       #user => ####
       #password => ####
       index => "wholelog-%{+YYYY.MM.dd}"
    } 
}

1 个答案:

答案 0 :(得分:0)

我认为您没有明确地放置映射,只是开始向索引添加文档。尝试创建一个像这样的新索引

client.CreateIndex(indexName,
    create => create.Mappings(
        mappings => mappings.Map<LogLine>(
            type => type.AutoMap()
        )
    )
);

并使用DateTime为您的字段。 DateMath用于构建搜索查询

[ElasticsearchType(Name = "logLine")]
public class LogLine
{
    public string Message { get; set; }
    public DateTime logTime { get; set; }
    public string type { get; set; }
    public string logServer { get; set; }
}

明确地放置映射后,尝试添加文档并搜索新索引。