温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

elasticsearch的DSL查询方法有哪些

发布时间:2021-12-16 10:02:17 来源:亿速云 阅读:329 作者:iii 栏目:大数据

这篇文章主要介绍“elasticsearch的DSL查询方法有哪些”,在日常操作中,相信很多人在elasticsearch的DSL查询方法有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”elasticsearch的DSL查询方法有哪些”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

query

1. term

查询,完全匹配,即不进行分词器分析

{
    "query": {
        "term": {
            "<field>": "<value>"
        }
    }
}

2. match

查询,模糊匹配,根据你给定的字段进行分词器分析,只包含一部分关键字就行

{
    "query": {
        "match": {
            "<field>": "<value>"
        }
    }
}

3. match_all

查询指定索引下的所有文档

{
    "query": {
        "match_all": {}
    }
}

通过 match_all 过滤出所有字段,然后通过 partial 再过滤出包含 preview 的字段和排除 title,price 的字段

{
    "query": {
        "match_all": {}
    },
    "partial_fields": {
        "partial": {
            "include": ["preview"],
            "exclude": ["title,price"]
        }
    }
}

4. match_phrase

短语查询,slop 定义的是关键词之间隔多少个未知单词

{
    "query": {
        "match_phrase": {
            "query": "aaa,bbb",
            "slop": 2
        }
    }
}

5. multi_match

查询,可以指定多个字段

查询 filed1filed2 这两个字段都包含 value 关键字的文档

{
    "query": {
        "multi_match": {
            "query": "<value>",
            "fileds": ["<field1>", "<field2>"]
        }
    }
}

6. bool

布尔查询

  • must: 条件必须满足,相当于 sql 语句的 and

  • should: 条件可以满足也可以不满足,相当于 sql 语句的 or

  • must_not: 条件不需要满足,相当于 sql 语句的 not

{
    "query": {
        "bool": {
            "should": [
                {"term": {"<field>": "<value>"}},
                {"term": {"<field>": "<value>"}}
            ],
            "must": [
                {"term": {"<field>": "<value>"}},
                {"term": {"<field>": "<value>"}}
            ],
            "must_not": [
                {"term": {"<field>": "<value>"}},
                {"term": {"<field>": "<value>"}}
            ],
        }
    }
}

7. filter

查询同时,通过 filter 条件在不影响打分的情况下筛选出想要的数据

{
    "query": {
        "filtered": {
            "query": {
                "match_all": {},
            },
            "filter": {
                "term": {
                    "<field>": "<value>"
                }
            }
        }
    }
}

filterbool 过滤查询

  • must: 条件必须满足,相当于 sql 语句的 and

  • should: 条件可以满足也可以不满足,相当于 sql 语句的 or

  • must_not: 条件不需要满足,相当于 sql 语句的 not

{
    "query": {
        "filtered": {
            "query": {
                "match_all": {},
            },
            "filter": {
                "bool": {
                    "should": [
                        {"term": {"<field>": "<value>"}},
                        {"term": {"<field>": "<value>"}}
                    ],
                    "must": [
                        {"term": {"<field>": "<value>"}},
                        {"term": {"<field>": "<value>"}}
                    ],
                    "must_not": [
                        {"term": {"<field>": "<value>"}},
                        {"term": {"<field>": "<value>"}}
                    ],
                }
            }
        }
    }
}

没有 bool, 也可以直接使用 and、or、not

{
    "query": {
        "filtered": {
            "query": {
                "match_all": {},
            },
            "filter": {
                "and": [
                    {"term": {"<field>": "<value>"}},
                    {"term": {"<field>": "<value>"}}
                ],
                "or": [
                    {"term": {"<field>": "<value>"}},
                    {"term": {"<field>": "<value>"}}
                ],
                "not": [
                    {"term": {"<field>": "<value>"}},
                    {"term": {"<field>": "<value>"}}
                ],
            }
        }
    }
}

filterrange 范围查询

  • gt: 大于

  • lt: 小于

  • gte: 大于等于

  • lte: 小于等于

{
    "query": {
        "filtered": {
            "query": {
                "match_all": {},
            },
            "filter": {
                "range": {
                    "<field>": {
                        "gt": "<value>",
                        "gte": "<value>",
                        "lt": "<value>",
                        "lte": "<value>",
                    }
                }
            }
        }
    }
}

8. boost

固定分数查询 我们查询到的每一个文档都有一个_score 参数,这是匹配度打分

  • constant_score: 固定分数查询关键字(它支持 filter, 不支持 match)

  • boost: 指定固定分数字段

{
    "query": {
        "constant_score": {
            "filter": {
                "match": {
                    "<field>": "<value>"
                }
            },
            "boost": 1
        }
    }
}

agg

聚合分析

1. terms

分组,对应 sql 语句中的 group by

{
    "aggs": {
        "<tag_name>": {
            "terms": {
                "field": "<value>"
            }
        }
    }
}

2. cardinality

去重,对应 sql 语句中的 distinct

{
    "aggs": {
        "<tag_name>": {
            "cardinality": {
                "field": "<value>"
            }
        }
    }
}

3. avg

求平均值

{
    "aggs": {
        "<tag_name>": {
            "avg": {
                "field": "<value>"
            }
        }
    }
}

4. max

求平均值

{
    "aggs": {
        "<tag_name>": {
            "max": {
                "field": "<value>"
            }
        }
    }
}

5. min

求平均值

{
    "aggs": {
        "<tag_name>": {
            "min": {
                "field": "<value>"
            }
        }
    }
}

6. sum

求平均值

{
    "aggs": {
        "<tag_name>": {
            "sum": {
                "field": "<value>"
            }
        }
    }
}

7. range

按照指定区间分组

{
    "aggs": {
        "<tag_name>": {
            "field": "<value>",
            "range": [
                {"from": 0, "to": 20},
                {"from": 20, "to": 40},
                {"from": 40, "to": 60}
            ]
        }
    }
}

8. date_histogram

按时间统计

  • min_doc_count: 强制返回所有 buckets,即使 buckets 可能为空

  • extended_bounds: 只返回你的数据中最小值和最大值之间的 buckets

{
   "aggs": {
      "<tag_name>": {
         "date_histogram": {
            "<field>": "<value>",
            "interval": "month",
            "format": "yyyy-MM-dd",
            "min_doc_count" : 0,
            "extended_bounds" : {
                "min" : "2014-01-01",
                "max" : "2014-12-31"
            }
         }
      }
   }
}

collapse

使用collapse字段后,查询结果中[hits]中会出现[fields]字段,其中包含了去重后的user_id

ES5.3版本之后才发布的
聚合&折叠只能针对keyword类型有效;

到此,关于“elasticsearch的DSL查询方法有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI