弹性搜索 - 多值字段内的匹配值
Elastic Search - Matching value inside multi-valued field
我有一个 ElasticSearch 索引,其中的文档如下所示:
{
"labels": ["Common label for doc 1", "Other possible label"],
"year": 1923,
"boolProp": true
},
{
"labels": ["Only one label here"],
"year": 1812,
"boolProp": true
},
...
当我查询 labels
字段时,我想检索最好的文档以及匹配的标签。
我读到这个字段实际上被索引为一个聚合字符串...
对于这种查询,我是否必须将我的 labels
字段转换为嵌套对象?我想知道我是否缺少一种更直接的方法...
一种方法是使用 Highlighting.
这是一个相当丰富的功能,但以下示例可能会帮助您实现目标。
{
"query": {
"match": {
"myfield": "another"
}
},
"highlight": {
"fields": {
"myfield": {
"type": "plain"
}
},
"pre_tags": [""],
"post_tags": [""]
}
}
您可以选择保持匹配的文本突出显示,或指定空白 pre_tags
和 post_tags
以仅显示原始文本。
响应中的 highlight
字段将仅包含原始源数组中匹配的命中。
{
...
"hits": {
"total": 1,
"max_score": 0.28582606,
"hits": [
{
"_index": "test",
"_type": "mytype",
"_id": "AWB6-u6V3-7fA7oZt-aX",
"_score": 0.28582606,
"_source": {
"myfield": [
"My favorite toy",
"Another toy for me"
]
},
"highlight": {
"myfield": [
"Another toy for me"
]
}
}
]
}
}
如果数组中有多个值匹配,则全部返回。
{
...
"hits": {
"total": 1,
"max_score": 0.3938048,
"hits": [
{
"_index": "blah",
"_type": "mytype",
"_id": "AWB6-u6V3-7fA7oZt-aX",
"_score": 0.3938048,
"_source": {
"myfield": [
"My favorite toy",
"Another toy for me"
]
},
"highlight": {
"myfield": [
"My favorite toy",
"Another toy for me"
]
}
}
]
}
}
正如您提到的,当然还有其他选择,使用嵌套文档或父子关系并从中获取内部匹配。突出显示是我能找到的唯一可以保持原始文档结构的解决方案。
我有一个 ElasticSearch 索引,其中的文档如下所示:
{
"labels": ["Common label for doc 1", "Other possible label"],
"year": 1923,
"boolProp": true
},
{
"labels": ["Only one label here"],
"year": 1812,
"boolProp": true
},
...
当我查询 labels
字段时,我想检索最好的文档以及匹配的标签。
我读到这个字段实际上被索引为一个聚合字符串...
对于这种查询,我是否必须将我的 labels
字段转换为嵌套对象?我想知道我是否缺少一种更直接的方法...
一种方法是使用 Highlighting.
这是一个相当丰富的功能,但以下示例可能会帮助您实现目标。
{
"query": {
"match": {
"myfield": "another"
}
},
"highlight": {
"fields": {
"myfield": {
"type": "plain"
}
},
"pre_tags": [""],
"post_tags": [""]
}
}
您可以选择保持匹配的文本突出显示,或指定空白 pre_tags
和 post_tags
以仅显示原始文本。
响应中的 highlight
字段将仅包含原始源数组中匹配的命中。
{
...
"hits": {
"total": 1,
"max_score": 0.28582606,
"hits": [
{
"_index": "test",
"_type": "mytype",
"_id": "AWB6-u6V3-7fA7oZt-aX",
"_score": 0.28582606,
"_source": {
"myfield": [
"My favorite toy",
"Another toy for me"
]
},
"highlight": {
"myfield": [
"Another toy for me"
]
}
}
]
}
}
如果数组中有多个值匹配,则全部返回。
{
...
"hits": {
"total": 1,
"max_score": 0.3938048,
"hits": [
{
"_index": "blah",
"_type": "mytype",
"_id": "AWB6-u6V3-7fA7oZt-aX",
"_score": 0.3938048,
"_source": {
"myfield": [
"My favorite toy",
"Another toy for me"
]
},
"highlight": {
"myfield": [
"My favorite toy",
"Another toy for me"
]
}
}
]
}
}
正如您提到的,当然还有其他选择,使用嵌套文档或父子关系并从中获取内部匹配。突出显示是我能找到的唯一可以保持原始文档结构的解决方案。