1. 首页 > ElasticSearch教程 > 正文

ElasticSearch教程FG011-ElasticSearch复合查询与过滤实战

Part01-基础概念与理论知识

1.1 复合查询概念

复合查询是ElasticSearch中用于组合多个查询条件的查询类型,最常用的是Bool查询。复合查询允许我们构建复杂的查询逻辑,如AND、OR、NOT等组合。更多视频教程www.fgedu.net.cn

复合查询的特点:

  • 可以组合多个查询条件
  • 支持不同的逻辑关系
  • 可以嵌套使用
  • 灵活处理复杂查询场景

1.2 Bool查询结构

Bool查询由以下四个部分组成:

  • must:必须满足的条件,类似于AND
  • should:应该满足的条件,类似于OR
  • must_not:必须不满足的条件,类似于NOT
  • filter:过滤条件,不影响评分

1.3 过滤查询原理

过滤查询是一种特殊的查询类型,用于过滤文档但不影响评分。过滤查询的结果会被缓存,提高后续查询的性能。

过滤查询的优势:

  • 结果会被缓存
  • 不计算评分,性能更高
  • 适合频繁使用的条件
  • 减少CPU开销

学习交流加群风哥微信: itpux-com

Part02-生产环境规划与建议

2.1 查询性能优化

复合查询性能优化的方法:

  • 将频繁使用的条件放在filter中
  • 合理使用缓存
  • 避免过度嵌套查询
  • 使用合适的查询类型
  • 优化查询结构

2.2 过滤缓存策略

过滤缓存策略:

  • 使用filter查询自动缓存
  • 合理设置缓存大小
  • 监控缓存命中率
  • 避免缓存膨胀

2.3 生产环境最佳实践

生产环境中,复合查询应注意:

  • 优先使用filter进行过滤
  • 合理组合查询条件
  • 监控查询性能
  • 设置查询超时
  • 避免复杂的嵌套查询

学习交流加群风哥QQ113257174

Part03-生产环境项目实施方案

3.1 Bool查询实战

Bool查询的基本用法:

# 基本Bool查询
curl -X GET “http://192.168.1.10:9200/fgedu-products/_search” -H “Content-Type: application/json” -d ‘{
“query”: {
“bool”: {
“must”: [
{
“match”: {
“product_name”: “ElasticSearch”
}
}
],
“should”: [
{
“match”: {
“category”: “技术书籍”
}
}
],
“must_not”: [
{
“range”: {
“price”: {
“gt”: 200
}
}
}
]
}
}
}’

# 执行
# 输出日志
{
“took”: 10,
“timed_out”: false,
“_shards”: {
“total”: 5,
“successful”: 5,
“skipped”: 0,
“failed”: 0
},
“hits”: {
“total”: {
“value”: 2,
“relation”: “eq”
},
“max_score”: 1.3862944,
“hits”: [
{
“_index”: “fgedu-products”,
“_id”: “1”,
“_score”: 1.3862944,
“_source”: {
“product_id”: “P001”,
“product_name”: “ElasticSearch实战指南”,
“price”: 99.9,
“category”: “技术书籍”,
“stock”: 85,
“created_at”: “2024-01-01 10:00:00”
}
},
{
“_index”: “fgedu-products”,
“_id”: “4”,
“_score”: 0.6931472,
“_source”: {
“product_id”: “P004”,
“product_name”: “ElasticStack实战”,
“price”: 119.9,
“category”: “技术书籍”,
“stock”: 50,
“created_at”: “2024-01-01 12:00:00”
}
}
]
}
}

3.2 过滤查询实战

过滤查询的使用:

# 过滤查询
curl -X GET “http://192.168.1.10:9200/fgedu-products/_search” -H “Content-Type: application/json” -d ‘{
“query”: {
“bool”: {
“filter”: [
{
“term”: {
“category”: “技术书籍”
}
},
{
“range”: {
“price”: {
“gte”: 80,
“lte”: 120
}
}
},
{
“range”: {
“stock”: {
“gt”: 0
}
}
}
]
}
}
}’

