You know, for search
# 測驗ES是否啟動成功
GET ?pretty
# 計算集群中檔案的數量
GET /_count?pretty
{
"query": {
"match_all": {}
}
}
# 創建索引
# 有則追加資料,無索引則默認創建
# 索引名/型別名稱/id
PUT /megacorp/employee/1
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : ["sports", "music"]
}
PUT /megacorp/employee/2
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests" : ["music"]
}
PUT /megacorp/employee/3
{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about" : "I like to build cabinets",
"interests" : ["forestry"]
}
# 檢索檔案
# _source屬性是原始json檔案
GET /megacorp/employee/1
GET /megacorp/employee/2
GET /megacorp/employee/3
# 更新檔案
# 只需再次PUT就可以
# 注意,完全覆寫更新,包括減少欄位
PUT /megacorp/employee/3
{
"interests" : ["forestry", "programming1"]
}
# 洗掉檔案
DELETE /megacorp/employee/3
# 檢查檔案是否存在
HEAD /megacorp/employee/3
# 搜索所有雇員資訊
# 回傳的結果在hits中
GET /megacorp/employee/_search
# 使用query-string進行輕量化搜索
GET /megacorp/employee/_search?q=last_name:Smith
# 使用查詢運算式搜索
# 查詢型別 match
GET /megacorp/employee/_search
{
"query": {
"match": {
"last_name": "Smith"
}
}
}
# 使用更復雜的搜索
# 查詢Smith員工,年齡要大于30歲的
GET /megacorp/employee/_search
{
"query": {
"bool": {
"must": {
"match" : {
"last_name" : "Smith"
}
} ,
"filter": {
"range": {
"age": {
"gt": 30
}
}
}
}
}
}
# 全文搜索
# match基于詞搜索
GET /megacorp/employee/_search
{
"query": {
"match": {
"about": "rock climbing"
}
}
}
# match_phrase基于短語搜索
GET /megacorp/employee/_search
{
"query": {
"match_phrase": {
"about": "rock climbing"
}
}
}
#高亮搜索
GET /megacorp/employee/_search
{
"query": {
"match": {
"about": "rock climbing"
}
},
"highlight": {
"fields": {
"about": {}
}
}
}
# 分析
# 最受歡迎的愛好
GET /megacorp/employee/_search
{
"aggs": {
"all_interests": {
"terms": {
"field": "interests.keyword"
}
}
}
}
# Smith中最受歡迎的愛好
GET /megacorp/employee/_search
{
"query": {
"match": {
"last_name": "Smith"
}
},
"aggs": {
"all_interests": {
"terms": {
"field": "interests.keyword"
}
}
}
}
# 匯總
GET /megacorp/employee/_search
{
"aggs": {
"all_interests": {
"terms": {
"field": "interests.keyword"
},
"aggs": {
"avg_age": {
"avg": {
"field": "age"
}
}
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/257062.html
標籤:其他
