前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ES查询常见问题

ES查询常见问题

作者头像
YG
发布2018-05-23 17:14:19
7130
发布2018-05-23 17:14:19
举报
文章被收录于专栏:YG小书屋YG小书屋

1 must嵌套should条件查询

代码语言:javascript
复制
curl -XGET 'xxx/xxx/_search?pretty' -H 'Content-Type: application/json' -d'{
    "size": 0,
    "query": {
        "bool": {
            "must": [
                {
                    "range": {
                        "uisendtime": {
                            "gte": 1506535200,
                            "lte": 1506607200
                        }
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "match_phrase": {
                                    "strtitle.infosec": "两清"
                                }
                            },
                            {
                                "match_phrase": {
                                    "strdescription.infosec": "两清"
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}'

注意: 1)如果must或者should中用到多个条件,每个条件必须用大括号括起来,嵌套的bool查询必须从新指定bool

2 先过来后聚合,然后对聚合出的数据求top然后按照某属性的最大值排序
代码语言:javascript
复制
curl -XGET 'http://xxx/xxx/_search?pretty' -d '
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "strcommocrtxtcont": {
                            "query": "福利",
                            "type": "phrase"
                        }
                    }
                }
            ]
        }
    },
    "aggs": {
        "md5_distinct_count": {
            "cardinality": {
                "field": "strpicdownloadimgmd5"
            }
        },
        "top_tags": {
            "terms": {
                "field": "strpicdownloadimgmd5",
                "order": {
                    "max_uisendtime": "desc"
                },
                "size": 10
            },
            "aggs": {
                "top_url_hits": {
                    "top_hits": {
                        "sort": [
                            {
                                "_score": {
                                    "order": "desc"
                                }
                            }
                        ],
                        "size": 1
                    }
                },
                "max_uisendtime": {
                    "max": {
                        "field": "uisendtime"
                    }
                }
            }
        }
    },
    "size": 0
}'

注意:top_tags的属性strpicdownloadimgmd5是按照max_uisendtime排序的,而max_uisendtime(求出top_hits中uisendtime的最大值)是你自己在一个聚合中定义的。

3 查出的数据中过滤长度为0的字符串
代码语言:javascript
复制
curl xxx/xxx/_search?pretty -d'{
    "size": 5,
    "_source": [
        "strdescription",
        "uisendtime",
        "strtitle"
    ],
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "should": [
                            {
                                "match_phrase": {
                                    "strtitle": "\u738b\u8005"
                                }
                            },
                            {
                                "match_phrase": {
                                    "strtitle": "\u8363\u8000"
                                }
                            }
                        ]
                    }
                }
            ],
            "must_not": [
                {
                    "script": {
                        "script": {
                            "inline": "params._source.strdescription.length() < 1"
                        }
                    }
                }
            ]
        }
    }
}'

注意:params._source.strdescription.length() < 1 可用 d oc[\u0027strdescription\u0027].length()<1 或者doc['''strdescription''']<1代替。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.11.14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 must嵌套should条件查询
    • 2 先过来后聚合,然后对聚合出的数据求top然后按照某属性的最大值排序
      • 3 查出的数据中过滤长度为0的字符串
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档