IRavenQueryable<T> 用于 space-拆分搜索词和整个搜索字符串
IRavenQueryable<T> for both space-splitted search-terms and complete search-string as whole
在我们自己的RavenQueryableExtensions
class中我们有以下方法:
public static IRavenQueryable<T> SearchMultiple<T>(this IRavenQueryable<T> self,
Expression<Func<T, object>> fieldSelector, string queries,
decimal boost = 1, SearchOptions options = SearchOptions.Or)
{
if (String.IsNullOrEmpty(queries)) throw new ArgumentNullException("queries");
// All gabs of more than a single space (like tabs, two spaces, etc.)
// are reduced to a single space
var newQueries = Regex.Replace(queries, @"\s{2,}", " ");
//newQueries = SyncShared.ReplacePostcode(newQueries); // not important for this question
// The complete search-string is splitted into seperated search-terms
var searchValues = newQueries.Split(' ');
return self.SearchMultiple(fieldSelector, searchValues, boost, options);
}
public static IRavenQueryable<T> SearchMultiple<T>(this IRavenQueryable<T> self,
Expression<Func<T, object>> fieldSelector, IEnumerable<string> queries,
decimal boost = 1, SearchOptions options = SearchOptions.Or)
{
if (queries == null) throw new ArgumentNullException("queries");
return queries.Aggregate(self, (current, query) => current.Search(fieldSelector, query + "* ", boost, options, EscapeQueryOptions.AllowPostfixWildcard));
}
这会创建一个搜索查询,其中包含 searchValues 数组和 SearchOptions
参数中的所有松散搜索词。如果这总是 SearchOptions.Or
,我可以将完整的搜索字符串添加到这个数组中,一切都会好起来的,但是由于我们有时也使用 SearchOptions.And
作为参数,所以我遇到了问题。
我想要的是匹配所有单个搜索词组合的所有结果,或者整个搜索词的完整结果。
例如:假设我有以下搜索词:"This is a search term"
- 现在我们将得到以下查询:
"This" AND "is" AND "a" AND "search" AND "term"
- 然而我想要的是:
("This" AND "is" AND "a" AND "search" AND "term") OR "This is a search term"
问题主要在于我们对多个 classes 使用虚拟和动态查询,否则我们很可能只是将完整的 Search-term 添加到 Map-Index 中,就像 [=20] =].
你真的不需要打扰。
除非我有严重的脑残,否则它不会改变您的结果,因为 OR 条件将始终与前面的 AND 链匹配。
在我们自己的RavenQueryableExtensions
class中我们有以下方法:
public static IRavenQueryable<T> SearchMultiple<T>(this IRavenQueryable<T> self,
Expression<Func<T, object>> fieldSelector, string queries,
decimal boost = 1, SearchOptions options = SearchOptions.Or)
{
if (String.IsNullOrEmpty(queries)) throw new ArgumentNullException("queries");
// All gabs of more than a single space (like tabs, two spaces, etc.)
// are reduced to a single space
var newQueries = Regex.Replace(queries, @"\s{2,}", " ");
//newQueries = SyncShared.ReplacePostcode(newQueries); // not important for this question
// The complete search-string is splitted into seperated search-terms
var searchValues = newQueries.Split(' ');
return self.SearchMultiple(fieldSelector, searchValues, boost, options);
}
public static IRavenQueryable<T> SearchMultiple<T>(this IRavenQueryable<T> self,
Expression<Func<T, object>> fieldSelector, IEnumerable<string> queries,
decimal boost = 1, SearchOptions options = SearchOptions.Or)
{
if (queries == null) throw new ArgumentNullException("queries");
return queries.Aggregate(self, (current, query) => current.Search(fieldSelector, query + "* ", boost, options, EscapeQueryOptions.AllowPostfixWildcard));
}
这会创建一个搜索查询,其中包含 searchValues 数组和 SearchOptions
参数中的所有松散搜索词。如果这总是 SearchOptions.Or
,我可以将完整的搜索字符串添加到这个数组中,一切都会好起来的,但是由于我们有时也使用 SearchOptions.And
作为参数,所以我遇到了问题。
我想要的是匹配所有单个搜索词组合的所有结果,或者整个搜索词的完整结果。
例如:假设我有以下搜索词:"This is a search term"
- 现在我们将得到以下查询:
"This" AND "is" AND "a" AND "search" AND "term"
- 然而我想要的是:
("This" AND "is" AND "a" AND "search" AND "term") OR "This is a search term"
问题主要在于我们对多个 classes 使用虚拟和动态查询,否则我们很可能只是将完整的 Search-term 添加到 Map-Index 中,就像 [=20] =].
你真的不需要打扰。
除非我有严重的脑残,否则它不会改变您的结果,因为 OR 条件将始终与前面的 AND 链匹配。