elasticsearch - Elasticsearch : Highlight wildcrad search result

标签 elasticsearch wildcard highlight elasticsearch-highlight

我使用 elastic search 7.10,喜欢通过通配符搜索分析字段来查找文档,并在文本中突出显示这些文档。但这不起作用。

文档可以包含以下示例:“The color of the car is black.” 我希望得到 carblack 被标记的结果。

我有以下映射:

 "text": {
            "type": "text",
            "store": true,
            "term_vector": "with_positions_offsets",
            "analyzer": "my_analyzer",
            "fields": {
                "keyword": {
                    "type": "keyword",
                    "ignore_above": 8000
                },
                "wc" :{
                    "type": "wildcard"
                }
            }
        },

我使用以下查询:

{
    "query": {
        "bool": {
            "should": [ 
                {
                   "match": {"text": "car"}
                },
                {
                   "wildcard": { "text.wc": { "value": "bl*" } }
                }
            ]
        }
    },
    "fields": ["text", "text.wc"],
    "highlight": {
        "pre_tags": [
            "<span class='marker'>"
        ],
        "post_tags": [
            "</span>"
        ],
        "type": "fvh",
        "fields": {
            "*": {
                "pre_tags": [
                    "<em>"
                ],
                "post_tags": [
                    "</em>"
                ]
            }
        },
        "require_field_match": true
    }
}

查询结果集仅包含 text - 字段的高亮显示,但不包含 text.wc 字段的高亮显示。我还尝试了一个单独的通配符字段,它不是 text 的子字段,但这也不起作用。我还注意到,_source- 需要将字段设置为 enabled,即使所有字段都设置为存储,否则我会收到 Unable to retrieve the requested [字段] 消息。

问题:如何为 wildcrad 搜索词获取突出显示的文本?

最佳答案

我找到了解决方案,并愿意亲自回答我的问题,以防有人遇到同样的问题。

答案是,通配符、突出显示和文本分析(如词干提取)不适用于 matchwildcard - 像上面这个查询一样。

但是:您可以使用 query_string 而不是 matchwildcard。这是弹性查询 DSL 的一部分,但不幸的是它没有在此处列出:https://www.elastic.co/guide/en/elasticsearch/reference/7.16/query-dsl.html

在我看来,这个非常重要的功能位于此处更深的 2 个基本点击/级别:https://www.elastic.co/guide/en/elasticsearch/reference/7.16/query-dsl-query-string-query.html

query_string 允许您在分析字段上执行所有搜索和突出显示操作,就像来自 solr 的人在查询中所做的那样。

一个例子看起来像这样:

"query": {
    "bool": {
        "should": [
            {
                "query_string": {
                    "fields": [
                        "text"
                    ],
                    "query": "car and bl*"
                }
            ]
        }
    }
    "highlight": {
        "pre_tags": [
            "<span class='marker'>"
        ],
        "post_tags": [
            "</span>"
        ],
        "type": "fvh",
        "fields": {
            "*": {
                "pre_tags": [
                    "<em>"
                ],
                "post_tags": [
                    "</em>"
                ]
            }
        },
        "require_field_match": true
    }
}

关于elasticsearch - Elasticsearch : Highlight wildcrad search result,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70835954/

相关文章:

elasticsearch - 如何在 `Elasticsearch`上创建元数据?

bash - 没有匹配文件时如何跳过for循环?

Elasticsearch:查询差异中的通配符: "Alex*"与 "*lex*"

javascript - 突出显示输入字段 html 中的单词

Excel 突出显示匹配日期的单元格

elasticsearch - 如何在Logstash中将Groovy脚本与参数一起使用

Elasticsearch 作为 GCP 的服务

elasticsearch - 巢状更新索引设定

c++ - Windows 中通配符搜索中的问号

javascript - 在 html 文档中突出显示不同字符串的最快方法是什么?