es命令大全,elasticsearch命令詳解


 

 

參考鏈接

 

Relational DB Elasticsearch
數據庫(database) 索引(indices)
表(tables) types
行(rows) documents
字段(columns) fields

庫表行字段,index,type,id,fields,  索引類型文檔字段

 

創建一篇文檔

PUT t1/doc/1     
{
 "name": "小黑的小姨媽",
 "age": 18
}

index/type/id  id是單個文檔

查詢所有索引

GET _cat/indices?v

查詢指定的索引信息

GET t1

查詢文檔信息

GET t1/doc/1           #查詢指定文檔
GET t1/doc/_search #查詢所有文檔

刪除指定索引

DELETE /t1

 

修改文檔

PUT zhifou/doc/1
{
  "name":"顧老二",
  "age":30,
  "from": "gu",
  "desc": "皮膚黑、武器長、性格直",
  "tags": ["", "", ""]
}

我們要將黑修改成黃:
POST zhifou/doc/1/_update
{
  "doc": {
    "desc": "皮膚很黃,武器很長,性格很直",
    "tags": ["很黃","很長", "很直"]
  }
}

查詢字符串

方式一:
GET zhifou/doc/_search?q=from:gu
屬性是from,屬性值是gu的文檔

方式二:
GET zhifou/doc/_search
{
  "query": {
    "match": {
      "from": "gu"
    }
  }
}

match條件查詢,查詢含有(匹配)指定字段值的文檔

我們查看來自顧家的都有哪些人

GET zhifou/doc/_search
{
  "query": {
    "match": {
      "from": "gu"
    }
  }
}

match查詢全部文檔

查詢zhifou索引下的doc類型中的所有文檔,那就是查詢全部

GET zhifou/doc/_search
{
  "query": {
    "match_all": {}
  }
}

match_phrase(短語查詢)

GET t1/doc/_search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "中國"
      }
    }
  }
}

title字段中包含短語中國
GET t1/doc/_search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "中國世界",
        "slop": 2
      }
    }
  }
}
slop了。相當於正則中的中國.*?世界。這個間隔默認為0,指定短語間隔

match_phrase_prefix(最左前綴查詢)

GET t3/doc/_search
{
  "query": {
    "match_phrase_prefix": {
      "desc": "bea"
    }
  }
}
desc字段bea開頭字
GET t3/doc/_search
{
  "query": {
    "match_phrase_prefix": {
      "desc": {
        "query": "bea",
        "max_expansions": 1
      }
      
    }
  }
}
max_expansions來設置最大的前綴擴展數量

multi_match(多字段查詢)

方法一:
GET t3/doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "beautiful"
          }
        },
        {
          "match": {
            "desc": "beautiful"
          }
        }
      ]
    }
  }
}

方法二:
GET t3/doc/_search
{
  "query": {
    "multi_match": {
      "query": "beautiful",
      "fields": ["title", "desc"]
    }
  }
}

multi_match甚至可以當做match_phrase和match_phrase_prefix使用,只需要指定type類型即可:
GET t3/doc/_search
{
  "query": {
    "multi_match": {
      "query": "gi",
      "fields": ["title"],
      "type": "phrase_prefix"
    }
  }
}
GET t3/doc/_search
{
  "query": {
    "multi_match": {
      "query": "girl",
      "fields": ["title"],
      "type": "phrase"
    }
  }
}

 

term查詢#單個匹配項

POST _analyze
{
  "analyzer": "standard",
  "text": "Beautiful girl!"
}
# 結果
["beautiful", "girl"]

GET w10/doc/_search
{
  "query": {
    "term": {
      "t1": "beautiful"
    }
  }
}

terms查詢#多個匹配項

GET w10/doc/_search
{
  "query": {
    "terms": {
      "t1": ["beautiful", "sexy"]
    }
  }
}

排序查詢:sort#按某個字段降序查詢

查詢顧府都有哪些人,並根據age字段按照降序

GET zhifou/doc/_search
{
  "query": {
    "match": {
      "from": "gu"
    }
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}

按某個字段升序查詢

GET zhifou/doc/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}

分頁查詢:from/size#

GET zhifou/doc/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ], 
  "from": 2,
  "size": 1
}

from:從哪開始查
size:返回幾條結果

bool查詢

must#(and)並且,滿足多個條件

單個條件查詢:布爾查詢所有from屬性為gu的數據:
GET zhifou/doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "from": "gu"
          }
        }
      ]
    }
  }
}
多個條件查詢:想要查詢from為gu,並且age為30的數據
GET zhifou/doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "from": "gu"
          }
        },
        {
          "match": {
            "age": 30
          }
        }
      ]
    }
  }
}

