【问题标题】:How to perform search in RavenDB and ASP.NET MVC?如何在 RavenDB 和 ASP.NET MVC 中执行搜索?
【发布时间】:2016-11-27 01:02:27
【问题描述】:

我正在尝试使用 RavenDB 和 ASP.NET MVC 执行搜索查询。 我希望能够使用名称、美食、城市或州搜索餐厅。 所以我在索引文件夹中创建了索引Restaurants_ByNameRestaurants_ByCuisineRestaurants_ByCityRestaurants_ByState。现在我不确定如何在我的 Search.cshtml 中使用搜索功能

模型 - Restaurant.cs

  public class Restaurant
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Cuisine { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public int Postcode { get; set; }
    public string PhoneNumber { get; set; }
    public string Email { get; set; }
}

索引 - Search.cs

 public class Searching
{
    public class Restaurants_ByName : AbstractIndexCreationTask<Restaurant>
    {
        public Restaurants_ByName()
        {
            Map = restaurants => from restaurant in restaurants
                                 select new
                                 {
                                     restaurant.Name
                                 };
            //Indexes.Add(x = x.Name, FieldIndexing.Analyzed);
        }
    }

    public class Restaurant_ByCuisine : AbstractIndexCreationTask<Restaurant>
    {
        public Restaurant_ByCuisine()
        {
            Map = restaurants => from restaurant in restaurants
                                 select new
                                 {
                                     restaurant.Cuisine
                                 };
            //Indexes.Add(x = x.Cuisine, FieldIndexing.Analyzed);
        }
    }

    public class Restaurant_ByCity : AbstractIndexCreationTask<Restaurant>
    {
        public Restaurant_ByCity()
        {
            Map = restaurants => from restaurant in restaurants
                                 select new
                                 {
                                     restaurant.City
                                 };
            //Indexes.Add(x = x.City, FieldIndexing.Analyzed);
        }
    }

    public class Restaurant_ByState : AbstractIndexCreationTask<Restaurant>
    {
        public Restaurant_ByState()
        {
            Map = restaurants => from restaurant in restaurants
                                 select new
                                 {
                                     restaurant.State
                                 };
            //Indexes.Add(x = x.State, FieldIndexing.Analyzed);
        }
    }

    public Searching(string searchString)
    {
        using (var store = new DocumentStore
        {
            Url = "http://localhost:8080/",
            DefaultDatabase = "foodfurydb"
        })
        {
            store.Initialize();

            using (var session = store.OpenSession())
            {
                // using query
                IList<Restaurant> restaurants = session
                    .Query<Restaurant, Restaurants_ByName>()
                    .Where(x => x.Name == searchString)
                    .ToList();
            }
        }
    }
}

我需要在我的控制器中做一些事情,对吗?

public ActionResult SearchRestaurant()
    {
        using (var store = new DocumentStore
        {
            Url = "http://localhost:8080/",
            DefaultDatabase = "foodfurydb"
        })
        {
            store.Initialize();

            using (var session = store.OpenSession())
            {

            }
        }
        return View();
    }

部分视图 - SearchRestaurant.cshtml

    <div class="row">
    <div class="input-field col s12" id="search-bar">
        <i class="material-icons prefix">search</i>
        <input placeholder="Search by restaurant name / cuisine / location" id="search-restaurant"/>
    </div>
</div>

编辑

我想要实现的是通过输入名称/美食/位置它将返回相关结果

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 search razor ravendb


    【解决方案1】:

    不要为属性创建单独的索引,这样做效率不高。而且你最好一次搜索所有这些。

    索引将如下所示:

    public class Restaurant_Search : AbstractIndexCreationTask<Restaurant, Restaurant_Search.Result>
     {
            public class Result
            {
               public string Query;
            }
    
            public Restaurant_ByCuisine()
            {
                Map = restaurants => from restaurant in restaurants
                                     select new
                                     {
                                         Query = new []
                                         {
                                           restaurant.Cuisine, 
                                           restaurant.State,   
                                           restaurant.City,  
                                           restaurant.Name
                                         }
                                     };
    
                Indexes.Add(x => x.Cuisine, FieldIndexing.Analyzed); // => instead of = (previously)
            }
     }
    

    然后您可以使用以下方式查询它:

    using (var session = store.OpenSession())
    {
       var matches = session.Query<Restaurant_Search.Result, Restaurant_Search>()
          .Search(x => x.Query, "new york indian")
          .OfType<Restaurant>()
          .ToList();
    }
    

    请注意,您现在可以搜索所有这些字段,并且用户可以提供的查询类型更加有趣。

    【讨论】:

    • Indexes.Add(x = x.Cuisine, FieldIndexing.Analyzed); 对于这一行,它出错说它在当前上下文中不存在。我应该创建一个私有对象 x 来修复错误吗?
    • @jenna_3108 在您的代码 sn-p 中您缺少 &gt;。应该是Indexes.Add(x =&gt; x.Cuisine, FieldIndexing.Analyzed);
    猜你喜欢
    • 2011-12-31
    • 1970-01-01
    • 2019-04-28
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    相关资源
    最近更新 更多