码迷,mamicode.com
首页 > 其他好文 > 详细

elasticsearch之高亮查询

时间:2020-02-02 19:38:06      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:esc   style   自定义   默认   逗号   desc   span   注意   index   

准备数据

PUT zhifou/doc/4
{
  "name":"石头",
  "age":29,
  "from":"gu",
  "desc":"粗中有细,狐假虎威",
  "tags":["", "",""]
}

 

默认高亮显示  highlight

GET zhifou/doc/_search
{
  "query": {
    "match": {
      "name": "石头"
    }
  },
  "highlight": {
    "fields": {
      "name": {}
    }
  }
}

 

 

结果如下:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.5098256,
    "hits" : [
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "4",
        "_score" : 1.5098256,
        "_source" : {
          "name" : "石头",
          "age" : 29,
          "from" : "gu",
          "desc" : "粗中有细,狐假虎威",
          "tags" : [
            "",
            "",
            ""
          ]
        },
        "highlight" : {
          "name" : [
            "<em>石</em><em>头</em>"    #此处高亮显示
          ]
        }
      }
    ]
  }
}

 

 

自定义高亮显示

GET zhifou/chengyuan/_search
{
  "query": {
    "match": {
      "from": "gu"
    }
  },
  "highlight": {
    "pre_tags": "<b class=‘key‘ style=‘color:red‘>",
    "post_tags": "</b>",
    "fields": {
      "from": {}
    }
  }
}

需要注意的是:自定义标签中属性或样式中的逗号一律用英文状态的单引号表示,应该与外部elasticsearch语法的双引号区分开

 

elasticsearch之高亮查询

标签:esc   style   自定义   默认   逗号   desc   span   注意   index   

原文地址:https://www.cnblogs.com/s686zhou/p/12253164.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!