当位置经纬度和long值变为空时,Nest弹性搜索排序面临的问题

时间:2019-10-01 07:55:13

标签: elasticsearch nest

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

var result = this._esclient.EsClient().Search<CarSearchRequest>(a => a
                                .Index("XXX")
                                .Type("XXX")
                                .Size(10)
                                .From(pageCount)
                                .Query(q => q.Bool(b => b.Must(m => m.Terms(p => p.Field(ab => ab.carType).Terms(searchAjaxRequest.carBodyType)),
                                                                   m => m.Terms(p =>
                                                                   {
                                                                       var query = new TermsQueryDescriptor<CarSearchRequest>();
                                                                       if (searchAjaxRequest.CarCityRange < 2)
                                                                       {
                                                                           query = p.Field(ab => ab.dealerCity).Terms(searchAjaxRequest.dealerCity);
                                                                       }
                                                                       return query;
                                                                   }
                                                                   ),                                                                   
                                                                   m => m.Range(p =>
                                                                   {
                                                                       var query = new NumericRangeQueryDescriptor<CarSearchRequest>();
                                                                       if (searchAjaxRequest.kilometers == 75001)
                                                                       {
                                                                           query = p.Field(ab => ab.kmRun).GreaterThanOrEquals(searchAjaxRequest.kilometers);
                                                                       }
                                                                       else
                                                                       {
                                                                           query = p.Field(ab => ab.kmRun).LessThanOrEquals(searchAjaxRequest.kilometers);
                                                                       }
                                                                       return query;
                                                                   }
                                                                       ),                                                                   
                                                                   m => m.Range(p => p.Field(ab => 
                                                                   m => m.GeoDistance(p =>
                                                                   {
                                                                       var query = new GeoDistanceQueryDescriptor<CarSearchRequest>();
                                                                       if (searchAjaxRequest.location != null)
                                                                       {
                                                                           if (searchAjaxRequest.CarCityRange == 1)
                                                                           {
                                                                               query = p.Field(lk => lk.location).Location(searchAjaxRequest.location.lat, searchAjaxRequest.location.lon);
                                                                           }
                                                                           else
                                                                           {
                                                                               query = p.Field(lk => lk.location)
                                                                                .Location(searchAjaxRequest.location.lat, searchAjaxRequest.location.lon)
                                                                                .Distance(searchAjaxRequest.CarCityRange, DistanceUnit.Kilometers);
                                                                           }
                                                                           return query;
                                                                       }
                                                                       return query;
                                                                   }
                                                                       ))
                                                                       )
                                          )
                                    .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 == "introductiondatedesc")
                                                                    {
                                                                        query = gh.Field(tt => tt.mfYear).Order(SortOrder.Descending);
                                                                    }
                                                                    return query;
                                                                }
                                                                else if (searchAjaxRequest.category != null)
                                                                {

                                                                    if (searchAjaxRequest.category == "recentlyViewd")
                                                                    {
                                                                        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;
                                                                }
                                                                                ).Ascending(fk => fk.trvCertified).Ascending(sa => sa.kmRun)
                                                                ));

代码中出现的异常。

System.NullReferenceException:对象引用未设置为对象的实例。    在Nest.FieldResolver.Resolve(Field field)    在Nest.SortJsonConverter。<> c__DisplayClass6_1.b__0(JsonWriter w)    在Nest.ReserializeJsonConverter 2.Reserialize(JsonWriter writer, Object value, JsonSerializer serializer, Action 1 inlineWriter处)    在Nest.SortJsonConverter.WriteJson(JsonWriter编写器,对象值,JsonSerializer序列化器)处    在Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.SerializeConvertable(JsonWriter作家,JsonConverter转换器,对象值,JsonContract合同,JsonContainerContract collectionContract,JsonProperty containerProperty)    在Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter作家,IEnumerable值,JsonArrayContract合同,JsonProperty成员,JsonContainerContract collectionContract,JsonProperty containerProperty)    在Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty)中    在Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,对象值,类型objectType)处    在Elastic.Internal.JsonNet.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,对象值,类型objectType)    在Nest.InternalSerializer.Serialize [T](T数据,流writableStream,SerializationFormatting格式化)    在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方法,字符串路径,PostData数据,IRequestParameters requestParameters)

内部异常:对象引用未设置为对象的实例。

System.NullReferenceException:对象引用未设置为对象的实例。    在Nest.FieldResolver.Resolve(Field field)    在Nest.SortJsonConverter。<> c__DisplayClass6_1.b__0(JsonWriter w)    在Nest.ReserializeJsonConverter 2.Reserialize(JsonWriter writer, Object value, JsonSerializer serializer, Action 1 inlineWriter处)    在Nest.SortJsonConverter.WriteJson(JsonWriter编写器,对象值,JsonSerializer序列化器)处    在Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.SerializeConvertable(JsonWriter作家,JsonConverter转换器,对象值,JsonContract合同,JsonContainerContract collectionContract,JsonProperty containerProperty)    在Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter作家,IEnumerable值,JsonArrayContract合同,JsonProperty成员,JsonContainerContract collectionContract,JsonProperty containerProperty)    在Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty)中    在Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,对象值,类型objectType)处    在Elastic.Internal.JsonNet.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,对象值,类型objectType)    在Nest.InternalSerializer.Serialize [T](T数据,流writableStream,SerializationFormatting格式化)    在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方法,字符串路径,PostData数据,IRequestParameters requestParameters)

异常:

Elasticsearch.Net.UnexpectedElasticsearchClientException:对象引用未设置为对象的实例。 ---> System.NullReferenceException:对象引用未设置为对象的实例。    在Nest.FieldResolver.Resolve(Field field)    在Nest.SortJsonConverter。<> c__DisplayClass6_1.b__0(JsonWriter w)    在Nest.ReserializeJsonConverter 2.Reserialize(JsonWriter writer, Object value, JsonSerializer serializer, Action 1 inlineWriter处)    在Nest.SortJsonConverter.WriteJson(JsonWriter编写器,对象值,JsonSerializer序列化器)处    在Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.SerializeConvertable(JsonWriter作家,JsonConverter转换器,对象值,JsonContract合同,JsonContainerContract collectionContract,JsonProperty containerProperty)    在Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter作家,IEnumerable值,JsonArrayContract合同,JsonProperty成员,JsonContainerContract collectionContract,JsonProperty containerProperty)    在Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty)中    在Elastic.Internal.JsonNet.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,对象值,类型objectType)处    在Elastic.Internal.JsonNet.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,对象值,类型objectType)    在Nest.InternalSerializer.Serialize [T](T数据,流writableStream,SerializationFormatting格式化)    在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方法,字符串路径,PostData数据,IRequestParameters requestParameters)处    ---内部异常堆栈跟踪的结尾-

0 个答案:

没有答案