Elasticsearch 语法详细解释

Elasticsearch 语法主要基于 RESTful API 和 JSON 格式,支持丰富的查询语法和数据操作方式,用于管理和检索数据。Elasticsearch 提供了强大的全文搜索、结构化搜索、聚合查询等功能,其灵活的语法使得它可以适应各种搜索和分析场景。

1. 索引管理

Elasticsearch 中的索引相当于传统数据库中的“数据库”概念,索引管理是使用 Elasticsearch 的基础。

1.1 创建索引

在 Elasticsearch 中,使用 PUT 请求创建一个新索引。以下是创建一个名为 my_index 的索引的示例:

PUT /my_index

可以通过 settingsmappings 参数自定义索引配置,例如分片数量、副本数量、字段类型等:

PUT /my_index
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 2
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      },
      "date": {
        "type": "date"
      },
      "views": {
        "type": "integer"
      }
    }
  }
}
1.2 查看索引

使用 GET 请求可以查看已创建的索引及其配置:

GET /my_index
1.3 删除索引

可以通过 DELETE 请求删除一个索引:

DELETE /my_index
1.4 更新索引设置

索引创建后可以动态更新其设置,例如调整副本数量:

PUT /my_index/_settings
{
  "number_of_replicas": 1
}

2. 文档操作

Elasticsearch 中的数据以文档的形式存储,文档是 JSON 格式的数据结构。

2.1 创建和索引文档

使用 POSTPUT 请求可以将文档索引到某个索引中:

POST /my_index/_doc/1
{
  "title": "Introduction to Elasticsearch",
  "date": "2024-09-04",
  "views": 100
}

在上述示例中,_doc 是文档类型(从 7.0 版本开始,类型概念被淡化),1 是文档的唯一标识符。

2.2 获取文档

使用 GET 请求可以根据文档 ID 检索文档:

GET /my_index/_doc/1
2.3 更新文档

使用 POSTPUT 请求可以更新文档。部分更新可以使用 _update 端点:

POST /my_index/_update/1
{
  "doc": {
    "views": 150
  }
}
2.4 删除文档

使用 DELETE 请求可以删除特定文档:

DELETE /my_index/_doc/1
2.5 批量操作

批量操作(Bulk API)可以用来一次性执行多个操作(如索引、更新、删除):

POST /_bulk
{ "index": { "_index": "my_index", "_id": "2" } }
{ "title": "Learning Elasticsearch", "date": "2024-09-05", "views": 200 }
{ "update": { "_index": "my_index", "_id": "1" } }
{ "doc": { "views": 250 } }
{ "delete": { "_index": "my_index", "_id": "2" } }

每个操作行必须紧跟一个操作元数据行。

3. 查询语法

Elasticsearch 的查询语法非常灵活,支持多种查询类型和组合。查询可以用于执行全文搜索、精确匹配搜索、范围查询等操作。

3.1 查询(Query)和过滤(Filter)
  • 查询(Query):主要用于全文搜索,返回与查询相关的文档,并根据相关性(得分)排序。
  • 过滤(Filter):用于精确匹配和结构化数据查询,不计算相关性评分,因此速度更快,适合用于缓存。
3.2 Match 查询

match 查询是最常用的全文搜索查询类型,用于搜索文本字段。它会对输入的查询字符串进行分析,并返回匹配的结果:

GET /my_index/_search
{
  "query": {
    "match": {
      "title": "Elasticsearch"
    }
  }
}
3.3 Term 查询

term 查询用于精确匹配不经过分析的字段,通常用于结构化数据的查询:

GET /my_index/_search
{
  "query": {
    "term": {
      "views": 100
    }
  }
}
3.4 Bool 查询

bool 查询允许通过布尔逻辑组合多个查询,可以创建更复杂的查询条件。常见的子查询类型有 must(必须匹配)、should(应该匹配)、must_not(不匹配)和 filter(过滤):

GET /my_index/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "title": "Elasticsearch" } }
      ],
      "filter": [
        { "range": { "date": { "gte": "2024-01-01" } } }
      ]
    }
  }
}
3.5 Range 查询

range 查询用于数值、日期或其他可排序类型字段的范围查询:

GET /my_index/_search
{
  "query": {
    "range": {
      "views": {
        "gte": 100,
        "lte": 200
      }
    }
  }
}
3.6 Multi-Match 查询

multi_match 查询允许对多个字段执行全文搜索:

GET /my_index/_search
{
  "query": {
    "multi_match": {
      "query": "Elasticsearch",
      "fields": ["title", "content"]
    }
  }
}
3.7 聚合查询

Elasticsearch 支持强大的聚合功能,可以对数据进行分组、统计和分析。聚合查询使用 aggregationsaggs 关键字。

4. 聚合操作

聚合(Aggregation)是 Elasticsearch 的一大特性,允许用户对数据执行复杂的分析操作。

4.1 基本聚合
  • Terms 聚合:对字段值进行分组统计,类似于 SQL 中的 GROUP BY
