请教 ElasticSearch 简单语法问题

2020-03-06 14:24:38 +08:00
 nonea

我想卡 business_time 字段查询 180 天内的数据,并且卡 subject_type=BIGCUSTOMER 这部分的数据

用 sql 语言是:select * from XX where business_time>=sysdate(-180) and subject_type='BIGCUSTOMER'

es 语法如下这么写为什么跑出来是 0 啊 ?

{"query":{"range":{"business_time":{"gte":"${fmt(add(NTIME(),-180,'day'),'yyyy-MM-dd')}"}},"term":{"subject_type":"BIGCUSTOMER"}}}

感谢!

2882 次点击
所在节点    Elasticsearch
14 条回复
DonaldY
2020-03-06 14:36:27 +08:00
nonea
2020-03-06 14:50:07 +08:00
@DonaldY {"query":{"range":{"business_time":{"gte":"${fmt(add(NTIME(),-180,'day'),'yyyy-MM-dd')}"}},"match":{"subject_type":"BIGCUSTOMER"}}} term 改 match? es 完全小白,临时有一个需求点卡在这了 ,麻烦告知一下
hackerwin7
2020-03-06 14:52:38 +08:00
mapping
nonea
2020-03-06 14:54:26 +08:00
@hackerwin7 啥意思啊 es 大佬说话都这么含糊吗
DonaldY
2020-03-06 15:15:59 +08:00
`term`放在`query`下,试试
stiekel
2020-03-06 15:21:42 +08:00
```js
{
"query": {
"bool": {
"must": [
{
"range": {
"business_time": {
"gte": "now-180d"
}
}
},
{
"term": {
"subject_type": {
"value": "newCalling"
}
}
}
]
}
}
}
```
stiekel
2020-03-06 15:22:30 +08:00
多个条件,需要使用 bool / must 组织。
180 天内,可以使用 gte: now-180d。
ben1024
2020-03-06 15:23:27 +08:00
语法上 要把 bool must 层级聚合处理

或可以尝试 query string
query_string:"business_time>date AND subject_type: BIGCUSTOMER"
francis59
2020-03-06 15:26:00 +08:00
SQL to ElasticSearch Converter
https://sqltoelasticsearch.azurewebsites.net/

可以作为参考
stiekel
2020-03-06 15:39:20 +08:00
评论不支持 markdown,重新整理一下。
{
"query": {
"bool": {
"must": [
{
"range": {
"business_time": {
"gte": "now-180d"
}
}
},
{
"term": {
"subject_type": {
"value": "BIGCUSTOMER"
}
}
}
]
}
}
}
xz633
2020-03-06 15:39:30 +08:00
自带的 sql 了解一下
GET _xpack/sql
{
"query":"select count(1) from label_active where site_id in ('iosshus','andshus')"
}


加上 translate 可以看对应的 dsl 语法
GET _xpack/sql/translate
{
"query":"select count(1) from label_active where site_id in ('iosshus','andshus')"
}
zxc12300123
2020-03-06 16:14:03 +08:00
先看看你的 mapping,business_time 是不是时间类型。
rykinia
2020-03-06 17:04:17 +08:00
你把 term 里面的 subject_type 改成 subject_type.keyword 试试,term 查字符串并不是=的效果
nonea
2020-03-09 10:13:31 +08:00
@stiekel 感谢 尝试了是可以的
感谢其他回复的朋友 ,一发穿云箭,千军万马来相见

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/650392

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX