ElasticSearch的查询关键字整合

1. match_all - 查询所有文档

query = {
    "query": {
        "match_all": {}
    }
}

res = es.search(index="books", body=query)
for hit in res["hits"]["hits"]:
    print(hit["_source"])

2. match - 匹配字段内容

query = {
    "query": {
        "match": {
            "title": "python"
        }
    }
}

3. term - 精确匹配(不分词)

query = {
    "query": {
        "term": {
            "author": "张三"
        }
    }
}

4. terms - 匹配多个精确值(类似 SQL 的 IN)

query = {
    "query": {
        "terms": {
            "status": ["published", "draft"]
        }
    }
}

5. range - 范围查询

query = {
    "query": {
        "range": {
            "publish_date": {
                "gte": "2023-01-01",
                "lte": "2023-12-31"
            }
        }
    }
}

6. bool - 布尔组合查询

query = {
    "query": {
        "bool": {
            "must": [
                { "match": { "title": "python" } },
                { "term": { "status": "published" } }
            ],
            "should": [
                { "term": { "author": "张三" } },
                { "term": { "author": "李四" } }
            ],
            "must_not": [
                { "term": { "tag": "测试" } }
            ],
            "filter": [
                { "range": { "price": { "lte": 100 } } }
            ]
        }
    }
}

7. exists - 判断字段是否存在

query = {
    "query": {
        "exists": {
            "field": "summary"
        }
    }
}

8. wildcard - 通配符查询

query = {
    "query": {
        "wildcard": {
            "title.keyword": "pyth*"
        }
    }
}

9. prefix - 前缀匹配

query = {
    "query": {
        "prefix": {
            "title.keyword": "py"
        }
    }
}

10. fuzzy - 模糊查询(容错)

query = {
    "query": {
        "fuzzy": {
            "title": {
                "value": "pythin",
                "fuzziness": 2
            }
        }
    }
}

11. ids - 根据 ID 查询

query = {
    "query": {
        "ids": {
            "values": ["1", "2", "3"]
        }
    }
}

12. multi_match - 多字段匹配

query = {
    "query": {
        "multi_match": {
            "query": "python",
            "fields": ["title", "summary", "content"]
        }
    }
}

13. script - 自定义脚本查询

query = {
    "query": {
        "script": {
            "script": {
                "source": "doc['price'].value > params.min_price",
                "params": {
                    "min_price": 50
                }
            }
        }
    }
}

14. nested - 嵌套对象查询

query = {
    "query": {
        "nested": {
            "path": "comments",
            "query": {
                "bool": {
                    "must": [
                        { "match": { "comments.author": "张三" } }
                    ]
                }
            }
        }
    }
}

15. match_phrase - 短语精确匹配

query = {
    "query": {
        "match_phrase": {
            "content": "深入理解 python"
        }
    }
}

16. constant_score - 统一评分查询

query = {
    "query": {
        "constant_score": {
            "filter": {
                "term": { "status": "published" }
            },
            "boost": 1.5
        }
    }
}

17. script_score - 自定义评分函数

query = {
    "query": {
        "function_score": {
            "query": { "match_all": {} },
            "script_score": {
                "script": {
                    "source": "doc['views'].value / 10"
                }
            }
        }
    }
}

18. regexp - 正则匹配

query = {
    "query": {
        "regexp": {
            "title.keyword": "py[a-z]{3}"
        }
    }
}

19. geo_bounding_box - 地理边界框查询

query = {
    "query": {
        "geo_bounding_box": {
            "location": {
                "top_left": {
                    "lat": 40.73,
                    "lon": -74.1
                },
                "bottom_right": {
                    "lat": 40.01,
                    "lon": -71.12
                }
            }
        }
    }
}

20. geo_distance - 地理距离查询

query = {
    "query": {
        "geo_distance": {
            "distance": "50km",
            "location": {
                "lat": 40.715,
                "lon": -73.988
            }
        }
    }
}

21. has_child - 子文档查询父文档

query = {
    "query": {
        "has_child": {
            "type": "comment",
            "query": {
                "match": {
                    "comment_text": "赞"
                }
            }
        }
    }
}

22. has_parent - 父文档查询子文档

query = {
    "query": {
        "has_parent": {
            "parent_type": "blog",
            "query": {
                "term": {
                    "category": "技术"
                }
            }
        }
    }
}

23. nested + inner_hits - 查询嵌套文档并返回匹配内容

query = {
    "query": {
        "nested": {
            "path": "comments",
            "query": {
                "match": {
                    "comments.text": "es"
                }
            },
            "inner_hits": {}
        }
    }
}

24. span_near - 词组接近查询(用于复杂语义匹配)

query = {
    "query": {
        "span_near": {
            "clauses": [
                { "span_term": { "content": "python" } },
                { "span_term": { "content": "教程" } }
            ],
            "slop": 3,
            "in_order": True
        }
    }
}

25. dis_max - 多查询中得分最高的作为结果

query = {
    "query": {
        "dis_max": {
            "queries": [
                { "match": { "title": "es" } },
                { "match": { "summary": "elasticsearch" } }
            ],
            "tie_breaker": 0.3
        }
    }
}

26. function_score + weight - 权重评分控制

