一、查
1. 匹配查询
在Elasticsearch中,字符串搜索时,会对输入的查询字符串进行分析和分词,这可能导致意外匹配或未匹配的情况。使用以下语法进行匹配查询:
GET /your_index/_search
{
"query": {
"match": {
"your_field": "your_search_string"
}
}
}
2. 精确匹配字符串
term
查询用于精确匹配,在对日期字符串进行term
查询时,由于映射方式不同可能存在匹配不到的情况。语法如下:
GET /your_index/_search
{
"query": {
"term": {
"your_field.keyword": "your_exact_string"
}
}
}
3. 匹配某字段为空
查询某个字段为空的文档,可使用以下语法:
GET /your_index/_search
{
"query": {
"bool": {
"must_not": {
"exists": {
"field": "your_field"
}
}
}
}
}
4. 多条件匹配
通过bool
查询来组合多个查询条件,must
表示所有条件都要满足,should
表示至少满足一个条件,must_not
表示不能满足该条件。示例如下:
GET /your_index/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"field1": "value1"
}
},
{
"match": {
"field2": "value2"
}
}
],
"should": [
{
"match": {
"field3": "value3"
}
}
],
"must_not": [
{
"match": {
"field4": "value4"
}
}
],
"filter": [
{
"range": {
"age": {
"gte": 18,
"lte": 30
}
}
}
]
}
}
}
5. 范围查询
查询某个字段在一定范围内的文档:
GET /your_index/_search
{
"query": {
"range": {
"age": {
"gte": 18,
"lt": 30,
"boost": 2.0
}
}
}
}
6. 前缀查询
查询某个字段以指定前缀开头的文档:
GET /your_index/_search
{
"query": {
"prefix": {
"username": {
"value": "john",
"boost": 1.0
}
}
}
}
7. 通配符查询
使用通配符*
(匹配任意字符序列)和?
(匹配单个字符)进行查询:
GET /your_index/_search
{
"query": {
"wildcard": {
"email": {
"value": "john*@example.com",
"boost": 1.0
}
}
}
}
8. 正则表达式查询
使用正则表达式进行匹配查询:
GET /your_index/_search
{
"query": {
"regexp": {
"username": {
"value": "joh?n(smith|doe)",
"flags": "ALL",
"max_determinized_states": 10000,
"boost": 1.0
}
}
}
}
9. 模糊查询
查询与指定词相似的文档,允许一定的编辑距离(Levenshtein距离):
GET /your_index/_search
{
"query": {
"fuzzy": {
"username": {
"value": "jon",
"fuzziness": "AUTO",
"prefix_length": 0,
"max_expansions": 50,
"transpositions": true,
"boost": 1.0
}
}
}
}
10. ids查询
通过文档ID列表查询多个文档:
GET /your_index/_search
{
"query": {
"ids": {
"values": ["123", "456", "789"]
}
}
}
二、改
1. 通过es_id修改内容
根据文档的_id
来修改文档内容,使用update
接口。例如:
POST /your_index/_update/_id_123456
{
"doc": {
"your_field": "new_value"
}
}
2. 匹配修改内容
匹配满足特定条件的文档并进行修改,使用_update_by_query
接口:
POST /your_index/_update_by_query
{
"query": {
"match": {
"your_field": "your_search_string"
}
},
"script": {
"source": "ctx._source.your_field = 'new_value'"
}
}
3. 增加字段
为匹配的文档增加新字段:
POST /your_index/_update_by_query
{
"script": {
"source": "ctx._source.new_field = 'new_value'"
},
"query": {
"match_all": {}
}
}
4. 删除字段
从匹配的文档中删除字段:
POST /your_index/_update_by_query
{
"script": {
"source": "ctx._source.remove('field_to_delete')"
},
"query": {
"match_all": {}
}
}
5. 原子更新计数器
对数值字段进行原子递增或递减操作:
POST /your_index/_update/_id_123456
{
"script": {
"source": "ctx._source.counter += params.count",
"params": {
"count": 1
}
}
}
三、增
1. 添加单个文档
向索引中添加单个文档,使用POST
请求,并指定文档的_id
(也可以由Elasticsearch自动生成)。示例:
POST /your_index/_doc/_id_123456
{
"field1": "value1",
"field2": "value2"
}
2. 批量添加文档(Bulk API)
一次性添加多个文档,提高写入效率:
POST /your_index/_bulk
{ "index" : { "_id" : "1" } }
{ "field1" : "value1", "field2" : "value2" }
{ "index" : { "_id" : "2" } }
{ "field1" : "value3", "field2" : "value4" }
3. 自动生成ID添加文档
不指定_id
,由Elasticsearch自动生成唯一ID:
POST /your_index/_doc
{
"field1": "value1",
"field2": "value2"
}
4. 创建文档(存在则失败)
使用create
操作,只有当文档ID不存在时才创建:
PUT /your_index/_create/_id_123456
{
"field1": "value1",
"field2": "value2"
}
四、删
1. 通过_id删除
根据文档的_id
删除文档,使用DELETE
请求:
DELETE /your_index/_doc/_id_123456
2. 匹配删除
匹配满足特定条件的文档并删除,使用_delete_by_query
接口:
POST /your_index/_delete_by_query
{
"query": {
"match": {
"your_field": "your_search_string"
}
}
}
3. 删除索引
删除整个索引及其所有文档:
DELETE /your_index
4. 按条件删除多个索引
删除名称匹配特定模式的多个索引:
DELETE /index1,index2
DELETE /index*
五、聚合查询
1. 统计聚合
计算某个字段的平均值:
GET /your_index/_search
{
"size": 0,
"aggs": {
"avg_age": {
"avg": {
"field": "age"
}
}
}
}
2. 分桶聚合
按某个字段的值进行分组统计:
GET /your_index/_search
{
"size": 0,
"aggs": {
"group_by_status": {
"terms": {
"field": "status.keyword",
"size": 10
}
}
}
}
3. 嵌套聚合
在聚合结果中进行二次聚合:
GET /your_index/_search
{
"size": 0,
"aggs": {
"group_by_category": {
"terms": {
"field": "category.keyword"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
4. 范围聚合
按数值范围进行分组统计:
GET /your_index/_search
{
"size": 0,
"aggs": {
"price_ranges": {
"range": {
"field": "price",
"ranges": [
{ "to": 100 },
{ "from": 100, "to": 200 },
{ "from": 200 }
]
}
}
}
}
5. 日期直方图聚合
按日期分组统计:
GET /your_index/_search
{
"size": 0,
"aggs": {
"sales_per_month": {
"date_histogram": {
"field": "date",
"calendar_interval": "month",
"format": "yyyy-MM"
}
}
}
}
六、索引管理
1. 创建索引
创建一个新索引并指定映射:
PUT /new_index
{
"mappings": {
"properties": {
"title": { "type": "text" },
"content": { "type": "text" },
"timestamp": { "type": "date" }
}
}
}
2. 获取索引信息
获取索引的设置和映射信息:
GET /your_index
3. 更新索引设置
动态更新索引的设置:
PUT /your_index/_settings
{
"index": {
"number_of_replicas": 2
}
}
4. 添加映射字段
向现有索引添加新字段:
PUT /your_index/_mapping
{
"properties": {
"new_field": { "type": "keyword" }
}
}
5. 关闭/打开索引
临时关闭索引以节省资源:
POST /your_index/_close
POST /your_index/_open
七、其他常用操作
1. 查看集群健康状态
检查集群的健康状态:
GET /_cluster/health
2. 查看节点信息
获取集群中所有节点的信息:
GET /_nodes
3. 刷新索引
使最近的更改可搜索:
POST /your_index/_refresh
4. 强制合并索引
减少索引段数量以提高性能:
POST /your_index/_forcemerge
5. 搜索模板
创建可重用的搜索模板:
PUT /_scripts/search_template
{
"script": {
"lang": "mustache",
"source": {
"query": {
"match": {
"{{field}}": "{{value}}"
}
}
}
}
}
GET /your_index/_search/template
{
"id": "search_template",
"params": {
"field": "title",
"value": "example"
}
}
以上就是Elasticsearch的一些基础语法,涵盖了查询、修改、添加和删除等常见操作。在实际应用中,你可以根据具体需求灵活运用这些语法来操作Elasticsearch索引和文档。