elasticsearch 基礎查詢語句與常用語句

整理常用的es查詢語句: 基於kibana的Dev Tools控制板

--------------- 索引相關查詢

//查詢所有索引及容量

    GET _cat/indices

//查詢索引映射結構
    GET my_index/_mapping

// 查詢所有索引映射結構    

    GET _all

// 查詢所有的相同前綴索引

    GET my-*/_search

// 查詢所有索引模板   

    GET _template

// 查詢具體索引模板

    GET _template/my_template

 

---------------集羣相關

 

//查詢集羣健康狀態

    GET _cluster/health

// 查詢所有節點

    GET _cat/nodes

// 查詢索引及分片的分佈
    GET _cat/shards

// 查詢所有插件

    GET _cat/plugins

 

-----------------寫入模塊

-----寫入索引模板

PUT _template/my_template
{
    "template" : "my-*",
    "order" : 0,
    "settings" : {
         "number_of_shards" : 10,
 "number_of_replicas" : 0
    },
    "mappings": {

      "default": {

  "_all": {
        "enabled": false
      },

        "properties": {
          "name": {
            "type": "text"
          },
          "age": {
            "type": "long"
          }
        }
    }
  }

}

-----------創建索引映射結構

PUT my_index
{
  "mappings": {
    "doc": {
      "properties": {
        "name": {
          "type": "text"
        },
        "blob": {
          "type": "binary"
        }
      }
    }
  }
}

-------------寫入索引

PUT my_index/doc/1
{
  "name": "Some binary blob",
  "blob": "U29tZSBiaW5hcnkgYmxvYg==" 
}

-------------刪除

// 索引

DELETE my-index

// 模板

DELETE  _template/my_template 

 

--------------DSL query查詢

---------使用本地插件查詢

{

"size": 10,

"from": 0,

"query": {
"function_score": {
"script_score": {
"script": {
"inline": "featurescore",
"lang": "native",
"params": {
"name": "you",
"age": "20"
}
}
},
"query": {
"bool": {
"filter": {
"term": {
"name": "you"
}
}
}
}
}
},
"_source": {
"includes": ["name", "age"]
},
"sort": {
"_score": {
"order": "asc"
}
}

}

// 說明 inline 指定插件名   lang指定插件形式  native是本地插件   param定義參數  插件裏使用XContentMapValues.nodeStringValue(params.get("name"), null)獲取  ,  elasticseach裏存儲的字段值使用 source().get("name") 來獲取,插件會並行處理es中每一條數據 ; 

_source 指定返回字段 , sort 指定插件處理結果的排序字段

---------基礎query

//查詢所有
GET _search
{
  "query": {
    "match_all": {}
  }
}

// 查詢單個索引 的 固定屬性

---精確匹配

GET _search
{
  "query": {
    "term": { "name" : "you" }
  }
}

 

---模糊匹配

GET _search
{
  "query": {
    "match": { "name" : "you" }
  }
}

---範圍查找

GET _search
{
  "query": {
    "range": {
        "age":{ "gte" : 15 , "lte" : 25 }
    }
  }
}

// 功能性查詢

-----過濾

GET my_index/_search
{
  "query": {
    "bool": {
      "filter": {
        "term":{"age":1095}
      }
    }
  }
}

---或  or

GET my - test / _search {
"query": {
"bool": {
"should": [{
"term": {
"name": "you"
}
}, {
"match": {
"age": 20
}
}]
}
}
}

---與 AND

GET my-test/_search
{
  "query": {
    "bool": {
      "must" : [{
        "match" : {
          "name" : "you"
        }
      },{
        "range":{
        "age":{
          "from" : 10 , "to" : 20
        } 
        }
      }]
    }
  }
}

---必須 =

GET my_index/_search
{
  "query": {
    "bool": {
      "must" : {
        "range" : {
          "age" : { "from" : 10, "to" : 20 }
        }
      }
    }
  }
}

---必須不 not

GET my_index/_search
{
  "query": {
    "bool": {
      "must_not" : {
        "term" : {
          "name" : "you"
        }
      }
    }
  }
}

----複合查找

GET my_index/_search 
{
"query": {
"bool": {
"should": [{
"match": {
"age": 40
}
}, 
{
"match": {
"age": 20
}
}],
"filter": {
  "match":{
    "name":"you"
  }
}
}
}
}

------索引遷移

---場景 從A索引 複製到B索引
POST _reindex
{
  "source": {
    "index": "my_index"
  },
  "dest": {
    "index": "new_my_index"
  }
}

 

-------------基於查詢的刪除

POST test-index/_delete_by_query
{
  "query":{
        "term": {
         "cameraId":"00000000002"
        }
  }

}

--查詢

GET test-index/_search
{
  "query":{
        "term": {
         "cameraId":"00000000002"
        }
  }
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章