node.js - 弹性搜索中如何根据 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 name和createddate。我在正文部分通过了这些字段。
它返回一个错误。请帮助我在哪里做错了。谢谢!
解决方案
您需要使用以下方式组合这两个约束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"
}
}
}
]
}
}
}
})
推荐阅读
- android - react native FlatList水平滚动不流畅
- javascript - 如何将视频(使用 opencv-python)从 python 函数返回到 html?
- java - MVC中的搜索功能
- arrays - 如何在该特定索引的数组中分配值?
- java - 使用通用参数会引发错误
- javascript - 嵌套 for 循环。第二个循环不识别第一个循环变量
- javascript - 如何使用我正在使用的代码避免除法计算输出余数,即 4.333333
- javascript - 像在 Android 中一样反应图形导航
- python - 从 Python/Pandas 导入数据帧时,SQL 会产生常量错误:SQL 错误 [42P01]:错误:关系“表”不存在
- php - 如何在 woo 购物车页面中显示产品变体图像?