I have an index named store
and it's contain a nested object as a list which is WorkTimes
.
WorkTimes
list has open time and close time and maybe it has more than one OpenTime
and CloseTime
.
What I need to do is sort this index based on open time and close time.
OpenTime
need to be greater than current time and CloseTime
need to be less than current time.
here is my implementation so far:
var allfilters = new List<Func<QueryContainerDescriptor<Store>, QueryContainer>>();
if (storeCategoryIds.Any())
{
allfilters.Add(fq => fq.Terms(t => t.Field(f => f.StoreCategoryIds).Terms(storeCategoryIds)));
}
if (storeSubCategoryIds.Any())
{
allfilters.Add(fq => fq.Terms(t => t.Field(f => f.StoreSubCategoryIds).Terms(storeSubCategoryIds)));
}
var stores = await _elasticClient.SearchAsync<Store>(s => s.Index(index).From(from).Size(size)
.ScriptFields(sf => sf
.ScriptField("distance", descriptor => descriptor
.Source("doc[\u0027location\u0027].arcDistance(params.lat,params.lon)")
.Lang("painless")
.Params(p => p.Add("lat", lat).Add("lon", lng))))
.Query(query => query
.Bool(b=>b.Filter(allfilters)))
.Query(query=>query
.Bool(b => b
.Filter(filter => filter
.GeoDistance(geo => geo
.Field(f => f.Location)
.Distance(10, DistanceUnit.Kilometers).Location(lat, lng)
.DistanceType(GeoDistanceType.Arc)))
.Must(m => m
.QueryString(qs => qs
.Fields(f => f
.Fields(f1 => f1.Title))
.Query(searchQuery)))))
.Sort(sort => sort
.GeoDistance(g => g
.Field(f => f.Location)
.Order(SortOrder.Ascending)
.Points(new GeoLocation(lat, lng))
.DistanceType(GeoDistanceType.Arc)
)).Source(sr => sr.IncludeAll()));
var storesArray = stores.Documents?.ToArray();
var arrayCount = 0;
foreach (var fieldValues in stores.Fields)
{
var distance = fieldValues.Value<double>("distance");
storesArray[arrayCount].Distance = distance;
arrayCount ;
}
return storesArray?.ToList();
CodePudding user response:
you can do like this
if (storeCategoryIds.Any())
{
allfilters.Add(fq => fq.Terms(t => t.Field(f => f.StoreCategoryIds).Terms(storeCategoryIds)));
}
if (storeSubCategoryIds.Any())
{
allfilters.Add(fq => fq.Terms(t => t.Field(f => f.StoreSubCategoryIds).Terms(storeSubCategoryIds)));
}
var stores = await _elasticClient.SearchAsync<Store>(s => s.Index(index).From(from).Size(size)
.ScriptFields(sf => sf
.ScriptField("distance", descriptor => descriptor
.Source("doc[\u0027location\u0027].arcDistance(params.lat,params.lon)")
.Lang("painless")
.Params(p => p.Add("lat", lat).Add("lon", lng))))
.Query(query => query
.Bool(b => b.Filter(allfilters)) &&
query.Bool(b => b.Filter(filter => filter
.GeoDistance(geo => geo
.Field(f => f.Location)
.Distance(10, DistanceUnit.Kilometers).Location(lat, lng)
.DistanceType(GeoDistanceType.Arc)))
.Must(m => m
.QueryString(qs => qs
.Fields(f => f
.Fields(f1 => f1.Title))
.Query(searchQuery)))))
.Sort(sort => sort
.GeoDistance(g => g
.Field(f => f.Location)
.Order(SortOrder.Ascending)
.Points(new GeoLocation(lat, lng))
.DistanceType(GeoDistanceType.Arc)
)).Source(sr => sr.IncludeAll()));
var storesArray = stores.Documents?.ToArray();
var arrayCount = 0;
foreach (var fieldValues in stores.Fields)
{
var distance = fieldValues.Value<double>("distance");
storesArray[arrayCount].Distance = distance;
arrayCount ;
}
return storesArray?.ToList();```