• 【elasticsearch】查询结果 高亮显示 ——【转载】


    总的来说:三种高亮显示
    一、plain highlight(默认)
    二、posting highlight(性能)
       对磁盘的消耗更少
       将文本切割为句子,并且对句子进行高亮,效果更好
       性能比plain highlight高,因为不需要重新对高亮文本进行分词
    三、fast vector highlight(文件)
       对大文件而言(大于1M),性能更高

    代码时间
    默认创建索引时添加分词

    复制代码
    PUT /blog_website
    {
      "mappings": {
        "blogs": {
          "properties": {
            "title": {
              "type": "text",
              "analyzer": "ik_max_word"
            },
            "content": {
              "type": "text",
              "analyzer": "ik_max_word"
            }
          }
        }
      }
    }
    复制代码

    查询

    如果包含了那个搜索词的话,就会在那个field的文本中,对搜索词进行红色的高亮显示

    posting highlight

    创建索引时在要搜索的字段添加”index_options”: “offsets”

    复制代码
    PUT /blog_website
    {
      "mappings": {
        "blogs": {
          "properties": {
            "title": {
              "type": "text",
              "analyzer": "ik_max_word"
            },
            "content": {
              "type": "text",
              "analyzer": "ik_max_word",
              "index_options": "offsets"
            }
          }
        }
      }
    }
    复制代码

    fast vector highlight

    建索引时term vector设置在mapping中

    复制代码
    PUT /blog_website
    {
      "mappings": {
        "blogs": {
          "properties": {
            "title": {
              "type": "text",
              "analyzer": "ik_max_word"
            },
            "content": {
              "type": "text",
              "analyzer": "ik_max_word",
              "term_vector" : "with_positions_offsets"
            }
          }
        }
      }
    }
    复制代码

    自定义:

    强制使用某种高亮:指定type

    复制代码
    GET /blog_website/blogs/_search 
    {
      "query": {
        "match": {
          "content": "博客"
        }
      },
      "highlight": {
        "fields": {
          "content": {
            "type": "plain"//强制使用某种highlighter
          }
        }
      }
    }
    复制代码

    设置高亮标签,取代<em>

     "pre_tags": ["<tag1>"],
      "post_tags": ["</tag2>"],

    如下:

    复制代码
    GET /blog_website/blogs/_search 
    {
      "query": {
        "match": {
          "content": "博客"
        }
      },
      "highlight": {
        "pre_tags": ["<tag1>"],
        "post_tags": ["</tag2>"], 
        "fields": {
          "content": {
            "type": "plain"
          }
        }
      }
    }
    复制代码

    设置文本片段:长度、个数

    复制代码
    GET /_search
    {
        "query" : {
            "match": { "user": "kimchy" }
        },
        "highlight" : {
            "fields" : {
                "content" : {"fragment_size" : 150, "number_of_fragments" : 3, "no_match_size": 150 }
            }
        }
    }
    
    fragment_size: 将要显示的文本拆成*长的一段来进行显示
        (设置要显示出来的文本片段的长度,默认是100)
    
    number_of_fragments:可能高亮的文本片段有多个片段,指定显示几个片段
    复制代码


    ————————————————
    版权声明:本文为CSDN博主「星小丫头辰」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/ma15732625261/article/details/79722791

  • 相关阅读:
    【转】ANSI与GB2312的编码问题
    asp.net相关文件后缀名都是什么意思?
    【转】HTTP 通信http通信过程简介
    【笔记】【转载】设计模式
    【转载】DataGridView之将数据导出成Excel和Word格式
    新开博客
    ASP.NET 从Excel文件导入数据到数据库
    Asp.net 图片异步上传的简单实现
    【笔记】【转载】JohnConnor设计模式笔记(一) 学习设计模式之前你必须掌握的-看懂UML类图
    boost Smart Pointer
  • 原文地址:https://www.cnblogs.com/kelelipeng/p/12322406.html
Copyright © 2020-2023  润新知