【问题标题】:Elasticsearch returns broken HTML tags when I use Highlighting当我使用 Highlighting 时,Elasticsearch 返回损坏的 HTML 标签
【发布时间】:2016-12-30 10:42:53
【问题描述】:

我在 content 中有一个 HTML 字符串,例如:

"content": "<h3><a href=\"http://blog.local/page/%D8%A2%D8%B2%D8%A7%D8%AF\">The Matrix has you </a></h3>follow the white rabbit."

我使用"fragment_size" : 150 来控制高亮片段的字符大小,但它返回一个带有损坏的 HTML 标签的子字符串:

           "highlight": {
                "content": [
                    "&#x2F;%D8%A2%D8%B2%D8%A7%D8%AF&quot;&gt;The <em>Matrix</em> has"
                ]
            }

如何在基于 JSON 的查询 DSL 中修复它?

{
    "query": {
        "filtered": { 
            "query": {
                "multi_match": {
                    "query": "matrix",
                    "fields": ["title","content"]
                    }
            },
            "filter": {
                "term": { "content_type": "page" }
            }
        }
    },
    "highlight" : {
            "order" : "score",
        "fields" : {
            "content" : {"fragment_size" : 150, "number_of_fragments" : 3}
        }
    }
}

这是一个示例响应:

{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.98773545,
        "hits": [
            {
                "_index": "myindex",
                "_type": "post",
                "_id": "101",
                "_score": 0.024953224,
                "_source": {
                    "ID": 101,
                    "content_type": "page",
                    "date": "1999-02-18 14:32:21",
                    "title": "Wake up, Neo",
                    "content": "<h3><a href=\"http://blog.local/page/%D8%A2%D8%B2%D8%A7%D8%AF\">The Matrix has you </a></h3>follow the white rabbit."
                },
                "highlight": {
                    "content": [
                        "&#x2F;%D8%A2%D8%B2%D8%A7%D8%AF&quot;&gt;the <em>matrix</em> has"
                    ]
                }
            }
        ]
    }
}

【问题讨论】:

    标签: html elasticsearch tags highlight querydsl


    【解决方案1】:

    我没有尝试过,但我认为您应该将突出显示部分中的encoder指定为html

    {
        "query": {
            "filtered": { 
                "query": {
                    "multi_match": {
                        "query": "matrix",
                        "fields": ["title","content"]
                        }
                },
                "filter": {
                    "term": { "content_type": "page" }
                }
            }
        },
        "highlight" : {
            "order" : "score",
            "fields" : {
                "content" : {"fragment_size" : 150, "number_of_fragments" : 3}
            },
            "encoder": "html"
        }
    }
    

    见:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html

    【讨论】:

    • 将编码器设置为 HTML 只会使 HTML 元素成为纯文本,例如&amp;lt;p&amp;gt; 将变为 &amp;lt;p&amp;gt;
    猜你喜欢
    • 2013-07-10
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    相关资源
    最近更新 更多