nonea

请教 ElasticSearch 简单语法问题

  •  
  •   nonea · Mar 6, 2020 · 6081 views
    This topic created in 2270 days ago, the information mentioned may be changed or developed.

    我想卡 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 replies    2020-03-09 10:13:31 +08:00
    nonea
        2
    nonea  
    OP
       Mar 6, 2020
    @DonaldY {"query":{"range":{"business_time":{"gte":"${fmt(add(NTIME(),-180,'day'),'yyyy-MM-dd')}"}},"match":{"subject_type":"BIGCUSTOMER"}}} term 改 match? es 完全小白,临时有一个需求点卡在这了 ,麻烦告知一下
    hackerwin7
        3
    hackerwin7  
       Mar 6, 2020 via iPhone
    mapping
    nonea
        4
    nonea  
    OP
       Mar 6, 2020
    @hackerwin7 啥意思啊 es 大佬说话都这么含糊吗
    DonaldY
        5
    DonaldY  
       Mar 6, 2020
    `term`放在`query`下,试试
    stiekel
        6
    stiekel  
       Mar 6, 2020
    ```js
    {
    "query": {
    "bool": {
    "must": [
    {
    "range": {
    "business_time": {
    "gte": "now-180d"
    }
    }
    },
    {
    "term": {
    "subject_type": {
    "value": "newCalling"
    }
    }
    }
    ]
    }
    }
    }
    ```
    stiekel
        7
    stiekel  
       Mar 6, 2020
    多个条件,需要使用 bool / must 组织。
    180 天内,可以使用 gte: now-180d。
    ben1024
        8
    ben1024  
       Mar 6, 2020
    语法上 要把 bool must 层级聚合处理

    或可以尝试 query string
    query_string:"business_time>date AND subject_type: BIGCUSTOMER"
    francis59
        9
    francis59  
       Mar 6, 2020
    SQL to ElasticSearch Converter
    https://sqltoelasticsearch.azurewebsites.net/

    可以作为参考
    stiekel
        10
    stiekel  
       Mar 6, 2020
    评论不支持 markdown,重新整理一下。
    {
    "query": {
    "bool": {
    "must": [
    {
    "range": {
    "business_time": {
    "gte": "now-180d"
    }
    }
    },
    {
    "term": {
    "subject_type": {
    "value": "BIGCUSTOMER"
    }
    }
    }
    ]
    }
    }
    }
    xz633
        11
    xz633  
       Mar 6, 2020
    自带的 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  
       Mar 6, 2020 via iPhone
    先看看你的 mapping,business_time 是不是时间类型。
    rykinia
        13
    rykinia  
       Mar 6, 2020
    你把 term 里面的 subject_type 改成 subject_type.keyword 试试,term 查字符串并不是=的效果
    nonea
        14
    nonea  
    OP
       Mar 9, 2020
    @stiekel 感谢 尝试了是可以的
    感谢其他回复的朋友 ,一发穿云箭,千军万马来相见
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5862 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 96ms · UTC 01:55 · PVG 09:55 · LAX 18:55 · JFK 21:55
    ♥ Do have faith in what you're doing.