V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
nonea
V2EX  ›  Elasticsearch

请教 ElasticSearch 简单语法问题

  •  
  •   nonea · 2020-03-06 14:24:38 +08:00 · 2815 次点击
    这是一个创建于 1504 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我想卡 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"}}}

    感谢!

    14 条回复    2020-03-09 10:13:31 +08:00
    nonea
        2
    nonea  
    OP
       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
        3
    hackerwin7  
       2020-03-06 14:52:38 +08:00 via iPhone
    mapping
    nonea
        4
    nonea  
    OP
       2020-03-06 14:54:26 +08:00
    @hackerwin7 啥意思啊 es 大佬说话都这么含糊吗
    DonaldY
        5
    DonaldY  
       2020-03-06 15:15:59 +08:00
    `term`放在`query`下,试试
    stiekel
        6
    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
        7
    stiekel  
       2020-03-06 15:22:30 +08:00
    多个条件,需要使用 bool / must 组织。
    180 天内,可以使用 gte: now-180d。
    ben1024
        8
    ben1024  
       2020-03-06 15:23:27 +08:00
    语法上 要把 bool must 层级聚合处理

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

    可以作为参考
    stiekel
        10
    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
        11
    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
        12
    zxc12300123  
       2020-03-06 16:14:03 +08:00 via iPhone
    先看看你的 mapping,business_time 是不是时间类型。
    rykinia
        13
    rykinia  
       2020-03-06 17:04:17 +08:00
    你把 term 里面的 subject_type 改成 subject_type.keyword 试试,term 查字符串并不是=的效果
    nonea
        14
    nonea  
    OP
       2020-03-09 10:13:31 +08:00
    @stiekel 感谢 尝试了是可以的
    感谢其他回复的朋友 ,一发穿云箭,千军万马来相见
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3069 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 00:33 · PVG 08:33 · LAX 17:33 · JFK 20:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.