bool查詢should(or),滿足一個就行

查詢只要是from為gu或者tags為閉月的數據
GET zhifou/doc/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "from": "gu"
          }
        },
        {
          "match": {
            "tags": "閉月"
          }
        }
      ]
    }
  }
}

 

bool查詢must_not(not)  既不,也不是

查詢from既不是gu並且tags也不是可愛,還有age不是18的數據
GET zhifou/doc/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "from": "gu"
          }
        },
        {
          "match": {
            "tags": "可愛"
          }
        },
        {
          "match": {
            "age": 18
          }
        }
      ]
    }
  }
}

 

bool查詢filter  滿足某個條件,某個字段還可以比較大小范圍

查詢from為gu,age大於25的數據 。比較符號有gt gte lt lte
GET zhifou/doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "from": "gu"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gt": 25
          }
        }
      }
    }
  }
}

查詢from是gu,age在25~30之間
GET zhifou/doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "from": "gu"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gte": 25,
            "lte": 30
          }
        }
      }
    }
  }
}
must改成should,滿足下面的filter但不滿足上面的match也會顯示出來
GET zhifou/doc/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "from": "sheng"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "lte": 25
          }
        }
      }
    }
  }
}
  • must:與關系,相當於關系型數據庫中的and
  • should:或關系,相當於關系型數據庫中的or
  • must_not:非關系,相當於關系型數據庫中的not
  • filter:過濾條件。
  • range:條件篩選范圍。
  • gt:大於,相當於關系型數據庫中的>
  • gte:大於等於,相當於關系型數據庫中的>=
  • lt:小於,相當於關系型數據庫中的<
  • lte:小於等於,相當於關系型數據庫中的<=

結果過濾:_source

在所有的結果中,我只需要查看name和age兩個屬性,其他的不要
GET zhifou/doc/_search
{
  "query": {
    "match": {
      "name": "顧老二"
    }
  },
  "_source": ["name", "age"]
}

 

avg

查詢from是gu的人的平均年齡
GET zhifou/doc/_search
{
  "query": {
    "match": {
      "from": "gu"
    }
  },
  "aggs": {
    "my_avg": {
      "avg": {
        "field": "age"
      }
    }
  },
  "_source": ["name", "age"]
}
首先匹配查詢from是gu的數據。在此基礎上做查詢平均值的操作,這里就用到了聚合函數,其語法被封裝在aggs中,而my_avg則是為查詢結果起個別名,封裝了計算出的平均值。那么,要以什么屬性作為條件呢?是age年齡,查年齡的什么呢?是avg,查平均年齡。

只想看平均值
GET zhifou/doc/_search
{
  "query": {
    "match": {
      "from": "gu"
    }
  },
  "aggs": {
    "my_avg": {
      "avg": {
        "field": "age"
      }
    }
  },
  "size": 0, 
  "_source": ["name", "age"]
}

max

GET zhifou/doc/_search
{
  "query": {
    "match": {
      "from": "gu"
    }
  },
  "aggs": {
    "my_max": {
      "max": {
        "field": "age"
      }
    }
  },
  "size": 0
}

min

GET zhifou/doc/_search
{
  "query": {
    "match": {
      "from": "gu"
    }
  },
  "aggs": {
    "my_min": {
      "min": {
        "field": "age"
      }
    }
  },
  "size": 0
}

sum

GET zhifou/doc/_search
{
  "query": {
    "match": {
      "from": "gu"
    }
  },
  "aggs": {
    "my_sum": {
      "sum": {
        "field": "age"
      }
    }
  },
  "size": 0
}

分組查詢和分組聚合

查詢所有人的年齡段,並且按照15~2020~25,25~30分組,
GET zhifou/doc/_search
{
  "size": 0, 
  "query": {
    "match_all": {}
  },
  "aggs": {
    "age_group": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 15,
            "to": 20
          },
          {
            "from": 20,
            "to": 25
          },
          {
            "from": 25,
            "to": 30
          }
        ]
      }
    }
  }
}

查詢所有人的年齡段,並且按照15~2020~25,25~30分組,並且算出每組的平均年齡。每個小組內的數據做平均年齡處理。
GET zhifou/doc/_search
{
  "size": 0, 
  "query": {
    "match_all": {}
  },
  "aggs": {
    "age_group": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 15,
            "to": 20
          },
          {
            "from": 20,
            "to": 25
          },
          {
            "from": 25,
            "to": 30
          }
        ]
      },
      "aggs": {
        "my_avg": {
          "avg": {
            "field": "age"
          }
        }
      }
    }
  }
}

 

 

 

 

 

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM