基本操作语法

增加


PUT zhizu_base_test/_create/1 { "name":"猴子", "skill":["七十二变","金箍棒","筋头云"] } PUT zhizu_base_test/_create/2 { "name":"老猪", "skill":["三十六变","九齿钉耙"] } PUT zhizu_base_test/_create/3 { "name":"老沙", "skill":["降妖宝杖"] } PUT zhizu_base_test/_create/4 { "name":"唐僧", "skill":["紧箍咒","叨叨叨"] }

删除

修改


POST zhizu_base_test/_update/3 { "doc": { "name":"沙和尚", "skill":["降妖宝杖","挑担"] } }

查询

#查询一条数据

GET zhizu_base_test/_doc/1

{
  "_index" : "zhizu_base_test",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "猴子",
    "skill" : [
      "七十二变",
      "金箍棒",
      "筋头云"
    ]
  }
}

#条件查询

GET zhizu_base_test/_search?q=name:唐僧

GET zhizu_base_test/_search
{
  "query": {
    "match": {
      "name": "唐僧"
    }
  }
}

{
  "took" : 27,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 2.8796844,
    "hits" : [
      {
        "_index" : "zhizu_base_test",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 2.8796844,
        "_source" : {
          "name" : "唐僧",
          "skill" : [
            "紧箍咒",
            "叨叨叨"
          ]
        }
      }
    ]
  }
}

#查询所有

GET zhizu_base_test/_search

GET zhizu_base_test/_search
{
  "query": {
    "match_all": {}
  }
}

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "zhizu_base_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "猴子",
          "skill" : [
            "七十二变",
            "金箍棒",
            "筋头云"
          ]
        }
      },
      {
        "_index" : "zhizu_base_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "老猪",
          "skill" : [
            "三十六变",
            "九齿钉耙"
          ]
        }
      },
      {
        "_index" : "zhizu_base_test",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "name" : "唐僧",
          "skill" : [
            "紧箍咒",
            "叨叨叨"
          ]
        }
      },
      {
        "_index" : "zhizu_base_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "沙和尚",
          "skill" : [
            "降妖宝杖",
            "挑担"
          ]
        }
      }
    ]
  }
}

#查询某些字段

GET zhizu_base_test/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["name"]
}


{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "zhizu_base_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "猴子"
        }
      },
      {
        "_index" : "zhizu_base_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "老猪"
        }
      },
      {
        "_index" : "zhizu_base_test",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "name" : "唐僧"
        }
      },
      {
        "_index" : "zhizu_base_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "沙和尚"
        }
      }
    ]
  }
}

#查询排序
注意只能用可排序的属性排序 数字 日志  ID

GET zhizu_base_test/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "num": {
        "order": "desc"
      }
    }
  ]
}

#分页查询

{
  "query": {
    "match_all": {}
  },
  "from": 0 , #开始条数
  "size": 1 # 返回几条数据
}

#布尔查询  类似mqsql AND

GET zhizu_base_test/_search
{
  "query": {
   "bool": {
      "must": [
        {
          "match": {
            "name": "白"
          }
        },
        {
          "match": {
            "num": 4
          }
        }
      ]
    }
  }
}

#类似mqsql OR

GET zhizu_base_test/_search
{
  "query": {
   "bool": {
      "should": [
        {
          "match": {
            "name": "猴"
          }
        },
        {
          "match": {
            "num": 4
          }
        }
      ]
    }
  }
}

# 查询不是  must_not (not)

GET zhizu_base_test/_search
{
  "query": {
   "bool": {
      "must_not": [
        {
          "match": {
            "name": "猴子"
          }
        }
      ]
    }
  }
}

# filter 条件过滤查询
# 过滤条件的范围用range
# gt 表示大于
# gte 表示大于等于
# lt 表示小于
# lte 表示小于等于

GET zhizu_base_test/_search
{
  "query": {
   "bool": {
      "filter": {
        "range": {
          "num": {
            "lt": 18
          }
        }
      }
    }
  }
}

#短语检索

GET zhizu_base_test/_search
{
  "query":{
    "match": {
      "tags": "黑"
    }
  }
}

#获取总条数

GET zhizu_base_test/_count


鲁ICP备16017569号-2