public ElasticsearchOdbBackend(string elasticsearchUrl, string indexName)
        {
            _indexName = indexName;

            client = new ElasticsearchRestClient(elasticsearchUrl)
            {
                DefaultIndexName = indexName
            };

            // TODO mappings
        }
        public async void Can_add_get_and_delete()
        {
            var client = new ElasticsearchRestClient("http://*****:*****@"foo\bar" }, "1111", "test", "test").Wait();
            client.Refresh("test").Wait();

            var getTask = client.Get<File>("1111", "test", "test");
            getTask.Wait();
            Assert.True(getTask.IsCompleted);

            var response = getTask.Result;
            Console.WriteLine("Found document {0}", response._id);
            Assert.Equal("jpg", response._source.FileType);
            Assert.Equal(@"foo\bar", response._source.Path);
            client.Delete("1111", "test", "test").Wait();
        }
        public async void Can_add_get_and_delete()
        {
            var client = new ElasticsearchRestClient("http://*****:*****@"foo\bar"
            }, "1111", "test", "test").Wait();
            client.Refresh("test").Wait();

            var getTask = client.Get <File>("1111", "test", "test");

            getTask.Wait();
            Assert.True(getTask.IsCompleted);

            var response = getTask.Result;

            Console.WriteLine("Found document {0}", response._id);
            Assert.Equal("jpg", response._source.FileType);
            Assert.Equal(@"foo\bar", response._source.Path);
            client.Delete("1111", "test", "test").Wait();
        }
示例#4
0
        public static SearchResponse <Fruit> FindFruit(string name, string quantity)
        {
            var client = new ElasticsearchRestClient("http://localhost:9200");
            SearchResponse <Fruit> result;

            if (!string.IsNullOrEmpty(name) && name.ToLowerInvariant() == "banana")
            {
                Thread.Sleep(2000);
            }

            if (!string.IsNullOrEmpty(quantity) && quantity == "10")
            {
                JsonLogger.LogErrorObject(new { FruitName = name, Quantity = quantity, EventName = "FruitSearchError" });
                throw new Exception("Something went horribly wrong!");
            }

            if (!string.IsNullOrEmpty(quantity) && !string.IsNullOrEmpty(name))
            {
                var requestBody = new
                {
                    size   = 10000,
                    filter = new
                    {
                        @bool = new
                        {
                            must = new object[]
                            { new { term = new { Quantity = quantity } },
                              new { term = new { Name = name.ToLowerInvariant() } } }
                        }
                    },
                    aggs = new
                    {
                        quantityAggs = new
                        {
                            terms = new { field = "Quantity" }
                        },
                        nameAggs = new
                        {
                            terms = new { field = "Name" }
                        }
                    }
                };
                result = client.Search <Fruit>(requestBody, "fruit", null);
            }
            else if (string.IsNullOrEmpty(quantity) && !string.IsNullOrEmpty(name))
            {
                var requestBody = new
                {
                    size   = 10000,
                    filter = new { term = new { Name = name.ToLowerInvariant() } },
                    aggs   = new
                    {
                        quantityAggs = new
                        {
                            terms = new { field = "Quantity" }
                        },
                        nameAggs = new
                        {
                            terms = new { field = "Name" }
                        }
                    }
                };

                result = client.Search <Fruit>(requestBody, "fruit", null);
            }
            else if (!string.IsNullOrEmpty(quantity) && string.IsNullOrEmpty(name))
            {
                var requestBody = new
                {
                    size   = 10000,
                    filter = new { term = new { Quantity = quantity } },
                    aggs   = new
                    {
                        quantityAggs = new
                        {
                            terms = new { field = "Quantity" }
                        },
                        nameAggs = new
                        {
                            terms = new { field = "Name" }
                        }
                    }
                };
                result = client.Search <Fruit>(requestBody, "fruit", null);
            }
            else
            {
                var requestBody = new
                {
                    size = 10000,
                    aggs = new
                    {
                        quantityAggs = new
                        {
                            terms = new { field = "Quantity" }
                        },
                        nameAggs = new
                        {
                            terms = new { field = "Name" }
                        }
                    }
                };
                result = client.Search <Fruit>(requestBody, "fruit", null);
            }

            return(result);
        }
