Elasticsearch使用示例

Elasticsearch使用示例

1. 基本操作示例

1.1 创建索引

# 创建索引
curl -X PUT "localhost:9200/my_index" -H "Content-Type: application/json" -d'
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "title": { "type": "text", "analyzer": "ik_max_word" },
      "content": { "type": "text", "analyzer": "ik_max_word" },
      "author": { "type": "keyword" },
      "publish_date": { "type": "date" },
      "views": { "type": "long" }
    }
  }
}'

1.2 添加文档

# 添加单个文档
curl -X POST "localhost:9200/my_index/_doc" -H "Content-Type: application/json" -d'
{
  "title": "Elasticsearch入门指南",
  "content": "Elasticsearch是一个开源的分布式搜索和分析引擎...",
  "author": "张三",
  "publish_date": "2024-04-03",
  "views": 100
}'

# 批量添加文档
curl -X POST "localhost:9200/my_index/_bulk" -H "Content-Type: application/json" -d'
{"index":{}}
{"title":"ES高级查询","content":"Elasticsearch提供了丰富的查询功能...","author":"李四","publish_date":"2024-04-02","views":50}
{"index":{}}
{"title":"ES性能优化","content":"如何优化Elasticsearch的性能...","author":"王五","publish_date":"2024-04-01","views":200}
'

1.3 查询文档

# 简单查询
curl -X GET "localhost:9200/my_index/_search" -H "Content-Type: application/json" -d'
{
  "query": {
    "match": {
      "title": "Elasticsearch"
    }
  }
}'

# 复杂查询
curl -X GET "localhost:9200/my_index/_search" -H "Content-Type: application/json" -d'
{
  "query": {
    "bool": {
      "must": [
        { "match": { "title": "Elasticsearch" }},
        { "range": { "views": { "gte": 100 }}}
      ],
      "filter": [
        { "term": { "author": "张三" }}
      ]
    }
  },
  "sort": [
    { "views": "desc" }
  ],
  "from": 0,
  "size": 10
}'

1.4 更新文档

# 更新文档
curl -X POST "localhost:9200/my_index/_update/1" -H "Content-Type: application/json" -d'
{
  "doc": {
    "views": 150
  }
}'

1.5 删除文档

# 删除单个文档
curl -X DELETE "localhost:9200/my_index/_doc/1"

# 删除索引
curl -X DELETE "localhost:9200/my_index"

2. 高级查询示例

2.1 全文搜索

# 使用IK分词器进行中文搜索
curl -X GET "localhost:9200/my_index/_search" -H "Content-Type: application/json" -d'
{
  "query": {
    "match": {
      "content": {
        "query": "分布式搜索",
        "analyzer": "ik_max_word"
      }
    }
  }
}'

2.2 聚合查询

# 按作者分组统计
curl -X GET "localhost:9200/my_index/_search" -H "Content-Type: application/json" -d'
{
  "aggs": {
    "group_by_author": {
      "terms": {
        "field": "author"
      },
      "aggs": {
        "total_views": {
          "sum": {
            "field": "views"
          }
        }
      }
    }
  }
}'

2.3 高亮显示

# 搜索结果高亮
curl -X GET "localhost:9200/my_index/_search" -H "Content-Type: application/json" -d'
{
  "query": {
    "match": {
      "content": "Elasticsearch"
    }
  },
  "highlight": {
    "fields": {
      "content": {}
    }
  }
}'

3. 实际应用场景示例

3.1 电商商品搜索

# 创建商品索引
curl -X PUT "localhost:9200/products" -H "Content-Type: application/json" -d'
{
  "mappings": {
    "properties": {
      "name": { "type": "text", "analyzer": "ik_max_word" },
      "description": { "type": "text", "analyzer": "ik_max_word" },
      "price": { "type": "double" },
      "category": { "type": "keyword" },
      "brand": { "type": "keyword" },
      "stock": { "type": "integer" },
      "sales": { "type": "long" },
      "create_time": { "type": "date" }
    }
  }
}'

# 商品搜索
curl -X GET "localhost:9200/products/_search" -H "Content-Type: application/json" -d'
{
  "query": {
    "bool": {
      "must": [
        { "match": { "name": "手机" }},
        { "range": { "price": { "gte": 1000, "lte": 5000 }}}
      ],
      "filter": [
        { "term": { "category": "电子产品" }}
      ]
    }
  },
  "sort": [
    { "sales": "desc" }
  ]
}'

