【问题标题】:ElasticSearch NEST adds $type in the serialized request in MVC appElasticSearch NEST 在 MVC 应用程序的序列化请求中添加 $type
【发布时间】:2014-07-26 08:18:31
【问题描述】:

我正在尝试使用 MVC 应用程序中的 NEST,但是我的请求序列化不正确:

iisexpress.exe Error: 0 : NEST POST http://localhost:9200/_search (00:00:00.8188240):
StatusCode: 400, 
    Method: POST, 
    Url: http://localhost:9200/_search, 
    Request: {
  "$type": "Nest.SearchDescriptor`1[[System.Object, mscorlib]], Nest",
  "aggs": {
    "Period": {
      "$type": "Nest.AggregationDescriptor`1[[System.Object, mscorlib]], Nest",
      "date_histogram": {
        "$type": "Nest.DateHistogramAggregationDescriptor`1[[System.Object, mscorlib]], Nest",
        "field": "Timestamp",
        "interval": "day",
        "format": "yyyy-MM-dd"
      }
    }
  }
}

查询很简单:

        var cs2 = new ConnectionSettings(new Uri("http://localhost:9200")).EnableTrace();
        var client = new ElasticClient(cs2);

        var res3 = client.Search<object>(q =>q.Aggregations(agg =>
                agg.DateHistogram("DayAgg", t => t.Field("Timestamp").Interval("day"))));

完全相同的代码在控制台应用程序中运行良好,所以我认为这可能与序列化有关,因为在不好的情况下添加了“$type”属性。

【问题讨论】:

    标签: elasticsearch nest


    【解决方案1】:

    发现相关问题:Serialization error with Elasticsearch NEST/C#

    根本原因是以下设置:

    config.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Objects;
    

    现在还有一个支持设置的新api:SetJsonSerializerSettingsModifier

            var cs2 = new ConnectionSettings(new Uri("http://localhost:9200"))
                .SetJsonSerializerSettingsModifier(settings => settings.TypeNameHandling = TypeNameHandling.None)
                .EnableTrace();
    

    【讨论】:

    • 请注意,JSON.NET 默认值可以全局更改:james.newtonking.com/archive/2013/05/08/…。我认为您的堆栈中的某些内容会更改这些内容以始终包含 CLR 类型名(NEST 不会这样做)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    • 2019-09-27
    • 2016-08-12
    • 2015-11-10
    相关资源
    最近更新 更多