这篇文章主要介绍“elasticsearch的DSL查询方法有哪些”,在日常操作中,相信很多人在elasticsearch的DSL查询方法有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”elasticsearch的DSL查询方法有哪些”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
查询,完全匹配,即不进行分词器分析
{
"query": {
"term": {
"<field>": "<value>"
}
}
}
查询,模糊匹配,根据你给定的字段进行分词器分析,只包含一部分关键字就行
{
"query": {
"match": {
"<field>": "<value>"
}
}
}
查询指定索引下的所有文档
{
"query": {
"match_all": {}
}
}
通过 match_all
过滤出所有字段,然后通过 partial
再过滤出包含 preview
的字段和排除 title,price
的字段
{
"query": {
"match_all": {}
},
"partial_fields": {
"partial": {
"include": ["preview"],
"exclude": ["title,price"]
}
}
}
短语查询,slop
定义的是关键词之间隔多少个未知单词
{
"query": {
"match_phrase": {
"query": "aaa,bbb",
"slop": 2
}
}
}
查询,可以指定多个字段
查询 filed1
和 filed2
这两个字段都包含 value
关键字的文档
{
"query": {
"multi_match": {
"query": "<value>",
"fileds": ["<field1>", "<field2>"]
}
}
}
布尔查询
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>"}}
],
}
}
}
查询同时,通过 filter
条件在不影响打分的情况下筛选出想要的数据
{
"query": {
"filtered": {
"query": {
"match_all": {},
},
"filter": {
"term": {
"<field>": "<value>"
}
}
}
}
}
filter
之 bool
过滤查询
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>"}}
],
}
}
}
}
filter
之 range
范围查询
gt
: 大于
lt
: 小于
gte
: 大于等于
lte
: 小于等于
{
"query": {
"filtered": {
"query": {
"match_all": {},
},
"filter": {
"range": {
"<field>": {
"gt": "<value>",
"gte": "<value>",
"lt": "<value>",
"lte": "<value>",
}
}
}
}
}
}
固定分数查询 我们查询到的每一个文档都有一个_score
参数,这是匹配度打分
constant_score
: 固定分数查询关键字(它支持 filter
, 不支持 match)
boost
: 指定固定分数字段
{
"query": {
"constant_score": {
"filter": {
"match": {
"<field>": "<value>"
}
},
"boost": 1
}
}
}
聚合分析
分组,对应 sql
语句中的 group by
{
"aggs": {
"<tag_name>": {
"terms": {
"field": "<value>"
}
}
}
}
去重,对应 sql
语句中的 distinct
{
"aggs": {
"<tag_name>": {
"cardinality": {
"field": "<value>"
}
}
}
}
求平均值
{
"aggs": {
"<tag_name>": {
"avg": {
"field": "<value>"
}
}
}
}
求平均值
{
"aggs": {
"<tag_name>": {
"max": {
"field": "<value>"
}
}
}
}
求平均值
{
"aggs": {
"<tag_name>": {
"min": {
"field": "<value>"
}
}
}
}
求平均值
{
"aggs": {
"<tag_name>": {
"sum": {
"field": "<value>"
}
}
}
}
按照指定区间分组
{
"aggs": {
"<tag_name>": {
"field": "<value>",
"range": [
{"from": 0, "to": 20},
{"from": 20, "to": 40},
{"from": 40, "to": 60}
]
}
}
}
按时间统计
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字段后,查询结果中[hits]中会出现[fields]字段,其中包含了去重后的user_id
ES5.3版本之后才发布的
聚合&折叠只能针对keyword类型有效;
到此,关于“elasticsearch的DSL查询方法有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/qiongtaoli/blog/4556142