阅读 34

es - elasticsearch search - highlight 高级

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问 :hight有什么特点?
答 :
在这里插入图片描述

问 :highlight如何使用?
答 :

# highlight
PUT /highlight_2_test
{
  "mappings" : {
    "dynamic"    : "strict",
    "properties" : {
      "head" : {
        "type" : "text"
      },
      "article" : {
        "properties" : {
          "name" : {
            "type"   : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword"
              }
            }
          },
          "content" : {
            "type" : "text"
          }
        }
      }
    }
  }
}

# 索引
POST /highlight_2_test/_doc/1
{
  "head" : "hello good",
  "article" : {
    "name"    : "hello",
    "content" : "hello hello good good me me"
  }
}

# 索引
POST /highlight_2_test/_doc/2
{
  "head" : "hello me",
  "article" : {
    "name"    : "hello",
    "content" : "hello hello me me me me"
  }
}

# 索引
POST /highlight_2_test/_doc/3
{
  "head" : "hello test",
  "article" : {
    "name"    : "hello",
    "content" : "hello hello test test me me"
  }
}

# 搜索
GET /highlight_2_test/_search
{
  "query" : {
    "multi_match": {
      "query"  : "hello",
      "fields" : [
        "head", "article.name"  
      ]
    }
  },
  "rescore" : {
    "query"       : {
      "rescore_query" : {
        "match_phrase" : {
          "article.content" : {
            "query" : "hello good",
            "slop"  : 1
          }
        }
      },
      "rescore_query_weight" : 10
    },
    "window_size" : 50
  }, 
  "highlight": {
    "type"                : "unified",
    "number_of_fragments" : 2,
    "fragment_size"       : 10,
    "tags_schema"         : "styled",
    "force_source"        : "true",
    "require_field_match" : "true",
    "no_match_size"       : 10, 
    "fields"              : [
      {
        "head" : {
          "pre_tags"  : ["<span>"],
          "post_tags" : ["</span>"]
        }
      },
      {
        "article.name" : {
          "number_of_fragments" : 0
        }
      },
      {
        "article.content" : {
          "number_of_fragments" : 5,
          "order"               : "score"
        }
      }
    ],
    "highlight_query" : {
      "bool" : {
        "must" : [
          {
            "multi_match" : {
              "query"  : "hello",
              "fields" : [
                "head", "article.name"
              ]
            }
          }
        ],
        "should" : [
          {
            "match" : {
              "article.content" : {
                "query" : "hello good",
                "boost" : 10
              }
            }
          }
        ]
      }
    }
  }
}

# 结果
{
  "took" : 14,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 11.277138,
    "hits" : [
      {
        "_index" : "highlight_2_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 11.277138,
        "_source" : {
          "head" : "hello good",
          "article" : {
            "name" : "hello",
            "content" : "hello hello good good me me"
          }
        },
        "highlight" : {
          "head" : [
            "<span>hello</span> good"
          ],
          "article.name" : [
            """<em class="hlt1">hello</em>"""
          ],
          "article.content" : [
            """<em class="hlt1">hello</em> <em class="hlt1">hello</em>""",
            """<em class="hlt1">good</em> <em class="hlt1">good</em>"""
          ]
        }
      },
      {
        "_index" : "highlight_2_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.13353139,
        "_source" : {
          "head" : "hello me",
          "article" : {
            "name" : "hello",
            "content" : "hello hello me me me me"
          }
        },
        "highlight" : {
          "head" : [
            "<span>hello</span> me"
          ],
          "article.name" : [
            """<em class="hlt1">hello</em>"""
          ],
          "article.content" : [
            """<em class="hlt1">hello</em> <em class="hlt1">hello</em>"""
          ]
        }
      },
      {
        "_index" : "highlight_2_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.13353139,
        "_source" : {
          "head" : "hello test",
          "article" : {
            "name" : "hello",
            "content" : "hello hello test test me me"
          }
        },
        "highlight" : {
          "head" : [
            "<span>hello</span> test"
          ],
          "article.name" : [
            """<em class="hlt1">hello</em>"""
          ],
          "article.content" : [
            """<em class="hlt1">hello</em> <em class="hlt1">hello</em>"""
          ]
        }
      }
    ]
  }
}
复制代码
文章分类
后端
文章标签