常用的查询过滤语句
(1)term 过滤:主要用于精确匹配,比如数字,日期,布尔值或 not_analyzed的字符串(未经分析的文本数据类型):
DEMO1:
{ “term”: { “age”: 26 }}
DEMO2:
{ “term”: { “date”: “2014-09-01” }}
DEMO3:
{ “term”: { “public”: true }}
DEMO4:
{ “term”: { “tag”: “full_text” }}
(2)terms 过滤:跟 term 有点类似,但 terms 允许指定多个匹配条件
DEMO:
{
"terms": {
"tag": [ "search", "full_text", "nosql" ]
}
}
(3)range 过滤:指定范围查找一批数据
范围操作符包含:
gt :: 大于
gte:: 大于等于
lt :: 小于
lte:: 小于等于
DEMO:
{
"range": {
"age": {
"gte": 20,
"lt": 30
}
}
}
(4)exists 和 missing 过滤:用于查找文档中是否包含指定字段或没有某个字段,类似于SQL语句中的IS_NULL条件
DEMO:
{
"exists": {
"field": "title"
}
}
(5)bool 过滤:用来合并多个过滤条件查询结果的布尔逻辑,它包含一下操作符:
must :: 多个查询条件的完全匹配,相当于 and。
must_not :: 多个查询条件的相反匹配,相当于 not。
should :: 至少有一个查询条件匹配, 相当于 or。
DEMO:
{
"bool": {
"must": { "term": { "folder": "inbox" }},
"must_not": { "term": { "tag": "spam" }},
"should": [
{ "term": { "starred": true }},
{ "term": { "unread": true }}
]
}
}
(6)match_all 查询:查询到所有文档,是没有查询条件下的默认语句
DEMO:
{
"match_all": {}
}
(7)match 查询:是一个标准查询,不管你需要全文本查询还是精确查询基本上都要用到它
如果用match下指定了一个确切值,在遇到数字,日期,布尔值或者not_analyzed 的字符串时,它将为你搜索你给定的值:
DEMO1:
{
“match”: {
“tweet”: “About Search”
}
}
DEMO2:
{ “match”: { “age”: 26 }}
DEMO3:
{ “match”: { “date”: “2014-09-01” }}
DEMO4:
{ “match”: { “public”: true }}
DEMO5:
{ “match”: { “tag”: “full_text” }}
(8)multi_match 查询:允许你做match查询的基础上同时搜索多个字段
DEMO:
{
"multi_match": {
"query": "full text search",
"fields": [ "title", "body" ]
}
}
(9)bool 查询:bool 查询与 bool 过滤相似,用于合并多个查询子句。不同的是,bool 过滤可以直接给出是否匹配成功, 而bool 查询要计算每一个查询子句的 _score (相关性分值)。
DEMO:
{
"bool": {
"must": { "match": { "title": "how to make millions" }},
"must_not": { "match": { "tag": "spam" }},
"should": [
{ "match": { "tag": "starred" }},
{ "range": { "date": { "gte": "2014-01-01" }}}
]
}
}
DSL的综合使用
–空查询,查询所有记录
GET /testindex/testtable/_search
{
"query": {
"match_all": {}
}
}
–查询指定字段demo字段包含指定值的记录
GET /testindex/testtable/_search
{
"query": {
"match": {
"demo": "kimchy"
}
}
}
以下查询将会找到 title 字段中包含 “how to make millions”,并且 “tag” 字段没有被标为 spam。 如果有标识为 “starred” 或者发布日期为2014年之前,那么这些匹配的文档将比同类网站等级高:
注: 如果bool 查询下没有must子句,那至少应该有一个should子句。但是 如果有must子句,那么没有should子句也可以进行查询。
GET /testindex/testtable/_search
{
"query": {
"bool": {
"must": { "match": { "title": "how to make millions" }},
"must_not": { "match": { "tag": "spam" }},
"should": [
{ "match": { "tag": "starred" }},
{ "range": { "date": { "gte": "2014-01-01" }}}
]
}
}
}
–分页查询
GET /testindex/testtable/_search
{
"from": 2,
"size": 2
}
–查询指定ID的记录
GET /testindex/testtable/_mget
{
"ids" : [ "2", "3" ]
}
–统计记录数量
GET testindex/testtable/_count
{
"query" : {
"term" : { "demo" : "kimchy" }
}
}
–单条过滤语句:
GET /testindex/testtable/_search
{
"query": {
"filtered": {
"filter": { "term": { "folder": "inbox" }}
}
}
}
–查询语句中的过滤
GET /testindex/testtable/_search
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": { "term": { "folder": "inbox" }},
"must_not": {
"query": {
"match": { "email": "urgent business proposal" }
}
}
}
}
}
}
}
–按照指定字段排序
(注:可以只指定要排序的字段名称,字段值默认以顺序排列,而 _score 默认以倒序排列。)
GET /testindex/testtable/_search
{
"query" : {
"filtered" : {
"filter" : { "term" : { "user_id" : 1 }}
}
},
"sort": { "date": { "order": "desc" }}
}
–按照多个字段排序
GET /testindex/testtable/_search
{
"query" : {
"filtered" : {
"query": { "match": { "tweet": "manage text search" }},
"filter" : { "term" : { "user_id" : 2 }}
}
},
"sort": [
{ "date": { "order": "desc" }},
{ "_score": { "order": "desc" }}
]
}
–为多值字段排序
一个拥有多值的字段就是一个集合,你可以从多个值中取出一个来进行排序,你可以使用min, max, avg 或 sum这些模式。
DEMO:在 dates 字段中用最早的日期来进行排序
"sort": {
"dates": {
"order": "asc",
"mode": "min"
}
}
–验证查询语句是否合法(注:只验证语法,不验证字段名称。)
GET /testindex/testtable/_validate/query
{
"query": {
"demo" : {
"match" : "kimchy"
}
}
}
–验证查询语句是否合法,并给出说明(可以提示字段名称错误)
GET /testindex/testtable/_validate/query?explain
{
"query": {
"match" : {
"demos" : "kimchy"
}
}
}