2016-10-17 59 views
0

TL; DR如何在RavenDB中運行完全等效的.Where(x => x.Name.Contains(value))查詢,即使value字符串中包含空格(白色字符)?RavenDB:使用空格搜索,行爲類似於String.Contains


我們在用LINQ查詢RavenDB。我們想用String.Contains類似的約束來查詢。由於RavenDb防止.Where(x => x.Name.Contains(value))查詢,我們使用LinqExtensions.Search擴展方法,以下文檔中的例子: query = query.Search(t => t.Name, $"*{value}*", escapeQueryOptions: EscapeQueryOptions.AllowAllWildcards, options: SearchOptions.And);

不幸的是,當搜索詞包含空格字符如預期,這並不工作,很可能是因爲這個:https://github.com/ravendb/ravendb/blob/f3b5f3a186d07776bf38bf9effab4d7d75d5c647/Raven.Client.Lightweight/Document/AbstractDocumentQuery.cs#L1759

我們一直試圖手動空間逃逸,沒有成功至今:

var value = RavenQuery.Escape(filter.NameContains).Replace(" ", @"\ ");
query = query.Search(t => t.Name, $"*{value}*", escapeQueryOptions: EscapeQueryOptions.AllowAllWildcards, options: SearchOptions.And);

回答

2

您需要設置現場的210選項Analyzed

public class YourObject_ByName : AbstractIndexCreationTask<YourObject> 
{ 
    public YourObject_ByName() 
    { 
     Map = objs => objs .Select(x => new { x.Name }); 

     Indexes.Add(x => x.Name, FieldIndexing.Analyzed); 
    } 
} 

然後你就可以查詢使用DocumentQuery

session.Advanced.DocumentQuery<YourObject, YourObject_ByName>() 
    .Where("(Name: *term*)") 
    .ToList();