GET /my_index/_search
{
  "size": 0,
  "aggs": {
    "views_count": {
      "terms": {
        "field": "views"
      }
    }
  }
}
  • Range 聚合:对数值或日期字段进行分段统计。
GET /my_index/_search
{
  "size": 0,
  "aggs": {
    "views_ranges": {
      "range": {
        "field": "views",
        "ranges": [
          { "to": 100 },
          { "from": 100, "to": 200 },
          { "from": 200 }
        ]
      }
    }
  }
}
4.2 嵌套聚合

可以在聚合内部嵌套其他聚合,以实现更复杂的统计和分析。例如,首先按 date 聚合,然后对每个日期的 views 进行平均计算:

GET /my_index/_search
{
  "size": 0,
  "aggs": {
    "dates": {
      "date_histogram": {
        "field": "date",
        "interval": "day"
      },
      "aggs": {
        "average_views": {
          "avg": {
            "field": "views"
          }
        }
      }
    }
  }
}

5. 高级查询和功能

5.1 脚本查询

Elasticsearch 支持脚本查询,可以使用 Painless 脚本语言编写自定义查询和聚合逻辑:

GET /my_index/_search
{
  "query": {
    "script_score": {
      "query": {
        "match": { "title": "Elasticsearch" }
      },
      "script": {
        "source": "doc['views'].value * 0.1"
      }
    }
  }
}
5.2 Highlight 高亮查询

highlight 功能可以在搜索结果中对匹配的文本进行高亮显示,增强用户体验:

GET /my_index/_search
{
  "query": {
    "match": {
      "title": "Elasticsearch"
    }
  },
  "highlight": {
    "fields": {
      "title

": {}
    }
  }
}
5.3 Suggesters 自动补全

Suggesters 用于实现搜索自动补全和拼写建议等功能:

GET /my_index/_search
{
  "suggest": {
    "text": "Elasticsearch",
    "simple_phrase": {
      "phrase": {
        "field": "title",
        "size": 1,
        "real_word_error_likelihood": 0.95,
        "max_errors": 0.5,
        "gram_size": 2
      }
    }
  }
}

6. 结论

Elasticsearch 的语法非常丰富和灵活,支持多种查询类型和聚合操作,可以适应各种搜索和分析需求。从基础的索引管理到复杂的查询和聚合操作,Elasticsearch 提供了强大的功能来满足大数据环境下的搜索和分析任务。理解这些语法和功能可以帮助用户更好地利用 Elasticsearch 来构建高效的搜索和分析系统。

Elasticsearch 语法主要基于 RESTful API 和 JSON 格式,支持丰富的查询语法和数据操作方式,用于管理和检索数据。Elasticsearch 提供了强大的全文搜索、结构化搜索、聚合查询等功能,其灵活的语法使得它可以适应各种搜索和分析场景。本文将详细介绍 Elasticsearch 的语法,包括索引管理、文档操作、查询语法、聚合操作及其高级用法。

1. 索引管理

Elasticsearch 中的索引相当于传统数据库中的“数据库”概念,索引管理是使用 Elasticsearch 的基础。

1.1 创建索引

在 Elasticsearch 中,使用 PUT 请求创建一个新索引。以下是创建一个名为 my_index 的索引的示例:

PUT /my_index

可以通过 settingsmappings 参数自定义索引配置,例如分片数量、副本数量、字段类型等:

PUT /my_index
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 2
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      },
      "date": {
        "type": "date"
      },
      "views": {
        "type": "integer"
      }
    }
  }
}
1.2 查看索引

使用 GET 请求可以查看已创建的索引及其配置:

GET /my_index
1.3 删除索引

可以通过 DELETE 请求删除一个索引:

DELETE /my_index
1.4 更新索引设置

索引创建后可以动态更新其设置,例如调整副本数量:

PUT /my_index/_settings
{
  "number_of_replicas": 1
}

2. 文档操作

Elasticsearch 中的数据以文档的形式存储,文档是 JSON 格式的数据结构。

2.1 创建和索引文档

使用 POSTPUT 请求可以将文档索引到某个索引中:

POST /my_index/_doc/1
{
  "title": "Introduction to Elasticsearch",
  "date": "2024-09-04",
  "views": 100
}

在上述示例中,_doc 是文档类型(从 7.0 版本开始,类型概念被淡化),1 是文档的唯一标识符。

2.2 获取文档

使用 GET 请求可以根据文档 ID 检索文档:

GET /my_index/_doc/1
2.3 更新文档

使用 POSTPUT 请求可以更新文档。部分更新可以使用 _update 端点:

POST /my_index/_update/1
{
  "doc": {
    "views": 150
  }
}
2.4 删除文档

使用 DELETE 请求可以删除特定文档:

DELETE /my_index/_doc/1
2.5 批量操作

批量操作(Bulk API)可以用来一次性执行多个操作(如索引、更新、删除):

