RavenDB Query Suggest Multiple Words

杀马特。学长 韩版系。学妹 提交于 2019-12-06 00:09:45

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.

The Index

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

The Query

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

ilivewithian,

We build the suggestions based on the terms that we use. You can index the Query field twice, once using Analyzed (in which case it breaks it into words) and once using default, in which case it uses the full term. That might give you what you want.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!