elasticsearch全文搜索,-Elasticsearch 高级查询:全文检索与结构化数据查询的结合使用 - term&#34

📅 2026-11-30 👤 拼多多助力营销技巧 📂 2026拼多多助力 👁️ 443 次阅读

Elasticsearch 高级查询:全文检索与结构化数据查询的结合使用

一、前言二、核心概念1. 全文检索(match)2. 结构化查询(term / range / terms)3. 两者结合 = **bool 查询**

三、全文检索 + 结构化查询 结合流程图四、核心语法(必须背会)五、为什么 must + filter 是最优组合?六、真实业务案例(直接可用)需求:最终查询 DSL:

七、日志场景结合查询(超级常用)八、Mapping 必须这样设计(关键!)九、最常见的 4 个错误(90%的人中招)1. 把结构化过滤写在 must 里2. 用 text 字段做精确过滤3. 不用 bool 组合4. 全文与结构化混用

十、总结(超级好记)Elasticsearch 最标准查询模型:一句话口诀:

The Begin点点关注,收藏不迷路

一、前言

在实际业务中(电商搜索、日志检索、内容平台、订单搜索),几乎 100% 的查询都是“全文搜索 + 结构化过滤”的组合。

例如:

搜索标题包含“手机”(全文检索)并且是华为品牌(结构化)价格 1000-3000(结构化)状态上架(结构化)

只使用全文检索,结果不准; 只使用结构化查询,没有搜索体验。

两者结合,才是 Elasticsearch 的真正威力!

二、核心概念

1. 全文检索(match)

对文本分词用于标题、内容、描述字段类型:text

2. 结构化查询(term / range / terms)

不分词、完全匹配用于状态、ID、枚举、数字、日期、城市、品牌字段类型:keyword / integer / boolean / date

3. 两者结合 = bool 查询

must:全文搜索(打分)filter:结构化过滤(不打分、高性能)

这就是 ES 最强大、最常用、最标准的查询模型!

三、全文检索 + 结构化查询 结合流程图

#mermaid-svg-YH6PHLwQbNBlsNHY{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-YH6PHLwQbNBlsNHY .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-YH6PHLwQbNBlsNHY .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-YH6PHLwQbNBlsNHY .error-icon{fill:#552222;}#mermaid-svg-YH6PHLwQbNBlsNHY .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-YH6PHLwQbNBlsNHY .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-YH6PHLwQbNBlsNHY .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-YH6PHLwQbNBlsNHY .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-YH6PHLwQbNBlsNHY .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-YH6PHLwQbNBlsNHY .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-YH6PHLwQbNBlsNHY .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-YH6PHLwQbNBlsNHY .marker{fill:#333333;stroke:#333333;}#mermaid-svg-YH6PHLwQbNBlsNHY .marker.cross{stroke:#333333;}#mermaid-svg-YH6PHLwQbNBlsNHY svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-YH6PHLwQbNBlsNHY p{margin:0;}#mermaid-svg-YH6PHLwQbNBlsNHY .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-YH6PHLwQbNBlsNHY .cluster-label text{fill:#333;}#mermaid-svg-YH6PHLwQbNBlsNHY .cluster-label span{color:#333;}#mermaid-svg-YH6PHLwQbNBlsNHY .cluster-label span p{background-color:transparent;}#mermaid-svg-YH6PHLwQbNBlsNHY .label text,#mermaid-svg-YH6PHLwQbNBlsNHY span{fill:#333;color:#333;}#mermaid-svg-YH6PHLwQbNBlsNHY .node rect,#mermaid-svg-YH6PHLwQbNBlsNHY .node circle,#mermaid-svg-YH6PHLwQbNBlsNHY .node ellipse,#mermaid-svg-YH6PHLwQbNBlsNHY .node polygon,#mermaid-svg-YH6PHLwQbNBlsNHY .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-YH6PHLwQbNBlsNHY .rough-node .label text,#mermaid-svg-YH6PHLwQbNBlsNHY .node .label text,#mermaid-svg-YH6PHLwQbNBlsNHY .image-shape .label,#mermaid-svg-YH6PHLwQbNBlsNHY .icon-shape .label{text-anchor:middle;}#mermaid-svg-YH6PHLwQbNBlsNHY .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-YH6PHLwQbNBlsNHY .rough-node .label,#mermaid-svg-YH6PHLwQbNBlsNHY .node .label,#mermaid-svg-YH6PHLwQbNBlsNHY .image-shape .label,#mermaid-svg-YH6PHLwQbNBlsNHY .icon-shape .label{text-align:center;}#mermaid-svg-YH6PHLwQbNBlsNHY .node.clickable{cursor:pointer;}#mermaid-svg-YH6PHLwQbNBlsNHY .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-YH6PHLwQbNBlsNHY .arrowheadPath{fill:#333333;}#mermaid-svg-YH6PHLwQbNBlsNHY .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-YH6PHLwQbNBlsNHY .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-YH6PHLwQbNBlsNHY .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-YH6PHLwQbNBlsNHY .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-YH6PHLwQbNBlsNHY .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-YH6PHLwQbNBlsNHY .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-YH6PHLwQbNBlsNHY .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-YH6PHLwQbNBlsNHY .cluster text{fill:#333;}#mermaid-svg-YH6PHLwQbNBlsNHY .cluster span{color:#333;}#mermaid-svg-YH6PHLwQbNBlsNHY div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-YH6PHLwQbNBlsNHY .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-YH6PHLwQbNBlsNHY rect.text{fill:none;stroke-width:0;}#mermaid-svg-YH6PHLwQbNBlsNHY .icon-shape,#mermaid-svg-YH6PHLwQbNBlsNHY .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-YH6PHLwQbNBlsNHY .icon-shape p,#mermaid-svg-YH6PHLwQbNBlsNHY .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-YH6PHLwQbNBlsNHY .icon-shape .label rect,#mermaid-svg-YH6PHLwQbNBlsNHY .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-YH6PHLwQbNBlsNHY .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-YH6PHLwQbNBlsNHY .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-YH6PHLwQbNBlsNHY :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