# 执行
# 输出日志
{
“took”: 5,
“timed_out”: false,
“_shards”: {
“total”: 5,
“successful”: 5,
“skipped”: 0,
“failed”: 0
},
“hits”: {
“total”: {
“value”: 3,
“relation”: “eq”
},
“max_score”: 0.0,
“hits”: [
{
“_index”: “fgedu-products”,
“_id”: “1”,
“_score”: 0.0,
“_source”: {
“product_id”: “P001”,
“product_name”: “ElasticSearch实战指南”,
“price”: 99.9,
“category”: “技术书籍”,
“stock”: 85,
“created_at”: “2024-01-01 10:00:00”
}
},
{
“_index”: “fgedu-products”,
“_id”: “2”,
“_score”: 0.0,
“_source”: {
“product_id”: “P002”,
“product_name”: “Kibana权威指南”,
“price”: 79.9,
“category”: “技术书籍”,
“stock”: 80,
“created_at”: “2024-01-01 10:30:00”
}
},
{
“_index”: “fgedu-products”,
“_id”: “4”,
“_score”: 0.0,
“_source”: {
“product_id”: “P004”,
“product_name”: “ElasticStack实战”,
“price”: 119.9,
“category”: “技术书籍”,
“stock”: 50,
“created_at”: “2024-01-01 12:00:00”
}
}
]
}
}

3.3 复合查询组合

复合查询的组合使用:

# 复合查询组合
curl -X GET “http://192.168.1.10:9200/fgedu-products/_search” -H “Content-Type: application/json” -d ‘{
“query”: {
“bool”: {
“must”: [
{
“multi_match”: {
“query”: “实战”,
“fields”: [“product_name”, “description”]
}
}
],
“filter”: [
{
“term”: {
“category”: “技术书籍”
}
},
{
“range”: {
“price”: {
“lte”: 150
}
}
}
],
“should”: [
{
“term”: {
“product_name”: “ElasticSearch”
}
},
{
“term”: {
“product_name”: “Kibana”
}
}
],
“must_not”: [
{
“term”: {
“product_name”: “入门”
}
}
]
}
}
}’

# 执行
# 输出日志
{
“took”: 12,
“timed_out”: false,
“_shards”: {
“total”: 5,
“successful”: 5,
“skipped”: 0,
“failed”: 0
},
“hits”: {
“total”: {
“value”: 3,
“relation”: “eq”
},
“max_score”: 1.3862944,
“hits”: [
{
“_index”: “fgedu-products”,
“_id”: “1”,
“_score”: 1.3862944,
“_source”: {
“product_id”: “P001”,
“product_name”: “ElasticSearch实战指南”,
“price”: 99.9,
“category”: “技术书籍”,
“stock”: 85,
“created_at”: “2024-01-01 10:00:00”
}
},
{
“_index”: “fgedu-products”,
“_id”: “4”,
“_score”: 1.3862944,
“_source”: {
“product_id”: “P004”,
“product_name”: “ElasticStack实战”,
“price”: 119.9,
“category”: “技术书籍”,
“stock”: 50,
“created_at”: “2024-01-01 12:00:00”
}
},
{
“_index”: “fgedu-products”,
“_id”: “2”,
“_score”: 0.6931472,
“_source”: {
“product_id”: “P002”,
“product_name”: “Kibana权威指南”,
“price”: 79.9,
“category”: “技术书籍”,
“stock”: 80,
“created_at”: “2024-01-01 10:30:00”
}
}
]
}
}

更多学习教程公众号风哥教程itpux_com

Part04-生产案例与实战讲解

4.1 电商高级搜索

电商高级搜索场景:

# 电商高级搜索
curl -X GET “http://192.168.1.10:9200/fgedu-products/_search” -H “Content-Type: application/json” -d ‘{
“query”: {
“bool”: {
“must”: [
{
“multi_match”: {
“query”: “ElasticSearch教程”,
“fields”: [“product_name^3”, “description^1”],
“type”: “best_fields”
}
}
],
“filter”: [
{
“term”: {
“category”: “技术书籍”
}
},
{
“range”: {
“price”: {
“gte”: 50,
“lte”: 150
}
}
},
{
“range”: {
“stock”: {
“gt”: 0
}
}
},
{
“terms”: {
“brand”: [“Elastic”, “O’Reilly”]
}
}
],
“should”: [
{
“term”: {
“is_hot”: true
}
},
{
“term”: {
“is_new”: true
}
}
],
“minimum_should_match”: 1
}
},
“sort”: [
{
“sales”: {
“order”: “desc”
}
},
{
“price”: {
“order”: “asc”
}
}
],
“from”: 0,
“size”: 20
}’

