Elasticsearch高亮显示 #

一、高亮概述 #

1.1 高亮作用 #

text
高亮显示作用
├── 标记匹配内容
│   └── 突出显示搜索词
├── 提升用户体验
│   └── 快速定位匹配内容
└── 支持多种类型
    ├── unified(默认)
    ├── plain
    └── fvh(Fast Vector Highlighter)

二、基本高亮 #

2.1 简单高亮 #

bash
GET /products/_search
{
  "query": {
    "match": { "description": "iPhone" }
  },
  "highlight": {
    "fields": {
      "description": {}
    }
  }
}

响应:

json
{
  "hits": {
    "hits": [
      {
        "_source": {
          "description": "The iPhone 15 is the latest smartphone from Apple"
        },
        "highlight": {
          "description": [
            "The <em>iPhone</em> 15 is the latest smartphone from Apple"
          ]
        }
      }
    ]
  }
}

2.2 多字段高亮 #

bash
GET /products/_search
{
  "query": {
    "multi_match": {
      "query": "iPhone",
      "fields": ["name", "description"]
    }
  },
  "highlight": {
    "fields": {
      "name": {},
      "description": {}
    }
  }
}

三、高亮类型 #

3.1 unified高亮器(默认) #

bash
GET /products/_search
{
  "query": {
    "match": { "description": "iPhone" }
  },
  "highlight": {
    "type": "unified",
    "fields": {
      "description": {}
    }
  }
}

特点

  • 性能好
  • 支持phrase和fuzzy查询
  • 需要term_vector

3.2 plain高亮器 #

bash
GET /products/_search
{
  "query": {
    "match": { "description": "iPhone" }
  },
  "highlight": {
    "type": "plain",
    "fields": {
      "description": {}
    }
  }
}

特点

  • 简单直接
  • 适合单字段
  • 性能较差

3.3 fvh高亮器 #

需要映射配置:

bash
PUT /products
{
  "mappings": {
    "properties": {
      "description": {
        "type": "text",
        "term_vector": "with_positions_offsets"
      }
    }
  }
}

查询:

bash
GET /products/_search
{
  "query": {
    "match": { "description": "iPhone" }
  },
  "highlight": {
    "type": "fvh",
    "fields": {
      "description": {}
    }
  }
}

特点

  • 性能最好
  • 需要term_vector
  • 支持多字段

四、高亮配置 #

4.1 自定义标签 #

bash
GET /products/_search
{
  "query": {
    "match": { "description": "iPhone" }
  },
  "highlight": {
    "pre_tags": ["<span class='highlight'>"],
    "post_tags": ["</span>"],
    "fields": {
      "description": {}
    }
  }
}

4.2 片段设置 #

bash
GET /products/_search
{
  "query": {
    "match": { "description": "iPhone" }
  },
  "highlight": {
    "fields": {
      "description": {
        "fragment_size": 150,
        "number_of_fragments": 3
      }
    }
  }
}
参数 说明 默认值
fragment_size 片段大小 100
number_of_fragments 片段数量 5

4.3 无片段 #

bash
GET /products/_search
{
  "query": {
    "match": { "description": "iPhone" }
  },
  "highlight": {
    "fields": {
      "description": {
        "number_of_fragments": 0
      }
    }
  }
}

返回完整字段内容。

4.4 分隔符 #

bash
GET /products/_search
{
  "query": {
    "match": { "description": "iPhone" }
  },
  "highlight": {
    "fields": {
      "description": {
        "fragment_size": 50,
        "number_of_fragments": 3,
        "fragmenter": "simple"
      }
    }
  }
}
fragmenter 说明
simple 按句子分割
span 按词项跨度分割

4.5 边界设置 #

bash
GET /products/_search
{
  "query": {
    "match": { "description": "iPhone" }
  },
  "highlight": {
    "boundary_scanner": "sentence",
    "boundary_scanner_locale": "en-US",
    "fields": {
      "description": {}
    }
  }
}
boundary_scanner 说明
chars 按字符边界
sentence 按句子边界
word 按词边界

