有没有办法反序列化Elasticsearch Nest搜索查询?

时间:2014-05-16 19:54:59

标签: elasticsearch nest

在使用Nest构建我的Elasticsearch查询时,我希望能够看到发送到Elasticsearch的JSON版本。这可能吗?我想是某种解串器。

这里是我的跟进问题的信息:

{
  "_infer": {
    "defaultIndex": "myindex"
  },
  "acknowledged": false,
  "isValid": false,
  "connectionStatus": {
    "success": false,
    "requestMethod": "POST",
    "requestUrl": "http://localhost:9200/myindex",
    "settings": {},
    "request": "ew0KICAic2V0dGluZ3MiOiB7DQogICAgImluZGV4Ijogew0KICAgICAgImFuYWx5c2lzIjogew0KICAgICAgICAiYW5hbHl6ZXIiOiB7DQogICAgICAgICAgImZ1bGxUZXJtQ2FzZUluc2Vuc2l0aXZlIjogew0KICAgICAgICAgICAgInRva2VuaXplciI6ICJrZXl3b3JkIiwNCiAgICAgICAgICAgICJmaWx0ZXIiOiBbDQogICAgICAgICAgICAgICJzdGFuZGFyZCIsDQogICAgICAgICAgICAgICJsb3dlcmNhc2UiDQogICAgICAgICAgICBdLA0KICAgICAgICAgICAgInR5cGUiOiAiY3VzdG9tIg0KICAgICAgICAgIH0sDQogICAgICAgICAgImZ1bGxUZXJtIjogew0KICAgICAgICAgICAgInRva2VuaXplciI6ICJrZXl3b3JkIiwNCiAgICAgICAgICAgICJmaWx0ZXIiOiBbDQogICAgICAgICAgICAgICJzdGFuZGFyZCINCiAgICAgICAgICAgIF0sDQogICAgICAgICAgICAidHlwZSI6ICJjdXN0b20iDQogICAgICAgICAgfSwNCiAgICAgICAgICAiY2FzZUluc2Vuc2l0aXZlIjogew0KICAgICAgICAgICAgInRva2VuaXplciI6ICJsb3dlcmNhc2UiLA0KICAgICAgICAgICAgImZpbHRlciI6IFsNCiAgICAgICAgICAgICAgInN0YW5kYXJkIg0KICAgICAgICAgICAgXSwNCiAgICAgICAgICAgICJ0eXBlIjogImN1c3RvbSINCiAgICAgICAgICB9DQogICAgICAgIH0NCiAgICAgIH0NCiAgICB9DQogIH0sDQogICJtYXBwaW5ncyI6IHsNCiAgICAic2VhcmNoZGF0YSI6IHsNCiAgICAgICJwcm9wZXJ0aWVzIjogew0KICAgICAgICAidXBjIjogew0KICAgICAgICAgICJ0eXBlIjogInN0cmluZyINCiAgICAgICAgfSwNCiAgICAgICAgInByb2R1Y3RpZCI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJ0aXRsZSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJzdG9yZSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJtZWRpYWZvcm1hdCI6IHsNCiAgICAgICAgICAidHlwZSI6ICJtdWx0aV9maWVsZCIsDQogICAgICAgICAgImZpZWxkcyI6IHsNCiAgICAgICAgICAgICJtZWRpYWZvcm1hdCI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtQ2FzZUluc2Vuc2l0aXZlIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJyYXciOiB7DQogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgICAgICJpbmRleCI6ICJub3RfYW5hbHl6ZWQiDQogICAgICAgICAgICB9DQogICAgICAgICAgfQ0KICAgICAgICB9LA0KICAgICAgICAibGFiZWwiOiB7DQogICAgICAgICAgInR5cGUiOiAic3RyaW5nIg0KICAgICAgICB9LA0KICAgICAgICAidmVuZG9yIjogew0KICAgICAgICAgICJ0eXBlIjogInN0cmluZyINCiAgICAgICAgfSwNCiAgICAgICAgImF2YWlsYWJsZWRhdGUiOiB7DQogICAgICAgICAgInR5cGUiOiAiZGF0ZSINCiAgICAgICAgfSwNCiAgICAgICAgInJlbGVhc2VkYXRlIjogew0KICAgICAgICAgICJ0eXBlIjogImRhdGUiDQogICAgICAgIH0sDQogICAgICAgICJsYXN0cmV0dXJuZGF0ZSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJkYXRlIg0KICAgICAgICB9LA0KICAgICAgICAibm9ucmV0dXJuYWJsZSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJtcGFhIjogew0KICAgICAgICAgICJ0eXBlIjogInN0cmluZyINCiAgICAgICAgfSwNCiAgICAgICAgImVzcmIiOiB7DQogICAgICAgICAgInR5cGUiOiAibXVsdGlfZmllbGQiLA0KICAgICAgICAgICJmaWVsZHMiOiB7DQogICAgICAgICAgICAiZXNyYiI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtQ2FzZUluc2Vuc2l0aXZlIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJyYXciOiB7DQogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgICAgICJpbmRleCI6ICJub3RfYW5hbHl6ZWQiDQogICAgICAgICAgICB9DQogICAgICAgICAgfQ0KICAgICAgICB9LA0KICAgICAgICAiY3NucmZsYWciOiB7DQogICAgICAgICAgInR5cGUiOiAic3RyaW5nIg0KICAgICAgICB9LA0KICAgICAgICAiY292ZXIiOiB7DQogICAgICAgICAgInR5cGUiOiAic3RyaW5nIg0KICAgICAgICB9LA0KICAgICAgICAic2l6ZSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJjb2xvciI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJzdHlsZSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJxb2giOiB7DQogICAgICAgICAgInR5cGUiOiAiaW50ZWdlciINCiAgICAgICAgfSwNCiAgICAgICAgImFkdWx0Ijogew0KICAgICAgICAgICJ0eXBlIjogImludGVnZXIiDQogICAgICAgIH0sDQogICAgICAgICJzYWxlc3JhbmsiOiB7DQogICAgICAgICAgInR5cGUiOiAiaW50ZWdlciINCiAgICAgICAgfSwNCiAgICAgICAgImxpc3RwcmljZSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJkb3VibGUiDQogICAgICAgIH0sDQogICAgICAgICJzZWxsaW5ncHJpY2UiOiB7DQogICAgICAgICAgInR5cGUiOiAiZG91YmxlIg0KICAgICAgICB9LA0KICAgICAgICAiY29zdHByaWNlIjogew0KICAgICAgICAgICJ0eXBlIjogImRvdWJsZSINCiAgICAgICAgfSwNCiAgICAgICAgInNwZWNpYWxvcmRlciI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJmbGFncyI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJzYWxlc2NhdGVnb3J5Ijogew0KICAgICAgICAgICJ0eXBlIjogInN0cmluZyINCiAgICAgICAgfSwNCiAgICAgICAgImFydGlzdHMiOiB7DQogICAgICAgICAgInR5cGUiOiAib2JqZWN0IiwNCiAgICAgICAgICAicHJvcGVydGllcyI6IHsNCiAgICAgICAgICAgICJuYW1lIjogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiYW5hbHl6ZXIiOiAiZnVsbFRlcm0iLA0KICAgICAgICAgICAgICAiaW5kZXgiOiAibm90X2FuYWx5emVkIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJpZCI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtIiwNCiAgICAgICAgICAgICAgImluZGV4IjogIm5vdF9hbmFseXplZCINCiAgICAgICAgICAgIH0NCiAgICAgICAgICB9DQogICAgICAgIH0sDQogICAgICAgICJkaXJlY3RvcnMiOiB7DQogICAgICAgICAgInR5cGUiOiAib2JqZWN0IiwNCiAgICAgICAgICAicHJvcGVydGllcyI6IHsNCiAgICAgICAgICAgICJuYW1lIjogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiYW5hbHl6ZXIiOiAiZnVsbFRlcm0iLA0KICAgICAgICAgICAgICAiaW5kZXgiOiAibm90X2FuYWx5emVkIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJpZCI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtIiwNCiAgICAgICAgICAgICAgImluZGV4IjogIm5vdF9hbmFseXplZCINCiAgICAgICAgICAgIH0NCiAgICAgICAgICB9DQogICAgICAgIH0sDQogICAgICAgICJzdGFycmluZyI6IHsNCiAgICAgICAgICAidHlwZSI6ICJvYmplY3QiLA0KICAgICAgICAgICJwcm9wZXJ0aWVzIjogew0KICAgICAgICAgICAgIm5hbWUiOiB7DQogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgICAgICJhbmFseXplciI6ICJmdWxsVGVybSIsDQogICAgICAgICAgICAgICJpbmRleCI6ICJub3RfYW5hbHl6ZWQiDQogICAgICAgICAgICB9LA0KICAgICAgICAgICAgImlkIjogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiYW5hbHl6ZXIiOiAiZnVsbFRlcm0iLA0KICAgICAgICAgICAgICAiaW5kZXgiOiAibm90X2FuYWx5emVkIg0KICAgICAgICAgICAgfQ0KICAgICAgICAgIH0NCiAgICAgICAgfSwNCiAgICAgICAgImNyZWRpdHMiOiB7DQogICAgICAgICAgInR5cGUiOiAib2JqZWN0IiwNCiAgICAgICAgICAicHJvcGVydGllcyI6IHsNCiAgICAgICAgICAgICJuYW1lIjogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiYW5hbHl6ZXIiOiAiZnVsbFRlcm0iLA0KICAgICAgICAgICAgICAiaW5kZXgiOiAibm90X2FuYWx5emVkIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJpZCI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtIiwNCiAgICAgICAgICAgICAgImluZGV4IjogIm5vdF9hbmFseXplZCINCiAgICAgICAgICAgIH0NCiAgICAgICAgICB9DQogICAgICAgIH0sDQogICAgICAgICJhZGRpdGlvbmFsdGl0bGVzIjogew0KICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgImFuYWx5emVyIjogImNhc2VJbnNlbnNpdGl2ZSINCiAgICAgICAgfSwNCiAgICAgICAgImF0dHJpYnV0ZXMiOiB7DQogICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAiYW5hbHl6ZXIiOiAiY2FzZUluc2Vuc2l0aXZlIg0KICAgICAgICB9LA0KICAgICAgICAic3BlY2lhbHR5Ijogew0KICAgICAgICAgICJ0eXBlIjogIm11bHRpX2ZpZWxkIiwNCiAgICAgICAgICAiZmllbGRzIjogew0KICAgICAgICAgICAgInNwZWNpYWx0eSI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtQ2FzZUluc2Vuc2l0aXZlIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJyYXciOiB7DQogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgICAgICJpbmRleCI6ICJub3RfYW5hbHl6ZWQiDQogICAgICAgICAgICB9DQogICAgICAgICAgfQ0KICAgICAgICB9LA0KICAgICAgICAiZ2VucmVzIjogew0KICAgICAgICAgICJ0eXBlIjogIm11bHRpX2ZpZWxkIiwNCiAgICAgICAgICAiZmllbGRzIjogew0KICAgICAgICAgICAgImdlbnJlcyI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtQ2FzZUluc2Vuc2l0aXZlIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJyYXciOiB7DQogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgICAgICJpbmRleCI6ICJub3RfYW5hbHl6ZWQiDQogICAgICAgICAgICB9DQogICAgICAgICAgfQ0KICAgICAgICB9LA0KICAgICAgICAidGhlbWUiOiB7DQogICAgICAgICAgInR5cGUiOiAibXVsdGlfZmllbGQiLA0KICAgICAgICAgICJmaWVsZHMiOiB7DQogICAgICAgICAgICAidGhlbWUiOiB7DQogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgICAgICJhbmFseXplciI6ICJmdWxsVGVybUNhc2VJbnNlbnNpdGl2ZSINCiAgICAgICAgICAgIH0sDQogICAgICAgICAgICAicmF3Ijogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiaW5kZXgiOiAibm90X2FuYWx5emVkIg0KICAgICAgICAgICAgfQ0KICAgICAgICAgIH0NCiAgICAgICAgfSwNCiAgICAgICAgInByaWNlIjogew0KICAgICAgICAgICJ0eXBlIjogIm11bHRpX2ZpZWxkIiwNCiAgICAgICAgICAiZmllbGRzIjogew0KICAgICAgICAgICAgInByaWNlIjogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiYW5hbHl6ZXIiOiAiZnVsbFRlcm1DYXNlSW5zZW5zaXRpdmUiDQogICAgICAgICAgICB9LA0KICAgICAgICAgICAgInJhdyI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImluZGV4IjogIm5vdF9hbmFseXplZCINCiAgICAgICAgICAgIH0NCiAgICAgICAgICB9DQogICAgICAgIH0sDQogICAgICAgICJ0cmFja3MiOiB7DQogICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAiYW5hbHl6ZXIiOiAiY2FzZUluc2Vuc2l0aXZlIg0KICAgICAgICB9LA0KICAgICAgICAiZGVsZXRlZCI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJ1cGRhdGVkYXRlIjogew0KICAgICAgICAgICJ0eXBlIjogImRhdGUiDQogICAgICAgIH0sDQogICAgICAgICJwcm9jZXNzZWQiOiB7DQogICAgICAgICAgInR5cGUiOiAic3RyaW5nIg0KICAgICAgICB9LA0KICAgICAgICAiZGVjYWRlcyI6IHsNCiAgICAgICAgICAidHlwZSI6ICJtdWx0aV9maWVsZCIsDQogICAgICAgICAgImZpZWxkcyI6IHsNCiAgICAgICAgICAgICJkZWNhZGVzIjogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiYW5hbHl6ZXIiOiAiZnVsbFRlcm1DYXNlSW5zZW5zaXRpdmUiDQogICAgICAgICAgICB9LA0KICAgICAgICAgICAgInJhdyI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImluZGV4IjogIm5vdF9hbmFseXplZCINCiAgICAgICAgICAgIH0NCiAgICAgICAgICB9DQogICAgICAgIH0sDQogICAgICAgICJwZXJpb2QiOiB7DQogICAgICAgICAgInR5cGUiOiAibXVsdGlfZmllbGQiLA0KICAgICAgICAgICJmaWVsZHMiOiB7DQogICAgICAgICAgICAicGVyaW9kIjogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiYW5hbHl6ZXIiOiAiZnVsbFRlcm1DYXNlSW5zZW5zaXRpdmUiDQogICAgICAgICAgICB9LA0KICAgICAgICAgICAgInJhdyI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImluZGV4IjogIm5vdF9hbmFseXplZCINCiAgICAgICAgICAgIH0NCiAgICAgICAgICB9DQogICAgICAgIH0sDQogICAgICAgICJwbGF0Zm9ybSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJtdWx0aV9maWVsZCIsDQogICAgICAgICAgImZpZWxkcyI6IHsNCiAgICAgICAgICAgICJwbGF0Zm9ybSI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtQ2FzZUluc2Vuc2l0aXZlIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJyYXciOiB7DQogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgICAgICJpbmRleCI6ICJub3RfYW5hbHl6ZWQiDQogICAgICAgICAgICB9DQogICAgICAgICAgfQ0KICAgICAgICB9DQogICAgICB9DQogICAgfQ0KICB9DQp9",
    "numberOfRetries": 0,
    "httpStatusCode": 400,
    "serializer": {},
    "successOrKnownError": true
  },
  "infer": {
    "defaultIndex": "myindex"
  }
}

