我试图做一个简单的搜索,使用NEST为客户提供特定的客户位置。这些人是:
class Customer
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Identifiers { get; set; }
public string isIndividual { get; set; }
public double Balance { get; set; }
public List<CustomerLocation> Locations { get; set; }
class CustomerLocation
{
public int CustomerLocationId { get; set; }
public string StreetLine1 { get; set; }
public string Streetline2 { get; set; }
public string City { get; set; }
}目前我正在使用这个搜索例程--但是它失败了:
var searchResponse = _client.Search<Customer>(s => s
.Query(q => q
.HasChild<CustomerLocation >(hp => hp
.Query(qq => qq
.Match(m => m
.Field(f => f.City )
.Query(queryText)
)
)
)
)
.Size(500)
);提供的错误消息是:
System.Exception HResult=0x80131500 Message=Invalid搜索错误是:从POST上不成功的(400)低级别调用构建的无效嵌套响应:/customers/_search?typed_key=true
此API调用的审计跟踪:
00:00:00.2448689
Elasticsearch.Net.ElasticsearchClientException:远程服务器返回一个错误:(400)坏请求。调用:状态代码400来自: POST /customers/_search?typed_key=true。ServerError: search_phase_execution_exception原因:“所有碎片失败”--> System.Net.WebException:远程服务器返回了一个错误:(400)坏请求。
在Elasticsearch.Net.HttpWebRequestConnection.RequestTResponse at C:\Users\russc\source\elasticsearch-net\src\Elasticsearch.Net\Connection\HttpWebRequestConnection.cs:line 63的System.Net.HttpWebRequest.GetResponse()
任何想法-非常感谢。
发布于 2020-08-24 03:24:48
在Elasticsearch术语中,客户和位置之间的关系不是父/子关系,这是使用the has_child query所必需的。
除非显式映射,否则Customer上的Customer属性将是一个object type mapping,这将允许您执行
var queryText = "Sydney";
var searchResponse = client.Search<Customer>(s => s
.Query(q => q
.Match(m => m
.Field(f => f.Locations.First().City)
.Query(queryText)
)
)
.Size(500)
);注意:f => f.Locations.First().City只是一个表达式,它通过导航对象图,以强类型的方式构建到JSON字段的路径。It 并不的意思是“第一个位置的城市”,而是计算为“任何位置的城市”。
这将生成以下查询
{
"query": {
"match": {
"locations.city": {
"query": "Sydney"
}
}
},
"size": 500
}但是,对于给定的POCO结构,很可能需要跨多个位置属性进行搜索。在这种情况下,应该将Locations显式映射为nested data type。
映射为嵌套数据类型时,查询将为
var queryText = "Sydney";
var searchResponse = client.Search<Customer>(s => s
.Query(q => q
.Nested(n => n
.Path(p => p.Locations)
.Query(nq => nq
.Match(m => m
.Field(f => f.Locations.First().City)
.Query(queryText)
)
)
)
)
.Size(500)
);https://stackoverflow.com/questions/63500480
复制相似问题