【Elasticsearch/CURL】CURL訪問ES常用語句之一--CRUD


注:下文中-u elastic:123456是為了適應本機設上了密碼的ES,如讀者的沒有設置,那么請忽略這部分內容。

 

【查看索引信息】

命令:

curl -u elastic:123456 -XGET 'localhost:9200/_cat/indices?v'

反饋:

health status index       uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .security-7 c3ozcN3FRAyYEgn2V21ypA   1   0          7            0     25.7kb         25.7kb

 

【顯示node節點信息】

命令:

curl -u elastic:123456 -XGET 'localhost:9200/_cat/nodes?v'

反饋:

ip             heap.percent ram.percent cpu load_1m load_5m load_15m node.role  master name
192.168.32.130           48          93   3    0.00    0.10     0.13 cdhilmrstw *      node-1

 

【查看集群健康狀況】

命令:

curl -u elastic:123456 -XGET 'localhost:9200/_cat/health?v'

反饋:

epoch      timestamp cluster   status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1650113556 12:52:36  liangshan green           1         1      1   1    0    0        0             0                  -                100.0%

以上三個命令都是非業務相關,只關注業務的可以先跳過。

 

【CRUD之插數據】

命令:

curl -u elastic:123456 -H "Content-Type: application/json" -XPUT 'localhost:9200/liangshan/emp/1?pretty' -d' {"name":"songjiang","age":"45","salary":"100000","hire-date":"2002-1-1T12:12:12","title":"boss"}'

反饋:

{
  "_index" : "liangshan",
  "_type" : "emp",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

命令:

curl -u elastic:123456 -H "Content-Type: application/json" -XPUT 'localhost:9200/liangshan/emp/2?pretty' -d' {"name":"wuyong","age":"44","salary":"90000","hire-date":"2001-1-1T12:12:12","title":"manager"}'

反饋:

  "_index" : "liangshan",
  "_type" : "emp",
  "_id" : "2",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

命令:

curl -u elastic:123456 -H "Content-Type: application/json" -XPUT 'localhost:9200/liangshan/emp/3?pretty' -d' {"name":"likui","age":"43","salary":"80000","hire-date":"2000-1-1T12:12:12","title":"killer","skill":"axe-wing"}'

反饋:

{
  "_index" : "liangshan",
  "_type" : "emp",
  "_id" : "3",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 4,
  "_primary_term" : 1
}

通過以上三次插值操作,就給梁山公司添加了三名雇員:宋江、吳用和李逵。

 

【CRUD之找出全部雇員】

命令:

curl -u elastic:123456 -XGET 'localhost:9200/liangshan/emp/_search?pretty'

反饋:

{
  "took" : 940,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "liangshan",
        "_type" : "emp",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "songjiang",
          "age" : "45",
          "salary" : "100000",
          "hire-date" : "2002-1-1T12:12:12",
          "title" : "boss"
        }
      },
      {
        "_index" : "liangshan",
        "_type" : "emp",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "wuyong",
          "age" : "44",
          "salary" : "90000",
          "hire-date" : "2001-1-1T12:12:12",
          "title" : "manager"
        }
      },
      {
        "_index" : "liangshan",
        "_type" : "emp",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "likui",
          "age" : "43",
          "salary" : "80000",
          "hire-date" : "2000-1-1T12:12:12",
          "title" : "killer",
          "skill" : "axe-wing"
        }
      }
    ]
  }
}

 

【CRUD之按名稱找雇員】

命令:

curl -u elastic:123456 -XGET 'localhost:9200/liangshan/emp/_search?q=name:likui&pretty'

反饋: 

{
  "took" : 110,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.9808291,
    "hits" : [
      {
        "_index" : "liangshan",
        "_type" : "emp",
        "_id" : "3",
        "_score" : 0.9808291,
        "_source" : {
          "name" : "likui",
          "age" : "43",
          "salary" : "80000",
          "hire-date" : "2000-1-1T12:12:12",
          "title" : "killer",
          "skill" : "axe-wing"
        }
      }
    ]
  }
}

可以看到,還真把逵子找出來了。

按名稱查詢的另一種方式:

命令:

curl -u elastic:123456 -H "Content-Type: application/json" -XGET 'localhost:9200/liangshan/emp/_search?pretty' -d' {
    "query":{
         "match":{               
               "name":"likui"
         }
    }
}'

反饋: 

{
  "took" : 12,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.9808291,
    "hits" : [
      {
        "_index" : "liangshan",
        "_type" : "emp",
        "_id" : "3",
        "_score" : 0.9808291,
        "_source" : {
          "name" : "likui",
          "age" : "43",
          "salary" : "80000",
          "hire-date" : "2000-1-1T12:12:12",
          "title" : "killer",
          "skill" : "axe-wing"
        }
      }
    ]
  }
}

這樣也可找出逵子。

 

