You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RavenDB多属性模糊搜索:改用Search方法替代Lucene查询

Refactor to Use RavenDB's Search Method Instead of Raw Lucene Queries

Got it, let's switch your multi-property fuzzy search to use RavenDB's built-in Search method—this will make your code type-safe, easier to maintain, and avoid messy raw Lucene string hacks.

Here's the Refactored Code

I'll cover both the Query API (recommended for most use cases) and the Advanced DocumentQuery approach, since you were using DocumentQuery originally:

Option 1: Using RavenDB's Query API (Type-Safe, Preferred)

// Keep the space escape logic to match your original requirement of treating spaces as literals
var escapedTerm = $"\"{term.Replace(" ", "\\ ")}\"";

var query = session.Query<Order>()
    // Add wildcard-enabled search for each target property
    .Search(x => x.Property1, escapedTerm, options => options.AllowWildcards())
    .Search(x => x.Property2, escapedTerm, options => options.AllowWildcards())
    .Search(x => x.Property3, escapedTerm, options => options.AllowWildcards())
    // Ensure OR logic between all conditions (match at least one property)
    .UsingDefaultOperator(SearchOperator.Or);

Option 2: Using Advanced DocumentQuery (Matching Your Original Approach)

// Preserve the wildcard wrapping and space escape from your original Lucene query
var escapedTerm = $"*{term.Replace(" ", "\\ ")}*";

var query = session.Advanced.DocumentQuery<Order>()
    .Search(x => x.Property1, escapedTerm)
    .OrElse() // Chain OR logic between each search condition
    .Search(x => x.Property2, escapedTerm)
    .OrElse()
    .Search(x => x.Property3, escapedTerm);

Key Improvements & Explanations

  • Type Safety: Using lambda expressions (x => x.Property1) eliminates typos in property names that could break raw Lucene strings.
  • Maintainability: Adding/removing properties later is as simple as adding another .Search() call—no need to edit clunky string concatenation.
  • Preserved Behavior: We kept the space escape logic (term.Replace(" ", "\\ ")) to match your original requirement of treating spaces as literal characters (not word separators). The wildcard wrapping (*...*) ensures fuzzy matching (any occurrence of the term in the property).
  • OR Logic: Both approaches explicitly enforce OR logic, so any entry where at least one property matches the term will be returned—exactly like your original Lucene query.

Bonus: Simplify If Spaces Are Word Separators

If you don't need to treat spaces as literals (i.e., "hello world" should match entries with either "hello" or "world"), you can ditch the escape logic entirely:

var query = session.Query<Order>()
    .Search(x => x.Property1, term, options => options.AllowWildcards())
    .Search(x => x.Property2, term, options => options.AllowWildcards())
    .Search(x => x.Property3, term, options => options.AllowWildcards())
    .UsingDefaultOperator(SearchOperator.Or);

内容的提问来源于stack exchange,提问作者yoliva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 10:09:48