Elasticsearch 对结构化数据(Structured)与非结构化数据(Unstructured / 全文)的处理机制截然不同,其核心在于字段类型(Mapping)与底层存储结构的差异。正确区分并设计两类数据,是构建高性能、高可用搜索系统的基石。
一、本质区别:存储与查询模型
| 特性 | 结构化数据 | 非结构化数据(全文) |
|---|---|---|
| 典型字段 | keyword,integer,date,boolean | text |
| 存储结构 | Doc Values(列式存储) | 倒排索引(Inverted Index) |
| 查询用途 | 精确匹配、范围查询、聚合、排序 | 全文搜索、相关性评分 |
| 分析器 | 无(原始值存储) | 有(分词、转小写等) |
| 内存使用 | 低(压缩列存) | 中(倒排索引) |
💡关键认知:
结构化 = 精确值操作,非结构化 = 语义匹配
二、映射(Mapping)设计策略
▶ 1.结构化字段:用keyword/ 数值类型
{"product_id":{"type":"long"},"brand":{"type":"keyword"},"price":{"type":"float"},"in_stock":{"type":"boolean"},"created_at":{"type":"date"}}- 优势:
- 支持
term查询(精确匹配) - 支持
range查询(价格/时间范围) - 支持高效聚合(
terms聚合) - 支持排序(
sort)
- 支持
▶ 2.非结构化字段:用text+ 分析器
{"description":{"type":"text","analyzer":"ik_max_word","search_analyzer":"ik_smart"}}- 优势:
- 支持
match查询(分词后匹配) - 支持相关性评分(BM25)
- 支持高亮(
highlight)
- 支持
▶ 3.混合字段:多字段(Multi-fields)
{"product_name":{"type":"text","analyzer":"ik_max_word","fields":{"keyword":{"type":"keyword"}}}}- 用途:
product_name:全文搜索(“手机” → 匹配“智能手机”)product_name.keyword:精确过滤/聚合(品牌 = “Apple”)
三、具象化实战:电商商品搜索系统
▶ 场景需求
- 用户搜 “iPhone 手机” → 返回相关商品(全文搜索)
- 筛选 “品牌=Apple”、“价格 5000–10000”(结构化过滤)
- 按价格排序 + 聚合品牌分布(结构化聚合)
▶ 步骤 1:定义 Mapping
PUT/products{"mappings":{"properties":{"id":{"type":"long"},"name":{"type":"text","analyzer":"ik_max_word","fields":{"keyword":{"type":"keyword"}}},"description":{"type":"text","analyzer":"ik_max_word"},"brand":{"type":"keyword"},"price":{"type":"float"},"in_stock":{"type":"boolean"},"created_at":{"type":"date"}}}}▶ 步骤 2:构建查询(DSL)
POST/products/_search{"query":{"bool":{"must":[{"match":{"name":"iPhone 手机"}}],"filter":[{"term":{"brand":"Apple"}},{"range":{"price":{"gte":5000,"lte":10000}}},{"term":{"in_stock":true}}]}},"aggs":{"brands":{"terms":{"field":"brand","size":10}},"price_ranges":{"range":{"field":"price","ranges":[{"to":5000},{"from":5000,"to":10000},{"from":10000}]}}},"sort":[{"price":"desc"}],"highlight":{"fields":{"name":{},"description":{}}}}▶ 步骤 3:PHP 实战(Laravel)
useElasticsearch\ClientBuilder;classProductService{publicfunctionsearch($keyword,$filters){$client=ClientBuilder::create()->build();$query=['bool'=>['must'=>[['match'=>['name'=>$keyword]]],'filter'=>[]]];// 结构化过滤if(!empty($filters['brand'])){$query['bool']['filter'][]=['term'=>['brand'=>$filters['brand']]];}if(!empty($filters['min_price'])){$query['bool']['filter'][]=['range'=>['price'=>['gte'=>$filters['min_price']]]];}$params=['index'=>'products','body'=>['query'=>$query,'aggs'=>['brands'=>['terms'=>['field'=>'brand.keyword']]],'sort'=>['price'=>'desc'],'highlight'=>['fields'=>['name'=>new\stdClass]]]];return$client->search($params);}}四、避坑指南
| 陷阱 | 破局方案 |
|---|---|
| 对 text 字段聚合 | 必须使用.keyword子字段 |
| 忽略高基数 keyword | ID 类字段用long,非keyword |
| 全文搜索用 term | 全文用match,精确用term |
| 未配中文分词器 | text字段必须指定ik_max_word |
五、终极心法
**“结构化与非结构化,
不是数据之分,而是用途之别——
- 当你用 keyword,
你在定义精确边界;- 当你用 text,
你在释放语义力量;- 当你设计 multi-field,
你在融合二者之长。真正的搜索系统,
始于对数据的敬畏,
成于对用途的洞察。”
结语
从今天起:
- 所有字符串字段必设
.keyword - 全文搜索用
match,过滤用term - ID/数字字段用
long/float,非keyword
因为最好的搜索体验,
不是模糊匹配,
而是精准融合结构与语义。