ravendb - RavenDB查询建议多个单词

标签 ravendb

我有一些代码正在使用以下索引搜索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/

相关文章:

c# - IDocumentSession.SaveChanges() 与事务

c# - 如何搜索庞大的非文本数据集?

c# - 我应该在 RavenDB 上使用 LINQ 查询吗?

RavenDB 查询 ID

ravendb - 从 NServiceBus 和 ServiceInsight 删除消息

nosql - RavenDB:子文档的 ID 生成

powershell - 如何使用 powershell 删除 RavenDB?

Raven DB 的 Java 客户端 API

c# - 如何同步 nosql db (ravendb) 中的更改

ravendb - 在 RavenDb 中使用私有(private)支持字段反序列化 IEnumerable