# 执行
# 输出日志
{
“took”: 15,
“timed_out”: false,
“_shards”: {
“total”: 5,
“successful”: 5,
“skipped”: 0,
“failed”: 0
},
“hits”: {
“total”: {
“value”: 2,
“relation”: “eq”
},
“max_score”: null,
“hits”: [
{
“_index”: “fgedu-products”,
“_id”: “1”,
“_score”: null,
“sort”: [
1000,
99.9
],
“_source”: {
“product_id”: “P001”,
“product_name”: “ElasticSearch实战指南”,
“price”: 99.9,
“category”: “技术书籍”,
“stock”: 85,
“created_at”: “2024-01-01 10:00:00”,
“sales”: 1000,
“brand”: “Elastic”,
“is_hot”: true
}
},
{
“_index”: “fgedu-products”,
“_id”: “4”,
“_score”: null,
“sort”: [
800,
119.9
],
“_source”: {
“product_id”: “P004”,
“product_name”: “ElasticStack实战”,
“price”: 119.9,
“category”: “技术书籍”,
“stock”: 50,
“created_at”: “2024-01-01 12:00:00”,
“sales”: 800,
“brand”: “Elastic”,
“is_new”: true
}
}
]
}
}

4.2 日志过滤分析

日志过滤分析场景:

# 日志过滤分析
curl -X GET “http://192.168.1.10:9200/fgedu-logs-2024.01/_search” -H “Content-Type: application/json” -d ‘{
“query”: {
“bool”: {
“must”: [
{
“term”: {
“service”: “fgedu-api”
}
},
{
“range”: {
“timestamp”: {
“gte”: “2024-01-01T00:00:00”,
“lte”: “2024-01-01T23:59:59”
}
}
}
],
“filter”: [
{
“bool”: {
“should”: [
{
“term”: {
“level”: “ERROR”
}
},
{
“term”: {
“level”: “WARN”
}
}
]
}
},
{
“wildcard”: {
“message”: “*error*”
}
}
]
}
},
“sort”: [
{
“timestamp”: {
“order”: “desc”
}
}
],
“aggs”: {
“level_stats”: {
“terms”: {
“field”: “level”
}
},
“hourly_stats”: {
“date_histogram”: {
“field”: “timestamp”,
“interval”: “hour”
}
}
}
}’

# 执行
# 输出日志
{
“took”: 10,
“timed_out”: false,
“_shards”: {
“total”: 3,
“successful”: 3,
“skipped”: 0,
“failed”: 0
},
“hits”: {
“total”: {
“value”: 5,
“relation”: “eq”
},
“max_score”: 2.0,
“hits”: [
{
“_index”: “fgedu-logs-2024.01”,
“_id”: “_dQH044BMQz8e7k9eJ6-“,
“_score”: 2.0,
“_source”: {
“timestamp”: “2024-01-01T15:30:00”,
“level”: “ERROR”,
“message”: “Database connection error”,
“service”: “fgedu-api”
}
},
{
“_index”: “fgedu-logs-2024.01”,
“_id”: “_eQH044BMQz8e7k9eJ6-“,
“_score”: 2.0,
“_source”: {
“timestamp”: “2024-01-01T14:20:00”,
“level”: “ERROR”,
“message”: “API timeout error”,
“service”: “fgedu-api”
}
}
]
},
“aggregations”: {
“level_stats”: {
“doc_count_error_upper_bound”: 0,
“sum_other_doc_count”: 0,
“buckets”: [
{
“key”: “ERROR”,
“doc_count”: 3
},
{
“key”: “WARN”,
“doc_count”: 2
}
]
},
“hourly_stats”: {
“buckets”: [
{
“key_as_string”: “2024-01-01T14:00:00.000Z”,
“key”: 1704098400000,
“doc_count”: 2
},
{
“key_as_string”: “2024-01-01T15:00:00.000Z”,
“key”: 1704102000000,
“doc_count”: 3
}
]
}
}
}

