
前言
- 本文為es常見DSL搜索入門帖子
- 開始之前先貼個(gè)對(duì)應(yīng)關(guān)系,方便各位理解
- 在es7+的版本中,是沒(méi)有類型的概念的,所以,添加數(shù)據(jù)直接在索引中添加;
?請(qǐng)求es地址均為localhost:9200/{索引}/_search
,為了編寫方便些,在下面的例子中會(huì)直接寫請(qǐng)求體;
?搜索所有文檔
{
"query": {
"match_all": {}
},
"size": 1 // size關(guān)鍵詞為返回?cái)?shù)據(jù)條數(shù),不填寫默認(rèn)為10條
}
?根據(jù)指定字段倒序排列
{
"query": {
"match_all": {}
},
"sort": [
{
"patient_age": {
"order": "desc" // 根據(jù)用戶年齡倒序排列
}
}
],
"size": 10, // size 與 from搭配起到分頁(yè)的作用
"from": 1
}
?查詢返回指定字段
{
"query": {
"match_all": {}
},
"_source": ["bill_no", "patient_age"], // 只返回訂單號(hào)和用戶年齡
"sort": [
{
"patient_age": {
"order": "desc"
}
}
],
"size": 10,
"from": 1
}
?范圍查詢(range)
{
"query": {
"range": {
"patient_age": { // 查詢年齡大于1歲 小于10歲的用戶
"gte": 1,
"lte": 10
}
}
}
}
?前綴查詢(prefix)
{
"query": {
"prefix": {
"patient_name": {
"value": "李" // 查詢姓李的用戶
}
}
}
}
?組合查詢(bool)
- bool 關(guān)鍵字: 用來(lái)組合多個(gè)條件實(shí)現(xiàn)復(fù)雜查詢表達(dá)式
- ? must: 相當(dāng)于 &&
- should: 相當(dāng)于 ||
- must_not: 相當(dāng)于 !=
以下表達(dá)式的意思為:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-501365.html
- 查詢用戶年齡(
patient_age
)在0-18歲之間 - 且不姓李的用戶信息
- 并且按照用戶年齡字段倒序排列
- 并且返回指定字段的用戶信息
{
"query": {
"bool": {
"must": [
{
"range": {
"patient_age": {
"gte": 0,
"lte": 18
}
}
}
],
"must_not": [
{
"prefix": {
"patient_name": {
"value": "李"
}
}
}
]
}
},
"sort": [
{
"patient_age": {
"order": "desc"
}
}
],
"_source": [
"bill_no",
"patient_name",
"patient_age"
]
}
?高亮查詢(term、highlight)
-
term
查詢被用于精確值匹配,這些精確值可能是數(shù)字、時(shí)間、布爾或者那些 not_analyzed 的字符串;
{
"query": {
"term": {
"patient_name": {
"value": "李"
}
}
},
"highlight": {
"fields": {
"*": {}
}
}
}
在返回值中,符合條件的數(shù)據(jù)會(huì)被highlight標(biāo)簽包裹,并且有標(biāo)簽修飾,不過(guò)這個(gè)標(biāo)簽是可以通過(guò)es進(jìn)行修改的;文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-501365.html
"highlight": {
"patient_name": [
"<em>李</em>雪"
]
}
? terms 查詢
- terms 查詢和 term 查詢一樣,但它允許你指定多值進(jìn)行匹配;
- 如果這個(gè)字段包含了指定值中的任何一個(gè)值,那么這個(gè)文檔滿足條件
{
"query": {
"terms": {
"patient_name": [ // 查詢患者名字中包含 趙 錢 孫字符的文檔
"趙",
"錢",
"孫"
]
}
}
}
?多字段查詢(multi_match)
{
"query": {
"multi_match": {
"query": "山東",
"fields": [
"patient_address", // 這里為檢索的字段
"address"
]
}
}
}
到了這里,關(guān)于【ElasticSearch】ElasticSearch常用查詢api集合(一)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!