首页 > 解决方案 > 如何让 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: ...} 的任何想法?

标签: node.jselasticsearchelasticsearch-5

解决方案


推荐阅读