元素码农
基础
UML建模
数据结构
算法
设计模式
网络
TCP/IP协议
HTTPS安全机制
WebSocket实时通信
数据库
sqlite
postgresql
clickhouse
后端
rust
go
java
php
mysql
redis
mongodb
etcd
nats
zincsearch
前端
浏览器
javascript
typescript
vue3
react
游戏
unity
unreal
C++
C#
Lua
App
android
ios
flutter
react-native
安全
Web安全
测试
软件测试
自动化测试 - Playwright
人工智能
Python
langChain
langGraph
运维
linux
docker
工具
git
svn
🌞
🌙
目录
▶
环境准备
安装部署指南
配置文件详解
服务启动验证
▶
核心概念
索引与文档模型
数据存储结构
搜索语法基础
▶
数据操作
批量数据导入
基础查询示例
数据删除维护
▶
应用实践
日志分析实战
电商搜索实现
API接口调用
▶
系统优化
索引性能调优
缓存配置策略
发布时间:
2025-04-08 10:45
↑
☰
# ZincSearch基础查询示例 本文将通过实际示例,详细介绍ZincSearch的基础查询操作,帮助您快速掌握搜索功能的使用。 ## 准备工作 ### 1. 创建测试索引 ```bash curl -X PUT -H "Content-Type: application/json" \ http://localhost:4080/api/index -d '{ "name": "products", "mappings": { "properties": { "name": { "type": "text" }, "description": { "type": "text" }, "price": { "type": "float" }, "category": { "type": "keyword" }, "tags": { "type": "keyword" } } } }' ``` ### 2. 添加测试数据 ```bash curl -X POST -H "Content-Type: application/json" \ http://localhost:4080/api/_bulk -d ' {"index": {"_index": "products", "_id": "1"}} {"name": "智能手机", "description": "高性能5G手机", "price": 4999.00, "category": "电子产品", "tags": ["手机", "5G"]} {"index": {"_index": "products", "_id": "2"}} {"name": "笔记本电脑", "description": "轻薄商务本", "price": 6999.00, "category": "电子产品", "tags": ["电脑", "办公"]} {"index": {"_index": "products", "_id": "3"}} {"name": "无线耳机", "description": "蓝牙5.0降噪耳机", "price": 999.00, "category": "配件", "tags": ["耳机", "蓝牙"]} ' ``` ## 基础查询 ### 1. 全文搜索 ```bash # 搜索包含"手机"的商品 curl -X POST -H "Content-Type: application/json" \ http://localhost:4080/api/products/_search -d '{ "search_type": "match", "query": { "term": "手机", "field": "description" } }' ``` ### 2. 精确匹配 ```bash # 查找特定类别的商品 curl -X POST -H "Content-Type: application/json" \ http://localhost:4080/api/products/_search -d '{ "search_type": "term", "query": { "term": "电子产品", "field": "category" } }' ``` ### 3. 范围查询 ```bash # 查找特定价格区间的商品 curl -X POST -H "Content-Type: application/json" \ http://localhost:4080/api/products/_search -d '{ "search_type": "range", "query": { "field": "price", "gte": 1000, "lte": 5000 } }' ``` ## 复合查询 ### 1. 布尔查询 ```bash # 组合多个条件 curl -X POST -H "Content-Type: application/json" \ http://localhost:4080/api/products/_search -d '{ "search_type": "bool", "query": { "must": [ { "term": "电子产品", "field": "category" } ], "should": [ { "term": "5G", "field": "tags" } ], "must_not": [ { "range": { "field": "price", "gt": 10000 } } ] } }' ``` ### 2. 多字段查询 ```bash # 在多个字段中搜索 curl -X POST -H "Content-Type: application/json" \ http://localhost:4080/api/products/_search -d '{ "search_type": "multi_match", "query": { "term": "无线", "fields": ["name", "description"] } }' ``` ## 结果处理 ### 1. 分页 ```bash # 使用分页获取结果 curl -X POST -H "Content-Type: application/json" \ http://localhost:4080/api/products/_search -d '{ "from": 0, "size": 10, "search_type": "match", "query": { "term": "电子" } }' ``` ### 2. 排序 ```bash # 按价格排序 curl -X POST -H "Content-Type: application/json" \ http://localhost:4080/api/products/_search -d '{ "sort": [ { "field": "price", "order": "desc" } ], "search_type": "match_all" }' ``` ### 3. 高亮显示 ```bash # 高亮匹配内容 curl -X POST -H "Content-Type: application/json" \ http://localhost:4080/api/products/_search -d '{ "highlight": { "fields": ["description"], "pre_tags": ["<em>"], "post_tags": ["</em>"] }, "search_type": "match", "query": { "term": "无线" } }' ``` ## 聚合分析 ### 1. 分类统计 ```bash # 统计各个分类的商品数量 curl -X POST -H "Content-Type: application/json" \ http://localhost:4080/api/products/_search -d '{ "aggs": { "categories": { "terms": { "field": "category" } } } }' ``` ### 2. 价格分析 ```bash # 计算平均价格 curl -X POST -H "Content-Type: application/json" \ http://localhost:4080/api/products/_search -d '{ "aggs": { "avg_price": { "avg": { "field": "price" } } } }' ``` ## 查询优化 ### 1. 性能优化 - 使用合适的查询类型 - 避免大范围的通配符查询 - 合理设置分页大小 ### 2. 相关性优化 - 调整字段权重 - 使用同义词扩展 - 优化分词器配置 ## 常见问题 1. 查询无结果 - 检查查询语法 - 验证字段名称 - 确认数据存在 2. 性能问题 - 优化查询结构 - 调整索引配置 - 使用缓存 ## 调试技巧 ### 1. 查看查询计划 ```bash curl -X POST -H "Content-Type: application/json" \ http://localhost:4080/api/products/_explain -d '{ "search_type": "match", "query": { "term": "手机" } }' ``` ### 2. 性能分析 ```bash curl -X POST -H "Content-Type: application/json" \ http://localhost:4080/api/products/_profile -d '{ "search_type": "match", "query": { "term": "手机" } }' ``` ## 最佳实践 1. 查询设计 - 选择合适的查询类型 - 优化查询结构 - 注意性能影响 2. 结果处理 - 合理使用分页 - 适当排序 - 结果缓存 3. 错误处理 - 验证输入 - 处理异常 - 提供反馈