Elasticsearch---常用的Query DSL搜索語法整理

  • 查詢所有
GET /_search    //所有索引,所有type下的所有數(shù)據(jù)都搜索出來
{
  "query": {
    "match_all": {}
  }
}
GET /test_index/_search    //指定一個(gè)index,搜索其下所有type的數(shù)據(jù)
{
  "query": {
    "match_all": {}
  }
}
GET /test_index,test_index2/_search    //同時(shí)搜索兩個(gè)index下的數(shù)據(jù)
{
  "query": {
    "match_all": {}
  }
}
GET /*1,*2/_search    //按照通配符去匹配多個(gè)索引
{
  "query": {
    "match_all": {}
  }
}
GET /test_index/test_type/_search    //搜索一個(gè)index下指定的type的數(shù)據(jù)
{
  "query": {
    "match_all": {}
  }
}
GET /test_index/test_type,test_type2/_search    //可以搜索一個(gè)index下多個(gè)type的數(shù)據(jù)
{
  "query": {
    "match_all": {}
  }
}
GET /test_index,test_index2/test_type,test_type2/_search    //搜索多個(gè)index下的多個(gè)type的數(shù)據(jù)
{
  "query": {
    "match_all": {}
  }
}
GET /_all/test_type,test_type2/_search    //可以代表搜索所有index下的指定type的數(shù)據(jù)
{
  "query": {
    "match_all": {}
  }
}
  • match
GET /_search
{
  "query": {
    "match": {
      "title": "elasticsearch"
    }
  }
}
  • multi match
GET /_search
{
  "query": {
    "multi_match": {
      "query": "elasticsearch",
      "fields": ["title","content"]
    }
  }
}
  • range query
GET /company/employee/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 30
      }
    }
  }
}
  • term query
//term 查詢被用于精確值 匹配,這些精確值可能是數(shù)字、時(shí)間、布爾或者那些 not_analyzed 的字符串
//term 查詢對(duì)于輸入的文本不 分析 ,所以它將給定的值進(jìn)行精確查詢
GET /_search
{
  "query": {
    "term": {
      "title":"test hello"
    }
  }
}
  • terms query
//terms 查詢和 term 查詢一樣,但它允許你指定多值進(jìn)行匹配。
//如果這個(gè)字段包含了指定值中的任何一個(gè)值,那么這個(gè)文檔滿足條件:
{ "terms": { "tag": [ "search", "full_text", "nosql" ] }}

定制排序

查詢的默認(rèn)情況是按照_score排序的,然而某些情況下,可能沒有有用的_score,比如說filter或constant_score

GET _search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "author_id": 110
        }
      }
    }
  }
}
GET _search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "author_id": 110
        }
      }
    }
  }
}

定制排序規(guī)則

GET /company/employee/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "range":{
          "age":{
            "gte":30
          }
        }
      }
    }
  },
  "sort": [
    {
      "join_date": {
        "order": "asc"
      }
    }
  ]
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容