3.2 日志分析

# 创建日志索引
curl -X PUT "localhost:9200/logs" -H "Content-Type: application/json" -d'
{
  "mappings": {
    "properties": {
      "timestamp": { "type": "date" },
      "level": { "type": "keyword" },
      "message": { "type": "text" },
      "service": { "type": "keyword" },
      "host": { "type": "keyword" }
    }
  }
}'

# 日志分析查询
curl -X GET "localhost:9200/logs/_search" -H "Content-Type: application/json" -d'
{
  "query": {
    "bool": {
      "must": [
        { "match": { "level": "ERROR" }},
        { "range": { "timestamp": { "gte": "now-1d" }}}
      ]
    }
  },
  "aggs": {
    "group_by_service": {
      "terms": {
        "field": "service"
      },
      "aggs": {
        "error_count": {
          "value_count": {
            "field": "level"
          }
        }
      }
    }
  }
}'

3.3 用户行为分析

# 创建用户行为索引
curl -X PUT "localhost:9200/user_actions" -H "Content-Type: application/json" -d'
{
  "mappings": {
    "properties": {
      "user_id": { "type": "keyword" },
      "action": { "type": "keyword" },
      "page": { "type": "keyword" },
      "timestamp": { "type": "date" },
      "duration": { "type": "long" }
    }
  }
}'

# 用户行为分析
curl -X GET "localhost:9200/user_actions/_search" -H "Content-Type: application/json" -d'
{
  "query": {
    "range": {
      "timestamp": {
        "gte": "now-7d"
      }
    }
  },
  "aggs": {
    "popular_pages": {
      "terms": {
        "field": "page",
        "size": 10
      },
      "aggs": {
        "avg_duration": {
          "avg": {
            "field": "duration"
          }
        }
      }
    }
  }
}'

4. 性能优化示例

4.1 索引优化

# 优化索引设置
curl -X PUT "localhost:9200/my_index/_settings" -H "Content-Type: application/json" -d'
{
  "index": {
    "refresh_interval": "30s",
    "number_of_replicas": 1,
    "max_result_window": 10000
  }
}'

4.2 查询优化

# 使用过滤器缓存
curl -X GET "localhost:9200/my_index/_search" -H "Content-Type: application/json" -d'
{
  "query": {
    "bool": {
      "filter": [
        { "term": { "status": "active" }},
        { "range": { "create_time": { "gte": "now-1d" }}}
      ]
    }
  }
}'

5. 监控和维护示例

5.1 集群健康检查

# 检查集群健康状态
curl -X GET "localhost:9200/_cluster/health?pretty"

# 检查节点状态
curl -X GET "localhost:9200/_cat/nodes?v"

# 检查索引状态
curl -X GET "localhost:9200/_cat/indices?v"

5.2 备份和恢复

# 创建快照仓库
curl -X PUT "localhost:9200/_snapshot/my_backup" -H "Content-Type: application/json" -d'
{
  "type": "fs",
  "settings": {
    "location": "/path/to/backup"
  }
}'

# 创建快照
curl -X PUT "localhost:9200/_snapshot/my_backup/snapshot_1?wait_for_completion=true"

# 恢复快照
curl -X POST "localhost:9200/_snapshot/my_backup/snapshot_1/_restore?wait_for_completion=true"

6. 高级功能示例

6.1 地理位置搜索

# 创建包含地理位置的索引
curl -X PUT "localhost:9200/restaurants" -H "Content-Type: application/json" -d'
{
  "mappings": {
    "properties": {
      "name": { "type": "text" },
      "location": { "type": "geo_point" },
      "cuisine": { "type": "keyword" },
      "rating": { "type": "float" }
    }
  }
}'

# 添加餐厅数据
curl -X POST "localhost:9200/restaurants/_bulk" -H "Content-Type: application/json" -d'
{"index":{}}
{"name":"北京烤鸭店","location":{"lat":39.9042,"lon":116.4074},"cuisine":"中餐","rating":4.5}
{"index":{}}
{"name":"上海小笼包","location":{"lat":31.2304,"lon":121.4737},"cuisine":"中餐","rating":4.2}
'