示例#5
0
        public HomeModule(ElasticsearchRestClient client)
        {
            const int pageSize = 10;

            Get["/search", true] = async (p, ct) =>
                       {
                           var vm = new HomeViewModel();
                           ViewBag.Query = string.Empty;

                           string q = Request.Query.q;
                           int page = 1;

                           if (q != null)
                           {
                               ViewBag.Query = q;
                               var filter = new {missing = new {field = "redirect"}};

                               var analyzer = "hebrew";
                               SearchType searchType;
                               if (Enum.TryParse(Request.Query.type, true, out searchType))
                               {
                                   switch (searchType)
                                   {
                                       case SearchType.Morphological:
                                           analyzer = "hebrew_query";
                                           break;
                                       case SearchType.MorphologicalOptimized:
                                           analyzer = "hebrew_query_light";
                                           break;
                                       case SearchType.Exact:
                                       case SearchType.Naive:
                                           analyzer = "hebrew_exact";
                                           break;
                                       default:
                                           analyzer = "hebrew_query";
                                           break;
                                   }
                               }

                               var query = new {multi_match = new {query = q, fields = new[] {"title", "text"}, analyzer = analyzer}};

                               var results = client.Search<ContentPage>(new
                                                          {
                                                              query = new { filtered = new
                                                              {
                                                                  query = query,
                                                                  filter = filter,
                                                              }},

                                                              highlight = new
                                                                          {
                                                                              fields = new
                                                                                       {
                                                                                           title = new {number_of_fragments = 0 },
                                                                                           text = new {},
                                                                                       },
                                                                              pre_tags = new[] {"<b>"}, post_tags = new[]{"</b>"},

                                                                          },

                                                              aggregations = new {categories = new
                                                                                  {
                                                                                      terms = new
                                                                                              {
                                                                                                  field = "link",
                                                                                                  size = 50,
                                                                                              }
                                                                                  }},

                                                              _source = new[] { "title", "categories", "author" },

                                                              from = pageSize * (page - 1),
                                                              size = pageSize,
                                                          }
                                                          , "hebrew-wikipedia-20140610", "contentpage");

                               // TODO
            //                               if (results.status != HttpStatusCode.OK)
            //                               {
            //                                   vm.ErrorString =
            //                               }

                               vm.TotalResults = results.hits.total;
                               vm.Results = new List<SearchResult>();
                               foreach (var hit in results.hits.hits)
                               {
                                   var r = new SearchResult();
                                   if (hit.highlight.ContainsKey("title"))
                                   {
                                       r.Title = new NonEncodedHtmlString(String.Join("... ", hit.highlight["title"]));
                                   }
                                   else
                                   {
                                       r.Title = new NonEncodedHtmlString(hit._source.Title);
                                   }

                                   if (hit.highlight.ContainsKey("text"))
                                   {
                                       r.Snippets = new NonEncodedHtmlString(String.Join("... ", hit.highlight["text"]));
                                   }

                                   r.Categories = hit._source.Categories;
                                   r.Id = hit._id;

                                   vm.Results.Add(r);
                               }

                               vm.CategoryFacets = results.aggregations["categories"].Buckets.Select(x => new TermAndCount
                                                                                                            {
                                                                                                                Term = x.Key,
                                                                                                                Count = x.DocCount,
                                                                                                            });
                           }

                           return View["Search", vm];
                       };

            Get["/"] = p =>
                       {
                           var vm = new HomeViewModel();

                           return View["Main", vm];
                       };

            Get["/about"] = p => View["About"];

            Get["/contact"] = p => View["Contact"];
        }