DSL語句查詢
查詢字符串搜索便于通過命令行完成特定(ad hoc)的搜索,但是它也有局限性(參閱簡單搜索章節(jié))。Elasticsearch提供豐富且靈活的查詢語言叫做DSL查詢(Query DSL),它允許你構(gòu)建更加復雜、強大的查詢。
DSL(Domain Specific Language特定領域語言)以JSON請求體的形式出現(xiàn)
一、query context
主要特點:
- 是否包含
確定文檔是否應該成為結(jié)果的一部分
- 相關(guān)度得分多少
除了確定文檔是否匹配外,查詢子句還計算了表示文檔與其他文檔相比匹配程度的_score
- 得分越高,相關(guān)度越高
更相關(guān)的文件,在搜索排名更高
應用場景:
- 全文檢索——這種相關(guān)性的概念非常適合全文搜索,因為很少有完全“正確”的答案
文本查詢語法:
match-模糊匹配:如 搜索"四大名著" 會查出包含 "四大" OR "名著" OR "四大名著"的記錄
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"last_name" : "四大名著"
}
}
}
match_phrase-短語匹配:如 搜索"四大名著" 不會查出包含 "四大" OR "名著" 只會查出包含 "四大名著"的記錄
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"last_name" : "四大名著"
}
}
}
multi_match-多字段匹配:如 搜索"孫悟空" 只要first_name或者about字段中包含 "孫悟空" OR "孫"等分詞都會被查找出來;
GET /megacorp/employee/_search
{
"query": {
"multi_match": {
"query": "孫悟空",
"fields":["first_name","about"]
}
}
}
query_string-lucene查詢語法查詢(kibana查詢語法)
GET /megacorp/employee/_search
{
"query": {
"query_string": {
"query": "孫悟空 OR 張三"
}
}
}
query_string-多字段
GET /megacorp/employee/_search
{
"query": {
"query_string": {
"fields":["about","last_name"],
"query": "孫悟空 OR 張三"
}
}
}
高亮搜索
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "西游記"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}
結(jié)構(gòu)查詢語法:
term-將按照存儲在倒排索引中的確切字詞進行操作,這些查詢通常用于數(shù)字,日期和枚舉等結(jié)構(gòu)化數(shù)據(jù),而不是全文本字段。 或者,它們允許您制作低級查詢,并在分析過程之前進行
GET /megacorp/employee/_search
{
"query": {
"term": {
"age":"33"
}
}
}
range范圍查詢-如查詢age大于等于20小于等于30的記錄
GET /megacorp/employee/_search
{
"query": {
"range": {
"age":{
"gte":20,
"lte":30
}
}
}
}
二、fiter context
主要特點:
- 是否包含
確定是否包含在檢索結(jié)果中,回答只有“是”或“否”,精確搜索
- 不涉及評分
在搜索中沒有額外的相關(guān)度排名
- 針對結(jié)構(gòu)化數(shù)據(jù)
適用于完全精確匹配,范圍檢索
- 更快
只確定是否包括結(jié)果中,不需要考慮得分。
為什么會更快?——經(jīng)常使用的過濾器將被Elasticsearch自動緩存,以提高性能。
查詢語法:
bool關(guān)鍵字
GET /megacorp/employee/_search
{
"query": {
"bool": {
"filter":{
"term":{
"age":33
}
}
}
}
}
復合查詢
constant_score-將查詢內(nèi)部的結(jié)果文檔得分都設定為1或者boost的值,多用于結(jié)合bool查詢實現(xiàn)自定義得分
GET /megacorp/employee/_search
{
"query": {
"constant_score": {
"filter":{
"match":{
"about":"張三"
}
}
}
}
}
自定義分數(shù)
GET /megacorp/employee/_search
{
"query": {
"constant_score": {
"filter":{
"match":{
"about":"張三"
}
}
},
"boots":2
}
}
should-至少滿足一個條件
GET /megacorp/employee/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"first_name": "孫悟空"
}
},
{
"match": {
"last_name": "張三"
}
}
]
}
}
}
must-必須同時滿足所有條件
GET /megacorp/employee/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"first_name": "孫悟空"
}
},
{
"match": {
"about": "四大名著"
}
}
]
}
}
}
must_not-必須不滿足條件 如:查不等于33歲的記錄
GET /megacorp/employee/_search
{
"query": {
"bool": {
"must_not":
{
"term": {
"age": "33"
}
}
}
}
}
組合查詢
GET /megacorp/employee/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"first_name": "孫悟空"
}
},
{
"match": {
"about": "四大名著"
}
}
],
"filter":{
"term":{
"age":35
}
}
}
}
}
聚合查詢GROUP BY
//根據(jù)年齡字段分組
GET /megacorp/employee/_search
{
"aggs": {
"group_by_age": {
"terms": { "field": "age" }
}
}
}
//根據(jù)條件分組
GET /megacorp/employee/_search
{
"query": {
"match": {
"last_name": "孫悟空"
}
},
"aggs": {
"group_by_age": {
"terms": { "field": "age" }
}
}
}
//分組并求平均值
GET /megacorp/employee/_search
{
"aggs": {
"group_by_age": {
"terms": { "field": "age" },
"aggs" : {
"avg_age" : {
"avg" : { "field" : "age" }
}
}
}
}
}