【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

 

posted @ 2022-04-16 21:45  逆火狂飙  阅读(1455)  评论(0编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东