1. 程式人生 > 實用技巧 >elasticsearch查詢DLS

elasticsearch查詢DLS

檢查叢集健康狀況:

GET /_cat/health?v

檢視所有索引:

GET /_cat/indices?v

建立索引:

PUT /test_index1
PUT /product
{
  "settings" : {
      "number_of_shards" : 3,
      "number_of_replicas" : 1
   }
}

POST /product/_mapping?pretty 
{
  "_source": {"enabled": true},
  "dynamic": true,
  "properties" : {
    "imei" : {"type
" : "text","analyzer" : "keyword","fielddata": true}, "ispid" : {"type":"integer","ignore_malformed": true}, "msisdn" : {"type" : "text","analyzer" : "keyword","fielddata": true}, "maddr_s" : {"type" : "text","analyzer" : "ik_max_word","norms" : false,"fields" : {"raw" : {"type" : "keyword
","doc_values":true}}}, "maddr_p" : {"type" : "text","analyzer" : "ik_max_word","norms" : false,"fields" : {"raw" : {"type" : "keyword","doc_values":true}}}, "maddr_c" : {"type" : "text","analyzer" : "ik_max_word","norms" : false,"fields" : {"raw" : {"type" : "keyword","doc_values":true}}},
"netaddr_s" : {"type" : "text","analyzer" : "ik_max_word","norms" : false,"fields" : {"raw" : {"type" : "keyword","doc_values":true}}}, "netaddr_p" : {"type" : "text","analyzer" : "ik_max_word","norms" : false,"fields" : {"raw" : {"type" : "keyword","doc_values":true}}}, "netaddr_c" : {"type" : "text","analyzer" : "ik_max_word","norms" : false,"fields" : {"raw" : {"type" : "keyword","doc_values":true}}}, "seraddr_s" : {"type" : "text","analyzer" : "ik_max_word","norms" : false,"fields" : {"raw" : {"type" : "keyword","doc_values":true}}}, "seraddr_p" : {"type" : "text","analyzer" : "ik_max_word","norms" : false,"fields" : {"raw" : {"type" : "keyword","doc_values":true}}}, "seraddr_c" : {"type" : "text","analyzer" : "ik_max_word","norms" : false,"fields" : {"raw" : {"type" : "keyword","doc_values":true}}}, "uli" : {"type" : "text","analyzer" : "ik_max_word","norms" : false,"fields" : {"raw" : {"type" : "keyword","doc_values":true}}}, "x_sip" : {"type" : "ip"}, "x_dip" : {"type" : "ip"}, "i_sport" : {"type" : "keyword","doc_values":true}, "i_dport" : {"type" : "keyword","doc_values":true}, "guti" : {"type" : "keyword","doc_values":true}, "i_nsapi" : {"type" : "long","ignore_malformed": true}, "apn" : {"type" : "text", "analyzer" : "keyword"}, "rai" : {"type" : "text", "analyzer" : "keyword"}, "gsnu" : {"type" : "text", "analyzer" : "keyword"}, "teid" : {"type" : "text", "analyzer" : "keyword"}, "endtime" : {"type" : "long","ignore_malformed": true}, "id" : {"type" : "long"}, "i_type" : {"type" : "text","analyzer" : "keyword"}, "conndirect" : {"type" : "text", "analyzer" : "keyword"}, "protocoltype" : {"type" : "text" ,"analyzer" : "keyword"}, "i_trojan_type" : {"type" : "long","ignore_malformed": true}, "pguti":{"type" : "text" ,"analyzer" : "keyword"}, "sid":{"type" : "text" ,"analyzer" : "keyword"}, "x_imsi":{"type" : "text" ,"analyzer" : "keyword","fielddata": true}, "pteid":{"type" : "text" ,"analyzer" : "keyword"}, "flow":{"type" : "long","ignore_malformed": true}, "vpsfirm":{"type" : "text" ,"analyzer" : "keyword"}, "x_begintime":{"type" : "long","ignore_malformed": true}, "netaddr" : {"type" : "keyword","doc_values":true}, "seraddr" : {"type" : "keyword","doc_values":true}, "maddr" : {"type" : "keyword","doc_values":true}, "paddr_s" : {"type" : "text","analyzer" : "ik_max_word","norms" : false,"fields" : {"raw" : {"type" : "keyword","doc_values":true}}}, "paddr_p" : {"type" : "text","analyzer" : "ik_max_word","norms" : false,"fields" : {"raw" : {"type" : "keyword","doc_values":true}}}, "paddr_c" : {"type" : "text","analyzer" : "ik_max_word","norms" : false,"fields" : {"raw" : {"type" : "keyword","doc_values":true}}}, "paddr" : {"type" : "keyword","doc_values":true}, "sshclient" : {"type" : "keyword","doc_values":true}, "sshserver" : {"type" : "keyword","doc_values":true}, "sshver" : {"type" : "keyword","doc_values":true}, "b_firstpacket" : {"type" : "binary"}, "b_firstpacket1" : {"type" : "text", "analyzer" : "keyword"}, "appbigt" :{"type" : "integer"}, "appsubt" :{"type" : "integer"} } }

檢視索引:

GET /posts/_search

刪除索引:

DELETE /test_index

插入資料:

POST /student/_bulk
{ "index": { "_id": 1 }}
{ "name" : "liubei", "age" : 20 , "sex": "boy", "birth": "1996-01-02" , "about": "i like diaocan he girl" }
{ "index": { "_id": 2 }}
{ "name" : "guanyu", "age" : 21 , "sex": "boy", "birth": "1995-01-02" , "about": "i like diaocan" }
{ "index": { "_id": 3 }}
{ "name" : "zhangfei", "age" : 18 , "sex": "boy", "birth": "1998-01-02" , "about": "i like travel" }
{ "index": { "_id": 4 }}
{ "name" : "diaocan", "age" : 20 , "sex": "girl", "birth": "1996-01-02" , "about": "i like travel and sport" }
{ "index": { "_id": 5 }}
{ "name" : "panjinlian", "age" : 25 , "sex": "girl", "birth": "1991-01-02" , "about": "i like travel and wusong" }
{ "index": { "_id": 6 }}
{ "name" : "caocao", "age" : 30 , "sex": "boy", "birth": "1988-01-02" , "about": "i like xiaoqiao" }
{ "index": { "_id": 7 }}
{ "name" : "zhaoyun", "age" : 31 , "sex": "boy", "birth": "1997-01-02" , "about": "i like travel and music" }
{ "index": { "_id": 8 }}
{ "name" : "xiaoqiao", "age" : 18 , "sex": "girl", "birth": "1998-01-02" , "about": "i like caocao" }
{ "index": { "_id": 9 }}
{ "name" : "daqiao", "age" : 20 , "sex": "girl", "birth": "1996-01-02" , "about": "i like travel and history" }

match匹配, term精確匹配, range範圍過濾, exists包含某個欄位. all_interests自己定義的聚合名稱:

GET /student/_search
{
    "query": {
      "bool":{
        "must":[
          {
         "match": {
           "about": "travel"
             }
          },
          {
            "term":{
                "sex":"boy"
            }
            
          },
          {
            "range":{
              "age":{
                "gt":16,
                "lte":25
              }
            }
            
          },
          {
            "exists":{
              "field":"age"
            }
          }
          ]
      }
     },
    "aggs": {
    "all_interests": {
      "terms": { "field": "age" }
    }
  }
}

模糊查詢:

GET /student/_search
{"query": {
    "wildcard":{
      "name": "li"
    }
   
  }  
}

聚合查詢:all_interests自己定義的聚合名稱

GET /student/_search
{
  "aggs": {
    "all_interests": {
      "terms": { "field": "age" }
    }
  }
}