ElasticSearch入门语法基础知识
1、创建测试索引
PUT /test_index_person
{"settings": {"analysis": {"analyzer": {"ik_analyzer": {"type": "custom","tokenizer": "ik_smart"}}}},"mappings": {"properties": {"person_id": { "type": "integer" },"person_name": { "type": "text","analyzer": "ik_analyzer"},"gmt_create": { "type": "date","format": "strict_date_optional_time"},"gmt_modified": { "type": "date","format": "strict_date_optional_time"}}}
}
2、添加数据
POST /test_index_person/_doc
{"person_id": 1,"person_name": "张三 李四","gmt_create": "2024-07-31T10:00:00","gmt_modified": "2024-07-31T10:00:00"
}POST /test_index_person/_doc
{"person_id": 2,"person_name": "王五 赵六","gmt_create": "2024-07-31T11:00:00","gmt_modified": "2024-07-31T11:00:00"
}
POST /test_index_person/_doc
{"person_id": 3,"person_name": "葛哈哈哈张三","gmt_create": "2024-07-31T11:00:00","gmt_modified": "2024-07-31T11:00:00"
}
POST /test_index_person/_doc
{"person_id": 4,"person_name": "葛哈哈哈小黑子","gmt_create": "2024-07-31T11:00:00","gmt_modified": "2024-07-31T11:00:00"
}
POST /test_index_person/_doc
{"person_id": 5,"person_name": "赵六不是小黑子","gmt_create": "2024-07-31T11:00:00","gmt_modified": "2024-07-31T11:00:00"
}
POST /test_index_person/_doc
{"person_id": 6,"person_name": "张三是一个小黑子","gmt_create": "2024-07-31T11:00:00","gmt_modified": "2024-07-31T11:00:00"
}
3、修改数据
#更新数据,因为没有设置id,所以需要先查询到id
GET /test_index_person/_search
{"query": {"term": {"person_id": {"value": "1"}}}
}
POST /test_index_person/_update/VHvlB5EBxT0rb-io56C7
{"doc": {"person_name": "张三 李四哈哈哈"}
}
4、查询索引下的分词和ik分词器对搜索内容的分词
#查看test_index_person索引下对person_name某个内容的分词
GET /test_index_person/_analyze
{"field": "person_name","text": "张三是一个小黑子"
}
# IK分词器对搜索内容的分词
GET /_analyze
{"analyzer": "ik_max_word", "text": "张三"
}
5、查询所有文档
#匹配所有文档
GET /test_index_person/_search
{"query": {"match_all": {}}
}
6、分词查询测试
#查询分词测试
GET /test_index_person/_search
{"query": {"match": {"person_name": "葛赵六"}}
}
7、精确查询
#term查询适合用于匹配未分词的字段或者需要精确匹配的场景
GET /test_index_person/_search
{"query": {"term": {"person_id": {"value": "1"}}}
}
8、短语匹配
#短语匹配 返回所有person_name字段精确匹配短语"小黑子"的文档
GET /test_index_person/_search
{"query": {"match_phrase": {"person_name": "小黑子"}}
}
9、范围查询
#范围查询 gt(>) gte(>=) lt(<) lte(<=)
GET /test_index_person/_search
{"query": {"range": {"gmt_create": {"gte": "2024-01-31T11:00:00","lt": "2024-07-31T11:00:00"}}}
}
10、模糊匹配
#模糊匹配
GET /test_index_person/_search
{"query": {"fuzzy": {"person_name": "张三"}}
}
11、前缀匹配查询(适用非分词字段)
#前缀匹配查询(适用非分词字段)
GET /test_index_person/_search
{"query": {"prefix": {"person_name": {"value": "张三"}}}
}
12、布尔查询
#布尔查询 必须匹配(must) 过滤(filter) 至少匹配一个(should)
GET /test_index_person/_search
{"query": {"bool": {"must": [{"term": {"person_id": {"value": "1"}}}],"filter": [{"range": {"gmt_create": {"gte": "2024-07-31T10:00:00"}}}],"should": [{"term": {"person_name": {"value": "11"}}}],"must_not": [{"term": {"person_name": {"value": "哈哈"}}}]}}
}
13、分页
#分页 from开始 size 结束
GET /test_index_person/_search
{"query": {"match_all": {}},"from": 1, "size": 2
}
14、聚合查询
#对查询出来的值进行聚合查询 统计每个唯一值的个数
GET /test_index_person/_search
{"query": {"match": {"person_name": "张三"}}, "aggs": {"test_aggs": {"terms": {"field": "gmt_create"}}}
}
#对查询出来的值进行聚合查询 统计每个唯一值的个数 仅仅返回聚合结果
GET /test_index_person/_search
{"size": 0, "query": {"match": {"person_name": "张三"}}, "aggs": {"test_aggs": {"terms": {"field": "gmt_create"}}}
}
#多个聚合
GET /test_index_person/_search
{"aggs": {"test_aggs": {"terms": {"field": "gmt_create"}},"test2_aggs":{"avg": {"field": "person_id"}}}
}
#子聚合 先分组,再计算
GET /test_index_person/_search
{"aggs": {"test_aggs": {"terms": {"field": "gmt_create"}, "aggs": {"son_aggs_test": {"avg": {"field": "person_id"}}}}}
}