RavenDB 查询建议多个单词 [英] RavenDB Query Suggest Multiple Words

查看:40
本文介绍了RavenDB 查询建议多个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码正在使用以下索引搜索 RavenDB 数据库:

I have some code that is searching a RavenDB database with the following index:

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);
    }
}

如果我这样质疑(草莓蛋白都拼错了):

If I query against this like this (both strawberry protein are spelt wrong):

var query = RavenSession.Query<Products_Search.Result, Products_Search>()
                        .Where(x => x.Query == "strawbery protien");

var suggestions = query.Suggest().Suggestions.Take(5)

我希望建议是草莓蛋白",而不是草莓"和另一个蛋白质".RavenDB 可以做到这一点吗?

I would like the suggestions be be "strawberry protein" not one of "strawberry" and another of "protein". Is this possible with RavenDB?

推荐答案

我必须做类似的事情,我使用 LuceneQuery 语法来实现它.我正在使用 OR 运算符,但您会想要使用 AND 运算符.

I have to do something similar and I use the LuceneQuery syntax to achieve it. I am using the OR operator but you will want to use the AND operator.

索引

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 查询建议多个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