进阶-第73__elasticsearch高手进阶_深入剖析搜索结果的highlight高亮显示

时间:2019-03-19
本文章向大家介绍进阶-第73__elasticsearch高手进阶_深入剖析搜索结果的highlight高亮显示,主要包括进阶-第73__elasticsearch高手进阶_深入剖析搜索结果的highlight高亮显示使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

 

1、一个最基本的高亮例子

创建index的mappings

PUT /blog_website

{

  "mappings": {

    "blogs": {

      "properties": {

        "title": {

          "type": "text",

          "analyzer": "ik_max_word"

        },

        "content": {

          "type": "text",

          "analyzer": "ik_max_word"

        }

      }

    }

  }

}

 

看一下分词效果

GET /_analyze

{

  "text":"我发表的第一篇博课",

  "analyzer":"ik_max_word"

}

结果:

{

  "tokens": [

    {

      "token": "我",

      "start_offset": 0,

      "end_offset": 1,

      "type": "CN_CHAR",

      "position": 0

    },

    {

      "token": "发表",

      "start_offset": 1,

      "end_offset": 3,

      "type": "CN_WORD",

      "position": 1

    },

    {

      "token": "发",

      "start_offset": 1,

      "end_offset": 2,

      "type": "CN_WORD",

      "position": 2

    },

    {

      "token": "表",

      "start_offset": 2,

      "end_offset": 3,

      "type": "CN_WORD",

      "position": 3

    },

    {

      "token": "第一篇",

      "start_offset": 4,

      "end_offset": 7,

      "type": "CN_WORD",

      "position": 4

    },

    {

      "token": "第一",

      "start_offset": 4,

      "end_offset": 6,

      "type": "CN_WORD",

      "position": 5

    },

    {

      "token": "一篇",

      "start_offset": 5,

      "end_offset": 7,

      "type": "CN_WORD",

      "position": 6

    },

    {

      "token": "一",

      "start_offset": 5,

      "end_offset": 6,

      "type": "TYPE_CNUM",

      "position": 7

    },

    {

      "token": "篇",

      "start_offset": 6,

      "end_offset": 7,

      "type": "COUNT",

      "position": 8

    },

    {

      "token": "博",

      "start_offset": 7,

      "end_offset": 8,

      "type": "CN_CHAR",

      "position": 9

    },

    {

      "token": "课",

      "start_offset": 8,

      "end_offset": 9,

      "type": "CN_CHAR",

      "position": 10

    }

  ]

}

添加测试数据

PUT /blog_website/blogs/1

{

  "title": "我的第一篇博客",

  "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"

}

结果:

{

  "_index": "blog_website",

  "_type": "blogs",

  "_id": "1",

  "_version": 1,

  "result": "created",

  "_shards": {

    "total": 2,

    "successful": 1,

    "failed": 0

  },

  "created": true

}

 

高亮显示测试

GET /blog_website/blogs/_search

{

  "query": {

    "match": {

      "title": "博客"

    }

  },

  "highlight": {

    "fields": {

      "title": {}//意思高亮的就在我们上面那个title里面,如果指定的field中包含了搜索词的话,那么就会对该搜索词高亮显示;注意你这里指定的fields 必须和上面查询的一一对应的,否则没有效果

    }

  }

}

结果:

{

  "took": 103,

  "timed_out": false,

  "_shards": {

    "total": 5,

    "successful": 5,

    "failed": 0

  },

  "hits": {

    "total": 1,

    "max_score": 0.28582606,

    "hits": [

      {

        "_index": "blog_website",

        "_type": "blogs",

        "_id": "1",

        "_score": 0.28582606,

        "_source": {

          "title": "我的第一篇博客",

          "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"

        },

        "highlight": {

          "title": [

            "我的第一篇<em>博客</em>"

          ]

        }

      }

    ]

  }

}

 

 

 

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

GET /blog_website/blogs/_search

{

  "query": {

    "bool": {

      "should": [

        {

          "match": {

            "title": "博客"

          }

        },

        {

          "match": {

            "content": "博客"

          }

        }

      ]

    }

  },

  "highlight": {

    "fields": {

      "title": {},

      "content": {}

    }

  }

}

结果:

{

  "took": 8,

  "timed_out": false,

  "_shards": {

    "total": 5,

    "successful": 5,

    "failed": 0

  },

  "hits": {

    "total": 1,

    "max_score": 0.6642535,

    "hits": [

      {

        "_index": "blog_website",

        "_type": "blogs",

        "_id": "1",

        "_score": 0.6642535,

        "_source": {

          "title": "我的第一篇博客",

          "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"

        },

        "highlight": {

          "title": [

            "我的第一篇<em>博客</em>"

          ],

          "content": [

            "大家好,这是我写的第一篇<em>博客</em>,特别喜欢这个<em>博客</em>网站!!!"

          ]

        }

      }

    ]

  }

}

 

 

highlight中的field,必须跟query中的field一一对齐的

 

2、三种highlight介绍

plain highlight

plain highlight:     lucene highlight,默认

posting highlight

posting highlight:   index_options=offsets

 

(1)性能比plain highlight要高,因为不需要重新对高亮文本进行分词

(2)对磁盘的消耗更少

