elasticsearch - 如何在 Elasticsearch v5 中突出显示嵌套对象命中

标签 elasticsearch

我试图让嵌套查询的命中返回突出显示。根据文档,这是可能的。文档说:

The parent/child and nested features allow the return of documents that have matches in a different scope. In the nested case, documents are returned based on matches in nested inner objects. The inner hits feature returns per search hit in the search response additional nested hits that caused a search hit to match in a different scope. Inner hits can be used by defining an inner_hits definition on a nested, has_child or has_parent query and filter. Inner hits also supports the following per document features:

'Highlighting', 'Explain Source', 'filtering', 'Script fields', 'Doc value', 'fields', 'Include versions'.

https://www.elastic.co/guide/en/elasticsearch/reference/5.0/search-request-inner-hits.html#nested-inner-hits

但是,我无法根据以下映射和查询使它正常工作。谁能解释我哪里出错了?

注意:我特指的是嵌套类型,而不是 parent_child。

我发现它仅在将 referenceIdValue 和 ReferenceIdType 从关键字更改为文本时才有效,但显然这不是我想要的。 ES 文档说:

"The field name supports wildcard notation. For example, using comment_* will cause all text and keyword fields (and string from versions before 5.0) that match the expression to be highlighted. Note that all other fields will not be highlighted." https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html#_highlight_query I'm thinking this may be a bug?

非常感谢。

映射:

{
"entity": {
    "dynamic_templates": [{
        "catch_all": {
            "match_mapping_type": "*",
            "mapping": {
                "type": "text",
                "store": true,
                "analyzer": "phonetic_index",
                "search_analyzer": "phonetic_query"
            }
        }
    }],
    "_all": {
        "enabled": false
    },
    "properties": {
        "entityName": {
            "type": "text",
            "store": true,
            "analyzer": "indexed_index",
            "search_analyzer": "indexed_query",
        "referenceIds": {
            "type": "nested",
            "properties": {
                "referenceIdValue": {
                    "type": "keyword",
                    "norms": true,
                    "store": true
                },
                "referenceIdType": {
                    "type": "keyword",
                    "store": true,
                    "norms": true
                }
            }
        },
        "isPublished": {
            "type": "boolean",
            "store": true
        }
    }
}
}

查询:

curl -XGET "127.0.0.1:9200/test/entity/_search?pretty" -d"
{
"query": {
    "bool": {
        "filter": {
            "term": {
                "isPublished": "true"
            }
        },
        "must": [
            [{
                "nested": {
                    "query": {
                        "bool": {
                            "must": [
                                [{
                                    "term": {
                                        "referenceIds.referenceIdValue": "123456"
                                    }
                                }, {
                                    "term": {
                                        "referenceIds.referenceIdType": "ENCO"
                                    }
                                }]
                            ]
                        }
                    },
                    "inner_hits": {
                        "highlight": {
                            "fields": {
                                "referenceIds.referenceIdValue": {},
                                "referenceIds.referenceIdType": {}
                            }
                        }
                    },
                    "path": "referenceIds"
                }
            }]
        ]
    }
}
}"

结果:

{
"took": 99,
"timed_out": false,
"_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
},
"hits": {
    "total": 1,
    "max_score": 14.319202,
    "hits": [{
        "_index": "test",
        "_type": "entity",
        "_id": "3423631",
        "_score": 14.319202,
        "_source": {
            "entityName": "Test Entity",
            "referenceIds": [{
                "referenceIdValue": "AAAABBBB",
                "referenceIdType": "SULA"
            }, {
                "referenceIdValue": "123456",
                "referenceIdType": "ENCO"
            }]
        },
        "inner_hits": {
            "referenceIds": {
                "hits": {
                    "total": 1,
                    "max_score": 14.319202,
                    "hits": [{
                        "_nested": {
                            "field": "referenceIds",
                            "offset": 3
                        },
                        "_score": 14.319202,
                        "_source": {
                            "referenceIdValue": "123456",
                            "referenceIdType": "ENCO"
                        }
                    }]
                }
            }
        }
    }]
}
}

最佳答案

我认为这是因为在查询的突出显示部分,您需要设置相对于嵌套文档的路径。 试试这个:

                "inner_hits": {
                    "highlight": {
                        "fields": {
                            "referenceIdValue": {},
                            "referenceIdType": {}
                        }
                    }
                },

关于elasticsearch - 如何在 Elasticsearch v5 中突出显示嵌套对象命中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41717963/

相关文章:

elasticsearch - Elasticsearch重新索引-unknown_host_exception

elasticsearch - Elastic Search 7.6.2中的随机文档-弃用警告消息

java - 在 lucene 索引中存储和检索 Json 对象

mysql - Elasticsearch + 还有什么?

java - elasticsearch java查询匹配我的列表中的任何一个

elasticsearch - Elasticsearch在热节点和热节点上的搜索查询

amazon-web-services - 我如何从AWS控制台了解Elasticsearch存储使用情况

php - ElasticSearch 查询卡在 laravel 作业队列中

python - 如何创建嵌套字典,以便通过Python为Elasticsearch创建映射?

elasticsearch - 如何在Elasticsearch中将某些单词组合成 token ?