嵌套查询中聚合排序问题

时间:2019-01-14 05:58:08

标签: elasticsearch nest elasticsearch-aggregation

我需要从Elasticsearch中查询指定序列号范围内指定区域中的最后注册记录。 因此,我将索引映射为以下形状:

{ "settings": {
"index": {
  "number_of_shards": 5,
  "number_of_replicas": 2
}
},
"mapping": {
"AssetStatus": {
  "properties": {
    "serialnumber": {
      "type": "text",
      "fielddata": true
    },
    "vehiclestate": {
      "type": "text",
      "fielddata": true
    },
    "vehiclegeopoint": {
      "type": "geo-point",
      "fielddata": true
    },
    "vehiclespeed": {
      "type": "number",
      "fielddata": true
    },
    "vehiclefuelpercent": {
      "type": "text",
      "fielddata": true
    },
    "devicebatterypercent": {
      "type": "text",
      "fielddata": true
    },
    "networklatency": {
      "type": "text",
      "fielddata": true
    },
    "satellitescount": {
      "type": "number",
      "fielddata": true
    },
    "createdate": {
      "type": "date",
      "fielddata": true
    }
  }
}
}
}

此查询正常工作

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "serialnumber.keyword": "2228187d-b1a5-4e18-82bb-4d12438e0ec0"
          }
        },
        {
          "range": {
            "vehiclegeopoint.lat": {
              "gt": "31.287958",
              "lt": "31.295485"
            }
          }
        },
        {
          "range": {
            "vehiclegeopoint.lon": {
              "gt": "48.639844",
              "lt": "48.652032"
            }
          }
        }
      ],
      "must_not": [],
      "should": []
    }
  },
  "from": 0,
  "size": 0,
  "sort": [],
  "aggs": {
    "SerialNumberGroups": {
      "terms": {
        "field": "serialnumber.keyword"
      },
      "aggs": {
        "tops": {
          "top_hits": {
            "sort": [
              {
                "createdate.keyword": {
                  "order": "desc"
                }
              }
            ],
            "size": 1
          }
        }
      }
    }
  }
}

而我的嵌套查询有此错误

  

无效的NEST响应是由不成功的低级别调用建立的   POST:/ fms2 / AssetStatus / _search?typed_keys = true

     

此API调用的审核记录:

     
      
  • [1] BadResponse:节点:http://localhost:9200/参加了:00:00:00.1917118      

    OriginalException:Elasticsearch.Net.ElasticsearchClientException:请求执行失败。呼叫:状态代码400,来自:POST

  •   
     

/ fms2 / AssetStatus / _search?typed_keys = true。 ServerError:类型:   parsing_exception原因:“ [field]中VALUE_STRING的未知键。”

     请求:      

强制将其设置为响应。>

     响应:      

ConnectionSettings强制在响应上进行设置。>

我的巢式查询是这个

    var searchResponse =
        Client.Search<AssetStatus>(x => x
            .Index(settings.DefaultIndex)
            .Type("AssetStatus")
            .Query(fq => fq.GeoBoundingBox(c => c.Field(f => f.VehicleGeoPoint).BoundingBox(new GeoLocation(TopLeft.Lat, TopLeft.Lon), new GeoLocation(BottomRight.Lat, BottomRight.Lon))))
            .Query(fq =>
                fq.Bool(b => b.
                Filter(
                    f => f.Match(m => m.Field(g => g.SerialNumber.Suffix("keyword").Equals(sn)))
                )
            ))
            .Aggregations(a => a
                .Terms("group_by_SerialNumber", st => st
                    .Field(o => o.SerialNumber.Suffix("keyword"))
                    .Size(0)
                    .Aggregations(b=> b.TopHits("top_hits", lastRegistered => lastRegistered
                        .Field(bf=> bf.CreateDate.Suffix("keyword"))
                        .Size(1)))
                        ))
                        );

此问题用于汇总排序

1 个答案:

答案 0 :(得分:0)

问题出在我的POCO中,正如您在嵌套查询中看到的那样,我使用大写字母命名属性。我应该使用Nest库将数据注释用于Elastic的POCO。

[ElasticsearchType(Name = "AssetStatus")]
public class AssetStatus
{
    [Text]
    [PropertyName("serialnumber")]
    public string SerialNumber { get; set; }
    [Text]
    [PropertyName("vehiclestate")]
    public string VehicleState { get; set; }
    [GeoPoint]
    [PropertyName("vehiclegeopoint")]
    public GeoPoint VehicleGeoPoint { get; set; }
    [Number]
    [PropertyName("vehiclespeed")]
    public int VehicleSpeed { get; set; }
    [Text]
    [PropertyName("vehiclefuelpercent")]
    public string VehicleFuelPercent { get; set; }
    [Text]
    [PropertyName("devicebatterypercent")]
    public string DeviceBatteryPercent { get; set; }
    [Text]
    [PropertyName("networklatency")]
    public string NetworkLatency { get; set; }
    [Number]
    [PropertyName("satellitescount")]
    public byte SatellitesCount { get; set; }
    [Date]
    [PropertyName("createdate")]
    public string CreateDate { get; set; }
}