首页 > 解决方案 > 弹性搜索中如何根据 createdDate 等字段进行搜索

问题描述

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.jselasticsearchelastic-stack

解决方案


您需要使用以下方式组合这两个约束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"
            }
          }
        }
      ]
    }
  }
}
})

推荐阅读