POST /_bulk
{ "index": { "_index": "my_index", "_id": "2" } }
{ "title": "Learning Elasticsearch", "date": "2024-09-05", "views": 200 }
{ "update": { "_index": "my_index", "_id": "1" } }
{ "doc": { "views": 250 } }
{ "delete": { "_index": "my_index", "_id": "2" } }

每个操作行必须紧跟一个操作元数据行。

3. 查询语法

Elasticsearch 的查询语法非常灵活,支持多种查询类型和组合。查询可以用于执行全文搜索、精确匹配搜索、范围查询等操作。

3.1 查询(Query)和过滤(Filter)
  • 查询(Query):主要用于全文搜索,返回与查询相关的文档,并根据相关性(得分)排序。
  • 过滤(Filter):用于精确匹配和结构化数据查询,不计算相关性评分,因此速度更快,适合用于缓存。
3.2 Match 查询

match 查询是最常用的全文搜索查询类型,用于搜索文本字段。它会对输入的查询字符串进行分析,并返回匹配的结果:

GET /my_index/_search
{
  "query": {
    "match": {
      "title": "Elasticsearch"
    }
  }
}
3.3 Term 查询

term 查询用于精确匹配不经过分析的字段,通常用于结构化数据的查询:

GET /my_index/_search
{
  "query": {
    "term": {
      "views": 100
    }
  }
}
3.4 Bool 查询

bool 查询允许通过布尔逻辑组合多个查询,可以创建更复杂的查询条件。常见的子查询类型有 must(必须匹配)、should(应该匹配)、must_not(不匹配)和 filter(过滤):

GET /my_index/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "title": "Elasticsearch" } }
      ],
      "filter": [
        { "range": { "date": { "gte": "2024-01-01" } } }
      ]
    }
  }
}
3.5 Range 查询

range 查询用于数值、日期或其他可排序类型字段的范围查询:

GET /my_index/_search
{
  "query": {
    "range": {
      "views": {
        "gte": 100,
        "lte": 200
      }
    }
  }
}
3.6 Multi-Match 查询

multi_match 查询允许对多个字段执行全文搜索:

GET /my_index/_search
{
  "query": {
    "multi_match": {
      "query": "Elasticsearch",
      "fields": ["title", "content"]
    }
  }
}
3.7 聚合查询

Elasticsearch 支持强大的聚合功能,可以对数据进行分组、统计和分析。聚合查询使用 aggregationsaggs 关键字。

4. 聚合操作

聚合(Aggregation)是 Elasticsearch 的一大特性,允许用户对数据执行复杂的分析操作。

4.1 基本聚合
  • Terms 聚合:对字段值进行分组统计,类似于 SQL 中的 GROUP BY
GET /my_index/_search
{
  "size": 0,
  "aggs": {
    "views_count": {
      "terms": {
        "field": "views"
      }
    }
  }
}
  • Range 聚合:对数值或日期字段进行分段统计。
GET /my_index/_search
{
  "size": 0,
  "aggs": {
    "views_ranges": {
      "range": {
        "field": "views",
        "ranges": [
          { "to": 100 },
          { "from": 100, "to": 200 },
          { "from": 200 }
        ]
      }
    }
  }
}
4.2 嵌套聚合

可以在聚合内部嵌套其他聚合,以实现更复杂的统计和分析。例如,首先按 date 聚合,然后对每个日期的 views 进行平均计算:

GET /my_index/_search
{
  "size": 0,
  "aggs": {
    "dates": {
      "date_histogram": {
        "field": "date",
        "interval": "day"
      },
      "aggs": {
        "average_views": {
          "avg": {
            "field": "views"
          }
        }
      }
    }
  }
}

5. 高级查询和功能

5.1 脚本查询

Elasticsearch 支持脚本查询,可以使用 Painless 脚本语言编写自定义查询和聚合逻辑:

GET /my_index/_search
{
  "query": {
    "script_score": {
      "query": {
        "match": { "title": "Elasticsearch" }
      },
      "script": {
        "source": "doc['views'].value * 0.1"
      }
    }
  }
}
5.2 Highlight 高亮查询

highlight 功能可以在搜索结果中对匹配的文本进行高亮显示,增强用户体验:

GET /my_index/_search
{
  "query": {
    "match": {
      "title": "Elasticsearch"
    }
  },
  "highlight": {
    "fields": {
      "title

": {}
    }
  }
}
5.3 Suggesters 自动补全

Suggesters 用于实现搜索自动补全和拼写建议等功能:

GET /my_index/_search
{
  "suggest": {
    "text": "Elasticsearch",
    "simple_phrase": {
      "phrase": {
        "field": "title",
        "size": 1,
        "real_word_error_likelihood": 0.95,
        "max_errors": 0.5,
        "gram_size": 2
      }
    }
  }
}

6. 结论

Elasticsearch 的语法非常丰富和灵活,支持多种查询类型和聚合操作,可以适应各种搜索和分析需求。从基础的索引管理到复杂的查询和聚合操作,Elasticsearch 提供了强大的功能来满足大数据环境下的搜索和分析任务。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值