一、初始化文档数据
-
在 Postman 中,向 ES 服务器发 POST 请求 :http://localhost:9200/user/_doc/1,请求体内容为:
{ "name":"zhangsan", "age":20, "sex":"男" }
-
在 Postman 中,向 ES 服务器发 POST 请求 :http://localhost:9200/user/_doc/2,请求体内容为:
{ "name":"zhangsan1", "age":21, "sex":"男" }
-
在 Postman 中,向 ES 服务器发 POST 请求 :http://localhost:9200/user/_doc/3,请求体内容为:
{ "name":"zhangsan2", "age":22, "sex":"男" }
-
在 Postman 中,向 ES 服务器发 POST 请求 :http://localhost:9200/user/_doc/4,请求体内容为:
{ "name":"lisi", "age":25, "sex":"女" }
二、高亮查询文档
2.1、概述
- 在进行关键字搜索时,搜索出的内容中的关键字会显示不同的颜色,称之为高亮。
- Elasticsearch 可以对查询内容中的关键字部分,进行标签和样式(高亮)的设置。
-
在使用
match
查询的同时,加上一个highlight
属性:属性 解释 pre_tags 前置标签 post_tags 后置标签 fields 需要高亮的字段 name 这里声明 name字段需要高亮,后面可以为这个字段设置特有配置,也可以空
2.2、示例
-
在 Postman 中,向 ES 服务器发 GET请求 :http://localhost:9200/user/_search,请求体内容为:
{ "query": { "match": { "name": "zhangsan" } }, "highlight": { "pre_tags": "<font color='red'>", "post_tags": "</font>", "fields": { "name": {} } } }
文章来源:https://www.toymoban.com/news/detail-427689.html
-
服务器响应结果
文章来源地址https://www.toymoban.com/news/detail-427689.html
到了这里,关于Elasticsearch7.8.0版本入门—— 高亮查询文档(高级查询)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!