node.js - 如何让 ElasticSearch 支持查询的非点扩展符号?
问题描述
[弹性搜索 5.5.1]
我动态构建弹性搜索查询(在 js/node.js 中使用 npm elasticsearch),我发现这样做的部分痛苦是我必须在分配之前初始化深度嵌套的对象(显然)。为了简化此代码,我创建了一个辅助函数,如果它们不存在,则自动初始化所有中间对象。
function esqAssign(esq, keyPath, value) { }
let esq = {};
esqAssign(esq, 'bool.must.range.timestamp.gte', <timestamp>)
==>
esq = { bool: { must: { range { timestamp: { gte: <timestamp> }}}}}}
这很好用,除非我有一个使用点扩展器语法的字段,例如:
esqAssign(esq, 'bool.filter.term.myfield.id', <id>)
=>
esq = { bool: { filter: { term: { myfield: { id: <id> }}}}}
当我进行实际查询时失败。但是,这有效
esq = { bool: { filter: { term: { "myfield.id": <id> }}}}}
我的索引架构包含:
...
"myfield": {
"properties": {
"id": {
"type": "long"
},
}
}
为了保持我的代码简单而不是函数签名esqAssign(esq, keyName, prop, value)
,我想避免使用点扩展符号。
如果我从 curl 的角度来看这个,我想知道为什么会这样:
curl localdev:9200/myindex/mytype/_search -XPOST -d '{ "query": { "bool": { "filter": { "term": { "myfield.id": 1234 } } } } }'
这不会:
curl localdev:9200/ myindex/mytype /_search -XPOST -d '{ "query": { "bool": { "filter": { "term": { "myfield": { "id": 1234 } } } } } }'
{
"error":{
"root_cause":[
{
"type":"parsing_exception",
"reason":"[term] query does not support [id]",
"line":1,
"col":69
}
],
"type":"parsing_exception",
"reason":"[term] query does not support [id]",
"line":1,
"col":69
},
"status":400
}
关于如何让 ES 接受 { myfield: { id ...} 而不是要求 { myfield.id: ...} 的任何想法?
解决方案
推荐阅读
- android - java.lang.String 类型的值无法转换为 JSONObject - Android
- javascript - 如何设置 JavaScript ip 记录器以使用 EmailJS?
- python - Python中的自定义析构函数
- data-structures - 为什么 AddAfter() 有恒定的时间?
- azure - 如何及时获取 Azure 应用服务的实例数量
- c# - C# - VS Code - 启动:程序...不存在
- python - 如何从给定范围生成固定随机数,在每次执行中产生相同的输出
- vba - Excel查找空白单元格
- java - Android studio 蓝牙连接 App 崩溃
- javascript - 三元运算符在反应js渲染功能中不起作用