query = {
    "query": {
        "function_score": {
            "query": { "match_all": {} },
            "functions": [
                {
                    "filter": { "term": { "status": "热门" } },
                    "weight": 2
                },
                {
                    "filter": { "term": { "status": "冷门" } },
                    "weight": 0.5
                }
            ],
            "score_mode": "sum",
            "boost_mode": "multiply"
        }
    }
}

27. match_phrase_prefix - 支持自动补全的短语查询

query = {
    "query": {
        "match_phrase_prefix": {
            "title": "深入理解"
        }
    }
}

28. more_like_this - 相似内容推荐查询

query = {
    "query": {
        "more_like_this": {
            "fields": ["title", "content"],
            "like": "Python 入门教程",
            "min_term_freq": 1,
            "max_query_terms": 12
        }
    }
}

29. scripted_metric - 聚合中使用脚本做自定义计算

query = {
    "aggs": {
        "total_profit": {
            "scripted_metric": {
                "init_script": "state.profit = 0",
                "map_script": "state.profit += doc['revenue'].value - doc['cost'].value",
                "combine_script": "return state.profit",
                "reduce_script": "return states.sum()"
            }
        }
    }
}

30. 分页查询 - from + size

query = {
    "from": 0,
    "size": 10,
    "query": {
        "match_all": {}
    }
}

31. 排序查询 - sort

query = {
    "query": {
        "match_all": {}
    },
    "sort": [
        { "publish_date": { "order": "desc" } },
        "_score"
    ]
}

32. 高亮查询 - highlight

query = {
    "query": {
        "match": {
            "content": "elasticsearch"
        }
    },
    "highlight": {
        "fields": {
            "content": {}
        }
    }
}

33. 聚合查询 - terms 分组统计

query = {
    "size": 0,
    "aggs": {
        "category_count": {
            "terms": {
                "field": "category.keyword"
            }
        }
    }
}

34. 聚合查询 - avg 平均值

query = {
    "size": 0,
    "aggs": {
        "avg_price": {
            "avg": {
                "field": "price"
            }
        }
    }
}

35. 聚合查询 - date_histogram 时间分组

query = {
    "size": 0,
    "aggs": {
        "sales_over_time": {
            "date_histogram": {
                "field": "order_date",
                "calendar_interval": "month"
            }
        }
    }
}

36. 多条件聚合 - terms + sub_agg

query = {
    "size": 0,
    "aggs": {
        "by_category": {
            "terms": { "field": "category.keyword" },
            "aggs": {
                "avg_price": {
                    "avg": { "field": "price" }
                }
            }
        }
    }
}

37. filter 过滤条件聚合(不影响主查询)

query = {
    "query": {
        "match_all": {}
    },
    "aggs": {
        "filtered_price": {
            "filter": {
                "term": { "category.keyword": "技术" }
            },
            "aggs": {
                "avg_price": {
                    "avg": { "field": "price" }
                }
            }
        }
    }
}

38. stats 综合统计(最大、最小、平均等)

query = {
    "size": 0,
    "aggs": {
        "price_stats": {
            "stats": {
                "field": "price"
            }
        }
    }
}

39. cardinality 去重计数(唯一数量)

query = {
    "size": 0,
    "aggs": {
        "unique_users": {
            "cardinality": {
                "field": "user_id.keyword"
            }
        }
    }
}

40. 脚本字段计算 - script

query = {
    "query": {
        "match_all": {}
    },
    "script_fields": {
        "price_with_tax": {
            "script": {
                "source": "doc['price'].value * 1.13"
            }
        }
    }
}

41. 运行时字段 - runtime fields

query = {
    "runtime_mappings": {
        "price_with_tax": {
            "type": "double",
            "script": {
                "source": "emit(doc['price'].value * 1.13)"
            }
        }
    },
    "query": {
        "range": {
            "price_with_tax": { "gte": 100 }
        }
    }
}

42. 深度分页 - search_after

query = {
    "size": 10,
    "query": {
        "match_all": {}
    },
    "sort": [{ "publish_date": "desc" }, { "_id": "desc" }],
    "search_after": ["2023-01-01T00:00:00", "book_123"]
}

43. collapse 折叠字段(去重展示)

query = {
    "query": {
        "match_all": {}
    },
    "collapse": {
        "field": "author.keyword"
    }
}

44. 管道聚合 - bucket_sort(分页聚合结果)

query = {
    "size": 0,
    "aggs": {
        "top_authors": {
            "terms": {
                "field": "author.keyword",
                "size": 100
            },
            "aggs": {
                "avg_score": {
                    "avg": {
                        "field": "score"
                    }
                },
                "sort_bucket": {
                    "bucket_sort": {
                        "sort": [{ "avg_score": { "order": "desc" } }],
                        "size": 10
                    }
                }
            }
        }
    }
}

45. 管道聚合 - cumulative_sum 累加

query = {
    "size": 0,
    "aggs": {
        "sales_over_time": {
            "date_histogram": {
                "field": "order_date",
                "calendar_interval": "month"
            },
            "aggs": {
                "monthly_sales": {
                    "sum": {
                        "field": "sales"
                    }
                },
                "cumulative_sales": {
                    "cumulative_sum": {
                        "buckets_path": "monthly_sales"
                    }
                }
            }
        }
    }
}

posted @ 2025-04-10 10:29  wellplayed  阅读(18)  评论(0)    收藏  举报