【问题标题】:How to search based on createdDate and other fields in elastic search弹性搜索中如何根据 createdDate 等字段进行搜索
【发布时间】:2020-02-04 13:37:56
【问题描述】:
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
     host: 'ABC',
     log: 'trace'
});

client.search({
index: 'allevents_production',
"body": {
    "query": {
        "query_string": {
            "default_field": "clientname",
            "query": 'ser'
        },
        "range" : {
            "createddate" : {
                "gte" : "now-1d/d",
                "lt" :  "now/d"
            }
        }
    }
}
})

我想搜索 client namecreateddate 等多个字段。我在正文部分传递了这些字段。

它返回一个错误。请帮助我在哪里做错了。谢谢!

【问题讨论】:

  • 您能否将您的映射和详细错误添加到您的问题中? :)

标签: node.js elasticsearch elastic-stack


【解决方案1】:

您需要使用bool/must/filter 组合这两个约束:

client.search({
index: 'allevents_production',
"body": {
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "default_field": "clientname",
            "query": "ser"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "createddate": {
              "gte": "now-1d/d",
              "lt": "now/d"
            }
          }
        }
      ]
    }
  }
}
})

【讨论】:

  • 返回错误{ "error": { "root_cause": [ { "type": "parsing_exception", "reason": "[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]", "line": 12, "col": 5 } ], "type": "parsing_exception", "reason": "[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]", "line": 12, "col": 5 }, "status": 400 }
  • 复制/粘贴错误,抱歉,已修复。
猜你喜欢
  • 2021-01-24
  • 1970-01-01
  • 2020-06-14
  • 1970-01-01
  • 2012-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多