大数据利器Elasticsearch搜索之高亮搜索

156 阅读3分钟

这是我参与8月更文挑战的第30天,活动详情查看:8月更文挑战
本Elasticsearch相关文章的版本为:7.4.2

在使用浏览器的搜索引擎中,命中匹配到的字符一般都会高亮显示出来。那么在Elasticsearch中也支持直接返回带高亮显示的html标签样式的内容,使得用户一眼看出搜索词命中在哪里。

高亮显示.png

首先我们准备点测试数据:

POST /highlight_test_index/_doc/1
{
  "title": "掘金Elasticsearch模糊查询",
  "content": "Elasticsearch模糊查询有前缀模糊查询、正则模糊查询等"
}

POST /highlight_test_index/_doc/2
{
  "title": "掘金Elasticsearch重建索引",
  "content": "Elasticsearch数据时候需要定义字段的设置进行调整,那么就需要进行重建索引"
}

那么我们就进行搜索掘金Elasticsearch为例使用highlight进行高亮显示:

GET /highlight_test_index/_search
{
    "query" : {
        "multi_match": {
            "type": "cross_fields",
            "fields": ["title", "content"],
            "query": "掘金Elasticsearch"
        }
    },
    "highlight": {
        "fields" : {
            "title": {},
            "content": {}
        }
    }
}

fields:指定在哪些字段高亮显示匹配的内容

返回的结果:

{
  "took" : 156,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.5639512,
    "hits" : [
      {
        "_index" : "highlight_test_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.5639512,
        "_source" : {
          "title" : "掘金Elasticsearch模糊查询",
          "content" : "Elasticsearch模糊查询有前缀模糊查询、正则模糊查询等"
        },
        "highlight" : {
          "title" : [
            "<em>掘</em><em>金</em><em>Elasticsearch</em>模糊查询"
          ],
          "content" : [
            "<em>Elasticsearch</em>模糊查询有前缀模糊查询、正则模糊查询等"
          ]
        }
      },
      {
        "_index" : "highlight_test_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.5469647,
        "_source" : {
          "title" : "掘金Elasticsearch重建索引",
          "content" : "Elasticsearch索引数据有时候需要定义字段的设置进行调整,那么就需要进行重建索引"
        },
        "highlight" : {
          "title" : [
            "<em>掘</em><em>金</em><em>Elasticsearch</em>重建索引"
          ],
          "content" : [
            "<em>Elasticsearch</em>索引数据有时候需要定义字段的设置进行调整,那么就需要进行重建索引"
          ]
        }
      }
    ]
  }
}

从返回的数据中可看到,匹配到的数据被加上了html的斜体标签<em></em>。那么展示出来的效果是:
文档1:

Elasticsearch模糊查询
Elasticsearch模糊查询有前缀模糊查询、正则模糊查询等


文档2:

Elasticsearch重建索引
Elasticsearch索引数据有时候需要定义字段的设置进行调整,那么就需要进行重建索引

但是只是斜体还是不是很明显,我们需要加上红色颜色进行突出显示,可以这样查询:

GET /highlight_test_index/_search
{
    "query" : {
        "multi_match": {
            "type": "cross_fields",
            "fields": ["title", "content"],
            "query": "掘金Elasticsearch"
        }
    },
    "highlight": {
        "fields" : {
            "title": {
              "pre_tags": ["<em style=\"color: red\">"],
              "post_tags": ["</em>"]
            },
            "content": {
              "pre_tags": ["<em style=\"color: red\">"],
              "post_tags": ["</em>"]
            }
        }
    }
}

pre_tags: 左标签
post_tags: 右标签

pre_tagspost_tags的列表相同下标共同构成一组标签对。

返回数据:

{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.5681902,
    "hits" : [
      {
        "_index" : "highlight_test_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.5681902,
        "_source" : {
          "title" : "掘金Elasticsearch模糊查询",
          "content" : "Elasticsearch模糊查询有前缀模糊查询、正则模糊查询等"
        },
        "highlight" : {
          "title" : [
            """<em style="color: red">掘</em><em style="color: red">金</em><em style="color: red">Elasticsearch</em>模糊查询"""
          ],
          "content" : [
            """<em style="color: red">Elasticsearch</em>模糊查询有前缀模糊查询、正则模糊查询等"""
          ]
        }
      },
      {
        "_index" : "highlight_test_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.5469647,
        "_source" : {
          "title" : "掘金Elasticsearch重建索引",
          "content" : "Elasticsearch索引数据有时候需要定义字段的设置进行调整,那么就需要进行重建索引"
        },
        "highlight" : {
          "title" : [
            """<em style="color: red">掘</em><em style="color: red">金</em><em style="color: red">Elasticsearch</em>重建索引"""
          ],
          "content" : [
            """<em style="color: red">Elasticsearch</em>索引数据有时候需要定义字段的设置进行调整,那么就需要进行重建索引"""
          ]
        }
      }
    ]
  }
}

把数据展示在html上的效果就是:

高亮.png