(3)将文本切割为句子,并且对句子进行高亮,效果更好

PUT /blog_website

{

  "mappings": {

    "blogs": {

      "properties": {

        "title": {

          "type": "text",

          "analyzer": "ik_max_word"

        },

        "content": {

          "type": "text",

          "analyzer": "ik_max_word",

          "index_options": "offsets"

        }

      }

    }

  }

}

结果:

{

  "acknowledged": true,

  "shards_acknowledged": true

}

添加测试数据

PUT /blog_website/blogs/1

{

  "title": "我的第一篇博客",

  "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"

}

查询

GET /blog_website/blogs/_search

{

  "query": {

    "match": {

      "content": "博客"

    }

  },

  "highlight": {

    "fields": {

      "content": {}

    }

  }

}

结果:

{

  "took": 19,

  "timed_out": false,

  "_shards": {

    "total": 5,

    "successful": 5,

    "failed": 0

  },

  "hits": {

    "total": 1,

    "max_score": 0.37842745,

    "hits": [

      {

        "_index": "blog_website",

        "_type": "blogs",

        "_id": "1",

        "_score": 0.37842745,

        "_source": {

          "title": "我的第一篇博客",

          "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"

        },

        "highlight": {

          "content": [

            "大家好,这是我写的第一篇<em>博客</em>,特别喜欢这个<em>博客</em>网站!!!"

          ]

        }

      }

    ]

  }

}

 

fast vector highlight

index-time term vector设置在mapping中,就会用fast verctor highlight

 

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

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"

        }

      }

    }

  }

}

 

 

添加测试数据

PUT /blog_website/blogs/1

{

  "title": "我的第一篇博客",

  "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"

}

 

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

GET /blog_website/blogs/_search

{

  "query": {

    "match": {

      "content": "博客"

    }

  },

  "highlight": {

    "fields": {

      "content": {

        "type": "plain"

      }

    }

  }

}

 

 

 

总结一下,其实可以根据你的实际情况去考虑,一般情况下,用plain highlight也就足够了,不需要做其他额外的设置

如果对高亮的性能要求很高,可以尝试启用posting highlight

如果field的值特别大,超过了1M,那么可以用fast vector highlight

 

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

GET /blog_website/blogs/_search

{

  "query": {

    "match": {

      "content": "博客"

    }

  },

  "highlight": {

    "pre_tags": ["<tag1>"],

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

    "fields": {

      "content": {

        "type": "plain"

      }

    }

  }

}

结果:

{

  "took": 6,

  "timed_out": false,

  "_shards": {

    "total": 5,

    "successful": 5,

    "failed": 0

  },

  "hits": {

    "total": 1,

    "max_score": 0.37842745,

    "hits": [

      {

        "_index": "blog_website",

        "_type": "blogs",

        "_id": "1",

        "_score": 0.37842745,

        "_source": {

          "title": "我的第一篇博客",

          "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"

        },

        "highlight": {

          "content": [

            "大家好,这是我写的第一篇<tag1>博客</tag1>,特别喜欢这个<tag1>博客</tag1>网站!!!"

          ]

        }

      }

    ]

  }

}

 

 

4、高亮片段fragment的设置

GET /_search

{

    "query" : {

        "match": { "content": "博客" }

    },

    "highlight" : {

        "fields" : {

            "content" : {"fragment_size" : 30, "number_of_fragments" : 3, "no_match_size": 150 }

        }

    }

}

结果:

{

  "took": 6,

  "timed_out": false,

  "_shards": {

    "total": 50,

    "successful": 50,

    "failed": 0

  },

  "hits": {

    "total": 2,

    "max_score": 0.5930286,

    "hits": [

      {

        "_index": "blog_website",

        "_type": "blogs",

        "_id": "2",

        "_score": 0.5930286,

        "_source": {

          "title": "我的第一篇博客",

          "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"

        },

        "highlight": {

          "content": [

            "大家好,这是我写的第一篇<em>博客</em>,特别喜欢这个<em>博客</em>网站!!!大家好,这",

            "是我写的第一篇<em>博客</em>,特别喜欢这个<em>博客</em>网站!!!大家好,这是我",

            "写的第一篇<em>博客</em>,特别喜欢这个<em>博客</em>网站!!!大家好,这是我写的"

          ]

        }

      },

      {

        "_index": "blog_website",

        "_type": "blogs",

        "_id": "1",

        "_score": 0.37842745,

        "_source": {

          "title": "我的第一篇博客",

          "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"

        },

        "highlight": {

          "content": [

            "大家好,这是我写的第一篇<em>博客</em>,特别喜欢这个<em>博客</em>网站!!!"

          ]

        }

      }

    ]

  }

}

 

 

fragment_size: 你一个Field的值,比如有长度是1万,但是你不可能在页面上显示这么长啊。。。设置要显示出来的fragment文本判断的长度,默认是100,会按照你设置的fragment_size 的长度进行 ,  分割

number_of_fragments:你可能你的高亮的fragment文本片段有多个片段,你可以指定就显示几个片段(和fragment_size结合使用)

no_match_size:如果说,对于那些你的query没有匹配到的doc,你的高亮可以显示前缀多少字符的文本