关系型数据库: Databases(库)→Tables(表)→Rows(行)→Columns(列)
ElasticSearch:Indeces(索引)→Types(类型)→Documents(文档)→Fileds(属性)
*types: type的概念在ES7.0之后被删除了
基础操作
一、查看所有index
GET http://[es-ip]:9200/_cat/indices?v
返回结果:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open flfg RL2nQVVGSyO2iyC8CrWz0g 1 0 7749 69 854.2kb 854.2kb
yellow open flfg_test dIZKPbLDTQ6sLFuYvhF9Ag 1 1 1 0 4.2kb 4.2kb
二、创建索引
PUT http://[es-ip]:9200/flfg_test
参数:
{
"mappings": {
"properties": {
"flfgmc": {
"type": "text",
"analyzer": "ik_max_word"
},
"yxx": {
"type": "keyword",
"index": true
},
"content": {
"type": "text",
"analyzer": "ik_max_word"
},
"fbsj": {
"type": "date",
"format": "yyyy-MM-dd||yyyy-MM-dd HH:mm:ss||yyyy-MM-dd HH:mm:ss.SSS||strict_date_optional_time||epoch_millis"
}
}
}
}
type: text-可分词 keyword-不可分词
index:true-可索引查询 false-不可索引查询
三、索引添加字段
PUT http://[es-ip]/flfg_test/_mapping
参数:
{
"properties":{
"extContent": {
"type": "keyword",
"index": true
}
}
}
四、查询索引属性
GET http://[es-ip]:9200/flfg_test
返回结果:
{
"flfg_test": {
"aliases": {},
"mappings": {
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word"
},
"extContent": {
"type": "keyword"
},
"fbsj": {
"type": "date",
"format": "yyyy-MM-dd||yyyy-MM-dd HH:mm:ss||yyyy-MM-dd HH:mm:ss.SSS||strict_date_optional_time||epoch_millis"
},
"flfgmc": {
"type": "text",
"analyzer": "ik_max_word"
},
"yxx": {
"type": "keyword"
}
}
},
"settings": {
"index": {
"creation_date": "1708248253689",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "uxOoyDVdSWaS4CZNOX178w",
"version": {
"created": "7090399"
},
"provided_name": "flfg_test"
}
}
}
}
五、删除索引
DELETE http://[es-ip]:9200/flfg_test
六、插入或更新文档数据
POST http://[es-ip]:9200/flfg_test/_doc/{id}
(id不指定会随机生成)
参数:
{
"flfgmc": "hjx-测试001",
"content": "规定:123456",
"yxx": 1,
"fbsj": "2024-01-01 15:00:00",
"extContent": "备注内容xxx"
七、查询并更新文档数据
POSThttp://[es-ip]:9200/flfg_test/_update_by_query
参数:
{
"query":{
"match":{
"_id":"f2dfe789b2ff94d2858a4b9cb68b24g"
}
},
"script":{
"source":"ctx._source.extContent = \"备注内容123\""
}
}
八、根据编号查询文档数据
GET http://[es-ip]:9200/flfg_test/_doc/{id}
返回结果:
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "f2dfe789b2ff94d2858a4b9cb68b24g",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"flfgmc": "hjx-测试001",
"content": "规定:123456",
"yxx": 1,
"fbsj": "2024-01-01 15:00:00",
"extContent": "备注内容123"
}
}
九、全量查询文档数据
GET http://[es-ip]:9200/flfg_test/_search
返回结果:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "f2dfe789b2ff94d2858a4b9cb68b24g",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试001",
"content": "规定:123456",
"yxx": 1,
"fbsj": "2024-01-01 15:00:00",
"extContent": "备注内容123"
}
}
]
}
}
十、删除文档数据
DELETE http://[es-ip]:9200/flfg_test/_doc/{id}
高级查询
GET http://[es-ip]:9200/flfg_test/_search
当前引擎中的全部数据为:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 8,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "f2dfe789b2ff94d2858a4b9cb68b24g",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试001",
"content": "规定:123456",
"yxx": 1,
"fbsj": "2024-01-01 15:00:00",
"extContent": "备注内容123"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "JtqLu40BwkeWUgJOgYoS",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试002",
"content": "规定:234523534",
"yxx": 1,
"fbsj": "2024-01-05 15:00:00",
"extContent": "备注内容234"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "J9qLu40BwkeWUgJOw4qy",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试003",
"content": "规定:345365345",
"yxx": 1,
"fbsj": "2024-01-10 15:00:00",
"extContent": "备注内容345"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "KNqLu40BwkeWUgJO9opz",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试004",
"content": "规定:456467",
"yxx": 2,
"fbsj": "2024-01-15 15:00:00",
"extContent": "备注内容456"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "Ptq_u40BwkeWUgJOn4pM",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试消费",
"content": "规定:66666",
"yxx": 1,
"fbsj": "2024-01-03 15:00:00",
"extContent": "备注内容xxx"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "P9q_u40BwkeWUgJOxYqe",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试金融",
"content": "规定:68888",
"yxx": 1,
"fbsj": "2024-01-03 15:00:00",
"extContent": "备注内容xxx"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "QNq_u40BwkeWUgJO7Ioq",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试财产",
"content": "规定:687777",
"yxx": 1,
"fbsj": "2024-01-03 15:00:00",
"extContent": "备注内容xxx"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "QdrAu40BwkeWUgJORYqk",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试报告",
"content": "规定:688989",
"yxx": 1,
"fbsj": "2024-01-03 15:00:00",
"extContent": "备注内容xxx"
}
}
]
}
}
一、条件、分页查询并排序
_source:指定需要输出的字段
from:起始数据的位置
size:页大小
sort:排序
参数:
{
"query": {
"match": {
"yxx": "1"
}
},
"from": 0,
"size": 2,
"_source": [
"flfgmc",
"yxx",
"fbsj"
],
"sort": {
"fbsj": {
"order": "desc"
}
}
}
返回结果:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "J9qLu40BwkeWUgJOw4qy",
"_score": null,
"_source": {
"yxx": 1,
"fbsj": "2024-01-10 15:00:00",
"flfgmc": "hjx-测试003"
},
"sort": [
1704898800000
]
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "JtqLu40BwkeWUgJOgYoS",
"_score": null,
"_source": {
"yxx": 1,
"fbsj": "2024-01-05 15:00:00",
"flfgmc": "hjx-测试002"
},
"sort": [
1704466800000
]
}
]
}
}
二、多条件查询、范围查询
1. AND 且 bool - must
参数:
{
"query": {
"bool": {
"must": [
{
"match": {
"yxx": "1"
}
},
{
"match": {
"extContent": "备注内容345"
}
}
]
}
}
}
返回结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.5606477,
"hits": [
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "J9qLu40BwkeWUgJOw4qy",
"_score": 1.5606477,
"_source": {
"flfgmc": "hjx-测试003",
"content": "规定:345365345",
"yxx": 1,
"fbsj": "2024-01-10 15:00:00",
"extContent": "备注内容345"
}
}
]
}
}
2. OR 或 bool - should
参数:
{
"query": {
"bool": {
"should": [
{
"match": {
"yxx": "2"
}
},
{
"match": {
"extContent": "备注内容345"
}
}
]
}
}
}
返回结果:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.2039728,
"hits": [
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "J9qLu40BwkeWUgJOw4qy",
"_score": 1.2039728,
"_source": {
"flfgmc": "hjx-测试003",
"content": "规定:345365345",
"yxx": 1,
"fbsj": "2024-01-10 15:00:00",
"extContent": "备注内容345"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "KNqLu40BwkeWUgJO9opz",
"_score": 1.2039728,
"_source": {
"flfgmc": "hjx-测试004",
"content": "规定:456467",
"yxx": 2,
"fbsj": "2024-01-15 15:00:00",
"extContent": "备注内容456"
}
}
]
}
}
3. RANGE 范围 bool - filter - range
参数:
{
"query": {
"bool": {
"filter": {
"range": {
"fbsj": {
"lt": "2024-02-01",
"gt": "2024-01-10"
}
}
}
}
}
}
返回结果:
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0,
"hits": [
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "KNqLu40BwkeWUgJO9opz",
"_score": 0,
"_source": {
"flfgmc": "hjx-测试004",
"content": "规定:456467",
"yxx": 2,
"fbsj": "2024-01-15 15:00:00",
"extContent": "备注内容456"
}
}
]
}
}
三、模糊匹配
1. MATCH 匹配
先对参数进行分词,再进行匹配。
**GET **http://[es-ip]:9200/flfg_test/_search
参数:
{
"query": {
"match": {
"flfgmc" : "报告金融"
}
}
}
返回结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.8142502,
"hits": [
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "P9q_u40BwkeWUgJOxYqe",
"_score": 1.8142502,
"_source": {
"flfgmc": "hjx-测试金融",
"content": "规定:68888",
"yxx": 1,
"fbsj": "2024-01-03 15:00:00",
"extContent": "备注内容xxx"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "QdrAu40BwkeWUgJORYqk",
"_score": 1.6486912,
"_source": {
"flfgmc": "hjx-测试报告",
"content": "规定:688989",
"yxx": 1,
"fbsj": "2024-01-03 15:00:00",
"extContent": "备注内容xxx"
}
}
]
}
}
keyword类型不支持模糊匹配,可以使用下面的方式将一个字段添加两种类型
{
"mappings": {
"properties": {
"flfgmc": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
}
}
}
}
2. MATCH_PHRASE 完全匹配
不对参数进行分词,直接进行匹配。这里参数如果使用 "报告金融",查询结果为空。
参数:
{
"query": {
"match_phrase": {
"flfgmc" : "报告"
}
}
}
返回结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.6486912,
"hits": [
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "QdrAu40BwkeWUgJORYqk",
"_score": 1.6486912,
"_source": {
"flfgmc": "hjx-测试报告",
"content": "规定:688989",
"yxx": 1,
"fbsj": "2024-01-03 15:00:00",
"extContent": "备注内容xxx"
}
}
]
}
}
四、高亮查询
1. 默认高亮
参数:
{
"query": {
"match": {
"flfgmc": "金融报告消费"
}
},
"highlight": {
"fields": {
"flfgmc": {}
}
}
}
返回结果
{
"took": 11,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.8142502,
"hits": [
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "Ptq_u40BwkeWUgJOn4pM",
"_score": 1.8142502,
"_source": {
"flfgmc": "hjx-测试消费",
"content": "规定:66666",
"yxx": 1,
"fbsj": "2024-01-03 15:00:00",
"extContent": "备注内容xxx"
},
"highlight": {
"flfgmc": [
"hjx-测试<em>消费</em>"
]
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "P9q_u40BwkeWUgJOxYqe",
"_score": 1.8142502,
"_source": {
"flfgmc": "hjx-测试金融",
"content": "规定:68888",
"yxx": 1,
"fbsj": "2024-01-03 15:00:00",
"extContent": "备注内容xxx"
},
"highlight": {
"flfgmc": [
"hjx-测试<em>金融</em>"
]
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "QdrAu40BwkeWUgJORYqk",
"_score": 1.6486912,
"_source": {
"flfgmc": "hjx-测试报告",
"content": "规定:688989",
"yxx": 1,
"fbsj": "2024-01-03 15:00:00",
"extContent": "备注内容xxx"
},
"highlight": {
"flfgmc": [
"hjx-测试<em>报告</em>"
]
}
}
]
}
}
2. 自定义高亮标签
参数:
{
"query": {
"match": {
"flfgmc": "金融报告消费"
}
},
"highlight": {
"pre_tags": ["<red>"],
"post_tags": ["</red>"],
"fields": {
"flfgmc": {}
}
}
}
返回结果:
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.8142502,
"hits": [
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "Ptq_u40BwkeWUgJOn4pM",
"_score": 1.8142502,
"_source": {
"flfgmc": "hjx-测试消费",
"content": "规定:66666",
"yxx": 1,
"fbsj": "2024-01-03 15:00:00",
"extContent": "备注内容xxx"
},
"highlight": {
"flfgmc": [
"hjx-测试<red>消费</red>"
]
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "P9q_u40BwkeWUgJOxYqe",
"_score": 1.8142502,
"_source": {
"flfgmc": "hjx-测试金融",
"content": "规定:68888",
"yxx": 1,
"fbsj": "2024-01-03 15:00:00",
"extContent": "备注内容xxx"
},
"highlight": {
"flfgmc": [
"hjx-测试<red>金融</red>"
]
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "QdrAu40BwkeWUgJORYqk",
"_score": 1.6486912,
"_source": {
"flfgmc": "hjx-测试报告",
"content": "规定:688989",
"yxx": 1,
"fbsj": "2024-01-03 15:00:00",
"extContent": "备注内容xxx"
},
"highlight": {
"flfgmc": [
"hjx-测试<red>报告</red>"
]
}
}
]
}
}
五、聚合查询
1. 分组统计个数
参数:
{
"aggs": {
"yxx_group": {
"terms": {
"field": "yxx"
}
}
}
}
返回结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 8,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "f2dfe789b2ff94d2858a4b9cb68b24g",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试001",
"content": "规定:123456",
"yxx": 1,
"fbsj": "2024-01-01 15:00:00",
"extContent": "备注内容123"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "JtqLu40BwkeWUgJOgYoS",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试002",
"content": "规定:234523534",
"yxx": 1,
"fbsj": "2024-01-05 15:00:00",
"extContent": "备注内容234"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "J9qLu40BwkeWUgJOw4qy",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试003",
"content": "规定:345365345",
"yxx": 1,
"fbsj": "2024-01-10 15:00:00",
"extContent": "备注内容345"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "KNqLu40BwkeWUgJO9opz",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试004",
"content": "规定:456467",
"yxx": 2,
"fbsj": "2024-01-15 15:00:00",
"extContent": "备注内容456"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "Ptq_u40BwkeWUgJOn4pM",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试消费",
"content": "规定:66666",
"yxx": 1,
"fbsj": "2024-01-03 15:00:00",
"extContent": "备注内容xxx"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "P9q_u40BwkeWUgJOxYqe",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试金融",
"content": "规定:68888",
"yxx": 1,
"fbsj": "2024-01-03 15:00:00",
"extContent": "备注内容xxx"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "QNq_u40BwkeWUgJO7Ioq",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试财产",
"content": "规定:687777",
"yxx": 1,
"fbsj": "2024-01-03 15:00:00",
"extContent": "备注内容xxx"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "QdrAu40BwkeWUgJORYqk",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试报告",
"content": "规定:688989",
"yxx": 1,
"fbsj": "2024-01-03 15:00:00",
"extContent": "备注内容xxx"
}
}
]
},
"aggregations": {
"yxx_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "1",
"doc_count": 7
},
{
"key": "2",
"doc_count": 1
}
]
}
}
}
2. 求平均值
参数:
{
"aggs": {
"yxx_avg": {
"avg": {
"field": "fbsj"
}
}
}
}
返回结果
{
"took": 16,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 8,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "f2dfe789b2ff94d2858a4b9cb68b24g",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试001",
"content": "规定:123456",
"yxx": 1,
"fbsj": "2024-01-01 15:00:00",
"extContent": "备注内容123"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "JtqLu40BwkeWUgJOgYoS",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试002",
"content": "规定:234523534",
"yxx": 1,
"fbsj": "2024-01-05 15:00:00",
"extContent": "备注内容234"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "J9qLu40BwkeWUgJOw4qy",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试003",
"content": "规定:345365345",
"yxx": 1,
"fbsj": "2024-01-10 15:00:00",
"extContent": "备注内容345"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "KNqLu40BwkeWUgJO9opz",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试004",
"content": "规定:456467",
"yxx": 2,
"fbsj": "2024-01-15 15:00:00",
"extContent": "备注内容456"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "Ptq_u40BwkeWUgJOn4pM",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试消费",
"content": "规定:66666",
"yxx": 1,
"fbsj": "2024-01-03 15:00:00",
"extContent": "备注内容xxx"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "P9q_u40BwkeWUgJOxYqe",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试金融",
"content": "规定:68888",
"yxx": 1,
"fbsj": "2024-01-03 15:00:00",
"extContent": "备注内容xxx"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "QNq_u40BwkeWUgJO7Ioq",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试财产",
"content": "规定:687777",
"yxx": 1,
"fbsj": "2024-01-03 15:00:00",
"extContent": "备注内容xxx"
}
},
{
"_index": "flfg_test",
"_type": "_doc",
"_id": "QdrAu40BwkeWUgJORYqk",
"_score": 1,
"_source": {
"flfgmc": "hjx-测试报告",
"content": "规定:688989",
"yxx": 1,
"fbsj": "2024-01-03 15:00:00",
"extContent": "备注内容xxx"
}
}
]
},
"aggregations": {
"yxx_avg": {
"value": 1704499200000,
"value_as_string": "2024-01-06"
}
}
}