Ejemplo n.º 1
0
        public void Export_ReturnsValidJson()
        {
            var query = new ConjunctionQuery(
                new TermQuery("hotel").Field("type")
                );

            var result = query.Export().ToString(Formatting.None);

            var expected = JsonConvert.SerializeObject(new
            {
                query = new
                {
                    boost     = 0.0,
                    conjuncts = new[]
                    {
                        new
                        {
                            query = new
                            {
                                term          = "hotel",
                                boost         = 0.0,
                                field         = "type",
                                prefix_length = 0,
                                fuzziness     = 0
                            }
                        }
                    }
                }
            }, Formatting.None);

            Assert.AreEqual(expected, result);
        }
        public void Export_ReturnsValidJson()
        {
            var query = new ConjunctionQuery(
                new TermQuery("hotel").Field("type")
            );

            var result = query.Export().ToString(Formatting.None);

            var expected = JsonConvert.SerializeObject(new
            {
                query = new
                {
                    boost = 0.0,
                    conjuncts = new[]
                    {
                        new
                        {
                            query = new
                            {
                                term = "hotel",
                                boost = 0.0,
                                field = "type",
                                prefix_length = 0,
                                fuzziness = 0
                            }
                        }
                    }
                }
            }, Formatting.None);

            Assert.AreEqual(expected, result);
        }
        public void Export_ReturnsValidJson()
        {
            var query = new ConjunctionQuery(
                new TermQuery("hotel").Field("type")
                );

            var result = query.Export().ToString(Formatting.None);

            var expected = JsonConvert.SerializeObject(new
            {
                conjuncts = new[]
                {
                    new
                    {
                        term  = "hotel",
                        field = "type"
                    }
                }
            }, Formatting.None);

            Assert.Equal(expected, result);
        }
Ejemplo n.º 4
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)));
        }