美文网首页
elasticSearch Api

elasticSearch Api

作者: swoft_ | 来源:发表于2019-07-15 10:40 被阅读0次
  • URL search 在url中使用参数进行查询
  • request body search 使用es提供的json格式进行搜索

指定查询的索引

语法 范围
/_search (_search代表这是一个搜索的api) 集群上所有的索引
/index1/_search index1
/index1,index-2/_search index1 和index2
/index*/_search 以index开头的索引

CURD等常用语句

分页

GET test/_search
{
  "from": 0,  
  "size": 10, 
  "query": {
    "match_all": {}
  }
}

选取所需要的属性(select)

GET test/_search
{
  "_source": ["money","field1"],  # 选取自己关心的属性
  "sort": [{"money": "asc"}], 
  "from": 0,
  "size": 10, 
  "query": {
    "match_all": {}
  }
}

排序

GET test/_search
{
  "sort": [{"money": "asc"}], # desc 最好是在数值型和日期型上进行排序,多类型的排序,系统会随机选一个值
  "query": {
    "match_all": {}
  }
}

all

GET test/_search
{
  "profile":"true", # 可以查看具体的执行过程
  "query": {
    "match_all": {}
  }
}

# 返回结果说明
{
  "took" : 56,      # 花费时间
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2, # 总数
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0, # 得分
        "_source" : {
          "field1" : "value1",
          "field2" : "value2"
        }
      },
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "doc" : {
            "post_date" : "2019-05-15T14:12:12",
            "message" : "trying out Elasticsearch...."
          }
        }
      }
    ]
  }
}

create

#自动生成id
POST users/_doc     
{
    "user" : "Mike",
    "post_date" : "2019-04-15T14:12:12",
    "message" : "trying out Kibana"
}

#指定ID如果已经存在,就报错
PUT users/_create/1
{
     "user" : "Jack",
    "post_date" : "2019-05-15T14:12:12",
    "message" : "trying out Elasticsearch"
}

get

GET users/_doc/1

{
  "_index" : "users",   # 索引
  "_type" : "_doc",     
  "_id" : "1", # id
  "_version" : 5,  # 版本
  "_seq_no" : 33,  
  "_primary_term" : 7,
  "found" : true,
  "_source" : {  # 文档原始信息
    "user" : "Mike1"
  }
}


delete

DELETE users/_doc/1

update

POST users/_update/k6Li5WsBNdPsvl7a8qOP/
{
    "doc":{
        "post_date" : "2019-05-15T14:12:12",
        "message" : "trying out Elasticsearch...."
    }
}

index

# 先删除后写入
PUT users/_doc/k6Li5WsBNdPsvl7a8qOP
{
    "user" : "Mike1"

}

mget

GET /_mget
{
    "docs" : [
        {
            "_index" : "test",
            "_id" : "1"
        },
        {
            "_index" : "test",
            "_id" : "2"
        }
    ]
}

删除

DELETE index_name

bulk api

一次api的调用,可以对不同索引进行操作。其中一条出错不会影响所有的结果 (返回结果中包括每一条执行的结果)

POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }  
{ "field1" : "value1" }                          # 新增id为1的字段field1=value1
{ "delete" : { "_index" : "test", "_id" : "2" } } # 删除id为2 的失败,不存在
{ "create" : { "_index" : "test2", "_id" : "3" } } 
{ "field1" : "value3" }                         # 新增id为3的记录
{ "update" : {"_id" : "1", "_index" : "test"} } 
{ "doc" : {"field2" : "value2"} }                # 更新id为1的字段field2=value2

脚本字段

根据脚本字段计算,然后根据计算结果进行排序。

相关文章

网友评论

      本文标题:elasticSearch Api

      本文链接:https://www.haomeiwen.com/subject/xgvgkctx.html