Search code examples
elasticsearchlogstashnest

What is POCO Object with the Logstash document in Elasticsearch


I have documents in ElasticSearch import from LogStash like:

{
  "_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
    ]
  }
}

I use NEST lib to connect and get data from Elasticsearch. I read and do follow the link: https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/nest-getting-started.html Follow @sramalingam24 answer, i create my 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; }
}

But when i search, not found any docs match this object. My code search here:

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();

Both of them return: 0. When i use Kibana to query, i get amount of items.


Solution

  • There are a few ways of doing this

    1. You can use the _source part of your document above and jsonutils.com to generate a poco spec you need

    2. Just use

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

    instead of specifying a poco

    1. Use Newtonsoft.Json nuget then you can also do

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