Elasticsearch 中 Logstash 文档中的 POCO Object 是什么

What is POCO Object with the Logstash document in Elasticsearch

我在 ElasticSearch 中有从 LogStash 导入的文档,例如:

{
  "_index": "logstash-2018.03.13",
  "_type": "logs",
  "_id": "AWIdXdfYQVCHQ1iyikAx",
  "_score": 1,
  "_source": {
    "Action": "view",
    "offset": 34497789,
    "Ip": "113.161.25.104",
    "Url": "http://vietnamnet.vn/",
    "tags": [
      "beats_input_codec_plain_applied"
    ],
    "CateAlias": "trang-chu",
    "ActionDate": "2018-03-13 10:18:31",
    "@timestamp": "2018-03-13T03:18:31.000Z",
    "WebsiteCode": "VietNamNet",
    "@version": "1",
    "beat": {
      "hostname": "Tracking59",
      "name": "Tracking59",
      "version": "5.4.3"
    },
    "host": "Tracking59",
    "ArticleId": 0,
    "Domain": "vietnamnet.vn"
  },
  "fields": {
    "@timestamp": [
      1520911111000
    ]
  }
}

我使用 NEST 库连接 Elasticsearch 并从中获取数据。 我阅读并遵循 link:https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/nest-getting-started.html 按照@sramalingam24 的回答,我创建了我的 POCO Class:

 public class Beat
{
    public string hostname { get; set; }
    public string name { get; set; }
    public string version { get; set; }
}

public class LogItem
{
    public string Action { get; set; }
    public int offset { get; set; }
    public string Ip { get; set; }
    public string Url { get; set; }
    public IList<string> tags { get; set; }
    public string CateAlias { get; set; }
    public string ActionDate { get; set; }
    public DateTime @timestamp { get; set; }
    public string WebsiteCode { get; set; }
    public string @version { get; set; }
    public Beat beat { get; set; }
    public string host { get; set; }
    public int ArticleId { get; set; }
    public string Domain { get; set; }
}

但是当我搜索时,没有找到任何文档匹配这个对象。 我的代码在这里搜索:

var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
            .DefaultIndex("logstash-2018.03.12");

            var client = new ElasticClient(settings);
            var result = client.Search<LogItem>(s => s.Index("logstash-2018.03.12").Type("logs")
                                                    .Query(
                                                        q => q.Match(m => m.Field(f => f.CateAlias).Query("trang-chu"))
                                                    )
                                                );
            Console.Write(result.Total);

            var total = client.Count<dynamic>().Count;
            Console.Write(total);
            Console.ReadLine();

他们俩return:0。 当我使用 Kibana 查询时,我得到了项目数量。

有几种方法可以做到这一点

  1. 您可以使用上面文档的 _source 部分和 jsonutils.com 生成您需要的 poco 规范

  2. 只需使用

var searchRes = client.Search< dynamic >(...)

而不是指定 poco

  1. 使用 Newtonsoft.Json nuget 然后你也可以做

var searchRes = client.Search< Newtonsoft.Json.Linq.JObject >(...)