这是我参与8月更文挑战的第16天,活动详情查看:8月更文挑战
如果❤️我的文章有帮助,欢迎点赞、关注。这是对我继续技术创作最大的鼓励。更多往期文章在我的个人专栏
ElasticSearch 7 使用实践
索引操作
创建索引
下面代码中 API 及 JSON 用来创建 ElasticSearch 索引。
当用户将 JSON对象 发送到 ElasticSearch 时,ElasticSearch 引擎会解析 JSON对象 的索引、文档字段配置信息。根据配置创建索引、文档字段。也可以在此之前使用 uri 手动创建索引。
要创建 ElasticSearch 索引,JSON对象 最基本配置中需要带有 settings、mappings 和 aliases 的 post 请求,具体配置如下,
PUT /material_pass_category
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"bak_code": {
"type": "text"
},
"category": {
"type": "keyword"
},
"collection_num": {
"type": "integer"
},
"cost": {
"type": "long"
},
"create_time": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
},
"file_name": {
"type": "text",
"fielddata": true
},
"half_year_pay_precent": {
"type": "float"
},
"half_year_roi": {
"type": "float"
},
"show_file_name": {
"type": "text",
"fielddata": true
}
}
}
}
删除索引
Elasticsearch 虽然支持修改索引中的字段设置信息。但由于索引中的文档在存储进入 Elasticsearch 时, 搜索引擎会根据 索引中的字段设置 对存储数据进行 方便之后搜索的数据格式化。
而你修改修改索引中的字段设置信息, 只能作用于之后存储的数据。所以修改索引中的字段设置,一般是删除索引后重新存储文档数据。
而删除索引, 需要发起 DELETE 请求
也可以直接使用 curl -X DELETE http://localhost:9200/material_pass_category 删除
文档操作
创建文档
POST http://localhost:9200/material_pass_category/_doc/
{
"m_id": 59108,
"source_id": 304282,
"file_name": "0714zw\u5973\uff09-\u7ad6\u7248 -514e1.mp4",
"show_file_name": "0714zw\u5973\uff09-\u7ad6\u7248 -514e1.mp4",
"file_src": "material_pass\/zhangwsan\/",
"user_id": 378,
"user_name": "zhangwsan",
"designer_name": "\u5f20",
"file_name_aggs": "0714zw\u5973\uff09-\u7ad6\u7248 -514e1.mp4",
"material_size": "1080x1920",
"creativity_first": ",448,",
"creativity_second": ",2408,",
"creativity_third": ",2398,",
"free_tag": ",\u771f\u4eba\u5267\u60c5, ,\u5f02\u517d, ,UE4, ,\u5f02\u517d, ,\u5f02\u517d\u5bf9\u6bd4, ,\u5373\u5408, ,\u5f02\u517d\u56de\u6536, ,\u7535\u68af, ,\u5f02\u517d\u6355\u6349, ,\u6211\u5728\u6c5f\u6e56, ,\u4ed9\u4fa0, ,\u89c6\u9891, ,1080x1920, ,ALL,",
"game": ",968,",
"game_type": ",36,",
"material_pass": ",41,",
"platform": ",133,",
}
URI 查询
举个例子
curl --location --request GET 'http://192.168.20.182:9200/material_pass_category/_search?q=title:Beautiful Mind' --header 'Content-Type: application/json' --data-raw '{"profile":"true"}'
上述查询语句在标识
material_pass_category/_search表示查询material_pass_category索引q=title:Beautiful Mind则表示:title字段包含Beautiful或者Mind两个单词的文档{"profile":"true"}属于 DSL 语句,表示:查询结果中展示 Elasticsearch 搜索引擎展示执行计划
Query DSL
DSL 是 Elasticsearch 使用 json 为载体的特殊查询语言,以下我就会使用例子来一一举例具体使用方法
ignore_unavailable=true,标识忽略尝试访问不存在的索引“404_idx”导致的报错
curl --location --request GET 'http://192.168.20.182:9200/material_pass_category/_search?ignore_unavailable=true'
查询内容分页
在 DSL 中使用 from,size 关键字指定返回数据访问,达到分页效果。
作用与 sql 中 limit 作用相似,具体使用如下
POST /material_pass_category/_search?ignore_unavailable=true
{
"profile": true,
"from":10,
"size":20,
"query": {
"match_all": {}
}
}
对字段排序
在 DSL 中使用 sort 关键字使返回数据结果,按指定字段内容进行排序。具体使用如下:
POST kibana_sample_data_ecommerce/_search
{
"sort":[{"order_date":"desc"}],
"query":{
"match_all": {}
}
}
字段查询
POST material_pass_category/_search
{
"query": {
"match": {
"title": "last christmas"
}
}
}
POST material_pass_category/_search
{
"query": {
"match": {
"title": {
"query": "last christmas",
"operator": "and"
}
}
}
}