我有一些代码正在使用以下索引搜索RavenDB数据库:
public class Products_Search :
AbstractIndexCreationTask<Product, Products_Search.Result>
{
public class Result
{
public string Query { get; set; }
}
public Products_Search()
{
Map = products =>
from product in products
select new
{
Query = new
{
Categories = product.Categories.Boost(5),
Brands = product.Brands.Boost(8),
product.Description,
Name = product.Name.Boost(10),
product.SKU
},
product.Price
};
Index(x => x.Query, FieldIndexing.Analyzed);
}
}
如果我这样查询(两种草莓蛋白拼写错误):
var query = RavenSession.Query<Products_Search.Result, Products_Search>()
.Where(x => x.Query == "strawbery protien");
var suggestions = query.Suggest().Suggestions.Take(5)
我想建议是“草莓蛋白”而不是“草莓”之一,而又是“蛋白质”。 RavenDB有可能吗?
最佳答案
我必须做类似的事情,并且我使用LuceneQuery
语法来实现它。我正在使用OR
运算符,但您将要使用AND
运算符。
索引
public class ContentItem_BySearchParam : AbstractIndexCreationTask<ContentItem>
{
public ContentItem_BySearchParam()
{
Map = contentItems =>
from contentItem in contentItems
select new {contentItem.Title, contentItem.Description, contentItem.Keywords};
Store("Title", FieldStorage.Yes);
Index("Title", FieldIndexing.Analyzed);
Store("Description", FieldStorage.Yes);
Index("Description", FieldIndexing.Analyzed);
Store("Keywords", FieldStorage.Yes);
Index("Keywords", FieldIndexing.Analyzed);
}
}
查询
public SearchResults GetResults(IDocumentSession db, params string[] searchTerms)
{
var query =
GetLuceneQuery("Title", searchTerms) + " OR " +
GetLuceneQuery("Description", searchTerms) + " OR " +
GetLuceneQuery("Keywords", searchTerms);
var results = db
.Advanced
.LuceneQuery<ContentItem, RavenIndexes.ContentItem_BySearchParam>()
.Where(query)
.ToList();
.... do other stuff
}
private string GetLuceneQuery(string field, string[] terms, string searchOperator = "")
{
var join = " " + searchOperator;
var prefix = field + ":(" + searchOperator;
return prefix + String.Join(@join, terms) + ")";
}
关于ravendb - RavenDB查询建议多个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10935048/