【CRUD之按年齡過濾】

命令:

curl -u elastic:123456 -H "Content-Type: application/json" -XGET 'localhost:9200/liangshan/emp/_search?pretty' -d' {
    "query":{
         "bool":{               
               "filter":{
                   "range":{
                       "age":{"gt":"43"}
                   }
               }
         }
    }
}'

反饋:

{
  "took" : 67,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "liangshan",
        "_type" : "emp",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "name" : "songjiang",
          "age" : "45",
          "salary" : "100000",
          "hire-date" : "2002-1-1T12:12:12",
          "title" : "boss"
        }
      },
      {
        "_index" : "liangshan",
        "_type" : "emp",
        "_id" : "2",
        "_score" : 0.0,
        "_source" : {
          "name" : "wuyong",
          "age" : "44",
          "salary" : "90000",
          "hire-date" : "2001-1-1T12:12:12",
          "title" : "manager"
        }
      }
    ]
  }
}

想找43歲以上員工,於是45歲的宋江和44歲的吳用就被找出來了,43的逵子自然不在列。

 

【CRUD之按id找出數據】

命令:

curl -u elastic:123456 -XGET 'localhost:9200/liangshan/emp/3?pretty'

反饋:

{
  "_index" : "liangshan",
  "_type" : "emp",
  "_id" : "3",
  "_version" : 2,
  "_seq_no" : 7,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "likui",
    "age" : "43",
    "salary" : "80000",
    "hire-date" : "2000-1-1T12:12:12",
    "title" : "killer",
    "skill" : "axe-wing"
  }
}

命令目的是查找李逵,還真找到了。

 

【CRUD之 按名字查找】

命令:

curl -u elastic:123456 -XGET 'localhost:9200/liangshan/emp/_search?q=name:likui&pretty'

反饋:

{
  "took" : 638,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.9808291,
    "hits" : [
      {
        "_index" : "liangshan",
        "_type" : "emp",
        "_id" : "3",
        "_score" : 0.9808291,
        "_source" : {
          "name" : "likui",
          "age" : "43",
          "salary" : "80000",
          "hire-date" : "2000-1-1T12:12:12",
          "title" : "killer",
          "skill" : "axe-wing"
        }
      }
    ]
  }
}

 

【CRUD之按技能查找】

命令:

curl -u elastic:123456 -XGET 'localhost:9200/liangshan/emp/_search?q=skill:axe-wing&pretty'

反饋:

{
  "took" : 36,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "liangshan",
        "_type" : "emp",
        "_id" : "3",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "likui",
          "age" : "43",
          "salary" : "80000",
          "hire-date" : "2000-1-1T12:12:12",
          "title" : "killer",
          "skill" : "axe-wing"
        }
      }
    ]
  }
}

只有李逵一人有技能,名為旋風斧,還真找到了。

 

【CRUD之按年齡范圍查找】

命令:

curl -u elastic:123456 -H "Content-Type: application/json" -XGET 'localhost:9200/liangshan/emp/_search?pretty' -d' {
    "query":{
         "range":{
               "age":{"from":"43","to":"44"}
         }
    }
}'

反饋:

{
  "took" : 45,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "liangshan",
        "_type" : "emp",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "wuyong",
          "age" : "44",
          "salary" : "90000",
          "hire-date" : "2001-1-1T12:12:12",
          "title" : "manager"
        }
      },
      {
        "_index" : "liangshan",
        "_type" : "emp",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "likui",
          "age" : "43",
          "salary" : "80000",
          "hire-date" : "2000-1-1T12:12:12",
          "title" : "killer",
          "skill" : "axe-wing"
        }
      }
    ]
  }
}

43是李逵,44是吳用,還真找到了這兩人。

 

【CRUD之按薪水范圍查找】

命令:

curl -u elastic:123456 -H "Content-Type: application/json" -XGET 'localhost:9200/liangshan/emp/_search?pretty' -d' {
    "query":{
         "range":{               
               "salary":{"from":"75000","to":"85000"}
         }
    }
}'

輸出:

{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "liangshan",
        "_type" : "emp",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "likui",
          "age" : "43",
          "salary" : "80000",
          "hire-date" : "2000-1-1T12:12:12",
          "title" : "killer",
          "skill" : "axe-wing"
        }
      }
    ]
  }
}

薪水在七萬五到八萬之間的只有逵子,還真給找出來了。

 

【CRUD之刪除記錄】

命令:

curl -u elastic:123456 -XDELETE 'localhost:9200/liangshan/emp/2?pretty'

反饋:

{
  "_index" : "liangshan",
  "_type" : "emp",
  "_id" : "2",
  "_version" : 5,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 8,
  "_primary_term" : 1
}

確認是否刪除掉了:

curl -u elastic:123456 -XGET 'localhost:9200/liangshan/emp/2?pretty'

反饋:

