子条件查询
1. query context(主要是匹配有多好)
在查询过程中,除了判断文档是否满足查询条件外,ES还会计算一个_score来表示匹配的程度,旨在判断文档与查询条件的匹配的有多好。返回值有一个Score分数来标识。
match 查询
对查询字段进行分词,匹配一个分词,就能命中
{
"query":{
"match":{
"title":"elasticsearch 通"
}
}
}把查询内容进行分词 "elasticsearch" 和 "通",然后进行匹配
mutil_match 查询
多字段匹配查询
{
"query":{
"multi_match":{
"query":"elasticsearch 通",
"fields":["author", "title"]
}
}
}match_phrase 查询
短语匹配,把关键字当一个不可分的短语,但是可以支持slop,slop参数告诉match_phrase查询词条能够相隔多远时仍然将文档视为匹配。相隔多远的意思是,你需要移动一个词条多少次来让查询和文档匹配
{
"query":{
"match_phrase":{
"title":{
"query":"入通",
"slop":3
}
}
}
}查询结果是
query_string 语法查询
语法查询
{
"query":{
"query_string":{
"default_field":"title",
"default_operator":"or",
"query":"java 入门"
}
}
}
查询title字段 要有java或者入门
default_field 默认查询ALL 可以指定一个或者多个(default_field :["title"、"author"])
default_operator 默认是or,
query 查询内容 是要进行分词的
{
"query":{
"query_string":{
"query":"auhtor:java"
}
}
}查询 author 是否java
term查询
关键字查询,对查询字段不进行分词
{
"query":{
"term":{
"author":"test"
}
}
}range查询
范围查询
{
"query":{
"range":{
"price":{
"gte":100,
"lt":300
}
}
}
}查询价格在 [100-300) 之间
2. filter context(只在乎true或者false)
使用bool关键字,bool中可以圈套 must、must-Not、should、filter
bool must 使用
{
"query":{
"bool":{
"must":[{
"match":{
"author":"test"
}
},{
"match":{
"title":"java"
}
}]
}
}
}相当于 (author == 'test' and title == 'java')
bool must_not 使用
{
"query":{
"bool":{
"must_not":[{
"match":{
"author":"test"
}
},{
"match":{
"title":"java"
}
}]
}
}
}相当于 !(author == 'test' and title == 'java')
bool should 使用
{
"query":{
"bool":{
"should":[{
"match":{
"author":"test"
}
},{
"match":{
"title":"java"
}
}]
}
}
}相当于 (author == 'test' or title == 'java')
bool filter 使用
使用bool filter查询时,不会计算_score
{
"query":{
"bool":{
"filter":[{
"match":{
"author":"test"
}
},{
"match":{
"title":"java"
}
}]
}
}
}bool 混合使用
{
"query":{
"bool":{
"must":[{
"match":{
"author":"test"
}
}],
"must_not":[{
"match":{
"title":"java"
}
}],
"filter":[{
"term":{
"price":100
}
}]
}
}
}这个混合查询,是会计算_score分数的
3. 查询排序
{
"query":{
"bool":{
"filter":[{
"match":{
"author":"test"
}
},{
"match":{
"title":"java"
}
}]
}
},
"sort":{
"price":{
"order":"desc"
}
}
}
多个字段排序
{
"query":{
"bool":{
"filter":[{
"match":{
"author":"test"
}
},{
"match":{
"title":"java"
}
}]
}
},
"sort":[{
"price":{
"order":"desc"
}
},{
"wordCount":{
"order":"desc"
}
}]
}4. 指定字段查询
{
"_source":["title","author"]
}只返回 title 和author
5. 分页
{
"from":0,
"size":5
}from 起始位置,size 大小
版权声明:本文为weixin_41651116原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。