RavenDb 搜索多个字符串属性
RavenDb search over multiple string properties
我正在尝试搜索多个属性。我想获取至少其属性之一包含指定 term 的所有项目。以下代码按预期工作,但我想使用 Search 而不是 Lucene 查询来实现此目的。
var t = Regex.Replace(term, " ", @"\ ");
var query = session.Advanced
.DocumentQuery<Order>()
.Where($"Property1:*{t}* OR Property2:*{t}* OR Property3:*{t}*");
var result = session
.Query<Order>()
.Search(x => x.Property1, "*term*")
.Search(x => x.Property2, "*term*")
.Search(x => x.Property3, "*term*")
.ToList();
阅读文档(select 你的 ravendb 版本):https://ravendb.net/docs/article-page/4.0/csharp/indexes/querying/searching
来自 RavenDB 文档:
"By default, RavenDB attempts to guess and match up the semantics
between terms. If there are consecutive searches, they will be OR
together, otherwise the AND semantic will be used."
还要考虑:
"RavenDB allows you to search by using such queries, but you have to
be aware that leading wildcards drastically slow down searches.
Consider if you really need to find substrings. In most cases, looking
for whole words is enough. There are also other alternatives for
searching without expensive wildcard matches, e.g. indexing a reversed
version of text field or creating a custom analyzer."
在以前的 RavenDB 版本中,有必要在查询中允许使用通配符:
var result = session
.Query<Order>()
.Search(x => x.Property1, "*term*", escapeQueryOptions: EscapeQueryOptions.AllowAllWildcards)
.Search(x => x.Property2, "*term*", escapeQueryOptions: EscapeQueryOptions.AllowAllWildcards)
.Search(x => x.Property3, "*term*", escapeQueryOptions: EscapeQueryOptions.AllowAllWildcards)
.ToList();
@yoliva 评论后更新:
为了实现你想要的,我找到了一个解决方法,它包括用索引定义中的不同字符替换所有空格,当你查询时执行相同的操作。
索引:
public class OrdersIndex : Raven.Client.Documents.Indexes.AbstractIndexCreationTask<Order>
{
public OrdersIndex()
{
Map = orders => from order in orders
select new
{
Property1 = order.Property1.Replace(" ", "-"),
Property2 = order.Property2.Replace(" ", "-"),
Property3 = order.Property3.Replace(" ", "-"),
};
}
}
查询:
var result = session.Query<Order, OrdersIndex>()
.Search(x => x.Property1, "*me product n*".Replace(" ", "-"))
.Search(x => x.Property2, "*me prod*".Replace(" ", "-"))
.Search(x => x.Property3, "*product*".Replace(" ", "-"))
.ToList();
我问这个问题已经有一段时间了,但在过去的几天里,我又重新审视了这个问题。我最终按照下面显示的方式查询,一切正常。
var search = $"\"*{term}*\"";
var qOpt = EscapeQueryOptions.RawQuery;
query = query
.Search(o => o.Property1, search, escapeQueryOptions: qOpt)
.Search(o => o.Property2, search, escapeQueryOptions: qOpt)
.Search(o => o.Property3, search, escapeQueryOptions: qOpt)
.Search(o => o.Property4, search, escapeQueryOptions: qOpt);
我正在尝试搜索多个属性。我想获取至少其属性之一包含指定 term 的所有项目。以下代码按预期工作,但我想使用 Search 而不是 Lucene 查询来实现此目的。
var t = Regex.Replace(term, " ", @"\ ");
var query = session.Advanced
.DocumentQuery<Order>()
.Where($"Property1:*{t}* OR Property2:*{t}* OR Property3:*{t}*");
var result = session
.Query<Order>()
.Search(x => x.Property1, "*term*")
.Search(x => x.Property2, "*term*")
.Search(x => x.Property3, "*term*")
.ToList();
阅读文档(select 你的 ravendb 版本):https://ravendb.net/docs/article-page/4.0/csharp/indexes/querying/searching
来自 RavenDB 文档:
"By default, RavenDB attempts to guess and match up the semantics between terms. If there are consecutive searches, they will be OR together, otherwise the AND semantic will be used."
还要考虑:
"RavenDB allows you to search by using such queries, but you have to be aware that leading wildcards drastically slow down searches.
Consider if you really need to find substrings. In most cases, looking for whole words is enough. There are also other alternatives for searching without expensive wildcard matches, e.g. indexing a reversed version of text field or creating a custom analyzer."
在以前的 RavenDB 版本中,有必要在查询中允许使用通配符:
var result = session
.Query<Order>()
.Search(x => x.Property1, "*term*", escapeQueryOptions: EscapeQueryOptions.AllowAllWildcards)
.Search(x => x.Property2, "*term*", escapeQueryOptions: EscapeQueryOptions.AllowAllWildcards)
.Search(x => x.Property3, "*term*", escapeQueryOptions: EscapeQueryOptions.AllowAllWildcards)
.ToList();
@yoliva 评论后更新:
为了实现你想要的,我找到了一个解决方法,它包括用索引定义中的不同字符替换所有空格,当你查询时执行相同的操作。
索引:
public class OrdersIndex : Raven.Client.Documents.Indexes.AbstractIndexCreationTask<Order>
{
public OrdersIndex()
{
Map = orders => from order in orders
select new
{
Property1 = order.Property1.Replace(" ", "-"),
Property2 = order.Property2.Replace(" ", "-"),
Property3 = order.Property3.Replace(" ", "-"),
};
}
}
查询:
var result = session.Query<Order, OrdersIndex>()
.Search(x => x.Property1, "*me product n*".Replace(" ", "-"))
.Search(x => x.Property2, "*me prod*".Replace(" ", "-"))
.Search(x => x.Property3, "*product*".Replace(" ", "-"))
.ToList();
我问这个问题已经有一段时间了,但在过去的几天里,我又重新审视了这个问题。我最终按照下面显示的方式查询,一切正常。
var search = $"\"*{term}*\"";
var qOpt = EscapeQueryOptions.RawQuery;
query = query
.Search(o => o.Property1, search, escapeQueryOptions: qOpt)
.Search(o => o.Property2, search, escapeQueryOptions: qOpt)
.Search(o => o.Property3, search, escapeQueryOptions: qOpt)
.Search(o => o.Property4, search, escapeQueryOptions: qOpt);