Elasticsearch进阶笔记第三十五篇

439 阅读5分钟

Elasticsearch高手进阶篇(74)

elasticsearch高手进阶_深入剖析搜索结果的highlight高亮显示

一个最基本的高亮例子

  • 设置mapping
 PUT /waws_website
 {
   "mappings": {
     "blogs": {
       "properties": {
         "title": {
           "type": "text",
           "analyzer": "ik_max_word"
         },
         "content": {
           "type": "text",
           "analyzer": "ik_max_word"
         }
       }
     }
   }
 }
  • 插入数据
 PUT /waws_website/blogs/1
 {
   "title": "我的第一篇博客",
   "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"
 }
  • 获取数据
 GET /waws_website/blogs/_search 
 {
   "query": {
     "match": {
       "title": "博客"
     }
   },
   "highlight": {
     "fields": {
       "title": {}
     }
   }
 }
 
 {
   "took": 36,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 1,
     "max_score": 0.28582606,
     "hits": [
       {
         "_index": "waws_website",
         "_type": "blogs",
         "_id": "1",
         "_score": 0.28582606,
         "_source": {
           "title": "我的第一篇博客",
           "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"
         },
         "highlight": {
           "title": [
             "我的第一篇<em>博客</em>"
           ]
         }
       }
     ]
   }
 }

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

 GET /waws_website/blogs/_search 
 {
   "query": {
     "bool": {
       "should": [
         {
           "match": {
             "title": "博客"
           }
         },
         {
           "match": {
             "content": "博客"
           }
         }
       ]
     }
   },
   "highlight": {
     "fields": {
       "title": {},
       "content": {}
     }
   }
 }
 
 {
   "took": 3,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 1,
     "max_score": 0.6642535,
     "hits": [
       {
         "_index": "waws_website",
         "_type": "blogs",
         "_id": "1",
         "_score": 0.6642535,
         "_source": {
           "title": "我的第一篇博客",
           "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"
         },
         "highlight": {
           "title": [
             "我的第一篇<em>博客</em>"
           ],
           "content": [
             "大家好,这是我写的第一篇<em>博客</em>,特别喜欢这个<em>博客</em>网站!!!"
           ]
         }
       }
     ]
   }
 }

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

三种highlight介绍

plain highlightlucene highlight默认

posting highlight,index_options=offsets

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

  • 设置mapping
 PUT /waws_website
 {
   "mappings": {
     "blogs": {
       "properties": {
         "title": {
           "type": "text",
           "analyzer": "ik_max_word"
         },
         "content": {
           "type": "text",
           "analyzer": "ik_max_word",
           "index_options": "offsets"
         }
       }
     }
   }
 }
  • 添加数据
 PUT /waws_website/blogs/1
 {
   "title": "我的第一篇博客",
   "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"
 }
  • 查询数据
 GET /waws_website/blogs/_search 
 {
   "query": {
     "match": {
       "content": "博客"
     }
   },
   "highlight": {
     "fields": {
       "content": {}
     }
   }
 }
 
 {
   "took": 33,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 1,
     "max_score": 0.37842745,
     "hits": [
       {
         "_index": "waws_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 /waws_website
 {
   "mappings": {
     "blogs": {
       "properties": {
         "title": {
           "type": "text",
           "analyzer": "ik_max_word"
         },
         "content": {
           "type": "text",
           "analyzer": "ik_max_word",
           "term_vector" : "with_positions_offsets"
         }
       }
     }
   }
 }

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

 GET /waws_website/blogs/_search 
 {
   "query": {
     "match": {
       "content": "博客"
     }
   },
   "highlight": {
     "fields": {
       "content": {
         "type": "plain"
       }
     }
   }
 }
 
 {
   "took": 4,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 1,
     "max_score": 0.37842745,
     "hits": [
       {
         "_index": "waws_website",
         "_type": "blogs",
         "_id": "1",
         "_score": 0.37842745,
         "_source": {
           "title": "我的第一篇博客",
           "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"
         },
         "highlight": {
           "content": [
             "大家好,这是我写的第一篇<em>博客</em>,特别喜欢这个<em>博客</em>网站!!!"
           ]
         }
       }
     ]
   }
 }

总结一下,其实可以根据你的实际情况去考虑,

  • 一般情况下,用plain highlight也就足够了,不需要做其他额外的设置
  • 如果对高亮的性能要求很高,可以尝试启用posting highlight
  • 如果field的值特别大,超过了1M,那么可以用fast vector highlight

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

 GET /waws_website/blogs/_search 
 {
   "query": {
     "match": {
       "content": "博客"
     }
   },
   "highlight": {
     "pre_tags": ["<tag1>"],
     "post_tags": ["</tag2>"], 
     "fields": {
       "content": {
         "type": "plain"
       }
     }
   }
 }
 
 {
   "took": 2,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 1,
     "max_score": 0.37842745,
     "hits": [
       {
         "_index": "waws_website",
         "_type": "blogs",
         "_id": "1",
         "_score": 0.37842745,
         "_source": {
           "title": "我的第一篇博客",
           "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"
         },
         "highlight": {
           "content": [
             "大家好,这是我写的第一篇<tag1>博客</tag2>,特别喜欢这个<tag1>博客</tag2>网站!!!"
           ]
         }
       }
     ]
   }
 }

高亮片段fragment的设置

  • 在插入一条文本
PUT /waws_website/blogs/2
{
  "title": "我的第一篇博客",
  "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"
}
  • 数据查询
 GET /waws_website/blogs/_search
 {
     "query" : {
         "match": { "content": "博客" }
     },
     "highlight" : {
         "fields" : {
             "content" : {"fragment_size" : 20, "number_of_fragments" : 3, "no_match_size": 150 }
         }
     }
 }
 
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.5732731,
    "hits": [
      {
        "_index": "waws_website",
        "_type": "blogs",
        "_id": "2",
        "_score": 0.5732731,
        "_source": {
          "title": "我的第一篇博客",
          "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"
        },
        "highlight": {
          "content": [
            "大家好,这是我写的第一篇<em>博客</em>,特别喜欢这个<em>博客</em>网站!!!",
            "的第一篇<em>博客</em>,特别喜欢这个<em>博客</em>网站!!!",
            "的第一篇<em>博客</em>,特别喜欢这个<em>博客</em>网站!!!"
          ]
        }
      },
      {
        "_index": "waws_website",
        "_type": "blogs",
        "_id": "1",
        "_score": 0.37842745,
        "_source": {
          "title": "我的第一篇博客",
          "content": "大家好,这是我写的第一篇博客,特别喜欢这个博客网站!!!"
        },
        "highlight": {
          "content": [
            "大家好,这是我写的第一篇<em>博客</em>,特别喜欢这个<em>博客</em>网站!!!"
          ]
        }
      }
    ]
  }
}
  • fragment_size: 你一个Field的值,比如有长度是1万,但是你不可能在页面上显示这么长啊,设置要显示出来的fragment文本判断的长度,默认是100
  • number_of_fragments:你可能你的高亮的fragment文本片段有多个片段,你可以指定就显示几个片段