首页 > 其他分享 >ElasticSearch 实现分词全文检索 - Restful基本操作

ElasticSearch 实现分词全文检索 - Restful基本操作

时间:2023-03-06 09:00:48浏览次数:40  
标签:index 文档 索引 doc 全文检索 ElasticSearch 基本操作 type id

Restful 语法

GET 请求:

http://ip:port/index: 查询索引信息
http://ip;port/index/type/doc_id: 查询指定的文档信息

POST 请求:

http://ip;port/index/type/_search: 查询文档,可以在请求体中添加json字符串来代表查询条件
http://ip;port/index/type/doc_id/_update: 修改文档,在请求体中指定ison字符串代表修改的具体信息

PUT 请求:

http://ip;port/index: 创建一个索引,需要在请求体中指定索引的信息,类型,结构
http://ip:port/index/type/_mappings: 代表创建索引时,指定索引文档存储的属性的信息

DELETE 请求:

http://ip;port/index: 删除跑路
http://ip;port/index/type/doc_id: 删除指定的文档

操作

创建一个索引

Kibana 操作
创建 person 索引

# 创建一个索引
PUT /person
{
  "settings": {
    "number_of_shards": 5, #分片数
    "number_of_replicas": 1 #备份数
  }
}

返回值

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "person"
}

Kibana 中查看

Stack Management -> 索引管理

image
image
单机版,分片无法存放,所以 yellow 黄色

查看索引信息

# 查看索引信息
GET /person

image

删除索引信息

# 删除索引
DELETE /person

Kibana 操作
image

Field datatypes

String:
 text: 一般用于全文检索。将当前的Field进行分词
 keyword: 当前 Field 不会被分词 
数值类型:
 long、 integer、 short、 byte、 double、 float、
 half_float:精度比float小一半
 scaled_float:根据一个long和scaled来表达一个浮点型, long=345,scaled=100 => 3.45
时间类型:
 date:针对时间类型指定具体格式
布尔类型:
 boolean:表达true和false
二进制类型:
 binary:暂时支持Base64 encode string
范围类型(Range datatypes):
 long_range: 赋值时,无序指定具体的内容,只需要存储一个范围即可,指定gt,此,gte,lte
 integer_range:同上
 double_range:同上
 float_range: 同上
 date_range:同上
 ip_range: 同上。
经纬度类型:
 geo_point: 用来存储经纬度,结合定位的经纬度,来计算出距离
IP类型
 ip: 可以存付IPV4、IPV6

其它类型参考官网:

创建索引并指定结构

## 创建索引并指定结构
PUT /book
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  },
  "mappings": {
    // 文档存储的Field
    "properties":{
      "name":{
        // 类型
        "type":"text",
        // 指定分词器
        "analyzer":"ik_max_word",
        // 指定当前Field可以被作为查询的条件
        "index":true,
        // 是否需要额外存储
        "store":false
      },
      "author":{
        "type":"keyword"
      },
      "count":{
        "type":"long"
      },
      "on-sale":{
        "type":"date",
        // 时间类型时格式化方式
        "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      },
      "descr":{
        "type":"text",
        "analyzer":"ik_max_word"
      }
    }
  }
}

image

文档操作

文档在ES服务中的唯一标识,_index, _type, _id 三个内容为组合,锁定一个文档进行操作

新建文档

自动生成 _id

ID不方便使用

# 添加文档,自动生成 id
POST /book/_doc
{
  "name":"太极",
  "author":"伏羲",
  "count":888,
  "on-sale":"2023-02-23",
  "descr":"太极生两仪,两仪生四象,四象生八卦"
}

返回

{
  "_index" : "book",
  "_type" : "_doc", //创建时没指定,默认的
  "_id" : "YJJLfYYBGlLaT58LDoV5",  // 自动生成的ID
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

手动创建ID

POST /book/_doc/1
{
  "name":"太极拳",
  "author":"陈王廷",
  "count":666,
  "on-sale":"2023-02-23",
  "descr":"掤、捋、挤、按、采、列、肘、靠"
}

返回值

{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

修改文档

覆盖式修改

PUT /book/_doc/1
{
  "name":"太极拳",
  "author":"陈王廷",
  "count":666, //如果不赋值,原来的值将被更新成 0
  "on-sale":"2023-02-28",
  "descr":"掤、捋、挤、按、采、列、肘、靠"
}

doc 修改方式

Deprecation: [types removal] Specifying types in document update requests is deprecated, use the endpoint /{index}/_update/{id} instead.

POST /book/_doc/1/_update
{
  "doc":{
    // 指定Field单独修改
    "count":6666
  }  
}

返回值

#! Deprecation: [types removal] Specifying types in document update requests is deprecated, use the endpoint /{index}/_update/{id} instead.
{
  "_index" : "book",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}

删除文档

DELETE /book/_doc/YJJLfYYBGlLaT58LDoV5

查看数据

image
image
image
image
image
image

标签:index,文档,索引,doc,全文检索,ElasticSearch,基本操作,type,id
From: https://www.cnblogs.com/vipsoft/p/17147797.html

相关文章

  • C/C++ 数据结构使用数组实现队列的基本操作
    //使用数组实现队列#include<iostream>#include<Windows.h>usingnamespacestd;#defineMAXSIZE5//队列的最大容量typedefintDataType;//队列中的元素类型......
  • ElasticSearch 实现分词全文检索 - 概述
    需求做一个类似百度的全文搜索功能所用的技术如下:ElasticSearchKibana管理界面IKAnalysis分词器SpringBootElasticSearch简介ES是一个使用Java语言并且基......
  • elasticsearch 排错总结
    控制台乱码修改elasticsearch-7.6.2\config下的jvm.options文件,在任意行上加上-Dfile.encoding=GBKIK报错但成功启动,按照网上的说法是jdk权限不足,修改方式是改变jdk权限......
  • 数据库和表的基本操作
    1.进入mysql:“mysql-uroot-p123456”2.创建数据库:“createdatabase数据库名称;”3.查看数据库名称:“showdatabase;”4.查看已经创建的数据库信息:“showcreatedat......
  • JavaScript的Dom基本操作
    获取元素的方式:根据id名称获取   document.getElementById("id名称")根据元素类名获取    document.getElementsClassName("元素类名")根据元素标......
  • Ceph RGW ElasticSearch同步模块介绍
    ElasticSearch同步模块注意:截至2020年5月31日,仅支持Elasticsearch6及更低版本。不支持ElasticSearch7。此同步模块将其他区域的元数据写入ElasticSearch。......
  • xlwing基本操作
    参见https://blog.csdn.net/weixin_42146296/article/details/103647940 conf_cells.value=config_head#写入值conf_cells.color=(34,139,34)conf_cells.api......
  • Python elasticsearch 使用心得
    一、配置python==3.6/3.8#更高版本的elasticsearch会出现doc_type被统一成_doc导致旧版语句报错的情况pipinstallelasticsearch==7.8.0二、连接esfromelastics......
  • 关于Stream-流的基本操作
    concat:合并两个流 distinct:去重 limit:限制从流中获得前n个数据 skip:跳过前n个数据 iterate(1,x->x+2):无限流,一个起始值和一个生成下一个值的函数sorted(......
  • 推荐一个Dapper扩展CRUD基本操作的开源库
    推荐一个Dapper扩展CRUD基本操作的开源库 在C#众多ORM框架中,Dapper绝对称得上微型ORM之王,Dapper以灵活、性能好而著名,同样也是支持各种数据库,但是对于一些复杂的查询,......