elasticsearch基本操作

es基本操作

索引(index)操作

创建索引

语法:PUT /[index]

mappings配置索引中包含的字段信息

PUT test
{
  "settings": {
  	#分片数量
    "number_of_shards": 1, 
    #副本数量
    "number_of_replicas": 1
  }, 
  "mappings":{
    "properties": {
      "title":{
      	#类型
        "type":"text",
        #索引分词器
        "analyzer": "ik_max_word",
        #检索分词器
        "search_analyzer": "ik_smart"
      },
      "actor":{
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "score":{
        "type":"double"
      }
    }
  }
}

分词器ik_max_word和ik_smart都是ik插件提供的。两者区别:ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合,适合 Term Query;ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”,适合 Phrase 查询。

删除索引

DELETE /test

查看索引定义信息

GET test
#只查看mapping信息
GET test/_mapping
#判断索引是否存在
HEAD test1

文档(document)操作

添加数据

语法

PUT /<target>/_doc/<_id>
#自动创建ID
POST /<target>/_doc/
PUT /<target>/_create/<_id>
POST /<target>/_create/<_id>

操作

#添加数据 指定id,id若存在会报错
POST test/_create/1
{
 "title":"大话西游",
 "actor":"周星驰,吴孟达",
 "score":9.5
}

#返回
{
 #索引
  "_index" : "test",
  "_type" : "_doc",
  #docid
  "_id" : "1",
  #数据版本
  "_version" : 4,
  #执行结果
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 33,
  "_primary_term" : 1
}


#添加数据 指定id,对应id存在则修改否则修改
POST test/_doc/2
{
 "title":"西游降魔篇",
 "actor":"黄渤,文章",
 "score":9.0
}

修改

#按id修改 指定field修改
POST test/_update/1
{
  "doc": {
    "title":"大话西游1"
  }
}

删除

#按ID 删除数据
DELETE test/_doc/1
#按查询结果删除数据
POST test/_delete_by_query
{
  "query":{
    "match":{
      "title":"西游"
    }
  }
}

bulk批量操作

多个操作一次提交。支持操作指令 index、create、update、delete。

#批量添加
POST test/_bulk
{"index":{"_id":4}} #index操作,指定id,存在创建,不存在更新,下一行紧跟数据
{"title":"大话西游3","actor":"韩庚/唐嫣/吴京/莫文蔚","score":4.1}
{"create":{"_id":5}} #create
{"title":"西游记","actor":"六小龄童/迟重瑞/马德华/徐少华","score":9.7}
{"update":{"_id":6}}
{"title":"功夫","actor":"周星驰/元秋/元华/黄圣依/梁小龙/陈国坤","score":8.8}
{"index":{"_id":7}}
{"title":"国产凌凌漆","actor":"周星驰/袁咏仪/罗家英/陈宝莲","score":8.7}

查询

#按id查询
GET test/_doc/1
#多个查询
GET test/_mget
{
  "ids":["1","2"]
}
#搜索
GET test/_search/
{
  "query": {"match": {
    "title": "西游"
  }},
  "sort": [
    {
      "score": {
        "order": "desc"
      }
    }
  ]
}

检索分析

_analyze 会进行检索词条分词,field指定索引中的属性,会按其对应的analyzer进行分词。

#搜索分析
GET test/_analyze
{
  "field": "title",
  "text": ["大话西游"]
}
#这里指定test索引的title字段,上面定义其分词器使用的ik的ik_smart,这里会将检索内容“大话西游”分词成三个词条进行检索
#返回数据
{
  "tokens" : [
    {
      "token" : "大话西游",
      "start_offset" : 0,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 0
    },
        {
      "token" : "大话",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 1
    },{
      "token" : "西游",
      "start_offset" : 2,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 2
    }

参考:https://www.elastic.co/guide/en/elasticsearch/reference/7.17/index.html

posted @ 2023-05-06 16:54  朋羽  阅读(17)  评论(0编辑  收藏  举报