a. Match Query
{
"query": {
"match": {
"name": "apple phone"
}
}
}
name
字段中包含 “apple” 或 “phone” 的文档。使用分词器匹配,适合模糊搜索。b. Match with Fuzziness
{
"query": {
"match": {
"name": {
"query": "apple phone",
"fuzziness": "AUTO"
}
}
}
}
fuzziness: AUTO
自动调整容错范围。c. Term Query
{
"query": {
"term": {
"category": "electronics"
}
}
}
category
字段为 “electronics” 的文档,不分词。d. Terms Query
{
"query": {
"terms": {
"category": ["electronics", "accessories"]
}
}
}
category
字段为 “electronics” 或 “accessories” 的文档。e. Range Query
{
"query": {
"range": {
"price": {
"gte": 100,
"lte": 500
}
}
}
}
price
字段在 100 到 500 之间的文档。f. Bool Query
{
"query": {
"bool": {
"must": [
{ "match": { "name": "phone" } },
{ "range": { "price": { "lte": 1000 } } }
],
"filter": [
{ "term": { "category": "electronics" } }
]
}
}
}
must
要求 name
包含 “phone” 且 price
≤ 1000,filter
要求 category
为 “electronics”。filter
不影响评分,性能更高。g. Exists Query
{
"query": {
"exists": {
"field": "description"
}
}
}
description
字段的文档。h. Wildcard Query
{
"query": {
"wildcard": {
"name": "app*"
}
}
}
name
字段以 “app” 开头的文档,*
表示任意字符。i. Nested Query
{
"query": {
"nested": {
"path": "comments",
"query": {
"match": { "comments.text": "good" }
}
}
}
}
comments
中 text
包含 “good” 的文档。处理嵌套对象关系。a. Basic SELECT
SELECT name, price FROM products WHERE price > 100
price
大于 100 的文档,仅显示 name
和 price
字段。b. With Conditions
SELECT * FROM products WHERE category = 'electronics' AND price BETWEEN 200 AND 500
category
为 “electronics” 且 price
在 200-500 之间的所有字段。c. Aggregation
SELECT category, AVG(price) FROM products GROUP BY category
category
分组,计算每组的平均 price
。a. Sequence Query
{
"query": "sequence [event where event_type == 'login'] [event where event_type == 'logout' by user_id]"
}
user_id
关联的连续事件,先 login
后 logout
。b. Simple Event Query
{
"query": "event where event_type == 'error' and severity > 3"
}
event_type
为 “error” 且 severity
大于 3 的事件。a. Field Calculation
{
"query": {
"script_score": {
"query": { "match_all": {} },
"script": {
"source": "doc['price'].value * 0.9"
}
}
}
}
price
字段值乘以 0.9,影响搜索评分。b. Filter with Script
{
"query": {
"bool": {
"filter": {
"script": {
"script": {
"source": "doc['price'].value > params.min_price",
"params": { "min_price": 100 }
}
}
}
}
}
}
price
大于 100 的文档,使用参数化脚本。a. Simple GET
GET products/_search
{
"query": {
"match": {
"name": "phone"
}
}
}
name
包含 “phone” 的文档。b. Aggregation
GET products/_search
{
"aggs": {
"by_category": {
"terms": { "field": "category" }
}
}
}
category
字段分组,统计每个类别的文档数。