【问题标题】:Using Split in RavenDB index在 RavenDB 索引中使用拆分
【发布时间】:2016-07-12 23:54:19
【问题描述】:

我想在索引中使用几个正则表达式。这是一个简化的例子:

Map =
    books =>
      books.Select(x => new {Sentences = Regex.Split(x.Description, "<br>")})
        .Select(x => new {Results = x.Sentences.Where(y => Regex.IsMatch(y, "foo"))})
        .Where(x => x.Results.Any())
        .Select(x => new {});

但是,会抛出以下异常:

An exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll but was not handled in user code

Additional information: 'System.Array' does not contain a definition for 'Where'

我试过 .AsEnumerable() 但无济于事。这个可以吗?

【问题讨论】:

  • RavenDB 为您提供全文索引,可以将句子拆分为单词。只列出句子并让 Lucene 引擎为您做这件事会更有用吗?

标签: ravendb


【解决方案1】:

要解决此问题,您需要在要关闭的对象上调用 .AsEnumerable()。

所以不要使用 Regex.Split(...).Select(...),而是使用 Regex.Split(...).AsEnumerable().Select(...)

RavenDB 依赖于一个知道如何映射常用扩展方法的内部类。

但是,有时它可能无法动态解析扩展方法。为了帮助动态解析,调用 .AsEnumerable(),然后会找到扩展方法。

您的代码应如下所示:

Map =
    books =>
      books.Select(x => new {Sentences = Regex.Split(x.Description, "<br>")})
        .AsEnumerable()
        .Select(x => new {Results = x.Sentences.Where(y => Regex.IsMatch(y, "foo"))})
        .Where(x => x.Results.Any())
        .Select(x => new {});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    相关资源
    最近更新 更多