{
  "_index" : "liangshan",
  "_type" : "emp",
  "_id" : "2",
  "found" : false
}

 

【全文搜索之匹配】

這種模式能拆分短語,然后進行匹配。

首先我們需要創建三條數據:

curl -u elastic:123456 -H "Content-Type: application/json" -XPUT 'localhost:9200/liangshan/emp/4?pretty' -d' {"name":"linchong","age":"42","salary":"70000","hire-date":"1999-1-1T12:12:12","title":"clerk","interest":"rock climbming"}'
curl -u elastic:123456 -H "Content-Type: application/json" -XPUT 'localhost:9200/liangshan/emp/5?pretty' -d' {"name":"wusong","age":"41","salary":"60000","hire-date":"1998-1-1T12:12:12","title":"clerk","interest":"hunting"}'
curl -u elastic:123456 -H "Content-Type: application/json" -XPUT 'localhost:9200/liangshan/emp/6?pretty' -d' {"name":"huarong","age":"40","salary":"50000","hire-date":"1997-1-1T12:12:12","title":"clerk","interest":"rock discovery"}'

然后找興趣為攀岩的,命令是:

curl -u elastic:123456 -H "Content-Type: application/json" -XGET 'localhost:9200/liangshan/emp/_search?pretty' -d' {
    "query":{
         "match":{               
               "interest":"rock climbming"
         }
    }
}'

反饋是:

{
  "took" : 243,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.2832261,
    "hits" : [
      {
        "_index" : "liangshan",
        "_type" : "emp",
        "_id" : "4",
        "_score" : 1.2832261,
        "_source" : {
          "name" : "linchong",
          "age" : "42",
          "salary" : "70000",
          "hire-date" : "1999-1-1T12:12:12",
          "title" : "clerk",
          "interest" : "rock climbming"
        }
      },
      {
        "_index" : "liangshan",
        "_type" : "emp",
        "_id" : "6",
        "_score" : 0.4889865,
        "_source" : {
          "name" : "huarong",
          "age" : "40",
          "salary" : "50000",
          "hire-date" : "1997-1-1T12:12:12",
          "title" : "clerk",
          "interest" : "rock discovery"
        }
      }
    ]
  }
}

可以看到,林沖和花榮都被找出來了,他們按相關性高低排序,林沖得分1.28,他的興趣與查詢關鍵詞匹配度較高;花榮得分0.48,他的興趣只與關鍵詞有部分匹配。

這種查詢方式在傳統數據庫上實現起來已經很困難了。

 

【全文檢索之短語匹配】 

這種模式不進行關鍵詞拆分,而是去看全文中包含完整短語的匹配。

命令:

curl -u elastic:123456 -H "Content-Type: application/json" -XGET 'localhost:9200/liangshan/emp/_search?pretty' -d' {
    "query":{
         "match_phrase":{               
               "interest":"rock discovery"
         }
    }
}'

反饋:

{
  "took" : 31,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.7466557,
    "hits" : [
      {
        "_index" : "liangshan",
        "_type" : "emp",
        "_id" : "6",
        "_score" : 1.7466557,
        "_source" : {
          "name" : "huarong",
          "age" : "40",
          "salary" : "50000",
          "hire-date" : "1997-1-1T12:12:12",
          "title" : "clerk",
          "interest" : "rock discovery"
        }
      }
    ]
  }
}

結果花榮被找出來了。

 

【高亮搜索】

首先我們更新一下花榮的數據:

curl -u elastic:123456 -H "Content-Type: application/json" -XPUT 'localhost:9200/liangshan/emp/6?pretty' -d' {"name":"huarong","age":"40","salary":"50000","hire-date":"1997-1-1T12:12:12","title":"clerk","interest":"He likes rock discovery on spare time."}'

高亮搜索命令:

curl -u elastic:123456 -H "Content-Type: application/json" -XGET 'localhost:9200/liangshan/emp/_search?pretty' -d' {
    "query":{
         "match_phrase":{               
               "interest":"rock discovery"
         }
    },
    "highlight":{
       "fields":{
           "interest":{}
       }
   }
}'

反饋:

{
  "took" : 493,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.8474333,
    "hits" : [
      {
        "_index" : "liangshan",
        "_type" : "emp",
        "_id" : "6",
        "_score" : 0.8474333,
        "_source" : {
          "name" : "huarong",
          "age" : "40",
          "salary" : "50000",
          "hire-date" : "1997-1-1T12:12:12",
          "title" : "clerk",
          "interest" : "He likes rock discovery on spare time."
        },
        "highlight" : {
          "interest" : [
            "He likes <em>rock</em> <em>discovery</em> on spare time."
          ]
        }
      }
    ]
  }
}

可以看到查詢結果中多了一個highlight的部分,這個部分包含了interest屬性匹配的文本部分,並以HTML標簽<em></em>封裝。

 

END

 


免責聲明!

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



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