gpt4 book ai didi

c# - 使用NEST搜索时出现JsonSerializationException

转载 作者:行者123 更新时间:2023-12-03 00:27:22 28 4
gpt4 key购买 nike

我正在使用NEST在C#中搜索elasticsearch索引。
当我在Google Chrome扩展程序Sense中运行搜索查询时,它可以工作。
但是,当我尝试从C#代码搜索时,出现以下异常:An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user codeAdditional information: JsonConverter DictionaryKeysAreNotPropertyNamesJsonConverter on System.Collections.Generic.IDictionary2[System.String,System.Double] _IndicesBoost is not compatible with member type IDictionary2.
我的C#类如下所示:

[ElasticType(Name = "ElasticSearchModel")]
public class ElasticSearchModel
{
[ElasticProperty(Type = FieldType.string_type)]
public string Id { get; private set; }
[ElasticProperty(Type = FieldType.string_type)]
public string Url { get; set; }
protected Dictionary<string, string> Properties { get; set; }
[ElasticProperty(Type = FieldType.nested)]
public Array ModelProperties
{
get
{
List<string[]> returnvalue = new List<string[]>();
foreach (var keyvaluepair in Properties)
{
if (!string.IsNullOrEmpty(keyvaluepair.Key))
{
string[] props = new string[2];
props[0] = keyvaluepair.Key;
props[1] = keyvaluepair.Value;
returnvalue.Add(props);
}
}

return returnvalue.ToArray();
}
}

public ElasticSearchModel(string id)
{
this.Id = id;
this.Properties = new Dictionary<string, string>();
}

public void AddProperty(string key, string value)
{
Properties.Add(key, value);
}
}

并且此代码用于获取搜索结果:
var descriptor = new SearchDescriptor<ElasticSearchModel>().Indices(new string[] { "kickstartconcept" });
searchresult = ElasticClient.Search(descriptor.QueryString(terms)); //Where terms is a string with the content of the query

我先尝试索引字典,但这给出了相同的异常(exception)。
因此,我尝试了使用Array,但这也不起作用。
是否有人知道异常来自何处以及如何解决?

感谢您的宝贵时间,康妮

(编辑)这是我的ElasticSearch索引中的一项:
"_source": {
"id": "1082",
"url": "/",
"modelProperties": [
[
"title",
"MyPageTitle"
],
[
"content",
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec nec lacus luctus, pulvinar ante vitae, feugiat velit. In gravida, urna ac lacinia tincidunt, metus erat pellentesque sem, sed congue massa velit non enim. Suspendisse metus sapien, ornare vel lectus ut, pulvinar feugiat justo. In diam metus, ultricies id augue interdum, dapibus placerat est. Cras vel nulla sed arcu dictum molestie eu ut sem. Suspendisse potenti. Ut mattis odio a aliquam vehicula. Proin varius commodo quam, sed semper orci mattis et. Suspendisse lacinia purus quis arcu semper rhoncus. Integer ut quam ut elit pharetra malesuada."
],
[
"hideinnavigation",
"0"
],
[
"subtitle",
""
],
[
"gridLayout",
"<content><area name=\"Body\"><block>1137</block><block>1139</block><block>1080</block></area><area name=\"Footer\"><block>1080</block></area></content>"
],
[
"searchpage",
""
]
]
}

最佳答案

感谢您发布索引中的示例项。如果要检索与该确切结构匹配的匹配,请将您的类更改为具有以下属性。

 [ElasticType(Index = FieldType.@object]
public List<Dictionary<string,string>> ModelProperties { get; set; }

如果您正在寻找更多纯字典,请使用以下命令:
 [ElasticType(Index = FieldType.@object]
public Dictionary<string,string> ModelProperties { get; set; }

但是为了适应这种情况,您需要将索引项更改为以下内容:
   {
"id": "1082",
"url": "/",
"modelProperties": {
"title": "MyPageTitle",
"content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec nec lacus luctus, pulvinar ante vitae, feugiat velit. In gravida, urna ac lacinia tincidunt, metus erat pellentesque sem, sed congue massa velit non enim. Suspendisse metus sapien, ornare vel lectus ut, pulvinar feugiat justo. In diam metus, ultricies id augue interdum, dapibus placerat est. Cras vel nulla sed arcu dictum molestie eu ut sem. Suspendisse potenti. Ut mattis odio a aliquam vehicula. Proin varius commodo quam, sed semper orci mattis et. Suspendisse lacinia purus quis arcu semper rhoncus. Integer ut quam ut elit pharetra malesuada.",
"hideinnavigation": "0",
"subtitle": "",
"gridLayout": "<content><area name=\"Body\"><block>1137</block><block>1139</block><block>1080</block></area><area name=\"Footer\"><block>1080</block></area></content>",
"searchpage": ""
}
}

希望这可以帮助...

关于c# - 使用NEST搜索时出现JsonSerializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23106110/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com