ES/Nest (C#) 新手:“'解析值时遇到意外字符”错误

ES/Nest (C#) newbie : " 'Unexpected character encountered while parsing value" error

我完全是 ES/Nest 和 C# 的新手,确实遇到了一个问题。我有一个具有非常简单文档结构的 ES 索引,并试图在 context 字段上进行搜索。我得到这个错误:

Elasticsearch.Net.UnexpectedElasticsearchClientException: 'Unexpected character encountered while parsing value: [. Path 'hits.hits[0]._source.context', line 1, position 452.'

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

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

这是我的映射 class 定义:

 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; }
    }

问题是 JSON 返回 Context 属性 的数组,但 属性 本身是 string。我们知道它是一个数组,因为异常消息表示遇到左方括号

时出现解析错误

'Unexpected character encountered while parsing value: [

这很容易通过使 Context 成为一个字符串集合来解决,例如string[]IEnumerable<string> 或任何对您的模型最有意义的集合类型。

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