elasticsearch version: 7.10.1
Elastic Match是Elasticsearch中用于全文搜索的一种查询语法。它可以将搜索词进行分词,并与目标查询字段进行匹配。
match的语法
GET /<index>/_search
{"query": {"match": {"<field_name>": {"query": "<your_search_text>",// 可选参数"operator": "and|or", // 默认为 or,控制多个词语之间的关系"analyzer": "<analyzer_name>", // 指定分析器"boost": <boost_value>, // 权重提升"fuzziness": "<fuzziness_setting>", // 模糊匹配设置// 其他参数,如 prefix_length、max_expansions 等}}}
}
- operator: 控制如何处理查询词之间的关系,可以是and或or,默认为or
- fuzziness: 允许查询词与索引中的词有一定程度的差异,支持模糊匹配。
- boost: 提高匹配此查询的文档的评分,从而影响文档在搜索结果中的排序。
案列
场景1
假设我们有一个产品索引,其中包含商品描述字段 description,现在我们想找到描述中同时包含 “leather jacket” 和 “winter wear” 的商品。
索引创建
PUT /products
{"mappings": {"properties": {"description": {"type": "text"},"title": {"type": "text"}}}
}
索引插入
POST /products/_doc
{"title": "Leather Jacket for Winter Wear","description": "High-quality leather jacket designed for winter wear. Made with genuine leather and lined with warm fabric."
}POST /products/_doc
{"title": "Summer Denim Jacket","description": "Lightweight denim jacket perfect for summer days."
}POST /products/_doc
{"title": "Winter Wool Coat","description": "Warm wool coat suitable for cold winter months."
}POST /products/_doc
{"title": "Leather Gloves","description": "Soft leather gloves for both men and women, ideal for chilly weather."
}POST /products/_doc
{"title": "Summer Jacket with UV Protection","description": "Lightweight windbreaker jacket featuring built-in UV protection for sunny days outdoors."
}POST /products/_doc
{"title": "Winter Boots with Fur Lining","description": "Waterproof boots lined with cozy fur, providing excellent insulation against harsh winter conditions."
}POST /products/_doc
{"title": "Leather Sneakers for Casual Wear","description": "Stylish leather sneakers suitable for everyday casual wear, made from premium materials."
}POST /products/_doc
{"title": "Wool Scarf with Leather Trim","description": "Luxurious wool scarf featuring a leather trim accent, adding a touch of sophistication to your winter ensemble."
}
查询
GET /products/_search
{"query": {"match": {"description": {"query": "leather jacket winter wear","operator": "and"}}}
}
场景2
同样在上述的商品索引中,我们想找到描述中包含 “leather” 或 “jacket” 的商品。
查询语句
GET /products/_search
{"query": {"match": {"description": {"query": "leather jacket","operator": "or"}}}
}
分词
指定分词器
PUT my_index
{"mappings": {"properties": {"message": {"type": "text","analyzer": "standard"}}}
}
查询分词
POST my_index/_doc
{"message": "Please don't split words with hyphens like co-operate or well-known"
}