按位置在弹性搜索中排序,并且在位置lat和long不再出现时出现错误

时间:2019-09-29 12:02:31

标签: elasticsearch nest

我尝试使用下面的代码完全满足我在弹性搜索中的位置排序要求,当位置即将到来时它可以正常工作,但是当位置纬度和长距离不到时,我的Nest c#代码为我提供了异常

.Sort(s = >s.Ascending(fs = >fs.current_stage).Field(gh = >{
    var query = new SortFieldDescriptor < CarSearchRequest > ();
    if (searchAjaxRequest.sortingFilter != null) {
        if (searchAjaxRequest.sortingFilter == "titleasc") {
            query = gh.Field(tt = >tt.model).Order(SortOrder.Ascending);
        }
        else if (searchAjaxRequest.sortingFilter == "titledsc") {
            query = gh.Field(tt = >tt.model).Order(SortOrder.Descending);
        }
        else if (searchAjaxRequest.sortingFilter == "priceasc") {
            query = gh.Field(tt = >tt.price).Order(SortOrder.Ascending);
        }
        else if (searchAjaxRequest.sortingFilter == "Pricedsc") {
            query = gh.Field(tt = >tt.price).Order(SortOrder.Descending);
        }
        else if (searchAjaxRequest.sortingFilter == "Kmasc") {
            query = gh.Field(tt = >tt.kmRun).Order(SortOrder.Ascending);
        }
        else if (searchAjaxRequest.sortingFilter == "kmdsc") {
            query = gh.Field(tt = >tt.kmRun).Order(SortOrder.Descending);
        }
        else if (searchAjaxRequest.sortingFilter == "introductiondateasc") {
            query = gh.Field(tt = >tt.mfYear).Order(SortOrder.Ascending);
        }
        else if (searchAjaxRequest.sortingFilter == "introductiondatedesc") {
            query = gh.Field(tt = >tt.mfYear).Order(SortOrder.Descending);
        }
        return query;
    }
    else if (searchAjaxRequest.category != null) {
        if (searchAjaxRequest.category == "recentViewed") {
            query = gh.Field(tt = >tt.createdDate).Order(SortOrder.Descending);
        }
        else if (searchAjaxRequest.category == "topRated") {
            query = gh.Field(tt = >tt.overallRating).Order(SortOrder.Descending);
        }
        return query;
    }

    return query;
}).GeoDistance(g = >{
    var locatioSorting = new SortGeoDistanceDescriptor < CarSearchRequest > ();
    if (searchAjaxRequest.location != null) {
        locatioSorting = g.Field(ab = >ab.location).Order(SortOrder.Ascending).Unit(DistanceUnit.Kilometers).Mode(SortMode.Min).Points(new GeoLocation(searchAjaxRequest.location.lat, searchAjaxRequest.location.lon));
    }
    return locatioSorting;
})));

在我的代码中的空句柄之后出现的异常。

System.NullReferenceException: Object reference not set to an instance of an object.
   at Nest.FieldResolver.Resolve(Field field)
   at Nest.SortJsonConverter.<>c__DisplayClass6_1.<WriteJson>b__0(JsonWriter w)
   at Nest.ReserializeJsonConverter`2.Reserialize(JsonWriter writer, Object value, JsonSerializer serializer, Action`1 inlineWriter)
   at Nest.SortJsonConverter.WriteJson(JsonWriter writer, Object value, JsonSerializer serializer)
   at Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.SerializeConvertable(JsonWriter writer, JsonConverter converter, Object value, JsonContract contract, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
   at Elastic.Internal.JsonNet.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
   at Nest.InternalSerializer.Serialize[T](T data, Stream writableStream, SerializationFormatting formatting)
   at Elasticsearch.Net.SerializableData`1.Write(Stream writableStream, IConnectionConfigurationValues settings)
   at Elasticsearch.Net.HttpWebRequestConnection.Request[TResponse](RequestData requestData)
   at Elasticsearch.Net.RequestPipeline.CallElasticsearch[TResponse](RequestData requestData)
   at Elasticsearch.Net.Transport`1.Request[TResponse](HttpMethod method, String path, PostData data, IRequestParameters requestParameters)
# Inner Exception: Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
   at Nest.FieldResolver.Resolve(Field field)
   at Nest.SortJsonConverter.<>c__DisplayClass6_1.<WriteJson>b__0(JsonWriter w)
   at Nest.ReserializeJsonConverter`2.Reserialize(JsonWriter writer, Object value, JsonSerializer serializer, Action`1 inlineWriter)
   at Nest.SortJsonConverter.WriteJson(JsonWriter writer, Object value, JsonSerializer serializer)
   at Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.SerializeConvertable(JsonWriter writer, JsonConverter converter, Object value, JsonContract contract, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
   at Elastic.Internal.JsonNet.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
   at Nest.InternalSerializer.Serialize[T](T data, Stream writableStream, SerializationFormatting formatting)
   at Elasticsearch.Net.SerializableData`1.Write(Stream writableStream, IConnectionConfigurationValues settings)
   at Elasticsearch.Net.HttpWebRequestConnection.Request[TResponse](RequestData requestData)
   at Elasticsearch.Net.RequestPipeline.CallElasticsearch[TResponse](RequestData requestData)
   at Elasticsearch.Net.Transport`1.Request[TResponse](HttpMethod method, String path, PostData data, IRequestParameters requestParameters)
# Exception:
Elasticsearch.Net.UnexpectedElasticsearchClientException: Object reference not set to an instance of an object. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at Nest.FieldResolver.Resolve(Field field)
   at Nest.SortJsonConverter.<>c__DisplayClass6_1.<WriteJson>b__0(JsonWriter w)
   at Nest.ReserializeJsonConverter`2.Reserialize(JsonWriter writer, Object value, JsonSerializer serializer, Action`1 inlineWriter)
   at Nest.SortJsonConverter.WriteJson(JsonWriter writer, Object value, JsonSerializer serializer)
   at Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.SerializeConvertable(JsonWriter writer, JsonConverter converter, Object value, JsonContract contract, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
   at Elastic.Internal.JsonNet.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
   at Nest.InternalSerializer.Serialize[T](T data, Stream writableStream, SerializationFormatting formatting)
   at Elasticsearch.Net.SerializableData`1.Write(Stream writableStream, IConnectionConfigurationValues settings)
   at Elasticsearch.Net.HttpWebRequestConnection.Request[TResponse](RequestData requestData)
   at Elasticsearch.Net.RequestPipeline.CallElasticsearch[TResponse](RequestData requestData)
   at Elasticsearch.Net.Transport`1.Request[TResponse](HttpMethod method, String path, PostData data, IRequestParameters requestParameters)
   --- End of inner exception stack trace ---

0 个答案:

没有答案