【问题标题】:Using NEST for elasticsearch. How can I search for parents based on children attributes使用 NEST 进行弹性搜索。如何根据孩子属性搜索父母
【发布时间】:2020-08-20 07:42:05
【问题描述】:

我正在尝试做一个简单的搜索,使用 NEST 为给定特定客户位置的客户。 POCOS 是:

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

提供的错误信息是:

系统异常 H结果=0x80131500 消息=无效搜索。错误是: 从 POST 上的不成功 (400) 低级别调用构建的无效 NEST 响应:/customers/_search?typed_keys=true

此 API 调用的审计跟踪:

  • [1] SniffOnStartup:使用:00:00:00.2448689
  • [2] SniffSuccess:节点:http://localhost:9200/ 获取:00:00:00.2288784
  • [3] PingSuccess:节点:http://127.0.0.1:9200/ 接受:00:00:00.0029899
  • [4] 错误响应:节点:http://127.0.0.1:9200/ 接受:00:00:00.1639172

OriginalException:Elasticsearch.Net.ElasticsearchClientException:远程服务器返回错误:(400)错误请求..调用:状态代码 400 来自:POST /customers/_search?typed_keys=true。 ServerError:类型:search_phase_execution_exception 原因:“所有分片失败”---> System.Net.WebException:远程服务器返回错误:(400)错误请求。

在 System.Net.HttpWebRequest.GetResponse() 在 Elasticsearch.Net.HttpWebRequestConnection.Request[TResponse](RequestData requestData) 在 C:\Users\russc\source\elasticsearch-net\src\Elasticsearch.Net\Connection\HttpWebRequestConnection.cs:line 63

任何想法 - 非常感谢。

【问题讨论】:

    标签: c# elasticsearch nest


    【解决方案1】:

    客户和位置之间的关系不是 Elasticsearch 术语中的父/子关系,这是使用the has_child query 所必需的。

    除非显式映射,否则Customer 上的Locations 属性将是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 =&gt; f.Locations.First().City 只是一个表达式,通过导航对象图,以强类型方式构建到 JSON 字段的路径。它表示“第一个位置所在的城市”,而是计算为“任何位置所在的城市”。

    这会生成以下查询

    {
      "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)
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-04
      • 2014-07-19
      相关资源
      最近更新 更多