当前位置: 代码迷 >> 综合 >> elasticsearch 学习总结 - 请求交互
  详细解决方案

elasticsearch 学习总结 - 请求交互

热度:35   发布时间:2024-01-30 21:30:56.0
2、 elasticsearch 交互elasticsearch 支持 restful api 的请求标准请求结构体如下请求格式:curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'VERB http 的请求方式 GET POST PUT DELETEPROTOCOL http 或 httpsHOST 主机IPPORT 端口PATH API 端路径QUERY_STRING 任意可选的查询字符串参数BODY json格式的请求体PUT insert文档实例:curl -X PUT "localhost:9200/megacorp/employee/1?pretty" -H 'Content-Type: application/json' -d'{"first_name" : "John","last_name" :  "Smith","age" :        25,"about" :      "I love to go rock climbing","interests": [ "sports", "music" ]}请求结构 分析 请求方式为PUT host--localhost port--9200 path--/megacorp/employee/1 query_string-- pretty-H--注明请求头信息 -d--请求体path 中 第一个参数megacorp是索引,第二个参数employee为类型,第三个参数1为IDGET 检索文档实例:这在 Elasticsearch 中很简单。简单地执行 一个 HTTP GET 请求并指定文档的地址——索引库、类型和ID索引 相当于mysql 中的数据库 ;type 类型相当于表名 同一type保存同一类型的数据GET /megacorp/employee/1返回结果{"_index" :   "megacorp", #索引"_type" :    "employee", #类型"_id" :      "1", #ID"_version" : 1, #版本号 DELETE MAKER"found" :    true,##_source 是源数据保存路径"_source" :  {"first_name" :  "John","last_name" :   "Smith","age" :         25,"about" :       "I love to go rock climbing","interests":  [ "sports", "music" ]}}下面我们使用一些常用的文件检索curl -X GET "localhost:9200/megacorp/employee/_search?q=last_name:Smith&pretty"#使用_search 默认返回10条数据 返回的结果默认放在hits中{"took":      6,"timed_out": false,"_shards": { ... },"hits": {"total":      3,"max_score":  1,"hits": [{"_index":         "megacorp","_type":          "employee","_id":            "3","_score":         1,"_source": {"first_name":  "Douglas","last_name":   "Fir","age":         35,"about":       "I like to build cabinets","interests": [ "forestry" ]}},{"_index":         "megacorp","_type":          "employee","_id":            "1","_score":         1,"_source": {"first_name":  "John","last_name":   "Smith","age":         25,"about":       "I love to go rock climbing","interests": [ "sports", "music" ]}},{"_index":         "megacorp","_type":          "employee","_id":            "2","_score":         1,"_source": {"first_name":  "Jane","last_name":   "Smith","age":         32,"about":       "I like to collect rock albums","interests": [ "music" ]}}]}}具体查询某一条数据 GET /megacorp/employee/_search?q=last_name:Smith #这是一个简单查询的实例q(query)=last_name:Smith 查询last_name是Smith的数据 结果数据在hits中get 表达式检索curl -X GET "localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d'
{"query" : {"match" : {"last_name" : "Smith"}}
}
## bool多条件查询:  must,should,must_not 查询的三种方式 must 必须满足 should 应该满足 not_must 不满足, filter过滤
## must 列表 中是联合查询的条件 类型如下:term, perfix, wildcard, regexp, fuzzy, boost, dis_max , [match_all 查询所有的]
## from 设置偏移量, size显示条数 ,sort 排序
## es的aggs可以分为度量聚合和桶聚合,下面就直接实战开发中经常用到的语句
{"query": { ... },"size": 0,"aggs": {"custom_name1": {  //aggs后面接著的是一个自定义的name"桶": { ... }  //再来才是接桶},"custom_name2": {  //一个aggs裡可以有很多聚合"桶": { ... }},"custom_name3": {"桶": {               .....            },"aggs": {  //aggs可以嵌套在别的aggs裡面"in_name": { //记得使用aggs需要先自定义一个name"桶": { ... } //in_name的桶作用的文档是custom_name3的桶的结果}            }        }    }}
'
  相关解决方案