1.查看所有index:
GET _cat/indices
2.查看索引的type、mapping:
GET [indexName]/_mapping
GET [indexName]/_mappings
GET [indexName]/_mapping/[type]
3.查看某索引的某類型的數(shù)據(jù):
GET [indexName]/[type]/_search
GET browser_device_idx/tbl_device/_search
{
"query":
{
"match": {
"groupid":1174
}
}
}
GET browser_device_idx/tbl_device/_search
{
"query":
{
"match": {
"groupid":1174
}
}
}
4.過濾:
DELETE [indexName]/[type]/_query
{
"query": {
"filtered": {
"query": { "match": { "email": "business opportunity" }},
"filter": { "term": { "folder": "inbox" }}
}
}
}
5.查看分詞器分詞:
常用分詞器:standard whitespace simple english pinyin ik
GET /_analyze?analyzer=ik
{
"text":"ahaha傻笑high"
}
GET /_analyze
{
"analyzer":"ik",
"text":"ahaha傻笑high"
}
6.查詢指定的部分字段:
通過限定 _source 字段來請求指定字段
GET [indexName]/[type]/_search
{
"query": { "match_all": {}},
"_source": [ "title", "created" ]
}
7.范圍查詢:
GET [indexName]/[type]/_search
{
"query":{
"range":{
"字段名":{//范圍[148,200)
"gte":148,
"lt":200
}
}
}
}
8.重建索引:
使用【scan-scoll】來批量讀取舊索引的文檔,然后將通過【bulk API】來將它們推送給新的索引。
GET /old_index/_search?search_type=scan&scroll=1m
{
//獲取數(shù)據(jù)應(yīng)該通過范圍查詢進行
//scan-scoll性能比通過范圍在各個分片排序再獲取好
"query": {
"range": {
"date": {
"gte": "2014-01-01",
"lt": "2014-02-01"
}
}
},
"size": 1000
}
POST _bulk//批量操作
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
9.索引別名alias aliases:
檢測這個別名指向哪個索引:
GET /*/_alias/my_index
檢測哪些別名指向這個索引:
GET /my_index_v1/_alias/*
新索引中添加別名的同時從舊索引中刪除它。這個操作需要原子化,所以我們需要用 _aliases :
POST /_aliases
{
"actions": [
{ "remove": { "index": "my_index_v1", "alias": "my_index" }},
{ "add": { "index": "my_index_v2", "alias": "my_index" }}
]
}
10.范圍查詢并排序:
GET [indexName]/[type]/_search
{
"query": {
"range": {
"date": {
"gte": "2014-01-01",
"lt": "2014-02-01"
}
}
},
"sort": [
{
"createTime":"desc"
}
]
}
11.排序
GET mcms_iflow/tbl_news_iflow/_search
{
"_source": ["abstract_desc","title","channel_id"],
"query":{
"match_phrase": {
"abstract_desc":"可愛"
}
},
"sort": [
{
"channel_id": {//一級
"order": "asc",
"missing":"_last"http://該字段缺省的文檔排到最后
}
},
{
"add_time": {//二級
"order": "desc"
}
}
]
}
12.驗證查詢語句:
GET [indexName]/[type]/_validate/query?explain
{//能夠查看es對查詢語句的解釋
"query": {
...
}
}
13.查詢結(jié)果數(shù)量、分頁:
//如果無from、size設(shè)置,from默認(rèn)為0,size默認(rèn)為10,默認(rèn)查詢出前10個
GET mcms_iflow/tbl_news_iflow/_search
{
"query":{
"match_all": {}
},
"size":20//查前20條
}
//查詢出從第10條開始,取20條
GET mcms_iflow/tbl_news_iflow/_search
{
"query":{
"match_all": {}
},
"from":10,
"size":20
}
//如果搜索size大于10000,需要設(shè)置index.max_result_window參數(shù)
//注意:size的大小不能超過index.max_result_window這個參數(shù)的設(shè)置,默認(rèn)為10,000。
PUT _settings
{
"index": {
"max_result_window": "10000000"
}
}
進階:Elasticsearch——分頁查詢From&Size VS scroll、Elasticsearch from+size 超過10000結(jié)果解決方法
14.搜索 實例:
//(標(biāo)題含k或摘要含k)&&(范圍[a,b])&&(品類為x&&文本類型為y)
//bool可以嵌套,match_phrase精確匹配,范圍可lt lte gt gte,
GET mcms_iflow/tbl_news_iflow/_search
{
"query":{
"bool":{
"must":[{
"bool":{
"should":[{
"match":{
"title":{
"query":"櫻子",
"type":"phrase"
}
}
},{
"match_phrase":{
"abstract_desc":"櫻子"
}
}]
}
},{
"range":{
"add_time": {
"from": "1487927871500",
"to": "1492185599999",
"include_lower":true,
"include_upper":true
}
}
},{
"bool":{
"must":[{
"term":{
"variety_type": "1"
}
},{
"term":{
"articletype": "1"
}
}]
}
} ]
}
}
}
15.聚合-高級統(tǒng)計
POST mcms_iflow/tbl_news_iflow/_search
{
"size":0,
"aggs":{
"grades_stats":{
"extended_stats": {
"field": "add_time"
}
}
}
}
更多聚合:Elasticsearch分析聚合、官方-Aggregations
16.短語匹配 match_phrase
GET mcms_iflow/tbl_news_iflow/_search
{
"_source": ["abstract_desc","title"],//只顯示指定字段
"query":{
"match_phrase": {//短語匹配
"abstract_desc":"可愛"
}
}
}