五、高亮选项 #

5.1 encoder #

bash
GET /products/_search
{
  "query": {
    "match": { "description": "iPhone" }
  },
  "highlight": {
    "encoder": "html",
    "fields": {
      "description": {}
    }
  }
}
encoder 说明
default 不编码
html HTML编码

5.2 no_match_size #

bash
GET /products/_search
{
  "query": {
    "match": { "name": "iPhone" }
  },
  "highlight": {
    "fields": {
      "description": {
        "no_match_size": 150
      }
    }
  }
}

没有匹配时返回字段前N个字符。

5.3 matched_fields #

bash
GET /products/_search
{
  "query": {
    "match": { "description": "iPhone" }
  },
  "highlight": {
    "fields": {
      "description": {
        "matched_fields": ["description", "description.plain"]
      }
    }
  }
}

5.4 require_field_match #

bash
GET /products/_search
{
  "query": {
    "match": { "name": "iPhone" }
  },
  "highlight": {
    "require_field_match": false,
    "fields": {
      "description": {}
    }
  }
}

默认只高亮查询中涉及的字段,设置为false则高亮所有指定字段。

5.5 order #

bash
GET /products/_search
{
  "query": {
    "match": { "description": "iPhone" }
  },
  "highlight": {
    "order": "score",
    "fields": {
      "description": {}
    }
  }
}
order 说明
none 按出现顺序(默认)
score 按相关性得分

六、全局与字段配置 #

6.1 全局配置 #

bash
GET /products/_search
{
  "query": {
    "match": { "description": "iPhone" }
  },
  "highlight": {
    "pre_tags": ["<b>"],
    "post_tags": ["</b>"],
    "fields": {
      "description": {},
      "name": {
        "pre_tags": ["<i>"],
        "post_tags": ["</i>"]
      }
    }
  }
}

6.2 使用分析器 #

bash
GET /products/_search
{
  "query": {
    "match": { "description": "iPhone" }
  },
  "highlight": {
    "fields": {
      "description": {
        "highlight_query": {
          "match": {
            "description": {
              "query": "iPhone",
              "analyzer": "standard"
            }
          }
        }
      }
    }
  }
}

七、实际应用示例 #

7.1 电商搜索高亮 #

bash
GET /products/_search
{
  "query": {
    "multi_match": {
      "query": "iPhone",
      "fields": ["name^2", "description", "brand"]
    }
  },
  "highlight": {
    "pre_tags": ["<mark>"],
    "post_tags": ["</mark>"],
    "fields": {
      "name": {
        "number_of_fragments": 0
      },
      "description": {
        "fragment_size": 150,
        "number_of_fragments": 2
      }
    }
  }
}

7.2 日志搜索高亮 #

bash
GET /logs/_search
{
  "query": {
    "match": { "message": "error" }
  },
  "highlight": {
    "fields": {
      "message": {
        "fragment_size": 200,
        "number_of_fragments": 1,
        "pre_tags": ["["],
        "post_tags": ["]"]
      }
    }
  }
}

八、最佳实践 #

8.1 性能优化 #

text
性能优化建议
├── 使用unified高亮器
│   └── 性能和功能平衡
├── 限制片段数量
│   └── 合理设置number_of_fragments
├── 控制片段大小
│   └── 设置fragment_size
└── 使用term_vector
    └── fvh高亮器需要

8.2 配置建议 #

text
配置建议
├── 标题字段
│   ├── number_of_fragments: 0
│   └── 显示完整内容
├── 描述字段
│   ├── fragment_size: 150
│   └── number_of_fragments: 2-3
└── 长文本字段
    ├── fragment_size: 200
    └── number_of_fragments: 1-2

九、总结 #

本章介绍了Elasticsearch高亮显示:

  1. 高亮用于标记匹配内容
  2. 三种高亮器各有特点
  3. 可自定义高亮标签
  4. 片段设置控制显示内容
  5. 合理配置优化性能
  6. 根据场景选择配置

下一步,我们将学习排序与分页。

最后更新:2026-03-27