# 查找附近的餐厅
curl -X GET "localhost:9200/restaurants/_search" -H "Content-Type: application/json" -d'
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_distance": {
          "distance": "10km",
          "location": {
            "lat": 39.9042,
            "lon": 116.4074
          }
        }
      }
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "location": {
          "lat": 39.9042,
          "lon": 116.4074
        },
        "order": "asc",
        "unit": "km"
      }
    }
  ]
}'

6.2 嵌套文档

# 创建包含嵌套文档的索引
curl -X PUT "localhost:9200/blogs" -H "Content-Type: application/json" -d'
{
  "mappings": {
    "properties": {
      "title": { "type": "text" },
      "author": { "type": "keyword" },
      "comments": {
        "type": "nested",
        "properties": {
          "user": { "type": "keyword" },
          "content": { "type": "text" },
          "date": { "type": "date" }
        }
      }
    }
  }
}'

# 添加博客数据
curl -X POST "localhost:9200/blogs/_doc" -H "Content-Type: application/json" -d'
{
  "title": "Elasticsearch入门",
  "author": "张三",
  "comments": [
    {
      "user": "李四",
      "content": "很好的教程",
      "date": "2024-04-01"
    },
    {
      "user": "王五",
      "content": "期待更多内容",
      "date": "2024-04-02"
    }
  ]
}'

# 嵌套文档查询
curl -X GET "localhost:9200/blogs/_search" -H "Content-Type: application/json" -d'
{
  "query": {
    "nested": {
      "path": "comments",
      "query": {
        "bool": {
          "must": [
            { "match": { "comments.user": "李四" }},
            { "match": { "comments.content": "教程" }}
          ]
        }
      }
    }
  }
}'

6.3 脚本查询

# 使用脚本进行评分
curl -X GET "localhost:9200/products/_search" -H "Content-Type: application/json" -d'
{
  "query": {
    "function_score": {
      "query": {
        "match": { "name": "手机" }
      },
      "functions": [
        {
          "script_score": {
            "script": {
              "source": "doc[\"price\"].value < 2000 ? 2 : 1"
            }
          }
        }
      ]
    }
  }
}'

# 使用脚本进行字段计算
curl -X GET "localhost:9200/products/_search" -H "Content-Type: application/json" -d'
{
  "script_fields": {
    "discount_price": {
      "script": {
        "source": "doc[\"price\"].value * 0.8"
      }
    }
  }
}'

7. 实际业务场景示例

7.1 新闻推荐系统

# 创建新闻索引
curl -X PUT "localhost:9200/news" -H "Content-Type: application/json" -d'
{
  "mappings": {
    "properties": {
      "title": { "type": "text", "analyzer": "ik_max_word" },
      "content": { "type": "text", "analyzer": "ik_max_word" },
      "category": { "type": "keyword" },
      "tags": { "type": "keyword" },
      "publish_time": { "type": "date" },
      "views": { "type": "long" },
      "likes": { "type": "long" },
      "user_interests": { "type": "keyword" }
    }
  }
}'

# 个性化新闻推荐
curl -X GET "localhost:9200/news/_search" -H "Content-Type: application/json" -d'
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "should": [
            { "match": { "category": "科技" }},
            { "terms": { "tags": ["人工智能", "大数据"] }}
          ]
        }
      },
      "functions": [
        {
          "field_value_factor": {
            "field": "views",
            "factor": 0.1,
            "modifier": "log1p"
          }
        },
        {
          "field_value_factor": {
            "field": "likes",
            "factor": 0.2,
            "modifier": "log1p"
          }
        }
      ],
      "boost_mode": "sum"
    }
  },
  "sort": [
    { "_score": "desc" },
    { "publish_time": "desc" }
  ]
}'

7.2 电商商品搜索优化

# 商品搜索优化
curl -X GET "localhost:9200/products/_search" -H "Content-Type: application/json" -d'
{
  "query": {
    "multi_match": {
      "query": "智能手机",
      "fields": [
        "name^3",
        "description^2",
        "brand",
        "category"
      ],
      "type": "best_fields",
      "tie_breaker": 0.3
    }
  },
  "rescore": {
    "window_size": 10,
    "query": {
      "rescore_query": {
        "function_score": {
          "query": { "match_all": {} },
          "functions": [
            {
              "field_value_factor": {
                "field": "sales",
                "factor": 0.1,
                "modifier": "log1p"
              }
            },
            {
              "field_value_factor": {
                "field": "rating",
                "factor": 0.2
              }
            }
          ],
          "boost_mode": "sum"
        }
      }
    }
  },
  "aggs": {
    "price_ranges": {
      "range": {
        "field": "price",
        "ranges": [
          { "to": 1000 },
          { "from": 1000, "to": 3000 },
          { "from": 3000 }
        ]
      }
    },
    "brands": {
      "terms": {
        "field": "brand",
        "size": 10
      }
    }
  }
}'

