Nest - 如何用多个子句编写跨度近查询?
Nest - how to write a span near query with multiple clauses?
我想使用 NEST 编写查询,如下所述:
https://www.elastic.co/guide/en/elasticsearch/reference/1.7/query-dsl-span-near-query.html
我有一组值用作 SpanTerm 子句。集合大小因查询而异。
如果集合大小有限我可以这样做:
var sn = q.SpanNear(snr => snr.Clauses(c => c.SpanTerm(), c => c.SpanTerm(), ...))
如何动态执行(未知子句算在内)?
您可以使用此扩展方法:
public static class SpanNearQueryDescriptorExtensions
{
public static void SpanTermClauses<T>(this SpanNearQueryDescriptor<T> descriptor, Expression<Func<T, object>> field, string[] terms)
where T : class
{
descriptor.Clauses(terms
.Select(t => new Func<SpanQuery<T>, SpanQuery<T>>(query => query.SpanTerm(field, t)))
.ToArray());
}
}
用法:
var terms = new[] {"term1", "term2"};
client.Search<Docuemnt>(s => s
.Query(q => q
.SpanNear(sp => sp
.Slop(12)
.SpanTermClauses(f => f.Title, terms))));
希望对您有所帮助。
我想使用 NEST 编写查询,如下所述: https://www.elastic.co/guide/en/elasticsearch/reference/1.7/query-dsl-span-near-query.html
我有一组值用作 SpanTerm 子句。集合大小因查询而异。
如果集合大小有限我可以这样做:
var sn = q.SpanNear(snr => snr.Clauses(c => c.SpanTerm(), c => c.SpanTerm(), ...))
如何动态执行(未知子句算在内)?
您可以使用此扩展方法:
public static class SpanNearQueryDescriptorExtensions
{
public static void SpanTermClauses<T>(this SpanNearQueryDescriptor<T> descriptor, Expression<Func<T, object>> field, string[] terms)
where T : class
{
descriptor.Clauses(terms
.Select(t => new Func<SpanQuery<T>, SpanQuery<T>>(query => query.SpanTerm(field, t)))
.ToArray());
}
}
用法:
var terms = new[] {"term1", "term2"};
client.Search<Docuemnt>(s => s
.Query(q => q
.SpanNear(sp => sp
.Slop(12)
.SpanTermClauses(f => f.Title, terms))));
希望对您有所帮助。