public async Task <IEnumerable <GetUpcomingEventsQueryResult> > GetUpcomingEvents(GetUpcomingEventsQuery getUpcomingEventsQuery)
        {
            var geoDistanceQuery = new GeoDistanceQuery();

            geoDistanceQuery.Field("location");
            geoDistanceQuery.Latitude(getUpcomingEventsQuery.Latitude);
            geoDistanceQuery.Longitude(getUpcomingEventsQuery.Longitude);
            geoDistanceQuery.Distance($"{getUpcomingEventsQuery.Radius}km");

            var statusMatchQuery = new MatchQuery(EventStatuses.ACTIVE);

            statusMatchQuery.Field("status");

            var conjunctionQuery = new ConjunctionQuery(geoDistanceQuery, statusMatchQuery);

            if (!string.IsNullOrEmpty(getUpcomingEventsQuery.Keywords))
            {
                var subjectMatchQuery = new MatchQuery(getUpcomingEventsQuery.Keywords).Fuzziness(1);

                conjunctionQuery.And(subjectMatchQuery);
            }

            var searchParams = new SearchParams()
                               .Fields("*")
                               .Limit(10)
                               .Timeout(TimeSpan.FromMilliseconds(10000));

            var searchQuery = new SearchQuery
            {
                Query        = conjunctionQuery,
                Index        = "idx_geo_events",
                SearchParams = searchParams
            };

            var queryResult = await _eventsBucket.QueryAsync(searchQuery);

            var result = new List <GetUpcomingEventsQueryResult>();

            foreach (var hit in queryResult.Hits)
            {
                result.Add(new GetUpcomingEventsQueryResult
                {
                    EventId     = Guid.Parse(hit.Id),
                    Subject     = hit.Fields["subject"],
                    UrlKey      = hit.Fields["urlKey"],
                    Description = hit.Fields["description"],
                    Date        = DateTimeOffset.Parse(hit.Fields["date"].ToString())
                });
            }

            return(result);
        }
Ejemplo n.º 2
0
        public HttpResponseMessage FindHotel(string description = null, string location = null)
        {
            var query = new ConjunctionQuery(
                new TermQuery("hotel").Field("type")
                );

            if (!string.IsNullOrEmpty(description) && description != "*")
            {
                query.And(new DisjunctionQuery(
                              new PhraseQuery(description).Field("name"),
                              new PhraseQuery(description).Field("description")
                              ));
            }

            if (!string.IsNullOrEmpty(location) && location != "*")
            {
                query.And(new DisjunctionQuery(
                              new PhraseQuery(location).Field("address"),
                              new PhraseQuery(location).Field("city"),
                              new PhraseQuery(location).Field("state"),
                              new PhraseQuery(location).Field("country")
                              ));
            }

            var search = new SearchQuery();

            search.Index = "hotel";
            search.Query = query;
            search.Limit(100);

            var queryJson = query.Export().ToString(Formatting.None);
            var hotels    = new List <dynamic>();

            var result = _bucket.Query(search);

            foreach (var row in result)
            {
                var fragment = _bucket.LookupIn <dynamic>(row.Id)
                               .Get("name")
                               .Get("description")
                               .Get("address")
                               .Get("city")
                               .Get("state")
                               .Get("country")
                               .Execute();

                var address = string.Join(", ", new[]
                {
                    fragment.Content <string>("address"),
                    fragment.Content <string>("city"),
                    fragment.Content <string>("state"),
                    fragment.Content <string>("country")
                }.Where(x => !string.IsNullOrEmpty(x)));

                hotels.Add(new
                {
                    name        = fragment.Content <string>("name"),
                    description = fragment.Content <string>("description"),
                    address     = address
                });
            }

            return(Request.CreateResponse(new Result(hotels, queryJson)));
        }