我有一个 ElasticDatastore我需要能够根据业务逻辑中的任意标准返回文档列表。
该方法目前看起来像这样......
private ElasticContext esLinq;
private void initialise() {
esLinq = new ElasticContext(new ElasticConnection(endpoint, index: index));
}
public IEnumerable<Entities.Item> Items(Func<Entities.Item, bool> predicate) {
var ret = esLinq.Query<Item>().Where(predicate);
return ret;
}
我是这样调用它的
var newItems = dataStore.Items(x=>
x.SomeField == node.SomeValue.ToString()
& (x.AssignedTo == null
| x.AssigmentExpires < DateTime.UtcNow)
).ToList();
就目前而言,该方法返回零结果。通过使用 elastic head(和 curl),我可以验证索引中是否存在符合指定条件的文档。
我的第一个猜测是 EsLinq 预期的字段名称不正确(大小写...索引是使用 nest 构建的)。但是,我找不到检查 EsLinq 实际发送到 elasticsearch 的内容的好方法。
我可以
esLinq.Query<Item>().ToElasticSearchQuery();
然后获取表示(空白)查询的 json 字符串,然而,...Query<Item>().Where(predicate)返回 IEnumerable<Item>没有 ToElasticSearchQuery扩展名。
编译器接受
ret.AsQueryable().ToElasticSearchQuery()
但我得到一个 ArgumentException在运行时:
Query must be of type IElasticQuery<> to call ToElasticSearchQuery()
如何检查 EsLinq 发送到 elasticsearch 的查询,以便诊断我遇到的问题?
请您参考如下方法:
如果您安装了 Fiddler,您可以看到发送和返回的确切 HTTP。或者你可以使用:
- .ToQueryInfo() 方法并检查 .Body 和 .Uri 属性
- ElasticContext 上的 ILog 接口(interface),用于捕获原始查询和响应
我想问题是您的 CLR 对象和文档字段名称之间的映射 - 默认情况下 ElasticMapping 类驼峰式字段名称并尝试复数类型名称。您可以使用构造函数开关关闭它,或者根据您自己的特定约定将其子类化。
此外,我认为您的查询应该说 ||对于 OR 和 && 对于 AND - 支持这些 bool 运算符,但 |和 & 按位运算不是。
