无法从弹性客户端搜索响应中获取嵌套的键-值对字典

时间:2019-01-18 16:22:36

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

我正在尝试,但是无法从下面提到的访问hits [“ _ index”],hits“” _ type“],hits [” _ id“],hits”“ _ score”]和hits [“ _ source”]字典加载到数据库中。

尝试访问下面的每个键值对:

{
  "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);
        }
        });

}

2 个答案:

答案 0 :(得分:0)

一个想法:

首先,从根目录读取,从根目录创建地图类。接收,超时,碎片和点击。

第二,您的课程适用于“ hits.hits”。这是一个数组,因此需要像List这样的IEnumerable。请根据需要自己添加[PropertyName(“ ...”)]来完成此操作:

ARSCNViewDelegate

另外,您可能需要一个根类

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

  public string max_score {get;Set;}

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

还实现了碎片类

答案 1 :(得分:0)

@Russ Cam指出,以下解决方案更新已发布,以供其他人查看。

    const msg_area = document.getElementById('msg-area') as HTMLInputElement;
    if (msg-area.hasFocus()) {
        // somecode
    }