【问题标题】:Unable to fetch nested key-val pairs dictionary from elastic client search response无法从弹性客户端搜索响应中获取嵌套的键值对字典
【发布时间】:2019-01-18 16:22:36
【问题描述】:

我正在尝试,但无法访问下面提到的 hits["_index"]、hits["_type"]、hits["_id"]、hits["_score"] 和 hits["_source"]字典加载到数据库中。

尝试访问下面的每个 key-val 对:

{
  "took" : 12,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },

  **"hits" : {
    "total" : 2700881,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_index",
        "_type" : "doc",
        "_id" : "R22224!!5333e7e4-9ee3-45f4-9dc3-2a8b8d8cdcf8",
        "_score" : 1.0,
        "_source" : {
          "duration" : 14986283,
          "group_id" : "com",
          "var_time" : "2018-04-24T17:05:13.082+02:00",
          "var_name" : "2",
        }
      }
    ]
  }**
}

我尝试过的事情:

尝试了以下 c# 代码并查看了以下网址:

Unable to fetch _source dictionary key-val from elastic client search response https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/auto-map.html

public class HitsDocument
{

    [PropertyName("_index")]
    public string Hitsindex { get; set; }

    [PropertyName("_type")]
    public string Hitstype { get; set; }

    [PropertyName("_id")]
    public string Hitsid { get; set; }

    [PropertyName("_score")]
    public string Hitsscore { get; set; }

    [PropertyName("_source")]
    public RawDocument Hitssource { get; set; }

}

public class RawDocument
{

    [PropertyName("duration")]
    public long Duration { get; set; }

    [PropertyName("group_id")]
    public string GroupId { get; set; }

    [PropertyName("var_time")]
    public DateTime Vartime { get; set; }

    [PropertyName("var_name")]
    public string Varname { get; set; }

}

static void Main(string[] args)
{

    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

    var settings = new ConnectionSettings(pool)
        .DefaultMappingFor<HitsDocument>(m => m
              .IndexName("test_index")
              .TypeName("doc"));

    var searchResponse = client.Search<HitsDocument>();

    var numberOfSlices = 4;


    var scrollAllObservable = client.ScrollAll<HitsDocument>("3m", numberOfSlices)
        .Wait(TimeSpan.FromMinutes(5), onNext: s =>
    {

        var docs = s.SearchResponse.DebugInformation;
        var documents = s.SearchResponse.Hits;

        foreach (var document in documents)
            {
                    // do something with this set of documents
                    // business logic to load into the database.

                MessageBox.Show("document.Id=" + document.Id);
                MessageBox.Show("document.Score=" + document.Score);
                MessageBox.Show("document.Source=" + document.Source);
                MessageBox.Show("document.Type=" + document.Type);
                MessageBox.Show("document.Index=" + document.Index);
        }
        });

}

我做错了什么,还请指出文档的方向,以便更好地理解嵌套字典的 API 客户端?

提前致谢。

更新: 以下代码解决方案最初由 @Russ Cam 在此 link 中回答。 我之前没有意识到这一点。

public class RawDocument
{

    [PropertyName("duration")]
    public long Duration { get; set; }

    [PropertyName("group_id")]
    public string GroupId { get; set; }

    [PropertyName("var_time")]
    public DateTime Vartime { get; set; }

    [PropertyName("var_name")]
    public string Varname { get; set; }

}

static void Main(string[] args)
{

    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

    var settings = new ConnectionSettings(pool)
        .DefaultMappingFor<RawDocument>(m => m
              .IndexName("test_index")
              .TypeName("doc"));

    var searchResponse = client.Search<RawDocument>();

    var numberOfSlices = 4;


    var scrollAllObservable = client.ScrollAll<RawDocument>("3m", numberOfSlices)
        .Wait(TimeSpan.FromMinutes(5), onNext: s =>
    {

        var docs = s.SearchResponse.DebugInformation;
        var documents = s.SearchResponse.Hits;

        foreach (var document in documents)
            {
                    // do something with this set of documents
                    // business logic to load into the database.

                MessageBox.Show("document.Id=" + document.Id);
                MessageBox.Show("document.Score=" + document.Score);
                MessageBox.Show("document.Source.duration=" + document.Source.duration);
                MessageBox.Show("document.Source.var_time=" + document.Source.var_time);
                MessageBox.Show("document.Source.var_name=" + document.Source.var_name);
                MessageBox.Show("document.Type=" + document.Type);
                MessageBox.Show("document.Index=" + document.Index);
        }
        });

}

【问题讨论】:

  • 我没试过,但是,你不应该从root读取吗?
  • 可能值得在下面添加您的Update 作为答案,并接受它。这将在未来帮助其他人解决类似的问题:)

标签: c# elasticsearch visual-studio-2015 nest


【解决方案1】:

一个想法:

首先,从根目录读取,从根目录创建地图类。 Take、TimedOut、Shards 和 Hits。

其次,您的课程适用于“hits.hits”。这是一个数组,所以它需要一个类似 List 的 IEnumerable。请根据需要自行添加 [PropertyName("...")]:

public class OuterHits
{
  public string total {get;Set;}

  public string max_score {get;Set;}

  public List<RawDocument> hits {get;Set;} // or hits[]
}

你可能还需要一个根类

public class rootClass
{
public string string took {get;Set;}
public string timeout {get;Set;}
public Shards shards {get;Set;}
public OuterHits hits {get;Set;}
}

同时实现 Shards 类

【讨论】:

  • 嗨@Jorge Rojas。就像你提到的,我尝试实现继承的类结构。我可以访问 hits["_index"], hits["_type"], hits["_id"], hits["_score"] 但不能访问返回 null 的 hits["_source"]。我可以分别获取字典的两个级别,但不能在类层次结构中获取。
【解决方案2】:

正如@Russ Cam 所说,下面的解决方案更新已发布,以供其他人查看。

public class RawDocument
{

    [PropertyName("duration")]
    public long Duration { get; set; }

    [PropertyName("group_id")]
    public string GroupId { get; set; }

    [PropertyName("var_time")]
    public DateTime Vartime { get; set; }

    [PropertyName("var_name")]
    public string Varname { get; set; }

}

static void Main(string[] args)
{

    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

    var settings = new ConnectionSettings(pool)
        .DefaultMappingFor<RawDocument>(m => m
              .IndexName("test_index")
              .TypeName("doc"));

    var searchResponse = client.Search<RawDocument>();

    var numberOfSlices = 4;


    var scrollAllObservable = client.ScrollAll<RawDocument>("3m", numberOfSlices)
        .Wait(TimeSpan.FromMinutes(5), onNext: s =>
    {

        var docs = s.SearchResponse.DebugInformation;
        var documents = s.SearchResponse.Hits;

        foreach (var document in documents)
            {
                    // do something with this set of documents
                    // business logic to load into the database.

                MessageBox.Show("document.Id=" + document.Id);
                MessageBox.Show("document.Score=" + document.Score);
                MessageBox.Show("document.Source.duration=" + document.Source.duration);
                MessageBox.Show("document.Source.var_time=" + document.Source.var_time);
                MessageBox.Show("document.Source.var_name=" + document.Source.var_name);
                MessageBox.Show("document.Type=" + document.Type);
                MessageBox.Show("document.Index=" + document.Index);
        }
        });

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 2015-12-22
    • 1970-01-01
    相关资源
    最近更新 更多