这篇文章主要介绍“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
查询,可以指定多个字段
查询 filed1
和 filed2
这两个字段都包含 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>"
}
}
}
}
}
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>",
}
}
}
}
}
}
8. boost
固定分数查询 我们查询到的每一个文档都有一个_score
参数,这是匹配度打分
{
"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
按时间统计
{
"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查询方法有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注天达云网站,小编会继续努力为大家带来更多实用的文章!