7.3 日志监控系统

# 日志监控查询
curl -X GET "localhost:9200/logs-*/_search" -H "Content-Type: application/json" -d'
{
  "query": {
    "bool": {
      "must": [
        { "match": { "level": "ERROR" }},
        { "range": { "@timestamp": { "gte": "now-1h" }}}
      ]
    }
  },
  "aggs": {
    "error_trend": {
      "date_histogram": {
        "field": "@timestamp",
        "calendar_interval": "5m"
      },
      "aggs": {
        "error_count": {
          "value_count": {
            "field": "level"
          }
        }
      }
    },
    "top_errors": {
      "terms": {
        "field": "message.keyword",
        "size": 10
      }
    },
    "services": {
      "terms": {
        "field": "service.keyword",
        "size": 5
      },
      "aggs": {
        "error_rate": {
          "avg": {
            "script": {
              "source": "doc[\"level\"].value == \"ERROR\" ? 1 : 0"
            }
          }
        }
      }
    }
  }
}'

8. 性能优化示例

8.1 索引优化

# 优化索引设置
curl -X PUT "localhost:9200/my_index/_settings" -H "Content-Type: application/json" -d'
{
  "index": {
    "refresh_interval": "30s",
    "number_of_replicas": 1,
    "max_result_window": 10000,
    "blocks": {
      "read_only_allow_delete": false
    },
    "merge": {
      "scheduler": {
        "max_thread_count": 1
      }
    }
  }
}'

# 优化分片设置
curl -X PUT "localhost:9200/my_index/_settings" -H "Content-Type: application/json" -d'
{
  "index.routing.allocation.total_shards_per_node": 3,
  "index.unassigned.node_left.delayed_timeout": "5m"
}'

8.2 查询优化

# 使用过滤器缓存
curl -X GET "localhost:9200/my_index/_search" -H "Content-Type: application/json" -d'
{
  "query": {
    "bool": {
      "filter": [
        { "term": { "status": "active" }},
        { "range": { "create_time": { "gte": "now-1d" }}}
      ],
      "must": [
        { "match": { "title": "重要" }}
      ]
    }
  },
  "track_total_hits": true,
  "explain": true
}'

# 使用字段折叠
curl -X GET "localhost:9200/products/_search" -H "Content-Type: application/json" -d'
{
  "collapse": {
    "field": "brand.keyword",
    "inner_hits": {
      "name": "top_products",
      "size": 3,
      "sort": ["price"]
    }
  },
  "sort": ["_score"]
}'

9. 监控和维护示例

9.1 集群监控

# 检查集群健康
curl -X GET "localhost:9200/_cluster/health?pretty"

# 检查节点状态
curl -X GET "localhost:9200/_nodes/stats?pretty"

# 检查索引状态
curl -X GET "localhost:9200/_cat/indices?v&h=index,health,status,pri,rep,docs.count,store.size"

# 检查分片状态
curl -X GET "localhost:9200/_cat/shards?v&h=index,shard,prirep,state,docs,store,node"

9.2 性能监控

# 检查慢查询
curl -X GET "localhost:9200/_nodes/hot_threads"

# 检查索引性能
curl -X GET "localhost:9200/_nodes/stats/indices?pretty"

# 检查JVM状态
curl -X GET "localhost:9200/_nodes/stats/jvm?pretty"

9.3 数据维护

# 清理旧索引
curl -X DELETE "localhost:9200/logs-2024-03-*"

# 优化索引
curl -X POST "localhost:9200/my_index/_forcemerge?max_num_segments=1"

# 更新索引设置
curl -X PUT "localhost:9200/my_index/_settings" -H "Content-Type: application/json" -d'
{
  "index": {
    "refresh_interval": "1s",
    "number_of_replicas": 2
  }
}' 

你可能感兴趣的:(elasticsearch,大数据,搜索引擎)