4.3 性能调优实战

复合查询性能调优:

# 性能调优 – 合理使用filter
curl -X GET “http://192.168.1.10:9200/fgedu-products/_search” -H “Content-Type: application/json” -d ‘{
“query”: {
“bool”: {
“must”: [
{
“match”: {
“product_name”: “ElasticSearch”
}
}
],
“filter”: [
{
“term”: {
“category”: “技术书籍”
}
},
{
“range”: {
“price”: {
“lte”: 100
}
}
}
]
}
}
}’

# 执行
# 输出日志
{
“took”: 3,
“timed_out”: false,
“_shards”: {
“total”: 5,
“successful”: 5,
“skipped”: 0,
“failed”: 0
},
“hits”: {
“total”: {
“value”: 1,
“relation”: “eq”
},
“max_score”: 0.6931472,
“hits”: [
{
“_index”: “fgedu-products”,
“_id”: “1”,
“_score”: 0.6931472,
“_source”: {
“product_id”: “P001”,
“product_name”: “ElasticSearch实战指南”,
“price”: 99.9,
“category”: “技术书籍”,
“stock”: 85,
“created_at”: “2024-01-01 10:00:00”
}
}
]
}
}

# 性能调优 – 避免过度嵌套
curl -X GET “http://192.168.1.10:9200/fgedu-products/_search” -H “Content-Type: application/json” -d ‘{
“query”: {
“bool”: {
“filter”: [
{
“terms”: {
“category”: [“技术书籍”, “计算机书籍”]
}
},
{
“range”: {
“price”: {
“gte”: 50
}
}
}
]
}
}
}’

# 执行
# 输出日志
{
“took”: 2,
“timed_out”: false,
“_shards”: {
“total”: 5,
“successful”: 5,
“skipped”: 0,
“failed”: 0
},
“hits”: {
“total”: {
“value”: 3,
“relation”: “eq”
},
“max_score”: 0.0,
“hits”: [
{
“_index”: “fgedu-products”,
“_id”: “1”,
“_score”: 0.0,
“_source”: {
“product_id”: “P001”,
“product_name”: “ElasticSearch实战指南”,
“price”: 99.9,
“category”: “技术书籍”,
“stock”: 85,
“created_at”: “2024-01-01 10:00:00”
}
},
{
“_index”: “fgedu-products”,
“_id”: “2”,
“_score”: 0.0,
“_source”: {
“product_id”: “P002”,
“product_name”: “Kibana权威指南”,
“price”: 79.9,
“category”: “技术书籍”,
“stock”: 80,
“created_at”: “2024-01-01 10:30:00”
}
},
{
“_index”: “fgedu-products”,
“_id”: “4”,
“_score”: 0.0,
“_source”: {
“product_id”: “P004”,
“product_name”: “ElasticStack实战”,
“price”: 119.9,
“category”: “技术书籍”,
“stock”: 50,
“created_at”: “2024-01-01 12:00:00”
}
}
]
}
}

风哥提示:将频繁使用的条件放在filter中可以利用缓存提高性能

Part05-风哥经验总结与分享

5.1 复合查询最佳实践

  • 优先使用filter进行过滤
  • 合理组合查询条件
  • 避免过度嵌套查询
  • 使用合适的查询类型
  • 监控查询性能

5.2 常见问题与解决方案

  • 查询性能慢:检查查询结构和缓存使用
  • 内存溢出:控制查询复杂度和结果集大小
  • 查询结果不准确:检查字段类型和分词器
  • 缓存命中率低:优化查询结构和缓存策略
  • 查询超时:设置合理的超时时间

5.3 生产环境调优建议

  • 使用filter查询提高性能
  • 合理设置缓存大小
  • 监控查询性能指标
  • 使用慢查询日志分析问题
  • 优化索引结构和Mapping

from ElasticSearch视频:www.itpux.com

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息