es的基础语法

es查询http://ip:port/index/type,下面是从kibana上的查询语句

1.查看es的集群状态:
GET _cluster/health?pretty
结果:

{
  "cluster_name" : "xxy-es",  //集群名
  "status" : "green", //green:主节点和从节点正常,yellow:主节点正常,从节点不完全正常,red:主节点不完全正常
  "timed_out" : false,
  "number_of_nodes" : 2,//节点数
  "number_of_data_nodes" : 2, //数据节点数
  "active_primary_shards" : 7,//活跃的主分片数
  "active_shards" : 14,// 活跃分片数(主分片和副本分片数)
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,  //为分配分片数
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 100.0 //活跃分片数占总分片数的占比
} 
复制代码

2.建立索引

 PUT /teacher
复制代码

结果:

  {
      "acknowledged" : true,   //设置成功
      "shards_acknowledged" : true,
      "index" : "teacher"
}
复制代码

3.建立带有分词器的索引:(如需使用插件类的分词器,需要在建立索引的时候指定,索引一旦建立就不能改变分词器)

 PUT /student
 {
  "settings": {
    "analysis": {
       "analyzer": "ik"  //使用ik分词器
    }
  }
 }
复制代码

结果:

  {
      "acknowledged" : true,   //设置成功
      "shards_acknowledged" : true,
      "index" : "student"
 }
复制代码

4.设置分片数:

  PUT /blog
{
   "settings" : {
      "number_of_shards" : 3,  //设置分片数
      "number_of_replicas" : 1  //设置副本数
   }
}
复制代码

5.删除索引:

 DELETE /teacher 
复制代码

结果:

  {
      "acknowledged" : true //删除成功
  }    
复制代码

6.设置索引映射:映射分为dynamic mapping(动态映射),explicit mapping(静态映射),strict mapping(严格映射)

a.动态映射,当设置mapping是没有设置相关字段,插入的时候有,在查询_mapping时可以查到

b.静态映射,当设置mapping时没有设置相关字段,插入的时候有,在查询_mapping时查不到,数据会存储

c.严格映射,插入的时候会直接报错

  PUT  /student/_mapping
{
  "properties": {
    "first_name":{
      "type":"keyword",  //不分词
      "ignore_above":100
    },
    "last_name":{
      "type":"keyword",
      "ignore_above": 100
    },
    "sex":{
      "type":"integer", //整数类型
      "index":"false"
    },
    "introduce":{
      "type": "text",
      "analyzer": "ik_max_word", //使用ik分词器,默认使用es自带的stander分词器,es自带的分词器适应于英文分词,对于中文分词不适用
      "search_analyzer": "ik_smart"
    },
    "sports":{
      "type":"text"
    }
  }
}
复制代码

结果:

  {
      "acknowledged" : true
  }
复制代码

7.向es中插入数据:

 POST /student/_doc/1
{
  "first_name":"zhang",
  "last_name":"san",
  "introduce":"本人性格开朗,爱好广泛,在学习期间进取参加各种活动,多次组织一些学校活动。进取向上且对待工作认证负责,有上进心,勤于学习能不断提高,喜欢向高难度挑战,提升自身的本事与综合素质。我是一个很有时间观念的人,能进取认真做好每件事,不怕辛苦不怕累,更不怕挫折,待人真诚,善于沟通、协调,有较强的组织本事与团队精神。期望贵公司能给我一个学习和锻炼的平台,我会好好把握。",
  "sex":1,
  "sports":["足球","篮球"]
}
复制代码

结果:

 {
      "_index" : "student",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 1,
      "result" : "created",//创建成功
      "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
     },
     "_seq_no" : 0,
     "_primary_term" : 2
}
复制代码

8.更新数据:(更新不是修改原数据,是将原数据标记为删除,重新插入了一条数据)

 POST /student/_update/1
{
  "doc":{
    "height":1.75
  }
}
复制代码

结果:

   {
      "_index" : "user",
      "_type" : "_doc",
      "_id" : "2",
      "_version" : 2,
      "result" : "updated", //修改
      "_shards" : {
        "total" : 2,
        "successful" : 2,
        "failed" : 0
      },
      "_seq_no" : 1,
      "_primary_term" : 3
  }
复制代码

9.删除一条数据:

DELETE /user/_doc/3
复制代码

结果:

{
  "_index" : "user",
  "_type" : "_doc",
  "_id" : "3",
  "_version" : 2,
  "result" : "deleted", //删除数据
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 3
}
复制代码

10.获取全量数据:

   GET /student/_search
    {
      "query": {
        "match_all": {}
      }
    } 
复制代码

10.根据条件获取数据:

a.并:must(last_name=’hong’ and first_name=’xiao’)

b.或:should(last_name=’hong’ or first_name=’xiao’)

c.不等于:must_not(last_name!=’hong’ and first_name!=’xiao’)

  GET /student/_search
{
  "query": {
    "bool":{
      "must": [
        {
          "match":{
            "last_name":"hong"
          }
        },
        {
          "match": {
            "first_name": "xiao"
          }
        }
      ]
    }
  }
}
复制代码

e:根据某个字段排序&只返回需要的字段

  GET /student/_search
{
  "query": {
    "bool":{
      "must": [
        {
          "match":{
            "introduce":"热情随和"
          }
        }
      ]
    }
  },
  "sort":{            //根据last_name排序,默认升序
    "last_name":{
      "order":"desc"
    }
  },
  "_source": ["first_name","last_name"]  //只返回first_name和last_name字段
}
复制代码

f:match,match_phrase,match_phrase_prefix和multi_match之间的区别

  match是将词语分词后查询出所有符合条件的
  match_phrase是词组查询
  match_all:查询全部
  match_phrase_prefix:前缀查询
  multi_match:多字段查询,通过type(phrase,phrase_prefix)字段可以实现match_phrase和match_phrase_prefix
  
复制代码

h:filter(条件过滤查询,过滤条件的范围用range表示gt表示大于、lt表示小于、gte表示大于等于、lte表示小于等于)

   GET /student/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "height": {
            "gte": 1.5,
            "lte": 1.77
          }
        }
      }
    }
  }
}
复制代码

i:复合查询需要使用bool

j:keyword,text,match,item区别

keyword:存储的时候不会进行分词
text:存储的时候会进行分词操作
match:查询的时候会进行分词操作
item:查询的时候不会进行分词操作
复制代码

k:ES的聚合查询avg、max、min、sum

  GET /student/_search
{
  "query": {
    "match": {
      "first_name": "xiao"
    }
  },
  "aggs": {
    "my_height": {
      "avg": {
        "field": "height"
      }
    }
  },
  "size":0,   //不返回命中数据
  "_source": ["first_name","last_name", "height"]
}







   
复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享