ES / Nest(C#)新手:" '解析值时出现意外的字符"错误

时间:2017-04-06 18:00:48

标签: c# elasticsearch nest

我是ES / Nest& amp;的全新手。 C#并且确实遇到了问题。我有一个ES索引,它有一个非常简单的文档结构,并试图搜索context字段。我收到这个错误:

  

Elasticsearch.Net.UnexpectedElasticsearchClientException:'意外   解析值时遇到的字符:[。路径   ' hits.hits [0] ._ source.context',第1行,第452位。'

我收集错误是因为上下文字段中的空值(当我从类中注释掉上下文时,它工作正常)。但我不知道如何让它忽略空值或完全不让它们。

       var node = new Uri("http://localhost:9200/mydata");
        var settings = new ConnectionSettings(node);
        var client = new ElasticClient(settings);

        string[] qq = { "beach","dog" };
        var tq = new TermsQuery
        {
            Name = "named_query",
            Terms = qq,
        };
        var request = new SearchRequest
        {
            From = 0,
            Size = 10,
            Query = tq
        };

        var response = client.Search<Doc>(request);
        foreach (Doc value in response.Hits)
        {
            Console.WriteLine(value);
        }

这是我的映射类定义:

 public  class Doc
    { 
        public  string _id { get; set; }
        public  DateTime created_at { get;set;}
        public string image_url { get; set; }
        public string Context { get; set; }
    }

1 个答案:

答案 0 :(得分:3)

问题是JSON正在返回Context属性的数组,但属性本身是string。我们知道它是一个数组,因为异常消息表示遇到一个开放的方括号时出现解析错误

  

&#39;解析值时遇到意外的字符:[

通过将Context作为字符串集合,可以轻松解决此问题。 string[]IEnumerable<string>或任何集合类型对您的模型最有意义。

Any field can have 0, 1 or more values;您不需要创建特定的数据类型集合来索引字段的多个值。唯一需要注意的是,它们都必须是相同的数据类型。