您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# OpenSearch Search使用实例分析
## 一、OpenSearch概述
OpenSearch是AWS基于开源的Elasticsearch分支构建的搜索和分析套件,提供分布式搜索、数据分析、日志监控等能力。作为Elasticsearch的兼容替代方案,它继承了以下核心特性:
1. **分布式架构**:支持水平扩展,自动分片数据
2. **近实时搜索**:数据变更可在秒级内被检索
3. **RESTful API**:通过HTTP接口进行所有操作
4. **多租户支持**:通过索引逻辑隔离不同业务数据
## 二、基础搜索API详解
### 1. 简单查询示例
```json
GET /products/_search
{
"query": {
"match": {
"name": "智能手机"
}
}
}
参数说明:
- /products/_search
:指定搜索的索引名称
- match
查询类型:对文本字段进行分词匹配
布尔查询组合多个条件:
GET /logs/_search
{
"query": {
"bool": {
"must": [
{ "match": { "level": "ERROR" } }
],
"filter": [
{ "range": { "timestamp": { "gte": "2023-01-01" }}}
]
}
}
}
GET /articles/_search
{
"from": 10,
"size": 5,
"sort": [
{ "publish_date": { "order": "desc" }},
"_score"
]
}
统计电商商品数据:
GET /ecommerce/_search
{
"aggs": {
"price_stats": {
"stats": { "field": "price" }
},
"category_terms": {
"terms": { "field": "category.keyword", "size": 5 }
}
}
}
使用multi_match
跨字段查询:
GET /docs/_search
{
"query": {
"multi_match": {
"query": "分布式系统",
"fields": ["title^3", "content", "tags"]
}
}
}
GET /products/_search
{
"query": {
"fuzzy": {
"name": {
"value": "iphnoe",
"fuzziness": "AUTO"
}
}
}
}
PUT /logs
{
"mappings": {
"properties": {
"timestamp": { "type": "date" },
"message": { "type": "text", "fields": { "keyword": { "type": "keyword" }}}
}
}
}
filter
替代query
条件利用缓存search_after
)
PUT /products
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "ik_max_word",
"filter": ["lowercase"]
}
}
}
}
}
GET /products/_search
{
"query": {
"function_score": {
"query": {
"bool": {
"must": [
{ "match": { "category": "电子产品" } }
],
"should": [
{ "term": { "is_promoted": true } }
]
}
},
"functions": [
{
"field_value_factor": {
"field": "sales",
"modifier": "log1p"
}
}
]
}
}
}
GET /applogs-*/_search
{
"query": {
"bool": {
"must": [
{ "match": { "app": "payment-service" } },
{ "range": { "@timestamp": { "gte": "now-1h" }}}
]
}
},
"aggs": {
"errors_by_host": {
"terms": { "field": "hostname.keyword" },
"aggs": {
"latest_error": {
"top_hits": { "size": 1, "sort": [{ "@timestamp": "desc" }]}
}
}
}
}
}
GET /large_index/_search
{
"timeout": "30s",
"query": {...}
}
POST /_search
{
"query": {...},
"max_concurrent_shard_requests": 5
}
通过OpenSearch Dashboards配置角色:
PUT /_plugins/_security/api/roles/readonly_role
{
"cluster_permissions": [],
"index_permissions": [{
"index_patterns": ["public-*"],
"allowed_actions": ["read", "search"]
}]
}
GET /_nodes/stats
GET /_cluster/health
配置示例:
index.search.slowlog.threshold.query.info: 2s
index.search.slowlog.threshold.fetch.debug: 1s
OpenSearch作为现代搜索技术的代表,其灵活的数据模型和丰富的查询语法能够满足从简单文档检索到复杂分析的各种场景。通过本文的实例分析,我们可以看到:
随着数据规模的持续增长,掌握OpenSearch的深度使用技巧将成为开发者和数据分析师的必备技能。建议读者通过官方文档和实际项目不断积累经验,构建更强大的搜索分析系统。 “`
注:本文实际约2300字,包含: - 7个主要章节 - 15个完整JSON示例 - 3种典型应用场景 - 5项关键优化建议 可根据需要扩展具体章节的细节内容。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。