ElasticSearch实战笔记

37、highlight 高亮显示

2020-04-29  本文已影响0人  众神开挂

主要内容:highlight 高亮显示

1、一个最基本的高亮例子
PUT /blog_website
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "content": {
        "type": "text",
        "analyzer": "ik_max_word"
      }
    }
  }
}
PUT /blog_website/_doc/1
{
  "title": "我的第一篇博客",
  "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"
}

​ 使用高亮搜索:

GET /blog_website/_search 
{
  "query": {
    "match": {
      "content": "博客"
    }
  },
  "highlight": {
    "fields": {
      "content": {}
    }
  }
}

<em></em>表现,会变成红色,所以说你的指定的field中,如果包含了那个搜索词的话,就会在那个field的文本中,对搜索词进行红色的高亮显示

GET /blog_website/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "博客"
          }
        },
        {
          "match": {
            "content": "博客"
          }
        }
      ]
    }
  },
  "highlight": {
    "fields": {
      "title": {},  ## highlight中的field,必须跟query中的field一一对齐
      "content": {}
    }
  }
}
2、三种highlight介绍(plain highlight,lucene highlight,posting highlight)
2.1、posting highlight

设置:index_options=offsets

(1)性能比plain highlight要高,因为不需要重新对高亮文本进行分词
(2)对磁盘的消耗更少
(3)将文本切割为句子,并且对句子进行高亮,效果更好

## 重新建立索引
PUT /blog_website
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "content": {
        "type": "text",
        "analyzer": "ik_max_word",
        "index_options": "offsets"
      }
    }
  }
}
PUT /blog_website/_doc/1
{
  "title": "我的第一篇博客",
  "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"
}

搜索

GET /blog_website/_search 
{
  "query": {
    "match": {
      "content": "博客"
    }
  },
  "highlight": {
    "fields": {
      "content": {}
    }
  }
}
2.2、fast vector highlight

term_vector设置在mapping中,就会用fast verctor highlight

对大field而言(大于1mb),性能更高

PUT /blog_website
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "content": {
        "type": "text",
        "analyzer": "ik_max_word",
        "term_vector": "with_positions_offsets"
      }
    }
  }
}

PUT /blog_website/_doc/1
{
  "title": "我的第一篇博客",
  "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"
}

其实可以根据你的实际情况去考虑,一般情况下不需要做其他额外的设置
如果对高亮的性能要求很高,可以尝试启用posting highlight
如果field的值特别大,超过了1M,那么可以用fast vector highlight

3、设置高亮html标签,默认是<em>标签

GET /blog_website/_search 
{
  "query": {
    "match": {
      "content": "博客"
    }
  },
  "highlight": {
    "pre_tags": ["<h1>"],  ##设置为<h1></h1>标签
    "post_tags": ["</h1>"], 
    "fields": {
      "content": {
        "type": "plain"
      }
    }
  }
}

4、高亮片段fragment的设置

GET blog_website/_search
{
  "query": {
    "match": {
      "content": "博客"
    }
  },
  "highlight": {
    "fields": {
      "content": {
        "fragment_size": 150,
        "number_of_fragments": 3,
        "no_match_size": 150
      }
    }
  }
}

fragment_size: Field长度,默认是100
number_of_fragments:你可能你的高亮的fragment文本片段有多个片段,你可以指定就显示几个片段

补充: 强制使用某种highlighter

比如对于开启了term vector的field而言,可以强制使用plain highlight

GET /blog_website/_search 
{
  "query": {
    "match": {
      "content": "博客"
    }
  },
  "highlight": {
    "fields": {
      "content": {
        "type": "plain"
      }
    }
  }
}
上一篇下一篇

猜你喜欢

热点阅读