我试图在弹性搜索中使用Nest.Net库运行一个原始查询。查询如下:
var json4 = @"
{
""query"": {
""bool"": {
""filter"":{
""term"":{ ""schoolId"": ""c15677ea-3e1e-4767-936a-2b3c57b00503""}
},
""must"": [
{
""multi_match"": {
""query"": ""001 Ali"",
""fields"": [""firstName"",""lastName"", ""phoneNumber"", ""code"", ""title""],
""type"": ""cross_fields""
}
}
]
}
}
}
";
SearchRequest searchRequest;
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(json4)))
{
searchRequest = client.RequestResponseSerializer.Deserialize<SearchRequest>(stream);
}反序列化方法引发错误,如下所示:
无法将当前JSON对象(例如,{“名称”:“值”})反序列化为'System.Collections.Generic.IEnumerable`1Nest.QueryContainer‘类型,因为该类型需要一个JSON数组(例如,1,2,3)才能正确反序列化。要修复此错误,要么将JSON更改为JSON数组(例如,1,2,3),要么更改反序列化类型,使之成为可以从JSON对象反序列化的普通.NET类型(例如,不像整数这样的原始类型,而不是数组或列表之类的集合类型)。还可以将JsonObjectAttribute添加到类型中,以强制它从JSON对象反序列化。路径'query.bool.filter.term',第6行,位置51。
查询在基巴纳运行得很好。
谢谢
发布于 2018-05-31 23:54:34
嵌套反序列化只支持长形式的查询,即
bool查询filter子句必须是查询数组;它不支持传递对象的关键字为查询的对象term查询短格式的"term": { "field": "value" };它必须是表单"term": { "field" : { "value": "value" } }。以下几点将起作用
var json4 = @"
{
""query"": {
""bool"": {
""filter"":[
{ ""term"":{ ""schoolId"": { ""value"": ""c15677ea-3e1e-4767-936a-2b3c57b00503""}} }
],
""must"": [
{
""multi_match"": {
""query"": ""001 Ali"",
""fields"": [""firstName"",""lastName"", ""phoneNumber"", ""code"", ""title""],
""type"": ""cross_fields""
}
}
]
}
}
}
";
SearchRequest searchRequest;
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(json4)))
{
searchRequest = client.Serializer.Deserialize<SearchRequest>(stream);
}但是,NEST能够接受查询作为JSON字符串,并使用作为.LowLevel属性公开的低级客户端返回强类型的搜索响应。这样,就不需要将JSON字符串反序列化为SearchRequest,只需要在提交请求时将其序列化回JSON。此外,您还可以使用原始查询。
var json4 = @"
{
""query"": {
""bool"": {
""filter"": {
""term"":{ ""schoolId"": ""c15677ea-3e1e-4767-936a-2b3c57b00503"" }
},
""must"": [
{
""multi_match"": {
""query"": ""001 Ali"",
""fields"": [""firstName"",""lastName"", ""phoneNumber"", ""code"", ""title""],
""type"": ""cross_fields""
}
}
]
}
}
}
";
client.LowLevel.Search<SearchResponse<object>>("index", "type", json4);https://stackoverflow.com/questions/50614371
复制相似问题