5 个答案:

答案 0 :(得分:21)

烨。您可以使用ElasticClient公开的序列化程序,如下所示:

var query = Nest.Query<SomeObject>...
var json = Client.Serializer.SerializeToString(query);

您也可以直接使用Newtonsoft或任何JSON库来序列化您的查询对象。虽然在ElasticClient中使用了序列化程序(基本上包含了Newtonsoft),但它将为您提供NEST将生成的确切JSON。

详细了解如何在NEST中完成单元测试,以获取更多详细信息,特别是this

答案 1 :(得分:2)

我使用像这样的SearchDescriptor进行复杂的查询:

SearchDescriptor<T> sd = new SearchDescriptor<T>()
.From(0).Size(100)
    .Query(q => q
        .Bool(t => t
            .Must(u => u
                .Bool(v => v
                    .Should(
                        ...
                    )
                )
            )
        )
    );

得到了反序列化的JSON:

{
  "from": 0,
  "size": 100,
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              ...
            ]
          }
        }
      ]
    }
  }
}

这很烦人,NEST库应该有一些东西从请求中吐出JSON。然而,这对我有用:

using (MemoryStream mStream = new MemoryStream()) {
    client.Serializer.Serialize(sd, mStream);
    Console.WriteLine(Encoding.ASCII.GetString(mStream.ToArray()));
}

NEST库版本:2.0.0.0。 较新的版本可能有一个更简单的方法来获得这个(希望)。

答案 2 :(得分:1)

如果上面的答案不起作用,或者你想要一个更简单的答案,我在这里发布了另一种方式,可能是一个重复的(但措辞不同)问题: https://stackoverflow.com/a/31247636/261405

答案 3 :(得分:0)

将此添加到您的WebApiConfig.cs:

public static void Register(HttpConfiguration config)
{
    ...
    // ignore circular reference globally
    config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
    ...
}

然后你应该可以做这样的事情:

// Return raw
var esObj = await _client.SearchAsync<object>(searchRequest);

// more specific deserialization using Newtonsoft.Json example
foreach (var hit in esObj.Hits) 
{
    var myObj = JsonConvert.DeserializeObject<MyObject>(hit.xxx);
    ...
}

答案 4 :(得分:0)

Nest 5.3.0:

        var memoryStream = new System.IO.MemoryStream();
        client.Serializer.Serialize(query, memoryStream);
        var jsonQuery = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());