分析器是否会阻止字段突出显示?

Does analyzer prevent fields from highlighting?

你能帮我解决有关特定语言分析器和 elasticsearch 中高亮显示的小问题吗?

我需要通过查询字符串搜索文档并突出显示匹配的字符串。 这是我的映射:

{
    "usr": {
        "properties": {
            "text0": {
                "type": "string",
                "analyzer": "english"
            },
            "text1": {
                "type": "string"
            }
        }
    }
}

请注意,"text0" 字段设置了 "english" 分析器,"text1" 字段默认使用标准分析器。

我的索引中目前只有一个文档:

hits": [{
    "_index": "tt",
    "_type": "usr",
    "_id": "AUxvIPAv84ayQMZV-3Ll",
    "_score": 1,
    "_source": {
        "text0": "highlighted. need to be highlighted.",
        "text1": "highlighted. need to be highlighted."
    }
}]

考虑以下查询:

{
    "query": {
        "query_string" : {
            "query" : "highlighted"
        }
    },
    "highlight" : {
        "fields" : {
            "*" : {}
        }
    }
}

我希望文档中的每个字段都被突出显示,但突出显示仅出现在 "text1" 字段(未设置分析器):

"hits": [{
    "_type": "usr", 
    "_source": {
        "text0": "highlighted. need to be highlighted.", 
        "text1": "highlighted. need to be highlighted."
    }, 
    "_score": 0.19178301, 
    "_index": "tt", 
    "highlight": {
        "text1": [
            "<em>highlighted</em>. need to be <em>highlighted</em>."
        ]
    }, 
    "_id": "AUxvIPAv84ayQMZV-3Ll"
}]

让我们考虑以下查询(由于分析器,我预计 "highlighted" 匹配 "highlight"):

{
    "query": {
        "query_string" : {
                "query" : "highlight"
            }
        },
    "highlight" : {
             "fields" : {
                 "*" : {}
             }
        }
}

但是根本没有响应:(英语分析器在这里工作吗?)

"hits": {
    "hits": [], 
    "total": 0, 
    "max_score": null
}

最后,考虑一些 curl 命令(请求和响应):

curl "http://localhost:9200/tt/_analyze?field=text0" -d "highlighted"

{"tokens":[{ 
    "token":"highlight",
    "start_offset":0,
    "end_offset":11,
    "type":"<ALPHANUM>",
    "position":1
}]}

curl "http://localhost:9200/tt/_analyze?field=text1" -d "highlighted" 

{"tokens":[{
    "token":"highlighted",
    "start_offset":0,
    "end_offset":11,
    "type":"<ALPHANUM>",
    "position":1
}]}

我们看到,通过英语和标准分析器传递文本,结果是不同的。 最后,问题:分析器是否会阻止字段突出显示?如何在全文搜索时突出显示我的字段?

P.S。我在本地机器上使用 elasticsearch v1.4.4 windows 8.1.

这与您的查询有关。您正在使用 query_string 查询并且没有指定字段,因此它默认在 _all 字段上搜索。 这就是为什么您会看到奇怪的结果。将您的查询更改为 multi_match 搜索两个字段的查询:

{
    "query": {
        "multi_match": {
            "fields": [
                "text1",
                "text0"
            ],
            "query": "highlighted"
        }
    },
    "highlight": {
        "fields": {
            "*": {}
        }
    }
} 

现在,响应中将返回两个字段的突出显示结果。