term查詢
????????term 主要用于精確匹配哪些值,比如數(shù)字,日期,布爾值或 not_analyzed 的字符串(未經(jīng)分析的文本數(shù)據(jù)類型):
{ "term": { "age": 26 }}
{ "term": { "date": "2014-09-01" }}
{ "term": { "public": true }}
{ "term": { "tag": "full_text" }}
當(dāng)前數(shù)據(jù)庫中的數(shù)據(jù):
POST /study/_search
# 請求數(shù)據(jù)
{
"query": {
"term": {
"age": 20
}
}
}
# 響應(yīng)數(shù)據(jù)
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "study",
"_type": "_doc",
"_id": "SHjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "張三",
"age": 20,
"mail": "111@qq.com",
"hobby": "羽毛球、乒乓球、足球"
}
}
]
}
}
terms查詢
????????terms 跟 term 相似,但 terms 允許指定多個匹配條件。 如果某個字段指定了多個值,那么文檔需要一起去做匹配:
{
"terms": {
"tag": ["search", "full_text", "nosql"]
}
}
POST /study/_search
# 請求數(shù)據(jù)
{
"query": {
"terms": {
"age": [20, 21, 22]
}
}
}
# 響應(yīng)數(shù)據(jù)
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "study",
"_type": "_doc",
"_id": "SXjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "李四",
"age": 21,
"mail": "222@qq.com",
"hobby": "羽毛球、乒乓球、足球、籃球"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "SHjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "張三",
"age": 20,
"mail": "111@qq.com",
"hobby": "羽毛球、乒乓球、足球"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "SnjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "王五",
"age": 22,
"mail": "333@qq.com",
"hobby": "羽毛球、籃球、游泳、聽音樂"
}
}
]
}
}
range查詢
????????range 過濾允許按照指定范圍查找一批數(shù)據(jù)
范圍操作符包含:
- gt :大于
- gte :?大于等于
- lt :?小于
- lte :?小于等于
POST /study/_search
# 請求數(shù)據(jù)
{
"query": {
"range": {
"age": {
"gte": 20,
"lte": 22
}
}
}
}
# 響應(yīng)數(shù)據(jù)
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "study",
"_type": "_doc",
"_id": "SXjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "李四",
"age": 21,
"mail": "222@qq.com",
"hobby": "羽毛球、乒乓球、足球、籃球"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "SHjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "張三",
"age": 20,
"mail": "111@qq.com",
"hobby": "羽毛球、乒乓球、足球"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "SnjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "王五",
"age": 22,
"mail": "333@qq.com",
"hobby": "羽毛球、籃球、游泳、聽音樂"
}
}
]
}
}
exists查詢
????????exists 查詢可以用于查找文檔中是否包含指定字段或沒有某個字段,類似于SQL語句中的 IS_NULL 條件
注意:這個查詢只是針對已經(jīng)查出一批數(shù)據(jù)來,但是想?yún)^(qū)分出某個字段是否存在的時候使用。
POST /study/_search
# 請求數(shù)據(jù)
{
"query": {
"exists": {
"field": "age"
}
}
}
# 響應(yīng)數(shù)據(jù)
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "study",
"_type": "_doc",
"_id": "SXjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "李四",
"age": 21,
"mail": "222@qq.com",
"hobby": "羽毛球、乒乓球、足球、籃球"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "S3jOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "趙六",
"age": 23,
"mail": "444@qq.com",
"hobby": "跑步、游泳"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "SHjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "張三",
"age": 20,
"mail": "111@qq.com",
"hobby": "羽毛球、乒乓球、足球"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "SnjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "王五",
"age": 22,
"mail": "333@qq.com",
"hobby": "羽毛球、籃球、游泳、聽音樂"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "THjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "孫七",
"age": 24,
"mail": "555@qq.com",
"hobby": "聽音樂、看電影"
}
}
]
}
}
match 查詢
????????match 查詢是一個標(biāo)準(zhǔn)查詢,不管需要全文本查詢還是精確查詢基本上都要用到它。
????????如果使用 match 查詢一個全文本字段,它會在真正查詢之前用分析器先分析 match 一下查詢字符。
????????如果用 match 指定了一個確切值,在遇到數(shù)字,日期,布爾值或者 not_analyzed 的字符串時,它將為搜索給定的值。
{ "match": { "age": 26 }}
{ "match": { "date": "2014-09-01" }}
{ "match": { "public": true }}
{ "match": { "tag": "full_text" }}
POST /study/_search
# 請求數(shù)據(jù)
{
"query": {
"match": {
"age": "20"
}
}
}
# 響應(yīng)數(shù)據(jù)
{
"took": 18,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "study",
"_type": "_doc",
"_id": "SHjOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "張三",
"age": 20,
"mail": "111@qq.com",
"hobby": "羽毛球、乒乓球、足球"
}
}
]
}
}
數(shù)據(jù)庫中數(shù)據(jù):
bool查詢
bool 查詢可以用來合并多個條件查詢結(jié)果的布爾邏輯,它包含以下操作符:
- must :?多個查詢條件的完全匹配,相當(dāng)于 and 。
- must_not :多個查詢條件的相反匹配,相當(dāng)于 not 。
- should :?至少有一個查詢條件匹配, 相當(dāng)于 or 。
這些參數(shù)可以分別繼承一個查詢條件或者一個查詢條件的數(shù)組:
{
"bool": {
"must": {
"term": {
"age": "20"
}
},
"must_not": {
"term": {
"sex": "男"
}
},
"should": [{
"term": {
"age": 25
}
}, {
"term": {
"age": 26
}
}]
}
}
POST /study/_search
# 請求數(shù)據(jù)
{
"query": {
"bool": {
"must": {
"match": {
"hobby": "足球"
}
},
"must_not": {
"match": {
"hobby": "音樂"
}
}
}
}
}
# 響應(yīng)數(shù)據(jù)
{
"took": 20,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.7194062,
"hits": [
{
"_index": "study",
"_type": "_doc",
"_id": "SHjOVIIBpyNh4YQ4CVSN",
"_score": 1.7194062,
"_source": {
"name": "張三",
"age": 20,
"mail": "111@qq.com",
"hobby": "羽毛球、乒乓球、足球"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "SXjOVIIBpyNh4YQ4CVSN",
"_score": 1.6817665,
"_source": {
"name": "李四",
"age": 21,
"mail": "222@qq.com",
"hobby": "羽毛球、乒乓球、足球、籃球"
}
}
]
}
}
文章來源:http://www.zghlxwxcb.cn/news/detail-451207.html
POST /study/_search
# 請求數(shù)據(jù)
{
"query": {
"bool": {
"should": [
{
"match": {
"hobby": "足球"
}
},
{
"match": {
"age": 23
}
}
]
}
}
}
# 響應(yīng)數(shù)據(jù)
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1.7194062,
"hits": [
{
"_index": "study",
"_type": "_doc",
"_id": "SHjOVIIBpyNh4YQ4CVSN",
"_score": 1.7194062,
"_source": {
"name": "張三",
"age": 20,
"mail": "111@qq.com",
"hobby": "羽毛球、乒乓球、足球"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "SXjOVIIBpyNh4YQ4CVSN",
"_score": 1.6817665,
"_source": {
"name": "李四",
"age": 21,
"mail": "222@qq.com",
"hobby": "羽毛球、乒乓球、足球、籃球"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "S3jOVIIBpyNh4YQ4CVSN",
"_score": 1.0,
"_source": {
"name": "趙六",
"age": 23,
"mail": "444@qq.com",
"hobby": "跑步、游泳"
}
},
{
"_index": "study",
"_type": "_doc",
"_id": "SnjOVIIBpyNh4YQ4CVSN",
"_score": 0.6038003,
"_source": {
"name": "王五",
"age": 22,
"mail": "333@qq.com",
"hobby": "羽毛球、籃球、游泳、聽音樂"
}
}
]
}
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-451207.html
到了這里,關(guān)于ElasticSearch中結(jié)構(gòu)化查詢(term、terms、range、exists、match、bool)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!