public async Task <List <NearestPlace> > GetNearest(string type, double lat, double lng, int distance) { var client = GetClient(); var searchDescriptor = new SearchDescriptor <Place>(); var dumper = new NestDescriptorDumper(client.RequestResponseSerializer); searchDescriptor .Index <Place>() .Query(x => x.Bool(b => b.Must( m => m.Term(t => t.Field(f => f.Types).Value(type)), m => m.GeoDistance(g => g.Distance(new Distance(distance, DistanceUnit.Kilometers)) .Location(lat, lng) .Field(f => f.Geometry.Location)) ) )) .ScriptFields(x => x.ScriptField("distance", s => s.Source($"doc['geometry.location'].arcDistance({lat},{lng})"))) .Source(true) .DocValueFields(d => d.Field(f => f.Name) .Field(f => f.Vicinity) .Field(f => f.Types)) ; var sss = dumper.Dump <SearchDescriptor <Place> >(searchDescriptor); var results = await client.SearchAsync <Place>(searchDescriptor); return(ToNearestPlaces(results.Hits)); }
public async Task <TermVectorsModel> TermVectors(string queryString, double lat, double lng) { var client = GetClient(); var searchDescriptor = new SearchDescriptor <Place>(); var dumper = new NestDescriptorDumper(client.RequestResponseSerializer); #region Search QueryContainerDescriptor <Place> queryContainer = new QueryContainerDescriptor <Place>(); var query = queryContainer.Bool(b => b.Should( q => q.Match(m => m.Field(f => f.Name).Query(queryString)), q => q.Match(m => m.Field(f => f.Vicinity).Query(queryString)) ) ); Func <SortDescriptor <Place>, SortDescriptor <Place> > SortByGeo = (SortDescriptor <Place> s) => s.GeoDistance(g => g.Field(f => f.Geometry.Location) .DistanceType(GeoDistanceType.Arc) .Unit(DistanceUnit.Kilometers) .Order(SortOrder.Ascending) .Points(new GeoLocation(lat, lng))); Func <SortDescriptor <Place>, IPromise <IList <ISort> > > sort = s => SortByGeo(s.Descending(SortSpecialField.Score).Descending(d => d.Rating)); searchDescriptor .Index <Place>() .Query(x => query) .Sort(sort) .ScriptFields(x => x.ScriptField("distance", s => s.Source($"doc['geometry.location'].arcDistance({lat},{lng})"))) .Take(10) .Source(true) ; #endregion var results = await client.SearchAsync <Place>(searchDescriptor); var model = ToSearchPlaces(results.Hits).First(); var term = new TermVectorsDescriptor <Place>(IndexName.From <Place>(), TypeName.From <Place>()); term.Index <Place>() .Id(Id.From(model)) .Fields(tf => tf.Vicinity, tf => tf.Name) ; var sss = dumper.Dump <TermVectorsDescriptor <Place> >(term); var termVectorsResult = await client.TermVectorsAsync <Place>(term); return(ToTermVectorsModel(termVectorsResult, model)); }
public async Task <List <SearchPlace> > Search(string queryString, double lat, double lng, bool descRates) { var client = GetClient(); var searchDescriptor = new SearchDescriptor <Place>(); var dumper = new NestDescriptorDumper(client.RequestResponseSerializer); QueryContainerDescriptor <Place> queryContainer = new QueryContainerDescriptor <Place>(); var query = queryContainer.Bool(b => b.Should( q => q.Match(m => m.Field(f => f.Name).Query(queryString)), q => q.Match(m => m.Field(f => f.Vicinity).Query(queryString)) ) ); Func <SortDescriptor <Place>, SortDescriptor <Place> > SortByGeo = (SortDescriptor <Place> s) => s.GeoDistance(g => g.Field(f => f.Geometry.Location) .DistanceType(GeoDistanceType.Arc) .Unit(DistanceUnit.Kilometers) .Order(SortOrder.Ascending) .Points(new GeoLocation(lat, lng))); Func <SortDescriptor <Place>, IPromise <IList <ISort> > > sort = s => SortByGeo(descRates ? s.Descending(SortSpecialField.Score).Descending(d => d.Rating) : s.Descending(SortSpecialField.Score)); searchDescriptor .Index <Place>() .Query(x => query) .Sort(sort) .ScriptFields(x => x.ScriptField("distance", s => s.Source($"doc['geometry.location'].arcDistance({lat},{lng})"))) .Take(10) .Source(true) ; var sss = dumper.Dump <SearchDescriptor <Place> >(searchDescriptor); var results = await client.SearchAsync <Place>(searchDescriptor); return(ToSearchPlaces(results.Hits)); }
public async Task <object> Aggregation(double lat, double lng) { var client = GetClient(); var searchDescriptor = new SearchDescriptor <Place>(); var dumper = new NestDescriptorDumper(client.RequestResponseSerializer); var maxRating = "maxRating"; var minDistance = "minDistance"; searchDescriptor .Aggregations(aggs => aggs .Max(maxRating, max => max.Field(f => f.Rating)) .Min(minDistance, min => min.Script($"doc['geometry.location'].arcDistance({lat},{lng})")) ); var results = await client.SearchAsync <Place>(searchDescriptor); var sss = dumper.Dump <SearchDescriptor <Place> >(searchDescriptor); return(results.Aggregations); }
public async Task <List <BestPlaceAround> > GetBestPlacesAround(int distance, double lat, double lng, bool descRates, bool openedOnly) { var client = GetClient(); var searchDescriptor = new SearchDescriptor <Place>(); var dumper = new NestDescriptorDumper(client.RequestResponseSerializer); Func <SortDescriptor <Place>, bool, SortDescriptor <Place> > SortByRates = (SortDescriptor <Place> s, bool desc) => desc?s.Descending(d => d.Rating) : s; searchDescriptor .Index <Place>() .Query(x => x.Bool(b => b.Must( m => m.Term(t => t.Field(f => f.OpeningHours.OpenNow).Value(openedOnly)), m => m.GeoDistance(g => g.Distance(new Distance(distance, DistanceUnit.Kilometers)) .Location(lat, lng) .Field(f => f.Geometry.Location)) ) )) .Sort(s => SortByRates(s.GeoDistance(g => g.Field(f => f.Geometry.Location) .DistanceType(GeoDistanceType.Arc) .Unit(DistanceUnit.Kilometers) .Order(SortOrder.Ascending) .Points(new GeoLocation(lat, lng))), descRates)) .ScriptFields(x => x.ScriptField("distance", s => s.Source($"doc['geometry.location'].arcDistance({lat},{lng})"))) .Take(10) .Source(true) ; var sss = dumper.Dump <SearchDescriptor <Place> >(searchDescriptor); var results = await client.SearchAsync <Place>(searchDescriptor); return(ToBestPlacesAround(results.Hits)); }