IRavenQueryable <t>用于空格分割搜索术语和完整搜索字符串作为整体</t>

时间:2015-02-04 15:07:49

标签: c# search full-text-search ravendb iqueryable

在我们自己的RavenQueryableExtensions课程中,我们有以下方法:

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-array中的所有松散搜索词和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"

问题主要在于我们对多个类使用虚拟和动态查询,否则我们很可能只是将完整的搜索项添加到地图索引中,如this SO-answer中所示。

1 个答案:

答案 0 :(得分:1)

你真的不需要打扰。

除非我有一个严肃的脑力,否则它不会改变你的结果,因为OR条件将始终与前面的AND链相匹配。