用户搜索 拼多多帮助力入口

全文检索 match 关键词

结构化过滤 品牌/价格/状态/日期

bool 查询组合

must 负责搜索

filter 负责高性能过滤

返回精准且相关的结果

四、核心语法(必须背会)

{

"query": {

"bool": {

"must": [

// 全文检索(关键词搜索)

{ "match": { "title": "手机" } }

],

"filter": [

// 结构化查询(精确/范围/等值过滤)

{ "term": { "brand": "华为" } },

{ "range": { "price": { "gte": 1000, "lte": 3000 } } },

{ "term": { "status": 1 } }

]

}

}

}

五、为什么 must + filter 是最优组合?

must(全文) 负责相关性、分词、搜索、打分 filter(结构化) 负责快速过滤、不计算分数、自动缓存 性能比 must 高 5~10 倍

✅ 最佳实践:全文放 must,过滤放 filter!

六、真实业务案例(直接可用)

需求:

搜索标题包含“电动车”(全文)品牌是雅迪/爱玛(结构化 terms)价格 1000~5000(结构化 range)状态=上架(结构化 term)库存>0(结构化 range)

最终查询 DSL:

GET /goods/_search

{

"query": {

"bool": {

"must": [

{

"match": {

"title": "电动车" // 全文搜索

}

}

],

"filter": [

{

"terms": {

"brand": ["雅迪","爱玛"] // 多值过滤

}

},

{

"range": {

"price": { "gte":1000, "lte":5000 }

}

},

{ "term": { "status": 1 } },

{ "range": { "stock": { "gt": 0 } } }

]

}

}

}

七、日志场景结合查询(超级常用)

全文检索:message 包含“NullPointerException”结构化:level=ERROR结构化:serviceName=order-service结构化:最近 1 小时

GET /logs-*/_search

{

"query": {

"bool": {

"must": [

{ "match": { "message": "NullPointerException" } }

],

"filter": [

{ "term": { "level": "ERROR" } },

{ "term": { "serviceName": "order-service" } },

{ "range": { "timestamp": { "gte": "now-1h" } } }

]

}

}

}

八、Mapping 必须这样设计(关键!)

要想全文检索 + 结构化查询同时生效,字段必须使用 multi-fields:

"title": {

"type": "text", // 全文检索

"fields": {

"keyword": {

"type": "keyword" // 结构化排序/聚合/精确匹配

}

}

}

全文搜:title精确/排序:title.keyword

九、最常见的 4 个错误(90%的人中招)

1. 把结构化过滤写在 must 里

→ 性能差、浪费资源 ✅ 结构化必须写 filter

2. 用 text 字段做精确过滤

→ 查询不准、查不到 ✅ 精确查询必须用 keyword

3. 不用 bool 组合

→ 无法实现复杂业务 ✅ 必须使用 bool 组合查询

4. 全文与结构化混用

→ 业务混乱 ✅ 全文用 match,结构化用 term/range/terms

十、总结(超级好记)

Elasticsearch 最标准查询模型:

全文检索(match) + 结构化过滤(filter) = 完美查询

一句话口诀:

关键词搜索放 must, 精确范围放 filter, text 负责全文搜, keyword 负责结构化, bool 一组合,万能查询就搞定!

The End点点关注,收藏不迷路

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