【发布时间】: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